Perl and Bash load monitoring script


    Sitting at home on a cold Saturday evening, I thought it would be nice to write a script for monitoring the load on the server and, in which case, do certain actions.

    I started with the development of the algorithm, and how exactly can I export the value of the current load.
    We are all familiar with the wonderful uptime command which allows us to make a value in this format.

    10:52:52 up 25 min, 4 users, load average: 0.27, 0.38, 0.43

    But there is too much garbage, and we need to separate the current load from the rest of the garbage.
    Here the almighty grep comes to the rescue , with the help of tricky manipulations, you can get the load already in this form: load average: 0.14, 0.29, 0.38 using the following command:

    uptime | grep -o 'load average.*'

    Of course, you could do a search on ':. *' And we would get ' : 0.10, 0.29, 0.38 ' but in this case it does not matter.

    But even in this team there is still a bit of garbage, here the cut command comes to the rescue , which allows us to trim our team according to certain criteria. Having counted the number of characters to the value of interest to us, we get that we need to trim our output from 15 to 18 characters, so the complete command will look like this:

    uptime | grep -o 'load average.*' | cut -c 15-18

    Thus, we get the output in the following form: 0.10 We

    finished the algorithm, now we need to compare this value specified in the script.
    Perl was taken as a basis since I didn’t immediately understand how you can implement decimal comparison in Bash, I’ll say right away that I’m not a big fan of Perl and this is essentially my first working Perl script.

    Next comes the simplest script with an algorithm already obtained. (On Perl) On Bash, this script will look like this Thanks to the iSage habrayuzer In this particular case, I decided to just send an email to the specified mailbox using the mail command, but you have the right to modify the script as you like. After you have finished modifying the script, load it into the / bin folder, for example, give it the right to run and write it to the crowns using the command

    my $load = `uptime | grep -o 'load average.*' | cut -c 15-18`; # Текущая нагрузка
    my $maxload = 20; # Максимальная нагрузка

    if ( $load >= $maxload ) { # Сравниваем текущую нагрузку с максимальной
    `echo "$load" | mail -s 'High load [SERVERNAME]' admins\`; # Если текущая нагрузка выше максимальной, отсылать письмо


    LOAD=`uptime | grep -o 'load average.*' | cut -c 15-18`

    if [ LOAD \> MAXLOAD ]; then
    echo $LOAD | mail -s 'High load [SERVERNAME]' admins\;

    chmod +x scriptnamecrontab -e
    The entry for the script will look something like this:

    2 * * * * /usr/local/bin/perl /bin/scriptname

    Well, for Bash naturally,

    2 * * * * /bin/bash /bin/scriptname

    I hope this script is useful to someone.

    If you have any wishes, suggestions, additions to the script, I will be glad to listen to them and add to the topic.

    Thanks for attention.

    Thanks for the karma of the guys, I appreciate it, really. So share it after that ...

    Also popular now: