check_ram 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #!/usr/bin/perl -w
  2. # Heavily based on the script from:
  3. # check_mem.pl Copyright (C) 2000 Dan Larsson <dl@tyfon.net>
  4. # heavily modified by
  5. # Justin Ellison <justin@techadvise.com>
  6. #
  7. # The MIT License (MIT)
  8. # Copyright (c) 2011 justin@techadvise.com
  9. # Permission is hereby granted, free of charge, to any person obtaining a copy of this
  10. # software and associated documentation files (the "Software"), to deal in the Software
  11. # without restriction, including without limitation the rights to use, copy, modify,
  12. # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to the following conditions:
  14. # The above copyright notice and this permission notice shall be included in all copies
  15. # or substantial portions of the Software.
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  17. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  18. # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  19. # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
  20. # OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. # OTHER DEALINGS IN THE SOFTWARE.
  22. # Tell Perl what we need to use
  23. use strict;
  24. use Getopt::Std;
  25. #TODO - Convert to Nagios::Plugin
  26. #TODO - Use an alarm
  27. # Predefined exit codes for Nagios
  28. use vars qw($opt_c $opt_f $opt_u $opt_w $opt_C $opt_v %exit_codes);
  29. %exit_codes = ('UNKNOWN' , 3,
  30. 'OK' , 0,
  31. 'WARNING' , 1,
  32. 'CRITICAL', 2,
  33. );
  34. # Get our variables, do our checking:
  35. init();
  36. # Get the numbers:
  37. my ($free_memory_kb,$used_memory_kb,$caches_kb) = get_memory_info();
  38. print "$free_memory_kb Free\n$used_memory_kb Used\n$caches_kb Cache\n" if ($opt_v);
  39. if ($opt_C) { #Do we count caches as free?
  40. $used_memory_kb -= $caches_kb;
  41. $free_memory_kb += $caches_kb;
  42. }
  43. # Round to the nearest KB
  44. $free_memory_kb = sprintf('%d',$free_memory_kb);
  45. $used_memory_kb = sprintf('%d',$used_memory_kb);
  46. $caches_kb = sprintf('%d',$caches_kb);
  47. # Tell Nagios what we came up with
  48. tell_nagios($used_memory_kb,$free_memory_kb,$caches_kb);
  49. sub tell_nagios {
  50. my ($used,$free,$caches) = @_;
  51. # Calculate Total Memory
  52. my $total = $free + $used;
  53. print "$total Total\n" if ($opt_v);
  54. my $perf_warn;
  55. my $perf_crit;
  56. if ( $opt_u ) {
  57. $perf_warn = int(${total} * $opt_w / 100);
  58. $perf_crit = int(${total} * $opt_c / 100);
  59. } else {
  60. $perf_warn = int(${total} * ( 100 - $opt_w ) / 100);
  61. $perf_crit = int(${total} * ( 100 - $opt_c ) / 100);
  62. }
  63. my $perfdata = "|TOTAL=${total}KB;;;; USED=${used}KB;${perf_warn};${perf_crit};; FREE=${free}KB;;;; CACHES=${caches}KB;;;;";
  64. if ($opt_f) {
  65. my $percent = sprintf "%.1f", ($free / $total * 100);
  66. if ($percent <= $opt_c) {
  67. finish("CRITICAL - $percent% ($free kB) free!$perfdata",$exit_codes{'CRITICAL'});
  68. }
  69. elsif ($percent <= $opt_w) {
  70. finish("WARNING - $percent% ($free kB) free!$perfdata",$exit_codes{'WARNING'});
  71. }
  72. else {
  73. finish("OK - $percent% ($free kB) free.$perfdata",$exit_codes{'OK'});
  74. }
  75. }
  76. elsif ($opt_u) {
  77. my $percent = sprintf "%.1f", ($used / $total * 100);
  78. if ($percent >= $opt_c) {
  79. finish("CRITICAL - $percent% ($used kB) used!$perfdata",$exit_codes{'CRITICAL'});
  80. }
  81. elsif ($percent >= $opt_w) {
  82. finish("WARNING - $percent% ($used kB) used!$perfdata",$exit_codes{'WARNING'});
  83. }
  84. else {
  85. finish("OK - $percent% ($used kB) used.$perfdata",$exit_codes{'OK'});
  86. }
  87. }
  88. }
  89. # Show usage
  90. sub usage() {
  91. print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n";
  92. print "usage:\n";
  93. print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
  94. print "options:\n";
  95. print " -f Check FREE memory\n";
  96. print " -u Check USED memory\n";
  97. print " -C Count OS caches as FREE memory\n";
  98. print " -w PERCENT Percent free/used when to warn\n";
  99. print " -c PERCENT Percent free/used when critical\n";
  100. print "\nCopyright (C) 2000 Dan Larsson <dl\@tyfon.net>\n";
  101. print "check_mem.pl comes with absolutely NO WARRANTY either implied or explicit\n";
  102. print "This program is licensed under the terms of the\n";
  103. print "MIT License (check source code for details)\n";
  104. exit $exit_codes{'UNKNOWN'};
  105. }
  106. sub get_memory_info {
  107. my $used_memory_kb = 0;
  108. my $free_memory_kb = 0;
  109. my $total_memory_kb = 0;
  110. my $caches_kb = 0;
  111. my $uname;
  112. if ( -e '/usr/bin/uname') {
  113. $uname = `/usr/bin/uname -a`;
  114. }
  115. elsif ( -e '/bin/uname') {
  116. $uname = `/bin/uname -a`;
  117. }
  118. else {
  119. die "Unable to find uname in /usr/bin or /bin!\n";
  120. }
  121. print "uname returns $uname" if ($opt_v);
  122. if ( $uname =~ /Linux/ ) {
  123. my @meminfo = `/bin/cat /proc/meminfo`;
  124. foreach (@meminfo) {
  125. chomp;
  126. if (/^Mem(Total|Free):\s+(\d+) kB/) {
  127. my $counter_name = $1;
  128. if ($counter_name eq 'Free') {
  129. $free_memory_kb = $2;
  130. }
  131. elsif ($counter_name eq 'Total') {
  132. $total_memory_kb = $2;
  133. }
  134. }
  135. elsif (/^(Buffers|Cached|SReclaimable):\s+(\d+) kB/) {
  136. $caches_kb += $2;
  137. }
  138. }
  139. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  140. }
  141. elsif ( $uname =~ /HP-UX/ ) {
  142. # HP-UX, thanks to Christoph Fürstaller
  143. my @meminfo = `/usr/bin/sudo /usr/local/bin/kmeminfo`;
  144. foreach (@meminfo) {
  145. chomp;
  146. if (/^Physical memory\s\s+=\s+(\d+)\s+(\d+.\d)g/) {
  147. $total_memory_kb = ($2 * 1024 * 1024);
  148. }
  149. elsif (/^Free memory\s\s+=\s+(\d+)\s+(\d+.\d)g/) {
  150. $free_memory_kb = ($2 * 1024 * 1024);
  151. }
  152. }
  153. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  154. }
  155. elsif ( $uname =~ /FreeBSD/ ) {
  156. # The FreeBSD case. 2013-03-19 www.claudiokuenzler.com
  157. # free mem = Inactive*Page Size + Cache*Page Size + Free*Page Size
  158. my $pagesize = `sysctl vm.stats.vm.v_page_size`;
  159. $pagesize =~ s/[^0-9]//g;
  160. my $mem_inactive = 0;
  161. my $mem_cache = 0;
  162. my $mem_free = 0;
  163. my $mem_total = 0;
  164. my $free_memory = 0;
  165. my @meminfo = `/sbin/sysctl vm.stats.vm`;
  166. foreach (@meminfo) {
  167. chomp;
  168. if (/^vm.stats.vm.v_inactive_count:\s+(\d+)/) {
  169. $mem_inactive = ($1 * $pagesize);
  170. }
  171. elsif (/^vm.stats.vm.v_cache_count:\s+(\d+)/) {
  172. $mem_cache = ($1 * $pagesize);
  173. }
  174. elsif (/^vm.stats.vm.v_free_count:\s+(\d+)/) {
  175. $mem_free = ($1 * $pagesize);
  176. }
  177. elsif (/^vm.stats.vm.v_page_count:\s+(\d+)/) {
  178. $mem_total = ($1 * $pagesize);
  179. }
  180. }
  181. $free_memory = $mem_inactive + $mem_cache + $mem_free;
  182. $free_memory_kb = ( $free_memory / 1024);
  183. $total_memory_kb = ( $mem_total / 1024);
  184. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  185. $caches_kb = ($mem_cache / 1024);
  186. }
  187. elsif ( $uname =~ /joyent/ ) {
  188. # The SmartOS case. 2014-01-10 www.claudiokuenzler.com
  189. # free mem = pagesfree * pagesize
  190. my $pagesize = `pagesize`;
  191. my $phys_pages = `kstat -p unix:0:system_pages:pagestotal | awk '{print \$NF}'`;
  192. my $free_pages = `kstat -p unix:0:system_pages:pagesfree | awk '{print \$NF}'`;
  193. my $arc_size = `kstat -p zfs:0:arcstats:size | awk '{print \$NF}'`;
  194. my $arc_size_kb = $arc_size / 1024;
  195. print "Pagesize is $pagesize" if ($opt_v);
  196. print "Total pages is $phys_pages" if ($opt_v);
  197. print "Free pages is $free_pages" if ($opt_v);
  198. print "Arc size is $arc_size" if ($opt_v);
  199. $caches_kb += $arc_size_kb;
  200. $total_memory_kb = $phys_pages * $pagesize / 1024;
  201. $free_memory_kb = $free_pages * $pagesize / 1024;
  202. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  203. }
  204. elsif ( $uname =~ /SunOS/ ) {
  205. eval "use Sun::Solaris::Kstat";
  206. if ($@) { #Kstat not available
  207. if ($opt_C) {
  208. print "You can't report on Solaris caches without Sun::Solaris::Kstat available!\n";
  209. exit $exit_codes{UNKNOWN};
  210. }
  211. my @vmstat = `/usr/bin/vmstat 1 2`;
  212. my $line;
  213. foreach (@vmstat) {
  214. chomp;
  215. $line = $_;
  216. }
  217. $free_memory_kb = (split(/ /,$line))[5] / 1024;
  218. my @prtconf = `/usr/sbin/prtconf`;
  219. foreach (@prtconf) {
  220. if (/^Memory size: (\d+) Megabytes/) {
  221. $total_memory_kb = $1 * 1024;
  222. }
  223. }
  224. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  225. }
  226. else { # We have kstat
  227. my $kstat = Sun::Solaris::Kstat->new();
  228. my $phys_pages = ${kstat}->{unix}->{0}->{system_pages}->{physmem};
  229. my $free_pages = ${kstat}->{unix}->{0}->{system_pages}->{freemem};
  230. # We probably should account for UFS caching here, but it's unclear
  231. # to me how to determine UFS's cache size. There's inode_cache,
  232. # and maybe the physmem variable in the system_pages module??
  233. # In the real world, it looks to be so small as not to really matter,
  234. # so we don't grab it. If someone can give me code that does this,
  235. # I'd be glad to put it in.
  236. my $arc_size = (exists ${kstat}->{zfs} && ${kstat}->{zfs}->{0}->{arcstats}->{size}) ?
  237. ${kstat}->{zfs}->{0}->{arcstats}->{size} / 1024
  238. : 0;
  239. $caches_kb += $arc_size;
  240. my $pagesize = `pagesize`;
  241. $total_memory_kb = $phys_pages * $pagesize / 1024;
  242. $free_memory_kb = $free_pages * $pagesize / 1024;
  243. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  244. }
  245. }
  246. elsif ( $uname =~ /Darwin/ ) {
  247. $total_memory_kb = (split(/ /,`/usr/sbin/sysctl hw.memsize`))[1]/1024;
  248. my $pagesize = (split(/ /,`/usr/sbin/sysctl hw.pagesize`))[1];
  249. $caches_kb = 0;
  250. my @vm_stat = `/usr/bin/vm_stat`;
  251. foreach (@vm_stat) {
  252. chomp;
  253. if (/^(Pages free):\s+(\d+)\.$/) {
  254. $free_memory_kb = $2*$pagesize/1024;
  255. }
  256. # 'caching' concept works different on MACH
  257. # this should be a reasonable approximation
  258. elsif (/^Pages (inactive|purgable):\s+(\d+).$/) {
  259. $caches_kb += $2*$pagesize/1024;
  260. }
  261. }
  262. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  263. }
  264. elsif ( $uname =~ /AIX/ ) {
  265. my @meminfo = `/usr/bin/vmstat -vh`;
  266. foreach (@meminfo) {
  267. chomp;
  268. if (/^\s*([0-9.]+)\s+(.*)/) {
  269. my $counter_name = $2;
  270. if ($counter_name eq 'memory pages') {
  271. $total_memory_kb = $1*4;
  272. }
  273. if ($counter_name eq 'free pages') {
  274. $free_memory_kb = $1*4;
  275. }
  276. if ($counter_name eq 'file pages') {
  277. $caches_kb = $1*4;
  278. }
  279. if ($counter_name eq 'Number of 4k page frames loaned') {
  280. $free_memory_kb += $1*4;
  281. }
  282. }
  283. }
  284. $used_memory_kb = $total_memory_kb - $free_memory_kb;
  285. }
  286. else {
  287. if ($opt_C) {
  288. print "You can't report on $uname caches!\n";
  289. exit $exit_codes{UNKNOWN};
  290. }
  291. my $command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
  292. chomp $command_line;
  293. my @memlist = split(/ /, $command_line);
  294. # Define the calculating scalars
  295. $used_memory_kb = $memlist[0]/1024;
  296. $free_memory_kb = $memlist[1]/1024;
  297. $total_memory_kb = $used_memory_kb + $free_memory_kb;
  298. }
  299. return ($free_memory_kb,$used_memory_kb,$caches_kb);
  300. }
  301. sub init {
  302. # Get the options
  303. if ($#ARGV le 0) {
  304. &usage;
  305. }
  306. else {
  307. getopts('c:fuCvw:');
  308. }
  309. # Shortcircuit the switches
  310. if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0) {
  311. print "*** You must define WARN and CRITICAL levels!\n";
  312. &usage;
  313. }
  314. elsif (!$opt_f and !$opt_u) {
  315. print "*** You must select to monitor either USED or FREE memory!\n";
  316. &usage;
  317. }
  318. # Check if levels are sane
  319. if ($opt_w <= $opt_c and $opt_f) {
  320. print "*** WARN level must not be less than CRITICAL when checking FREE memory!\n";
  321. &usage;
  322. }
  323. elsif ($opt_w >= $opt_c and $opt_u) {
  324. print "*** WARN level must not be greater than CRITICAL when checking USED memory!\n";
  325. &usage;
  326. }
  327. }
  328. sub finish {
  329. my ($msg,$state) = @_;
  330. print "$msg\n";
  331. exit $state;
  332. }