|
1 #! /usr/bin/perl -w |
|
2 |
|
3 # Copyright (C) 2011 Christian Arnold |
|
4 # |
|
5 # This program is free software: you can redistribute it and/or modify |
|
6 # it under the terms of the GNU General Public License as published by |
|
7 # the Free Software Foundation, either version 3 of the License, or |
|
8 # (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 of |
|
12 # 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. If not, see <http://www.gnu.org/licenses/>. |
|
17 # |
|
18 # Christian Arnold <arnold@schlittermann.de> |
|
19 |
|
20 use 5.010; |
|
21 use strict; |
|
22 use File::Basename; |
|
23 use Getopt::Long; |
|
24 use Pod::Usage; |
|
25 |
|
26 delete @ENV{ grep /^LC_/ => keys %ENV }; |
|
27 $ENV{LANG} = "C"; |
|
28 $ENV{LC_ALL} = "C"; |
|
29 |
|
30 sub version($$); |
|
31 sub get_meminfo(); |
|
32 sub report($$$$); |
|
33 |
|
34 my %ERRORS = ( |
|
35 OK => 0, |
|
36 WARNING => 1, |
|
37 CRITICAL => 2, |
|
38 UNKNOWN => 3, |
|
39 DEPENDENT => 4 |
|
40 ); |
|
41 |
|
42 my $ME = basename $0; |
|
43 my $NAME = "MEM"; |
|
44 my $VERSION = "0.1"; |
|
45 |
|
46 my %opt = ( |
|
47 warning => 70, |
|
48 critical => 90 |
|
49 ); |
|
50 |
|
51 MAIN: { |
|
52 Getopt::Long::Configure('bundling'); |
|
53 GetOptions( |
|
54 "w|warning=i" => \$opt{warning}, |
|
55 "c|critical=i" => \$opt{critical}, |
|
56 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
|
57 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
|
58 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
|
59 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
|
60 |
|
61 my ( $percent, $total, $used, $free ) = get_meminfo(); |
|
62 report( $percent, $total, $used, $free ); |
|
63 } |
|
64 |
|
65 sub report($$$$) { |
|
66 my ( $percent, $total, $used, $free ) = @_; |
|
67 |
|
68 if ( $percent >= $opt{critical} ) { |
|
69 say |
|
70 "$NAME CRITICAL: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}"; |
|
71 exit $ERRORS{CRITICAL}; |
|
72 } |
|
73 if ( $percent >= $opt{warning} ) { |
|
74 say |
|
75 "$NAME WARNING: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}"; |
|
76 exit $ERRORS{WARNING}; |
|
77 } |
|
78 say |
|
79 "$NAME OK: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}"; |
|
80 exit $ERRORS{OK}; |
|
81 } |
|
82 |
|
83 sub get_meminfo() { |
|
84 my ( $total, $free, $used, $percent ); |
|
85 my $file = '/proc/meminfo'; |
|
86 |
|
87 open( FH, $file ) |
|
88 or say "$NAME CRITICAL: $file - $!." and exit $ERRORS{CRITICAL}; |
|
89 |
|
90 while (<FH>) { |
|
91 /^MemTotal:\s+(?<total>[\d.]+)/ |
|
92 and $total = ( sprintf( "%.0f", $+{total} / 1024 ) ) |
|
93 and next; |
|
94 /^MemFree:\s+(?<free>[\d.]+)/ |
|
95 and $free = ( sprintf( "%.0f", $+{free} / 1024 ) ) |
|
96 and last; |
|
97 } |
|
98 close(FH); |
|
99 |
|
100 $used = ( $total - $free ); |
|
101 $percent = ( sprintf( "%.0f", ( $used / $total ) * 100 ) ); |
|
102 |
|
103 return ( $percent, $total, $used, $free ); |
|
104 } |
|
105 |
|
106 sub version($$) { |
|
107 my ( $progname, $version ) = @_; |
|
108 |
|
109 say <<_VERSION; |
|
110 $progname version $version |
|
111 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
112 |
|
113 $ME comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
114 and you are welcome to redistribute it under certain conditions. |
|
115 See the GNU General Public Licence for details. |
|
116 _VERSION |
|
117 } |
|
118 |
|
119 __END__ |
|
120 |
|
121 =head1 NAME |
|
122 |
|
123 check_mem - nagios plugin to check memory usage |
|
124 |
|
125 =head1 SYNOPSIS |
|
126 |
|
127 check_mem [B<-w>|B<--warning> I<integer>] [B<-c>|B<--critical> I<integer>] |
|
128 |
|
129 check_mem [B<-h>|B<--help>] |
|
130 |
|
131 check_mem [B<-m>|B<--man>] |
|
132 |
|
133 check_mem [B<-v>|B<--version>] |
|
134 |
|
135 =head1 OPTIONS |
|
136 |
|
137 =over |
|
138 |
|
139 =item B<-w>|B<--warning> I<integer> |
|
140 |
|
141 Exit with WARNING status if memory usage in percent exceeds this value (default: 70) |
|
142 |
|
143 =item B<-c>|B<--critical> I<integer> |
|
144 |
|
145 Exit with CRITICAL status if memory usage in percent exceeds this value (default: 90) |
|
146 |
|
147 =item B<-h>|B<--help> |
|
148 |
|
149 Print detailed help screen. |
|
150 |
|
151 =item B<-m>|B<--man> |
|
152 |
|
153 Print manual page. |
|
154 |
|
155 =item B<-V>|B<--version> |
|
156 |
|
157 Print version information. |
|
158 |
|
159 =back |
|
160 |
|
161 =head1 DESCRIPTION |
|
162 |
|
163 This nagios plugin check the memory usage. |
|
164 |
|
165 =head1 VERSION |
|
166 |
|
167 This man page is current for version 0.1 of B<check_mem>. |
|
168 |
|
169 =head1 AUTHOR |
|
170 |
|
171 Written by Christian Arnold L<arnold@schlittermann.de> |
|
172 |
|
173 =head1 COPYRIGHT |
|
174 |
|
175 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
176 This is free software, and you are welcome to redistribute it under certain conditions. |
|
177 See the GNU General Public Licence for details. |