Merge ~sudeephb/charm-nrpe:bug/1988360 into charm-nrpe:master

Proposed by Sudeep Bhandari
Status: Merged
Approved by: Eric Chen
Approved revision: 4cda43666fee0c9561cb817607be8f3a2c21a5ef
Merged at revision: c42aebca6bb2c90fa958316a2d6eb851bbecd39a
Proposed branch: ~sudeephb/charm-nrpe:bug/1988360
Merge into: charm-nrpe:master
Diff against target: 315 lines (+157/-49)
1 file modified
files/plugins/check_mem.pl (+157/-49)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Eric Chen Approve
Erhan Sunar (community) Approve
BootStack Reviewers Pending
BootStack Reviewers Pending
Review via email: mp+430292@code.launchpad.net

Description of the change

We might need to test and reopen the bug - https://bugs.launchpad.net/charm-nrpe/+bug/1930307

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Erhan Sunar (esunar) wrote :

check_mem.pl results are not consistent with my checks. Approved referring to discussion(https://chat.canonical.com/canonical/pl/5n4sak7j57bqigyuugnffbsfmy)
Other parts are ok.

review: Approve
Revision history for this message
Eric Chen (eric-chen) wrote :
review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Failed to merge change (unable to merge source repository due to conflicts), setting status to needs review.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision c42aebca6bb2c90fa958316a2d6eb851bbecd39a

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/files/plugins/check_mem.pl b/files/plugins/check_mem.pl
2index a1cd87c..7ec79fe 100755
3--- a/files/plugins/check_mem.pl
4+++ b/files/plugins/check_mem.pl
5@@ -32,7 +32,7 @@ use Getopt::Std;
6 #TODO - Use an alarm
7
8 # Predefined exit codes for Nagios
9-use vars qw($opt_c $opt_f $opt_u $opt_w $opt_C $opt_v $opt_h %exit_codes);
10+use vars qw($opt_c $opt_f $opt_u $opt_a $opt_w $opt_C $opt_v $opt_h %exit_codes);
11 %exit_codes = ('UNKNOWN' , 3,
12 'OK' , 0,
13 'WARNING' , 1,
14@@ -43,8 +43,9 @@ use vars qw($opt_c $opt_f $opt_u $opt_w $opt_C $opt_v $opt_h %exit_codes);
15 init();
16
17 # Get the numbers:
18-my ($free_memory_kb,$used_memory_kb,$caches_kb,$hugepages_kb) = get_memory_info();
19+my ($free_memory_kb,$used_memory_kb,$caches_kb,$available_memory_kb,$hugepages_kb) = get_memory_info();
20 print "$free_memory_kb Free\n$used_memory_kb Used\n$caches_kb Cache\n" if ($opt_v);
21+print "$available_memory_kb Available\n" if ($opt_v and $opt_a);
22 print "$hugepages_kb Hugepages\n" if ($opt_v and $opt_h);
23
24 if ($opt_C) { #Do we count caches as free?
25@@ -59,56 +60,112 @@ if ($opt_h) {
26 print "$used_memory_kb Used (after Hugepages)\n" if ($opt_v);
27
28 # Round to the nearest KB
29-$free_memory_kb = sprintf('%d',$free_memory_kb);
30-$used_memory_kb = sprintf('%d',$used_memory_kb);
31-$caches_kb = sprintf('%d',$caches_kb);
32+$free_memory_kb = sprintf('%.0f',$free_memory_kb);
33+$used_memory_kb = sprintf('%.0f',$used_memory_kb);
34+$caches_kb = sprintf('%.0f',$caches_kb);
35
36 # Tell Nagios what we came up with
37-tell_nagios($used_memory_kb,$free_memory_kb,$caches_kb,$hugepages_kb);
38+tell_nagios($used_memory_kb,$free_memory_kb,$caches_kb,$available_memory_kb,$hugepages_kb);
39
40
41 sub tell_nagios {
42- my ($used,$free,$caches,$hugepages) = @_;
43+ my ($used,$free,$caches,$available,$hugepages) = @_; # KB
44
45 # Calculate Total Memory
46 my $total = $free + $used;
47 print "$total Total\n" if ($opt_v);
48
49+ # Absolute limits in KB
50+ my $limit_warn;
51+ my $limit_crit;
52+
53+ # is level absolute
54+ my $is_abs_warn;
55+ my $is_abs_crit;
56+ my $type_check = "percentage";
57+
58+
59+ # WARN
60+ ($limit_warn, $is_abs_warn) = parameterize_thresholds($opt_w, "WARNING", $total);
61+
62+ # CRITICAL
63+ ($limit_crit, $is_abs_crit) = parameterize_thresholds($opt_c, "CRITICAL", $total);
64+
65+ # Check if both levels are absolute or percentage
66+ if ($is_abs_crit != $is_abs_warn) {
67+ print "WARNING and CRITICAL should be both absolute or percentage";
68+ &usage;
69+ }
70+ elsif ($is_abs_crit){
71+ $type_check = "absolute";
72+ }
73+
74 my $perf_warn;
75 my $perf_crit;
76- if ( $opt_u ) {
77- $perf_warn = int(${total} * $opt_w / 100);
78- $perf_crit = int(${total} * $opt_c / 100);
79- } else {
80- $perf_warn = int(${total} * ( 100 - $opt_w ) / 100);
81- $perf_crit = int(${total} * ( 100 - $opt_c ) / 100);
82+ if ( $opt_u ) { # used
83+ $perf_warn = $limit_warn;
84+ $perf_crit = $limit_crit;
85+ } else { # free and available
86+ $perf_warn = $total - $limit_warn;
87+ $perf_crit = $total - $limit_crit;
88 }
89
90- my $perfdata = "|TOTAL=${total}KB;;;; USED=${used}KB;${perf_warn};${perf_crit};; FREE=${free}KB;;;; CACHES=${caches}KB;;;;";
91+ # Check if levels are sane
92+ if ($limit_warn <= $limit_crit and ($opt_f or $opt_a)) {
93+ my $opt = ($opt_f eq 1 ? "FREE" : "AVAILABLE");
94+ print "*** WARNING level must not be less than CRITICAL when checking $opt memory!\n";
95+ &usage;
96+ }
97+ elsif ($limit_warn >= $limit_crit and $opt_u) {
98+ print "*** WARNING level must not be greater than CRITICAL when checking USED memory!\n";
99+ &usage;
100+ }
101+
102+ my $perfdata = "|TOTAL=${total}KB;;;;";
103+ if ( !$opt_a ) {
104+ $perfdata .= " USED=${used}KB;${perf_warn};${perf_crit};;";
105+ } else {
106+ $perfdata .= " USED=${used}KB;;;;";
107+ }
108+ $perfdata .= " FREE=${free}KB;;;;";
109+ $perfdata .= " CACHES=${caches}KB;;;;";
110+ $perfdata .= " AVAILABLE=${available}KB;${perf_warn};${perf_crit};;" if ($opt_a);
111 $perfdata .= " HUGEPAGES=${hugepages}KB;;;;" if ($opt_h);
112
113- if ($opt_f) {
114+ if ($opt_f) { # free
115 my $percent = sprintf "%.1f", ($free / $total * 100);
116- if ($percent <= $opt_c) {
117- finish("CRITICAL - $percent% ($free kB) free!$perfdata",$exit_codes{'CRITICAL'});
118+ if ($free < $limit_crit) {
119+ finish("CRITICAL - $percent% ($free kB) free-$type_check!$perfdata",$exit_codes{'CRITICAL'});
120 }
121- elsif ($percent <= $opt_w) {
122- finish("WARNING - $percent% ($free kB) free!$perfdata",$exit_codes{'WARNING'});
123+ elsif ($free < $limit_warn) {
124+ finish("WARNING - $percent% ($free kB) free-$type_check!$perfdata",$exit_codes{'WARNING'});
125 }
126 else {
127- finish("OK - $percent% ($free kB) free.$perfdata",$exit_codes{'OK'});
128+ finish("OK - $percent% ($free kB) free-$type_check.$perfdata",$exit_codes{'OK'});
129 }
130 }
131- elsif ($opt_u) {
132+ elsif ($opt_a) { # available
133+ my $percent = sprintf "%.1f", ($available / $total * 100);
134+ if ($available <= $limit_crit) {
135+ finish("CRITICAL - $percent% ($available kB) available-$type_check!$perfdata",$exit_codes{'CRITICAL'});
136+ }
137+ elsif ($available <= $limit_warn) {
138+ finish("WARNING - $percent% ($available kB) available-$type_check!$perfdata",$exit_codes{'WARNING'});
139+ }
140+ else {
141+ finish("OK - $percent% ($available kB) available-$type_check.$perfdata",$exit_codes{'OK'});
142+ }
143+ }
144+ elsif ($opt_u) { # used
145 my $percent = sprintf "%.1f", ($used / $total * 100);
146- if ($percent >= $opt_c) {
147- finish("CRITICAL - $percent% ($used kB) used!$perfdata",$exit_codes{'CRITICAL'});
148+ if ($used > $limit_crit) {
149+ finish("CRITICAL - $percent% ($used kB) used-$type_check!$perfdata",$exit_codes{'CRITICAL'});
150 }
151- elsif ($percent >= $opt_w) {
152- finish("WARNING - $percent% ($used kB) used!$perfdata",$exit_codes{'WARNING'});
153+ elsif ($used > $limit_warn) {
154+ finish("WARNING - $percent% ($used kB) used-$type_check!$perfdata",$exit_codes{'WARNING'});
155 }
156 else {
157- finish("OK - $percent% ($used kB) used.$perfdata",$exit_codes{'OK'});
158+ finish("OK - $percent% ($used kB) used-$type_check.$perfdata",$exit_codes{'OK'});
159 }
160 }
161 }
162@@ -117,14 +174,29 @@ sub tell_nagios {
163 sub usage() {
164 print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n";
165 print "usage:\n";
166- print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
167+ print " check_mem.pl -<f|u|a> -w <warnlevel> -c <critlevel>\n\n";
168 print "options:\n";
169- print " -f Check FREE memory\n";
170- print " -u Check USED memory\n";
171- print " -C Count OS caches as FREE memory\n";
172- print " -h Remove hugepages from the total memory count\n";
173- print " -w PERCENT Percent free/used when to warn\n";
174- print " -c PERCENT Percent free/used when critical\n";
175+ print " -a Check AVAILABLE memory\n";
176+ print " -f Check FREE memory\n";
177+ print " -u Check USED memory\n";
178+ print " -C Count OS caches as FREE memory\n";
179+ print " -w PERCENTAGE Percent free/used/available when to warn\n";
180+ print " -w SIZE K/M/G Absolute size free/used/available when to warn\n";
181+ print " -c PERCENTAGE Percent free/used/available when critical\n";
182+ print " -c SIZE K/M/G Absolute size free/used/available when critical\n";
183+ print " -v Show verbose output\n";
184+ print "\nexample:\n";
185+ print "check_mem.pl -C -f -w 20 -c .5\n";
186+ print "\tReturns 1 (WARNING) if less than 20% free memory.\n";
187+ print "\tReturns 2 (CRITICAL) if less than 0.5% free memory.\n";
188+ print "\tTakes caches into account.\n";
189+ print "check_mem.pl -u -w 80 -c 95\n";
190+ print "\tReturns 1 (WARNING) if more than 80% memory in use.\n";
191+ print "\tReturns 2 (CRITICAL) if more than 95% memory in use.\n";
192+ print "check_mem.pl -C -f -w 2G -c 500M\n";
193+ print "\tReturns 1 (WARNING) if less than 2G free memory.\n";
194+ print "\tReturns 2 (CRITICAL) if less than 500M free memory.\n";
195+ print "\tTakes caches into account.\n";
196 print "\nCopyright (C) 2000 Dan Larsson <dl\@tyfon.net>\n";
197 print "check_mem.pl comes with absolutely NO WARRANTY either implied or explicit\n";
198 print "This program is licensed under the terms of the\n";
199@@ -166,15 +238,15 @@ sub get_memory_info {
200 $total_memory_kb = $2;
201 }
202 }
203- elsif (/^MemAvailable:\s+(\d+) kB/) {
204- $available_memory_kb = $1;
205- }
206 elsif (/^(Buffers|Cached|SReclaimable):\s+(\d+) kB/) {
207 $caches_kb += $2;
208 }
209 elsif (/^Shmem:\s+(\d+) kB/) {
210 $caches_kb -= $1;
211 }
212+ elsif (/^MemAvailable:\s+(\d+) kB/) {
213+ $available_memory_kb = $1;
214+ }
215 # These variables will most likely be overwritten once we look into
216 # /sys/kernel/mm/hugepages, unless we are running on linux <2.6.27
217 # and have to rely on them
218@@ -186,7 +258,7 @@ sub get_memory_info {
219 }
220 }
221 $hugepages_kb = $hugepages_nr * $hugepages_size;
222- $used_memory_kb = $total_memory_kb - $available_memory_kb;
223+ $used_memory_kb = $total_memory_kb - $free_memory_kb;
224
225 # Read hugepages info from the newer sysfs interface if available
226 my $hugepages_sysfs_dir = '/sys/kernel/mm/hugepages';
227@@ -373,7 +445,39 @@ sub get_memory_info {
228 $free_memory_kb = $memlist[1]/1024;
229 $total_memory_kb = $used_memory_kb + $free_memory_kb;
230 }
231- return ($free_memory_kb,$used_memory_kb,$caches_kb,$hugepages_kb);
232+ return ($free_memory_kb,$used_memory_kb,$caches_kb,$available_memory_kb,$hugepages_kb);
233+}
234+
235+sub parameterize_thresholds{
236+ my $limit;
237+ my $is_abs = 0;
238+ my ($opt, $level, $total) = @_;
239+
240+ if ($opt =~ /^((\d+)\s*([KMG]))$/) {
241+ # SIZE INTEGER K|M|G
242+ $is_abs = 1;
243+ $limit = $2;
244+ $3 eq 'K' ? $limit *= 1 :
245+ $3 eq 'M' ? $limit *= 1024 :
246+ $3 eq 'G' ? $limit *= 1024 * 1024 : die ;
247+ if ($limit > $total) {
248+ print "*** $level limit: $limit level is bigger than the total of the system memory: $total";
249+ &usage;
250+ }
251+ }
252+ elsif ($opt =~ /^(?=.)(([0-9]*)(\.([0-9]+))?)$/) {
253+ # PERCENTAGE (1, 95, 0.5)
254+ if ($1 > 100) {
255+ print "*** $level percentage > 100%!\n";
256+ &usage;
257+ }
258+ $limit = int(${total} * $1 / 100);
259+ }
260+ else {
261+ print "*** $level value not recognized!\n";
262+ &usage;
263+ }
264+ return ($limit, $is_abs);
265 }
266
267 sub init {
268@@ -382,26 +486,30 @@ sub init {
269 &usage;
270 }
271 else {
272- getopts('c:fuChvw:');
273+ getopts('c:fuaChvw:');
274 }
275
276 # Shortcircuit the switches
277- if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0) {
278+ if (! defined $opt_w or ! defined $opt_c) {
279 print "*** You must define WARN and CRITICAL levels!\n";
280 &usage;
281 }
282- elsif (!$opt_f and !$opt_u) {
283- print "*** You must select to monitor either USED or FREE memory!\n";
284+ elsif (!$opt_f and !$opt_u and !$opt_a) {
285+ print "*** You must select to monitor USED, FREE or AVAILABLE memory!\n";
286 &usage;
287 }
288-
289- # Check if levels are sane
290- if ($opt_w <= $opt_c and $opt_f) {
291- print "*** WARN level must not be less than CRITICAL when checking FREE memory!\n";
292+ elsif ($opt_f and $opt_u or $opt_f and $opt_a or $opt_u and $opt_a) {
293+ print "*** You must select to monitor either USED, FREE or AVAILABLE memory!\n";
294+ &usage;
295+ }
296+ elsif ($opt_w !~ /^((\d+)\s*([KMG])|(?=.)(([0-9]*)(\.([0-9]+))?))$/) {
297+ # SIZE INTEGER K|M|G OR PERCENTAGE INTEGER|FLOAT
298+ print "*** WARN level must be defined as PERCENTAGE (1 - 99) or SIZE K/M/G!\n";
299 &usage;
300 }
301- elsif ($opt_w >= $opt_c and $opt_u) {
302- print "*** WARN level must not be greater than CRITICAL when checking USED memory!\n";
303+ elsif ($opt_c !~ /^((\d+)\s*([KMG])|(?=.)(([0-9]*)(\.([0-9]+))?))$/) {
304+ # SIZE INTEGER K|M|G OR PERCENTAGE INTEGER|FLOAT
305+ print "*** CRITICAL level must be defined as PERCENTAGE (1 - 99) or SIZE K/M/G!\n";
306 &usage;
307 }
308 }
309@@ -410,4 +518,4 @@ sub finish {
310 my ($msg,$state) = @_;
311 print "$msg\n";
312 exit $state;
313-}
314+}
315\ No newline at end of file

Subscribers

People subscribed via source and target branches

to all changes: