Fix check_snmp passing single-quotes as -m argument when it should be empty
[monitoring-plugins.git] / contrib / check_nagios_db.pl
blob5811d7c6f6c30bf01f71f9643aad9d14c3c89fc7
1 #!/usr/local/bin/perl -w
3 use strict;
4 $|++;
6 use vars qw/$opt_e $opt_c/;
8 $ENV{"PATH"} = "/usr/bin:/usr/sbin:/bin";
10 use Getopt::Std;
11 use DBI;
13 my $driver = "mysql";
15 my $CFG_DEF = "/opt/nagios/etc/cgi.cfg";
16 my $QUERY = "select *, UNIX_TIMESTAMP(last_update) as ut from programstatus;";
17 my $EXPIRE_DEF = 5; ## expressed in minutes
18 my $PROCCNT = 0;
20 use constant OK => 1;
21 use constant WARN => 2;
23 my $STAT = WARN;
25 sub usage {
26 print STDERR "\n";
27 print STDERR "$0 -F -e <expire time in minutes> -C <process string>\n";
28 print STDERR "\n";
29 exit -1;
32 getopt("e:c:");
34 my $EXPIRE = $opt_e || $EXPIRE_DEF;
35 my $CFG = $opt_c || $CFG_DEF;
37 ( -f $CFG ) or die "Can't open config file '$CFG': $!\n";
39 my ($dbhost, $dbport, $dbuser, $dbpass, $dbname);
41 open(F, "< $CFG");
42 while ( <F> ) {
43 if (/^xsddb_host=(.+)/) { $dbhost = $1; next; };
44 if (/^xsddb_port=(.+)/) { $dbport = $1; next; };
45 if (/^xsddb_database=(.+)/) { $dbname = $1; next; };
46 if (/^xsddb_username=(.+)/) { $dbuser = $1; next; };
47 if (/^xsddb_password=(.+)/) { $dbpass = $1; next; };
49 close(F);
51 # print "($dbhost, $dbport, $dbuser, $dbpass, $dbname)\n";
53 my $dsn = "DBI:$driver:database=$dbname;host=$dbhost;port=$dbport";
54 my $dbh = DBI->connect($dsn, $dbuser, $dbpass, {'RaiseError' => 1});
56 my $sth = $dbh->prepare($QUERY);
57 if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
58 $sth->execute;
59 if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; }
61 my %status = ();
63 my $names = $sth->{'NAME'};
64 my $numFields = $sth->{'NUM_OF_FIELDS'};
65 my $ref = $sth->fetchrow_arrayref;
66 for (my $i = 0; $i < $numFields; $i++) {
67 $status{"$$names[$i]"} = $$ref[$i];
70 #foreach (keys(%status)) {
71 # print "$_: $status{$_}\n";
74 my $lastupdated = time() - $status{"ut"};
75 if ( $lastupdated < ($EXPIRE*60) ) { ## convert $EXPIRE to seconds
76 $STAT = OK;
79 open(PS, "ps -eaf | grep $status{nagios_pid} | grep -v grep | ");
80 $PROCCNT = 0;
81 while(<PS>) {
82 $PROCCNT++;
84 close(PS);
86 if ( $STAT == OK ) {
87 print "Nagios OK: located $PROCCNT processes, program status updated $lastupdated seconds ago\n";