DEV Community

Cover image for ตรวจ CPU/Memory ทุกชั่วโมงด้วย Garudust Agent Cron ในตัว
Garudust
Garudust

Posted on

ตรวจ CPU/Memory ทุกชั่วโมงด้วย Garudust Agent Cron ในตัว

garudust-agent มี cron scheduler ในตัวอยู่แล้ว ภายใต้ crates/garudust-cron ที่ wraps tokio_cron_scheduler ไว้ หน้าที่ของมันคือยิง agent task ตาม schedule โดยตรง — ไม่ต้องสร้าง process ใหม่ทุกรอบ session ยังอยู่ใน RAM ข้าม tick ได้ และ agent สร้าง/ลบ job เองตอน runtime ได้ด้วย

บทความนี้ใช้ระบบ cron ใน garudust ทำงานให้ถูกต้อง


สิ่งที่ต้องมี

  • Linux / macOS
  • garudust-server binary (v0.13.1+)
  • API key จาก OpenAI-compatible endpoint

1. ติดตั้ง Binary

ARCH=$(uname -m)

curl -Lo garudust.tar.gz \
  "https://github.com/ninenox/garudust-agent/releases/download/v${GARUDUST_VERSION}/garudust-${ARCH}-unknown-linux-gnu.tar.gz"

tar -xzf garudust.tar.gz
chmod +x garudust-server
sudo mv garudust-server /usr/local/bin/garudust-server

garudust-server --version
# garudust-server 0.13.1
Enter fullscreen mode Exit fullscreen mode
# macOS (Apple Silicon)
curl -Lo garudust.tar.gz \
  "https://github.com/ninenox/garudust-agent/releases/download/v${GARUDUST_VERSION}/garudust-aarch64-apple-darwin.tar.gz"
tar -xzf garudust.tar.gz
chmod +x garudust-server
sudo mv garudust-server /usr/local/bin/garudust-server
Enter fullscreen mode Exit fullscreen mode

2. ตั้ง Config

ไฟล์ config อยู่ที่ ~/.garudust/config.yaml — สร้าง directory ก่อน:

mkdir -p ~/.garudust
Enter fullscreen mode Exit fullscreen mode
# ~/.garudust/config.yaml

model: "gpt-4o-mini"
api_key: "sk-..."

tools:
  terminal:
    enabled: true
    sandbox: none          # รันบน host โดยตรง — monitoring อ่านอย่างเดียว ปลอดภัย
    approval_mode: auto    # cron รันโดยไม่มีคนนั่งดู ต้องเป็น auto

# ── cron ──────────────────────────────────────────────────────────────────────
cron:
  timezone: "Asia/Bangkok"   # IANA timezone — ถ้าไม่ระบุ default เป็น UTC

  jobs:
    - schedule: "0 * * * *"  # 5-field unix cron: ทุกชั่วโมง นาทีที่ 0
      task: >
        ตรวจสอบสุขภาพของระบบตอนนี้ด้วยคำสั่งต่อไปนี้:
        1. `top -bn1 | head -20`
        2. `ps aux --sort=-%cpu | head -10`
        3. `free -h`
        4. `ps aux --sort=-%mem | head -10`
        รายงานผลเป็นภาษาไทยตาม threshold ที่กำหนดไว้

  # (ทางเลือก) จัดระเบียบ memory ของ agent ทุกคืน
  memory_consolidation: "0 3 * * *"

  # (ทางเลือก) ลบ memory เก่ากว่า 30 วัน
  memory_expiry: "0 4 * * *"

system_prompt: |
  คุณคือ system monitor สำหรับเซิร์ฟเวอร์นี้

  Threshold สำหรับแจ้งเตือน:
  - CPU รวม > 80%  → ⚠️ WARNING
  - CPU รวม > 95%  → 🔴 CRITICAL
  - Memory > 75%   → ⚠️ WARNING
  - Memory > 90%   → 🔴 CRITICAL

  เมื่อเกิน threshold ให้:
  1. ระบุ process ที่กินสูงสุด 5 อันดับ
  2. ประเมินว่าเป็น spike ชั่วคราว หรือ trend ต่อเนื่อง
  3. แนะนำ action ถ้าจำเป็น

  ถ้าทุกอย่างปกติ สรุปสั้น 2–3 บรรทัดก็พอ
  ตอบเป็นภาษาไทยเสมอ
Enter fullscreen mode Exit fullscreen mode

3. รัน Server

garudust-server
Enter fullscreen mode Exit fullscreen mode

ที่ startup จะเห็น log ยืนยัน cron ถูก register:

INFO garudust-server 0.13.1 | model: gpt-4o-mini | port: 3000
INFO cron job registered  cron="0 * * * *"  task="ตรวจสอบสุขภาพของระบบ..."
INFO memory consolidation cron registered  cron="0 3 * * *"
INFO memory expiry cron registered  cron="0 4 * * *"
INFO garudust-server listening on 0.0.0.0:3000
Enter fullscreen mode Exit fullscreen mode

เมื่อ cron fire จะเห็น:

INFO cron job starting   task="ตรวจสอบสุขภาพของระบบ..."
INFO cron job completed  task="ตรวจสอบสุขภาพของระบบ..."  iterations=3
Enter fullscreen mode Exit fullscreen mode

หรือถ้า agent เจอปัญหา:

ERROR cron job failed  task="ตรวจสอบสุขภาพของระบบ..."  error="..."
Enter fullscreen mode Exit fullscreen mode

4. รัน Server แบบ Daemon ด้วย systemd

สำหรับ production ไม่ควรรันใน terminal ค้างไว้ — ใช้ systemd:

sudo tee /etc/systemd/system/garudust-monitor.service > /dev/null <<EOF
[Unit]
Description=Garudust System Monitor
After=network.target

[Service]
Type=simple
User=$USER
ExecStart=/usr/local/bin/garudust-server
Restart=on-failure
RestartSec=10
Environment=RUST_LOG=info

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now garudust-monitor
sudo systemctl status garudust-monitor
Enter fullscreen mode Exit fullscreen mode

5. เช็ก Log

เมื่อรันด้วย systemd:

# Log real-time
journalctl -u garudust-monitor -f

# Log ย้อนหลัง 1 ชั่วโมง
journalctl -u garudust-monitor --since "1 hour ago"

# กรองเฉพาะรอบที่ cron fire
journalctl -u garudust-monitor -g "cron job"

# หารอบที่มี WARNING หรือ CRITICAL
journalctl -u garudust-monitor --since today | grep -E "WARNING|CRITICAL|⚠️|🔴"
Enter fullscreen mode Exit fullscreen mode

ถ้าอยากเก็บ log เป็นไฟล์แยก ใส่เพิ่มใน systemd unit:

[Service]
StandardOutput=append:/var/log/garudust-monitor.log
StandardError=append:/var/log/garudust-monitor.log
Enter fullscreen mode Exit fullscreen mode

6. เพิ่ม Job แบบ Runtime ผ่าน API

นอกจาก static job ใน config แล้ว agent มี tool cron_create / cron_list / cron_delete ให้ใช้ตอน runtime — สั่งผ่าน HTTP API ได้เลย:

# สร้าง job ตรวจ disk ทุก 6 ชั่วโมง
curl -s -X POST http://localhost:3000/chat \
  -H "Content-Type: application/json" \
  -d '{
    "message": "สร้าง cron job label: disk_check ทุก 6 ชั่วโมง ให้รัน df -h แล้วแจ้งถ้า disk ใด partition ใดเกิน 85%"
  }' | jq .reply
Enter fullscreen mode Exit fullscreen mode

agent จะใช้ tool cron_create สร้าง job ให้อัตโนมัติ:

{
  "reply": "✅ สร้าง cron job 'disk_check' แล้ว\nschedule: 0 0 */6 * * *\ntask: รัน df -h ตรวจ disk..."
}
Enter fullscreen mode Exit fullscreen mode

หมายเหตุ — Cron syntax ต่างกัน 2 แบบ:

  • Static job ใน config.yaml → 5-field unix cron ปกติ เช่น 0 * * * *
  • Runtime job ผ่าน API → 6-field (เพิ่ม field วินาทีนำหน้า) เช่น 0 0 * * * * = ทุกชั่วโมง, 0 0 */6 * * * = ทุก 6 ชั่วโมง
# ดู job ทั้งหมดที่ active ตอนนี้
curl -s -X POST http://localhost:3000/chat \
  -d '{"message": "แสดง cron jobs ทั้งหมด"}' | jq .reply

# ลบ job
curl -s -X POST http://localhost:3000/chat \
  -d '{"message": "ลบ cron job label: disk_check"}' | jq .reply
Enter fullscreen mode Exit fullscreen mode

Tip: Runtime jobs หายเมื่อ restart — สำหรับ permanent job ให้ใส่ใน config.yaml เสมอ Runtime jobs เหมาะสำหรับทดสอบ schedule ก่อน commit config หรือ one-off jobs ชั่วคราว


7. ตั้ง Alert ผ่าน Telegram (ทางเลือก)

เพิ่ม platform Telegram เข้าไปใน config — agent จะส่งข้อความหา admin โดยตรงเมื่อ cron fire และเจอปัญหา:

# ~/.garudust/.env
TELEGRAM_TOKEN=bot_token_here
Enter fullscreen mode Exit fullscreen mode
# config.yaml — เพิ่ม/แก้ cron.jobs section นี้
cron:
  timezone: "Asia/Bangkok"
  jobs:
    - schedule: "0 * * * *"
      task: >
        ตรวจ CPU/Memory ของระบบ ถ้าพบ WARNING หรือ CRITICAL
        ให้ส่งข้อความแจ้งไปยัง admin ทาง Telegram ด้วย
        พร้อมระบุ process และ % ที่เกิน threshold
Enter fullscreen mode Exit fullscreen mode

เปรียบเทียบกับ System Cron

System Cron Garudust Cron (built-in)
Process per tick spawn ใหม่ทุกครั้ง ทำงานใน event loop เดิม
Session/memory โหลดใหม่ทุกรอบ อยู่ใน RAM ข้าม tick ได้
Dynamic schedule แก้ crontab เอง agent สร้าง/ลบตอน runtime
Log ไฟล์แยก รวมกับ server log เดียวกัน
Timezone ขึ้นกับ system TZ กำหนดใน config ได้ per-scheduler
Dependency ต้องมี cron daemon ไม่ต้องการ — built into server

สรุป config ทั้งหมด

~/.garudust/
├── config.yaml          ← cron.jobs, tools, system_prompt
└── .env                 ← API keys, tokens (ไม่ commit)
Enter fullscreen mode Exit fullscreen mode
garudust-server start
    ↓
register cron jobs จาก config.yaml
    ↓
ทุก 1 ชม. → agent run → terminal tool → top/free/ps
    ↓
log ออก stdout/journald
    ↓ (ถ้าต่อ Telegram)
send alert เมื่อเกิน threshold
Enter fullscreen mode Exit fullscreen mode

จุดเด่นที่ system cron ทำไม่ได้: agent จำ pattern ข้ามรอบได้ เช่น รู้ว่า CPU spike ทุก 14:00 คือ backup job ไม่ใช่ anomaly — เพราะ memory session ยังอยู่ในหน่วยความจำระหว่าง tick


garudust-agentgithub.com/garudust-org/garudust-agent

Top comments (0)