check_nagios.pl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/perl
  2. # denao - denao@uol.com.br - Systems Engineering
  3. # Universo Online - http://www.uol.com.br
  4. use DBI;
  5. use Time::Local;
  6. my $t_lambuja = 5; # (expire_minutes)
  7. my $databasename = ""; # The name of nagios database (i.e.: nagios)
  8. my $table = "programstatus";
  9. my $where = "localhost"; # The machine where the database
  10. my $port = "3306";
  11. my $base = "DBI:mysql:$databasename:$where:$port";
  12. my $user = ""; # the user to connect to the database
  13. # (needs permission to "select at programstatus table only"
  14. my $password = ""; # the password (if any)
  15. my %results;
  16. my @fields = qw( last_update );
  17. my $dbh = DBI->connect($base,$user,$password);
  18. my $fields = join(', ', @fields);
  19. my $query = "SELECT $fields FROM $table";
  20. my $sth = $dbh->prepare($query);
  21. $sth->execute();
  22. @results{@fields} = ();
  23. $sth->bind_columns(map { \$results{$_} } @fields);
  24. $sth->fetch();
  25. $sth->finish();
  26. $dbh->disconnect();
  27. check_update();
  28. sub check_update {
  29. ($yea,$mon,$day,$hou,$min,$sec)=($results{last_update}=~/(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/);
  30. ($sec_now, $min_now, $hou_now, $day_now, $mon_now, $yea_now) = (localtime(time))[0,1,2,3,4,5];
  31. $mon_now+=1; $yea_now+=1900;
  32. $unixdate=timelocal($sec,$min,$hou,$day,$mon,$yea);
  33. $unixdate_now=timelocal($sec_now,$min_now,$hou_now,$day_now,$mon_now,$yea_now);
  34. if (scalar($unixdate_now - $unixdate) > scalar($t_lambuja * 60)) {
  35. print "Nagios problem: nagios is down, for at least " . scalar($t_lambuja) . " minutes.\n";
  36. exit(1);
  37. } else {
  38. print "Nagios ok: status data updated " . scalar($unixdate_now - $unixdate) . " seconds ago\n";
  39. exit(0);
  40. }
  41. }