Nagios alert in perl

From Federal Burro of Information
Revision as of 14:53, 2 July 2021 by David (talk | contribs)
Jump to navigationJump to search

you could use the perl module, or you can just write to the cmd file.

sub alert ( $$$$ ) {
    my ($host, $test, $code , $message);
    ($host, $test, $code , $message) = @_;
    my $now = time;
    my $cmdfile = "/usr/local/nagios/var/rw/nagios.cmd";
    open(my $fh, ">", "/usr/local/nagios/var/rw/nagios.cmd")
     or die "$now [$$] CRITCAL cannot open > /usr/local/nagios/var/rw/nagios.cmd: $!";
    printf     "$now [$$] Alert: [%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n" , $now , $host, $test , $code , $message ;
    printf $fh "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n" , $now , $host, $test , $code , $message
     or die "$now [$$] CRITIAL Failed to write to /usr/local/nagios/var/rw/nagios.cmd\n";
    close $fh;
}