summaryrefslogtreecommitdiffstats
path: root/contrib/check_nagios_db.pl
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/check_nagios_db.pl')
-rw-r--r--contrib/check_nagios_db.pl89
1 files changed, 89 insertions, 0 deletions
diff --git a/contrib/check_nagios_db.pl b/contrib/check_nagios_db.pl
new file mode 100644
index 00000000..5811d7c6
--- /dev/null
+++ b/contrib/check_nagios_db.pl
@@ -0,0 +1,89 @@
1#!/usr/local/bin/perl -w
2
3use strict;
4$|++;
5
6use vars qw/$opt_e $opt_c/;
7
8$ENV{"PATH"} = "/usr/bin:/usr/sbin:/bin";
9
10use Getopt::Std;
11use DBI;
12
13my $driver = "mysql";
14
15my $CFG_DEF = "/opt/nagios/etc/cgi.cfg";
16my $QUERY = "select *, UNIX_TIMESTAMP(last_update) as ut from programstatus;";
17my $EXPIRE_DEF = 5; ## expressed in minutes
18my $PROCCNT = 0;
19
20use constant OK => 1;
21use constant WARN => 2;
22
23my $STAT = WARN;
24
25sub 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;
30}
31
32getopt("e:c:");
33
34my $EXPIRE = $opt_e || $EXPIRE_DEF;
35my $CFG = $opt_c || $CFG_DEF;
36
37( -f $CFG ) or die "Can't open config file '$CFG': $!\n";
38
39my ($dbhost, $dbport, $dbuser, $dbpass, $dbname);
40
41open(F, "< $CFG");
42while ( <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; };
48}
49close(F);
50
51# print "($dbhost, $dbport, $dbuser, $dbpass, $dbname)\n";
52
53my $dsn = "DBI:$driver:database=$dbname;host=$dbhost;port=$dbport";
54my $dbh = DBI->connect($dsn, $dbuser, $dbpass, {'RaiseError' => 1});
55
56my $sth = $dbh->prepare($QUERY);
57if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
58$sth->execute;
59if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; }
60
61my %status = ();
62
63my $names = $sth->{'NAME'};
64my $numFields = $sth->{'NUM_OF_FIELDS'};
65my $ref = $sth->fetchrow_arrayref;
66for (my $i = 0; $i < $numFields; $i++) {
67 $status{"$$names[$i]"} = $$ref[$i];
68}
69
70#foreach (keys(%status)) {
71# print "$_: $status{$_}\n";
72#}
73
74my $lastupdated = time() - $status{"ut"};
75if ( $lastupdated < ($EXPIRE*60) ) { ## convert $EXPIRE to seconds
76 $STAT = OK;
77}
78
79open(PS, "ps -eaf | grep $status{nagios_pid} | grep -v grep | ");
80$PROCCNT = 0;
81while(<PS>) {
82 $PROCCNT++;
83}
84close(PS);
85
86if ( $STAT == OK ) {
87 print "Nagios OK: located $PROCCNT processes, program status updated $lastupdated seconds ago\n";
88}
89