2022-07-16

Linux Perl script to run to monitor load average and send an a lert

#!/usr/bin/perl

$load = `/usr/bin/uptime`;

$load =~ /\s+load average:\s+(\S+),\s+/;
#print $1, "\n";

$me = `/bin/hostname`;

$load = $1;

if ($load > 5.5) {
        #system("/usr/bin/logger -p daemon.emerg load-ave-alert  load $load.");
        system("/usr/bin/wall load-ave-alert load $load.");
        $DATE = `/bin/date +'%Y%m%d%H%M'`;
        open(DOMAIL,"| /bin/mail -s \"load-ave-alert $me load high\" root");
        print DOMAIL "$me load is $load at $DATE";
        close(DOMAIL);
        }


exit;
# It seems likely with all the restrictrions on email servers due to spam, that 
# the email would work only locally on the Linux system
# This script could be fork with more perl scripting on with the "nohup" command
#
=============================================
#!/usr/bin/bash
# bash script to fork the perl script above
echo $*
(/usr/bin/nohup $* > /tmp/nohup$$.txt &)&
==================================================
# you could this for the perl script to fork itself after the first line #!/usr/bin/perl

$dofork = 1;
if ($dofork) {
if (open(PS, "/bin/ps ax | /bin/grep \"ping2failure $target\" | /bin/grep -v gre
p |")) {
    @psax = ;
    close(PS);
    #print "@psax";
    for(@psax) {
        s/^\s+//g;
        #print $_;
        if (/ping2failure $target/) {
            #print "$_";
            ($pid, @stuff) = split(/\s+/);
            #print "$$ pid is $pid and $stuff[0]\n";
            if ($pid != $$) {
                die "$target already being waited on\n";
                 # no reason to run if already doing this
            }
        }
    }
}
# deal with stdin, stdout, stderr, control terminal, and put self
# in background
$TIOCNOTTY = 0x20007471;
close(stdin);
open(stdout, ">/dev/null");
open(stderr, ">/dev/null");
if (open(tty, "/dev/tty")) {
    ioctl(tty, $TIOCNOTTY, 0);
    close(tty);
}
if (fork) {
    exit 0;
}
}

No comments:

Post a Comment

Matrox Monarch 5-minute video recordings assembled for Youtube.

This is a short bash scirpt I used to compose 5-minute mov files into one mpg file to upload to Youtube. We have a Matrox Monarch that rec...