Back to Blog
Automation & DevOps

Building a WhatsApp Bot for Linux Server Monitoring

A guide to creating early warning notifications for VPS status. Using Node.js to check CPU, RAM, and PM2 application Uptime, and dispatching automated alerts to a WhatsApp group.

Node.jsWhatsApp Web.jsPM2 APILinux SysstatCron Job
Grafik monitor performa server dengan warna neon
Mencegah downtime lebih baik daripada menangani komplain pelanggan. · © Luke Chesser

Why are Server Notifications So Extremely Critical?

Picture this horror scenario: Your client's e-commerce or POS system experiences a massive, sudden traffic spike in the middle of the night. The VPS RAM capacity is entirely drained, the Linux Kernel triggers the Out-Of-Memory (OOM) Killer mechanism which brutally terminates the PostgreSQL database process, and the entire site goes completely dark (Down) for 6 hours before you wake up. The transactional financial losses could be catastrophic.

In the DevOps realm, proactive engineering is perpetually superior to reactive scrambling. Architecting an independent monitoring system that delivers early warning alerts straight to your pocket (via WhatsApp) is a fundamental step to safeguarding business infrastructure stability (99.9% SLA) long before end-users detect a system failure.

Essential Metrics: Storage, CPU, RAM, and PM2 Uptime

To design an effective early warning bot, we must avoid flooding chat logs with garbage metrics (alert fatigue). Precisely extract these 4 critical indicators utilizing native Linux bash utilities, executed via Node.js's 'child_process' (exec) module:

  • Disk Storage (df -h): Trigger a CRITICAL alert if remaining disk storage drops below 10%. Accumulating application logs without proper log rotation (logrotate) is frequently the silent culprit behind abrupt server crashes.
  • RAM (free -m) & CPU (top): Alert if the average memory or CPU load remains pegged above 85% sustained over a 5-minute window. This heavily indicates a severe memory leak in the application code.
  • Application Uptime: Interface programmatically with the PM2 API to monitor the lifecycle of Next.js/Express application daemons. If the cluster status transitions to 'errored' or 'stopped', dispatch an emergency notification.
  • Service Health Status: Execute scripts to verify systemctl statuses, ensuring the reverse proxy (Nginx) and Database are functioning flawlessly (active/running).

The Principle of Least Privilege for Bot Execution

One of the most fatal architectural errors made by junior Sysadmins is running the WhatsApp bot daemon using pure 'root' access. Given the bot connects to the public internet, should a Remote Code Execution vulnerability be discovered in the bot library, attackers could seamlessly seize total control over the entire VPS server.

For maximum security, always run monitoring bots utilizing a dedicated, non-root Linux user with severely restricted permissions—authorized exclusively to read hardware metrics and view PM2 application logs in a read-only state.

Furthermore, strictly limit the bot's interactive capabilities. The bot should be engineered purely 'Read-Only'—solely transmitting outbound warning notifications, without supplying a parser to accommodate interactive commands like '!restart-server'. Placing shell script execution controls directly accessible from WhatsApp chat inputs represents an incredibly reckless security attack vector.

Integrating Node.js, Cron Jobs, and Headless Puppeteer

The open-source 'whatsapp-web.js' library operates entirely independently without requiring paid official APIs from the Meta Cloud infrastructure. It functions by launching a stealthy Chromium browser instance (headless Puppeteer) simulating a WhatsApp Web login session. For optimal VPS resource efficiency, ensure you pass the '--no-sandbox' option and persist session caching (session data) to bypass repetitive QR code scanning upon server restarts.

Simply construct a scheduling function (utilizing the node-cron library) that executes hardware interrogations every 5 minutes. Should any metric breach the red threshold, the script instantly triggers a sendMessage payload directed to the dedicated IT Incident Response Team's WhatsApp Group ID. Empowered by this automation, the technical team can immediately intervene (via secure SSH login) within minutes.

Website Anda Sering Down Tanpa Sepengetahuan?

Mari setup sistem monitoring mandiri yang dapat memberi tahu Anda sebelum klien menyadari adanya masalah.

Konsultasi Setup VPS
© 2026 Fajar Geran Arifin. All rights reserved.