forked from throwsb/nagios-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_memoryfree.pl
executable file
·253 lines (211 loc) · 6.25 KB
/
check_memoryfree.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/perl
###################
# check script to check for memory
# usage on linux. The base is
# using the free command
#
# The script is maintained at https://github.com/throwsb/nagios-checks/
#
# Licensed under GNU GPLv3 - see the LICENSE file in the git repository.
#
# Created by David Worth 3-Apr-2015
# version 0.10
###################
use POSIX;
use Getopt::Std;
use Sys::Hostname;
$TRUE = 1;
$FALSE = 0;
$STATUS_OK=0;
$STATUS_WARN=1;
$STATUS_CRIT=2;
$STATUS_UNKNOWN=3;
($MYNAME = $0) =~ s/.*\///;
($HOST) = split /\./, hostname();
$DEBUG=$FALSE;
$STATUS = "UNKNOWN";
$CRITICAL = "90";
$WARNING = "80";
$SWAP=$FALSE;
$DISPLAY_STATUS;
$PROB_STATUS;
$FREE="/usr/bin/free";
$FREEOPTS="-m";
$FREEVERNG="3.3"; ##This is to check for procps and procps-ng
$FREENGFLAG=$FALSE;
$MemTot=0;
$MemTotGB=0;
$MemFree=0;
$MemFreeGB=0;
$MemUsedPerc=0;
$MemUsed=0;
$MemUsedGB=0;
$MemType="";
$SwapTot=0;
$SwapTotGB=0;
$SwapFree=0;
$SwapFreeGB=0;
$SwapUsed=0;
$SwapUsedGB=0;
$SwapUsedPerc=0;
$UNIT="GB";
getopts('dsc:w:') or Usage();
$DEBUG = $TRUE if ($opt_d);
$CRITICAL = $opt_c if ($opt_c);
$WARNING = $opt_w if ($opt_w);
if ($opt_s) {
$SWAP=$TRUE;
}
if ($WARNING >= $CRITICAL) {
print ("\n Warning is greater than or equal to Critical: W:$WARNING C:$CRITICAL\n");
Usage();
}
sub Usage {
warn <<EOF;
$MYNAME - Checks the memory and swap usage on a Linux server using the free command.
usage: $MYNAME -ds -c <critical value> -w <warning value>
-s Check SWAP space too.
-c <interger>
This is the precentage with out % sign. E.G. 95 would
represent 95% utilized. Default value is 95.
Exit with a critical status if greater than the
percent of free memory.
-w <interger>
This is the precentage with out % sign. E.G. 95 would
represent 95% utilized. Default value is 90.
Exit with a warning status if greater than the
percent of free memory.
-d Debug
EOF
print ("UNKOWN: $DISPLAY_STATUS\n");
exit($STATUS_UNKNWN);
}
##Check version of free
$tmpfreever = (split / /, `$FREE -V`)[-1];
chomp($tmpfreever);
if ($tmpfreever =~ /^$FREEVERNG/) {
$FREENGFLAG=$TRUE;
}
Debug ("FREE VERSION > $tmpfreever FLAG > $FREENGFLAG ");
&Get_Memory_Status();
&CalcMem();
&Return_Status();
sub Get_Memory_Status
{
foreach (`$FREE $FREEOPTS`) {
Debug (" LINE > $_ ");
$tmpstrg = $_;
if ($FREENGFLAG) {
if ($tmpstrg =~ /^Mem/) {
Debug ("MATCHED 1 > $tmpstrg");
($mem, $MemTot, $MemUsed, $free, $shared, $buff, $MemFree) = (split /\s+/, $tmpstrg);
Debug ("MEM > $mem TotMem > $total used > $used avail > $avail");
next;
}elsif ($tmpstrg =~ /^Swap/) {
Debug ("MATCHED 3 > $tmpstrg");
($swap, $SwapTot, $SwapUsed, $SwapFree) = (split /\s+ /, $tmpstrg);
Debug ("STot > $SwapTot; SUsed > $SwapUsed; SFree > $SwapFree ");
next;
}
}else {
if ($tmpstrg =~ /^Mem/) {
Debug ("MATCHED 1 > $tmpstrg");
($mem, $MemTot, @junk) = (split /\s+/, $tmpstrg);
Debug ("MEM > $mem TotMem > $MemTot");
next;
}elsif ($tmpstrg =~ /^\-\/\+/) {
Debug ("MATCHED 2 > $tmpstrg");
($buff, $MemUsed, $MemFree) = (split /\s+ /, $tmpstrg);
Debug ("Used > $MemUsed Free > $MemFree");
next;
}elsif ($tmpstrg =~ /^Swap/) {
Debug ("MATCHED 3 > $tmpstrg");
($swap, $SwapTot, $SwapUsed, $SwapFree) = (split /\s+ /, $tmpstrg);
Debug ("STot > $SwapTot; SUsed > $SwapUsed; SFree > $SwapFree ");
next;
}
}
}
}
sub CalcMem {
##Physical Memory
$MemType="Physical";
##$MemUsedPerc = ceil((($MemTot - $MemFree)*100)/$MemTot);
$MemUsedPerc = ceil(($MemUsed*100)/$MemTot);
Debug("perc > $MemUsedPerc\%");
##Convert to GB
$MemTotGB = ($MemTot/1024);
$MemTotGB = sprintf"%.2f",$MemTotGB;
$MemFreeGB = ($MemFree/1024);
$MemFreeGB = sprintf"%.2f",$MemFreeGB;
$MemUsedGB = ($MemUsed/1024);
$MemUsedGB = sprintf"%.2f",$MemUsedGB;
Debug("MemTotGB:$MemTotGB$UNIT, MemFreeGB:$MemFreeGB$UNIT, TOT:$MemTot FREE:$MemFree USED:$MemUsedGB$UNIT PercUsed:$MemUsedPerc\%");
$DISPLAY_STATUS = "$MemType=$MemUsedPerc\%;$WARNING;$CRITICAL;;";
Debug("DISPLAY > $DISPLAY_STATUS");
##Call NAgios Status to determine health
&Nagios_Status($MemType,$MemUsedPerc,$MemTotGB,$MemFreeGB,$MemUsedGB);
if ($SWAP) {
##Swap Space
$MemType="SWAP";
$SwapUsedPerc = ceil(($SwapUsed*100)/$SwapTot);
Debug("perc > $SwapUsedPerc\%");
##Convert to GB
$SwapTotGB = ($SwapTot/1024);
$SwapTotGB = sprintf"%.2f",$SwapTotGB;
$SwapFreeGB = ($SwapFree/1024);
$SwapFreeGB = sprintf"%.2f",$SwapFreeGB;
$SwapUsedGB = ($SwapUsed/1024);
$SwapUsedGB = sprintf"%.2f",$SwapUsedGB;
Debug("SwapTotGB:$SwapTotGB$UNIT, SwapFreeGB:$SwapFreeGB$UNIT, TOT:$SwapTot FREE:$SwapFree USED:$SwapUsedGB$UNIT PercUsed:$SwapUsedPerc\%");
$DISPLAY_STATUS = "$DISPLAY_STATUS $MemType=$SwapUsedPerc\%;$WARNING;$CRITICAL;;";
Debug("DISPLAY > $DISPLAY_STATUS");
##Call NAgios Status to determine health
&Nagios_Status($MemType,$SwapUsedPerc,$SwapTotGB,$SwapFreeGB,$SwapUsedGB);
}
}
sub Nagios_Status {
my $MemType = shift;
my $PercUsed = shift;
my $TotMem = shift;
my $FreeMem = shift;
my $UsedMem = shift;
##Nagios Status
if ($CRITICAL < $PercUsed) {
$STATUS = "CRITICAL";
$PROB_STATUS = "$PROB_STATUS $MemType=$PercUsed\%;Tot:$TotMem$UNIT;Free:$FreeMem$UNIT;Used:$UsedMem$UNIT";
} elsif ($WARNING < $PercUsed) {
if ($STATUS eq "CRITICAL") {
$PROB_STATUS = "$PROB_STATUS $MemType=$PercUsed\%;Tot:$TotMem$UNIT;Free:$FreeMem$UNIT;Used:$UsedMem$UNIT";
} elsif (($STATUS eq "UNKNOWN") || ($STATUS eq "WARNING") || ($STATUS eq "OK")) {
$STATUS = "WARNING";
$PROB_STATUS = "$PROB_STATUS $MemType=$PercUsed\%;Tot:$TotMem$UNIT;Free:$FreeMem$UNIT;Used:$UsedMem$UNIT";
}
} else {
if(($STATUS eq "UNKNOWN") || ($STATUS eq "OK")) {
$STATUS = "OK";
}
}
&Debug("$STATUS $DISPLAY_STATUS");
&Debug("$STATUS PROB LIST > $PROB_STATUS");
}
sub Return_Status {
if ($STATUS eq "CRITICAL") {
print ("$STATUS $PROB_STATUS|$DISPLAY_STATUS\n");
exit($STATUS_CRIT);
}elsif ($STATUS eq "WARNING") {
print ("$STATUS $PROB_STATUS|$DISPLAY_STATUS\n");
exit($STATUS_WARN);
}elsif ($STATUS eq "OK") {
print ("OK|$DISPLAY_STATUS\n");
exit($STATUS_OK);
}else {
print ("UNKNOWN|$DISPLAY_STATUS\n");
exit($STATUS_UNKNOWN);
}
}
sub Debug
{
my ($msg) = @_;
warn "$msg\n" if ($DEBUG);
}