plugin_wrapper.pl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #! /usr/bin/perl
  2. #####################################################################
  3. # (c) 2012 Sebastian "tokkee" Harl <sh@teamix.net> #
  4. # and team(ix) GmbH, Nuernberg, Germany #
  5. # #
  6. # This file is part of "team(ix) Monitoring Plugins" #
  7. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  8. # #
  9. # This file is free software: you can redistribute it and/or modify #
  10. # it under the terms of the GNU General Public License as published #
  11. # by the Free Software Foundation, either version 2 of the License, #
  12. # or (at your option) any later version. #
  13. # #
  14. # This file is distributed in the hope that it will be useful, but #
  15. # WITHOUT ANY WARRANTY; without even the implied warranty of #
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  17. # GNU General Public License for more details. #
  18. # #
  19. # You should have received a copy of the GNU General Public License #
  20. # along with this file. If not, see <http://www.gnu.org/licenses/>. #
  21. #####################################################################
  22. use strict;
  23. use warnings;
  24. my $p_script = shift;
  25. my @p_args = @ARGV;
  26. if (! $p_script) {
  27. exit_usage(1);
  28. }
  29. my $p_stdout;
  30. my $p_pid = open($p_stdout, '-|', "$p_script @p_args 2>&1");
  31. if (! defined($p_pid)) {
  32. print "CRITICAL: Failed to execute plugin ($p_script): $!\n";
  33. print "Commandline: $p_script @p_args\n";
  34. exit 2;
  35. }
  36. my @p_output = <$p_stdout>;
  37. if (waitpid($p_pid, 0) != $p_pid) {
  38. print "UNKNOWN: Lost track of plugin process\n";
  39. exit 3;
  40. }
  41. my $p_rc = $?;
  42. my $p_sig = $p_rc & 127;
  43. my $p_cd = $p_rc & 128; # core dumped?
  44. $p_rc >>= 8;
  45. close $p_stdout;
  46. if ($p_sig || $p_cd) {
  47. print "CRITICAL: Plugin died with signal $p_sig (exit code: $p_rc)"
  48. . ($p_cd ? " (core dumped)" : "") . "\n";
  49. $p_rc = 2;
  50. }
  51. if ($p_rc == 255) {
  52. print "CRITICAL: Plugin died with status 255 "
  53. . "(see details for more info)\n";
  54. print "Commandline: $p_script @p_args\n";
  55. }
  56. my $p_output = join('', @p_output);
  57. print $p_output;
  58. if (($p_rc < 0) || ($p_rc > 3)) {
  59. $p_rc = 3;
  60. }
  61. exit $p_rc;
  62. sub exit_usage {
  63. my $status = shift || 0;
  64. print STDERR "Usage: $0 <plugin> [<args>]\n";
  65. exit $status;
  66. }