#!/usr/bin/perl -wT # # check if custom processes are running at certain times # # syntax: # ./check_apps --host= --community= --file # # jorgbosman@optiver.com - 14 juli 2004 use strict; use Getopt::Long; use Net::SNMP; use XML::Simple; use Data::Dumper; # # settings # my $hostname = ""; my $community = "public"; my $file; my ($min,$hour,$wday) = (localtime)[1,2,6]; my $now = $hour * 100 + $min; GetOptions("hostname:s" => \$hostname, "community:s" => \$community, "file:s" => \$file); my $errorcode = 0; # # get process-data # if (! defined $file) { print "ERROR: No applications to monitor\n"; exit 3; # OK } $file =~ /([\/\w\.\-]+)/s; # untaint if (! -f $file) { print "No applications to monitor\n"; exit 0; # OK } my ($config) = XMLin($file, forcearray=>['process']); my @config = @{$config->{'process'}}; # # connect # my ($session, $error) = Net::SNMP->session(-hostname => $hostname, -community => $community, -timeout => 30, -retries => 0); if (! defined $session) { printf("ERROR: %s.\n", $error); exit 3; # unknown } # # get data # # hrSWRunPath my $oid = ".1.3.6.1.2.1.25.4.2.1.4"; my $runpath = $session->get_table(-baseoid => $oid); if (! defined $runpath) { printf("ERROR: %s.\n", $session->error); exit 3; # unknown } my %runpath = %{$runpath}; # hrSWRunParameters $oid = ".1.3.6.1.2.1.25.4.2.1.5"; my $runparam = $session->get_table(-baseoid => $oid); if (! defined $runparam) { printf("ERROR: %s.\n", $session->error); exit 3; # unknown } my %runparam = %{$runparam}; # hrSWRunPerfCPU $oid = ".1.3.6.1.2.1.25.5.1.1.1"; my $runcpu = $session->get_table(-baseoid => $oid); if (! defined $runcpu) { printf("ERROR: %s.\n", $session->error); exit 3; # unknown } my %runcpu = %{$runcpu}; # hrSWRunPerfMem $oid = ".1.3.6.1.2.1.25.5.1.1.2"; my $runmem = $session->get_table(-baseoid => $oid); if (! defined $runmem) { printf("ERROR: %s.\n", $session->error); exit 3; # unknown } my %runmem = %{$runmem}; # # rewrite data and put it in one hash # my %runps; while (my ($key, $value) = each(%runpath)) { $key =~ s/.*\.(\d+)$/$1/; $runps{$key}{'ps'} = $value; } while (my ($key, $value) = each(%runparam)) { $key =~ s/.*\.(\d+)$/$1/; if ($value ne '') { $runps{$key}{'ps'} .= " $value"; } } while (my ($key, $value) = each(%runcpu)) { $key =~ s/.*\.(\d+)$/$1/; $runps{$key}{'cpu'} = $value; } while (my ($key, $value) = each(%runmem)) { $key =~ s/.*\.(\d+)$/$1/; $runps{$key}{'memory'} = $value; } # # parse confighash and pshash # while (my ($key, $value) = each(%runps)) { my %value = %{$value}; my $i = 0; while ($i <= ($#config)) { if ($value{'ps'} =~ /$config[$i]{'ps'}/) { # count running processes if (defined $config[$i]{'count'}) { $config[$i]{'count'}++; } else { $config[$i]{'count'} = 1; } # count total cpu-time per process-type if (defined $config[$i]{'cpu'}) { $config[$i]{'cpu'} += $value{'cpu'}; } else { $config[$i]{'cpu'} = $value{'cpu'}; } # count total memory per process-type if (defined $config[$i]{'memory'}) { $config[$i]{'memory'} += $value{'memory'}; } else { $config[$i]{'memory'} = $value{'memory'}; } } $i++; } } # # remove entries that are okay # my $text = ''; my $rrdtext = ''; my $i = 0; while ($i <= $#config) { if (! defined $config[$i]{'count'}) { $config[$i]{'count'} = 0; $config[$i]{'cpu'} = 'U'; $config[$i]{'memory'} = 'U'; } if (defined $config[$i]{'min'}) { if ($config[$i]{'count'} < $config[$i]{'min'}) { if (! defined $config[$i]{'days'}) { $config[$i]{'days'} = '0123456'; } if (! defined $config[$i]{'starttime'}) { $config[$i]{'starttime'} = '0000'; } if (! defined $config[$i]{'stoptime'}) { $config[$i]{'stoptime'} = '2400'; } if (($config[$i]{'days'} =~ /$wday/) && ($now >= $config[$i]{'starttime'}) && ($now <= $config[$i]{'stoptime'})) { $errorcode = 2; $text .= "" . $config[$i]{'count'} . "x " . $config[$i]{'description'} . ", "; } else { $text .= $config[$i]{'count'} . "x " . $config[$i]{'description'} . ", "; } } } if (defined $config[$i]{'max'}) { if ($config[$i]{'count'} > $config[$i]{'max'}) { $text .= $config[$i]{'count'} . "x " . $config[$i]{'description'} . ", "; } } $rrdtext .= $config[$i]{'description'} . "," . $config[$i]{'cpu'} . ":" . $config[$i]{'memory'} . " "; $i++; } # remove komma and space chop($text); chop($text); chop($rrdtext); # description:cpu:mem # # print result # if ($text eq '') { $text = 'OK'; } print "$text|$rrdtext\n"; # # exit # (0=ok 1=warning 2=critical 3=unknown 4=dependent) # exit $errorcode;