6 use vars qw
/$opt_e $opt_c/;
8 $ENV{"PATH"} = "/usr/bin:/usr/sbin:/bin";
15 my $CFG_DEF = "/etc/nagios/cgi.cfg";
16 # this works only in mysql
17 # my $QUERY = "select *, UNIX_TIMESTAMP(last_update) as ut from programstatus;";
18 # the following is the same for postgres
19 my $QUERY = "select *, round(date_part('epoch',last_update)) as ut from programstatus;";
20 my $EXPIRE_DEF = 5; ## expressed in minutes
24 use constant WARN
=> 2;
30 print STDERR
"$0 -F -e <expire time in minutes> -C <process string>\n";
37 my $EXPIRE = $opt_e || $EXPIRE_DEF;
38 my $CFG = $opt_c || $CFG_DEF;
40 ( -f
$CFG ) or die "Can't open config file '$CFG': $!\n";
42 my ($dbhost, $dbport, $dbuser, $dbpass, $dbname);
46 if (/^xsddb_host=(.+)/) { $dbhost = $1; next; };
47 if (/^xsddb_port=(.+)/) { $dbport = $1; next; };
48 if (/^xsddb_database=(.+)/) { $dbname = $1; next; };
49 if (/^xsddb_username=(.+)/) { $dbuser = $1; next; };
50 if (/^xsddb_password=(.+)/) { $dbpass = $1; next; };
54 #print "($dbhost, $dbport, $dbuser, $dbpass, $dbname)\n";
56 my $dsn = "DBI:$driver:dbname=$dbname;host=$dbhost;port=$dbport";
57 my $dbh = DBI
->connect($dsn, $dbuser, $dbpass, {'RaiseError' => 1});
59 my $sth = $dbh->prepare($QUERY);
60 if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
62 if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; }
66 my $names = $sth->{'NAME'};
67 my $numFields = $sth->{'NUM_OF_FIELDS'};
68 my $ref = $sth->fetchrow_arrayref;
69 for (my $i = 0; $i < $numFields; $i++) {
70 $status{"$$names[$i]"} = $$ref[$i];
73 #foreach (keys(%status)) {
74 # print "$_: $status{$_}\n";
77 my $lastupdated = time() - $status{"ut"};
78 if ( $lastupdated < ($EXPIRE*60) ) { ## convert $EXPIRE to seconds
82 open(PS
, "ps -eaf | grep $status{nagios_pid} | grep -v grep | ");
90 print "Nagios OK: located $PROCCNT processes, program status updated $lastupdated seconds ago\n";