check_rrd_data.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/perl -wT
  2. # check_rrd_data plugin for nagios
  3. #
  4. # usage:
  5. # check_rrd machine_id perlexp_warn perlexp_crit perlexp_default [ds]
  6. #
  7. # Checks data from a RRD file. machine_id is normally an IP address, that has
  8. # to be mapped to a RRD file, by means of the config file (by default
  9. # /var/spool/nagios/rrd-files, a file with pairs of (machine_id,rrd_file),
  10. # separated by whitespace). It can be a RRD file, too.
  11. #
  12. # The Perl expressions are expressions to be evaluated in the following cases:
  13. #
  14. # - perlexp_crit. The first one, to check if there is a critical situation. If
  15. # it returns other than "", it will be a critical message.
  16. # - perlexp_warn. The second one to be evaluated. If returns other than "", a
  17. # warning will be issued to Nagios.
  18. # - perlexp_default. If both of the above return "", it will be evaluated, and
  19. # wathever returns this expression will be returned by the script. NOTE that
  20. # this is different from the other two cases, to allow the user issue a
  21. # warning or critical failure even if the other two don't return it.
  22. #
  23. # Use these hosts.cfg entries as examples
  24. #
  25. # command[check_ping]=$USER1$/check_rrd_data.pl $HOSTADDRESS$ \
  26. # 'return "CHECK_CRICKET_PING: Warning\n" if ($value > 10);' 'return \
  27. # "CHECK_CRICKET_PING: Critical\n" if ($value > 100);' 'printf \
  28. # "PING OK - RTA = %.2fms\n", $value; return 0;' 1
  29. # service[machine]=PING;0;24x7;3;5;1;router-admins;240;24x7;1;1;1;;check_ping
  30. #
  31. # initial version: 28 Nov 2000 by Esteban Manchado Velázquez
  32. # current status: 0.1
  33. #
  34. # Copyright Notice: GPL
  35. #
  36. # Doesn't work! Why?
  37. # BEGIN {
  38. # my $runtimedir = substr($0,0,rindex($0,'/'));
  39. # require "$runtimedir/utils.pm";
  40. # }
  41. require '/usr/libexec/nagios/plugins/utils.pm';
  42. use RRD::File;
  43. # use strict; # RRD:File and utils.pm don't like this
  44. my $configfilepath = "/var/spool/nagios/rrd-files"; # Change if needed
  45. my %hostfile; # For storing config
  46. my $rrdfile; # RRD file to open
  47. $ENV{'PATH'} = "/bin:/usr/bin";
  48. $ENV{'ENV'} = "";
  49. if (scalar @ARGV != 4 && scalar @ARGV != 5) {
  50. print STDERR join "' '", @ARGV, "\n";
  51. my $foo = 'check_rrd_data';
  52. print STDERR $foo, " <file.rrd> <perl_exp_warn> <perl_exp_crit> <perl_exp_default> [<ds>]\n\n";
  53. print STDERR "<perl_exp_*> is an expression that gets evaluated with \$_ at the current\n";
  54. print STDERR "value of the data source. If it returns something other than \"\", there\n";
  55. print STDERR "will be a warning or a critical failure. Else, the expression\n";
  56. print STDERR "<perl_exp_default> will be evaluated\n";
  57. exit;
  58. }
  59. # Check configuration file
  60. open F, $configfilepath or do {
  61. print "Can't open config file $configfilepath\n";
  62. return $ERRORS{'UNKNOWN'};
  63. };
  64. while (<F>) {
  65. next unless /(.+)\s+(.+)/;
  66. $hostfile{$1} = $2;
  67. }
  68. close F;
  69. # Default
  70. my $ds = defined $ARGV[4]?$ARGV[4]:0;
  71. # print "\$ds = " . $ds . ":";
  72. # print "\$ARGV[4] = " . $ARGV[4] . ":";
  73. $ds =~ s/\$//g; # Sometimes Nagios gives 1$ as the last parameter
  74. # Guess which RRD file have to be opened
  75. $rrdfile = $ARGV[0] if (-r $ARGV[0]); # First the parameter
  76. $rrdfile = $hostfile{$ARGV[0]} unless $rrdfile; # Second, the config file
  77. # print "$ARGV[0]:";
  78. if (! $rrdfile) {
  79. print "Can't open data file for $ARGV[0]\n"; # Aaaargh!
  80. return $ERRORS{'UNKNOWN'}; # Unknown
  81. }
  82. # print "Opening file $rrdfile:";
  83. my $rrd = new RRD::File ( -file => $rrdfile );
  84. $rrd->open();
  85. if (! $rrd->loadHeader()) {
  86. print "Couldn't read header from $rrdfile\n";
  87. exit $ERRORS{'UNKNOWN'}; # Unknown
  88. }
  89. my $value = $rrd->getDSCurrentValue($ds);
  90. $rrd->close();
  91. # Perl expressions to evaluate
  92. my ($perl_exp_warn, $perl_exp_crit, $perl_exp_default) =
  93. ($ARGV[1], $ARGV[2], $ARGV[3]);
  94. my $result; # Result of the expressions (will be printed)
  95. my @data; # Special data reserved for the expressions, to pass data
  96. # First check for critical errors
  97. $perl_exp_crit =~ /(.*)/;
  98. $perl_exp_crit = $1;
  99. $result = eval $perl_exp_crit;
  100. if ($result) {
  101. print $result;
  102. exit 2; # Critical
  103. }
  104. # Check for warnings
  105. $perl_exp_warn =~ /(.*)/;
  106. $perl_exp_warn = $1;
  107. $result = eval $perl_exp_warn;
  108. if ($result) {
  109. print $result;
  110. exit 1; # Warning
  111. }
  112. $perl_exp_default =~ /(.*)/;
  113. $perl_exp_default = $1;
  114. eval $perl_exp_default; # Normally returns 0 (OK)