check_mem.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/perl -w
  2. # $Id$
  3. # check_mem.pl Copyright (C) 2000 Dan Larsson <dl@tyfon.net>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty
  12. # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # you should have received a copy of the GNU General Public License
  16. # along with this program (or with Nagios); if not, write to the
  17. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. # Boston, MA 02111-1307, USA
  19. # Tell Perl what we need to use
  20. use strict;
  21. use Getopt::Std;
  22. use vars qw($opt_c $opt_f $opt_u $opt_w
  23. $free_memory $used_memory $total_memory
  24. $crit_level $warn_level
  25. %exit_codes @memlist
  26. $percent $fmt_pct
  27. $verb_err $command_line);
  28. # Predefined exit codes for Nagios
  29. %exit_codes = ('UNKNOWN' ,-1,
  30. 'OK' , 0,
  31. 'WARNING' , 1,
  32. 'CRITICAL', 2,);
  33. # Turn this to 1 to see reason for parameter errors (if any)
  34. $verb_err = 0;
  35. # This the unix command string that brings Perl the data
  36. $command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
  37. chomp $command_line;
  38. @memlist = split(/ /, $command_line);
  39. # Define the calculating scalars
  40. $used_memory = $memlist[0];
  41. $free_memory = $memlist[1];
  42. $total_memory = $used_memory + $free_memory;
  43. # Get the options
  44. if ($#ARGV le 0)
  45. {
  46. &usage;
  47. }
  48. else
  49. {
  50. getopts('c:fuw:');
  51. }
  52. # Shortcircuit the switches
  53. if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
  54. {
  55. print "*** You must define WARN and CRITICAL levels!" if ($verb_err);
  56. &usage;
  57. }
  58. elsif (!$opt_f and !$opt_u)
  59. {
  60. print "*** You must select to monitor either USED or FREE memory!" if ($verb_err);
  61. &usage;
  62. }
  63. # Check if levels are sane
  64. if ($opt_w <= $opt_c and $opt_f)
  65. {
  66. print "*** WARN level must not be less than CRITICAL when checking FREE memory!" if ($verb_err);
  67. &usage;
  68. }
  69. elsif ($opt_w >= $opt_c and $opt_u)
  70. {
  71. print "*** WARN level must not be greater than CRITICAL when checking USED memory!" if ($verb_err);
  72. &usage;
  73. }
  74. $warn_level = $opt_w;
  75. $crit_level = $opt_c;
  76. if ($opt_f)
  77. {
  78. $percent = $free_memory / $total_memory * 100;
  79. $fmt_pct = sprintf "%.1f", $percent;
  80. if ($percent <= $crit_level)
  81. {
  82. print "Memory CRITICAL - $fmt_pct% ($free_memory kB) free\n";
  83. exit $exit_codes{'CRITICAL'};
  84. }
  85. elsif ($percent <= $warn_level)
  86. {
  87. print "Memory WARNING - $fmt_pct% ($free_memory kB) free\n";
  88. exit $exit_codes{'WARNING'};
  89. }
  90. else
  91. {
  92. print "Memory OK - $fmt_pct% ($free_memory kB) free\n";
  93. exit $exit_codes{'OK'};
  94. }
  95. }
  96. elsif ($opt_u)
  97. {
  98. $percent = $used_memory / $total_memory * 100;
  99. $fmt_pct = sprintf "%.1f", $percent;
  100. if ($percent >= $crit_level)
  101. {
  102. print "Memory CRITICAL - $fmt_pct% ($used_memory kB) used\n";
  103. exit $exit_codes{'CRITICAL'};
  104. }
  105. elsif ($percent >= $warn_level)
  106. {
  107. print "Memory WARNING - $fmt_pct% ($used_memory kB) used\n";
  108. exit $exit_codes{'WARNING'};
  109. }
  110. else
  111. {
  112. print "Memory OK - $fmt_pct% ($used_memory kB) used\n";
  113. exit $exit_codes{'OK'};
  114. }
  115. }
  116. # Show usage
  117. sub usage()
  118. {
  119. print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n";
  120. print "usage:\n";
  121. print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
  122. print "options:\n";
  123. print " -f Check FREE memory\n";
  124. print " -u Check USED memory\n";
  125. print " -w PERCENT Percent free/used when to warn\n";
  126. print " -c PERCENT Percent free/used when critical\n";
  127. print "\nCopyright (C) 2000 Dan Larsson <dl\@tyfon.net>\n";
  128. print "check_mem.pl comes with absolutely NO WARRANTY either implied or explicit\n";
  129. print "This program is licensed under the terms of the\n";
  130. print "GNU General Public License (check source code for details)\n";
  131. exit $exit_codes{'UNKNOWN'};
  132. }