summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AUTHORS2
-rw-r--r--contrib/check_nagios_db_pg.pl91
2 files changed, 92 insertions, 1 deletions
diff --git a/AUTHORS b/AUTHORS
index 3d5f9b7..ea8bd50 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -111,4 +111,4 @@ Flo Gleixner
111Howard Wilkinson 111Howard Wilkinson
112Torsten Werner 112Torsten Werner
113Roy Sigurd Karlsbakk 113Roy Sigurd Karlsbakk
114 114Kondoros Attila
diff --git a/contrib/check_nagios_db_pg.pl b/contrib/check_nagios_db_pg.pl
new file mode 100644
index 0000000..99e5d4e
--- /dev/null
+++ b/contrib/check_nagios_db_pg.pl
@@ -0,0 +1,91 @@
1#!/usr/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 = "Pg";
14
15my $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
19my $QUERY = "select *, round(date_part('epoch',last_update)) as ut from programstatus;";
20my $EXPIRE_DEF = 5; ## expressed in minutes
21my $PROCCNT = 0;
22
23use constant OK => 1;
24use constant WARN => 2;
25
26my $STAT = WARN;
27
28sub usage {
29 print STDERR "\n";
30 print STDERR "$0 -F -e <expire time in minutes> -C <process string>\n";
31 print STDERR "\n";
32 exit -1;
33}
34
35getopt("e:c:");
36
37my $EXPIRE = $opt_e || $EXPIRE_DEF;
38my $CFG = $opt_c || $CFG_DEF;
39
40( -f $CFG ) or die "Can't open config file '$CFG': $!\n";
41
42my ($dbhost, $dbport, $dbuser, $dbpass, $dbname);
43
44open(F, "< $CFG");
45while ( <F> ) {
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; };
51}
52close(F);
53
54#print "($dbhost, $dbport, $dbuser, $dbpass, $dbname)\n";
55
56my $dsn = "DBI:$driver:dbname=$dbname;host=$dbhost;port=$dbport";
57my $dbh = DBI->connect($dsn, $dbuser, $dbpass, {'RaiseError' => 1});
58
59my $sth = $dbh->prepare($QUERY);
60if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
61$sth->execute;
62if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; }
63
64my %status = ();
65
66my $names = $sth->{'NAME'};
67my $numFields = $sth->{'NUM_OF_FIELDS'};
68my $ref = $sth->fetchrow_arrayref;
69for (my $i = 0; $i < $numFields; $i++) {
70 $status{"$$names[$i]"} = $$ref[$i];
71}
72
73#foreach (keys(%status)) {
74# print "$_: $status{$_}\n";
75#}
76
77my $lastupdated = time() - $status{"ut"};
78if ( $lastupdated < ($EXPIRE*60) ) { ## convert $EXPIRE to seconds
79 $STAT = OK;
80}
81
82open(PS, "ps -eaf | grep $status{nagios_pid} | grep -v grep | ");
83$PROCCNT = 0;
84while(<PS>) {
85 $PROCCNT++;
86}
87close(PS);
88
89if ( $STAT == OK ) {
90 print "Nagios OK: located $PROCCNT processes, program status updated $lastupdated seconds ago\n";
91}