Scripting CPU and RAM Usage Reports on Enterprise Ubuntu
Stop Guessing Why Your Server Crashed
We've all been there. It's 3 AM. The pager goes off. Your enterprise Ubuntu box is completely unresponsive. You reboot it, cross your fingers, and pretend everything is fine. But it isn't fine. Without a baseline server health report, you are flying blind. Let's fix that. No expensive SaaS tools required. Just you, the terminal, and some basic shell logic.
Nailing Down the CPU Hogs
Writing a reliable cpu usage bash script isn't rocket science. Actually, it's mostly just knowing how to parse the `top` or `mpstat` command. Grab the idle percentage, subtract it from 100, and you have your usage. Throw that into a variable. Done. The trick is consistency. You want to sample the CPU over a few seconds, not grab a single microsecond snapshot. That prevents sudden, meaningless spikes from ruining your data.
Memory Leaks Are the Enemy
RAM issues will kill your server faster than anything else. The OOM (Out of Memory) killer wakes up and starts murdering your database processes. Brutal. Building a memory monitor ubuntu script requires understanding how `free -m` actually works. Don't look at the "free" memory column. Look at "available" memory. Linux caches aggressively. Your script needs to parse that available column and sound the alarm when it dips below a safe threshold. Like 20 percent.
Gluing It Together Into a Dashboard
Individual scripts are useless if you have to run them manually. Combine your CPU and RAM logic. Spit the output into a clean CSV format. Include a timestamp. Always include a timestamp. Now you have the foundation of a proper server health report. Push it to a local log file. If you are feeling ambitious, pipe that output directly into a Slack webhook. Let the server tell you it's dying before the clients complain.
Automate and Walk Away
Cron is your best friend here. Set your newly minted script to run every five minutes. `*/5 * * * * /path/to/script.sh`. That's it. You now have a historical record of your machine's vitals. Just remember to set up log rotation for your report file using `logrotate`. Otherwise, your tiny monitoring script will eventually consume your entire hard drive. Which is deeply ironic.