Poor man’s processes monitor (bash & awk script)

Recently a member of the Romanian Ubuntu Community asked for a script to monitor the running processes on his server . He didn’t requested for anything fancy, just a small utility that will be able to detect a hanging application (a process that is “eating” more than 80% of the CPU for a long period of time) and then log the results .

I am no sysadmin, but I am sure there are a lot of dedicated open-source solutions for monitoring a server .Still the functionality he asked for can be easily achieved by combining bash and awk .

One of the things I like Linux for is the power of the shell and the full-control over the your system . You can write a script for every repeating task that arise as bash is easy to learn but of course, hard to master .  More as an exercise for myself I’ve proposed the following solution :

If you run this script the output will probably look similar to this one :

The output can then be redirected to a file (>>) and interpreted as:

In this form the script support the following variables:

A float number (integers are accepted) representing the maximum memory percent a process can use before triggering the alarm .
A float number (integers are accepted) representing the maximum
The pause in the main loop . Every SEC_INT the process will be scanned

Those variables are passed as awk variables whilst using the ‘ -v ‘ flag .

The shortcomings of the script are obvious: sometimes a process can have a short spike of CPU consumption, so false positives may appear . Probably the best thing to do will be to write another script to analyze the log, and see how many times a certain command is repeated . For example the log should be ‘grep’ed to find a certain command, then use the ‘wc’ utility the count how many times the process triggered the alarm . All in all the problem worthed a try !

Leave a Reply

Your email address will not be published. Required fields are marked *

Are we human, or are we dancer *