📜 ⬆️ ⬇️

Perl and Bash load monitoring script

image

Sitting at home on a cold Saturday night, I thought that it would be nice to write a script monitoring the load on the server and if something happens, 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 remarkably uptime command that allows us to render 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 not 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 deduce ' : 0.10, 0.29, 0.38 ', but in this case it doesn’t matter.

But even in this team there is still some garbage, the cut command comes to the rescue, which allows us to trim our team according to certain criteria. Considering the number of characters to the value of interest, we find that we need to cut our output from 15 to 18 characters, so the full 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

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

Next comes the simplest script with the already received algorithm. (On perl)

#!/usr/bin/perl
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\@netlevel.ru`; # ,
}


On Bash, this script will look like this.
#!/bin/bash

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

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

Thanks to iSage

In this particular case, I decided to just send the letter to the specified box using the mail command, but you have the right to modify the script as you like.

After you have finished modifying the script, for example, load it into the / bin folder, give it the right to run chmod +x scriptname and write it in cron using the crontab -e command
An 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 someone this script will be useful.

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

Thanks for attention.

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

Source: https://habr.com/ru/post/114127/


All Articles