Fix memory leak in check_http for large pages (Jimmy Bergman - #2957455)
[monitoring-plugins.git] / contrib / check_nagios_db_pg.pl
blob99e5d4ed4742593cdcbedf22c7b178efcb4a6aec
1 #!/usr/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 = "Pg";
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
21 my $PROCCNT = 0;
23 use constant OK => 1;
24 use constant WARN => 2;
26 my $STAT = WARN;
28 sub 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;
35 getopt("e:c:");
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);
44 open(F, "< $CFG");
45 while ( <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; };
52 close(F);
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"; }
61 $sth->execute;
62 if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; }
64 my %status = ();
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
79 $STAT = OK;
82 open(PS, "ps -eaf | grep $status{nagios_pid} | grep -v grep | ");
83 $PROCCNT = 0;
84 while(<PS>) {
85 $PROCCNT++;
87 close(PS);
89 if ( $STAT == OK ) {
90 print "Nagios OK: located $PROCCNT processes, program status updated $lastupdated seconds ago\n";