File: 12_check_snmp_mem_perf

package info (click to toggle)
nagios-snmp-plugins 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,244 kB
  • sloc: perl: 7,453; sh: 309; makefile: 50
file content (85 lines) | stat: -rw-r--r-- 4,681 bytes parent folder | download | duplicates (4)
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
From 8855b778e5a4aa53667baaad3863112fd93aee64 Mon Sep 17 00:00:00 2001
From: jimbobmcgee <jimbobmcgee@users.noreply.github.com>
Date: Tue, 20 Mar 2018 23:22:09 +0000
Subject: [PATCH] Better support for buffered/cached memory in Linux

Exclude buffered memory from perfdata output, if `-b` option is specified
--------------------------------------------------------------------------

Status output show RAM usage as percentage, while perfdata output shows total literal ram usage.  If `-N -m` options are given, cached memory is included in both the plugin status output and the perfdata.  If `-N -b` options are given, buffered memory is *excluded* from the status output, but remains *included* in the perfdata.

This patch alters the behaviour of the `-N -b` option combination so that, if supplied, buffered memory is also excluded from the perfdata.


Add separate perfdata counters for cached and buffered memory, if `-ff` options is specified
---------------------------------------------------------------------------------------------

Changed `Getopt` behaviour for `-f` so it can be specified more than once.  If it is specified twice, perfdata is extended to include buffered and cached memory readings as separate perfdata values, e.g. `ram_used=12345 buffered=1234 cached=1234`.  If using `-ff`, neither buffered nor cached memory are included in the perfdata value for RAM used.
---
 plugins/check_snmp_mem.pl | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/plugins/check_snmp_mem.pl b/plugins/check_snmp_mem.pl
index 2ac7ec9..3f93a20 100755
--- a/plugins/check_snmp_mem.pl
+++ b/plugins/check_snmp_mem.pl
@@ -95,7 +95,7 @@
 
 sub print_usage {
     print
-"Usage: $0 [-v] -H <host> -C <snmp_community> [-2] | (-l login -x passwd [-X pass -L <authp>,<privp>])  [-p <port>] [-P <protocol>] -w <warn level> -c <crit level> [-I|-N|-E] [-f] [-m -b] [-t <timeout>] [-V]\n";
+"Usage: $0 [-v] -H <host> -C <snmp_community> [-2] | (-l login -x passwd [-X pass -L <authp>,<privp>])  [-p <port>] [-P <protocol>] -w <warn level> -c <crit level> [-I|-N|-E] [-f[f]] [-m -b] [-t <timeout>] [-V]\n";
 }
 
 sub isnnum {                     # Return true if arg is not a number
@@ -218,8 +218,8 @@ sub check_options {
         'memcache'    => \$o_cache,
         'b'           => \$o_buffer,
         'membuffer'   => \$o_buffer,
-        'f'           => \$o_perf,
-        'perfdata'    => \$o_perf
+        'f+'          => \$o_perf,
+        'perfdata+'   => \$o_perf
     );
     if (defined($o_help))    { help();      exit $ERRORS{"UNKNOWN"} }
     if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"} }
@@ -578,9 +578,7 @@ sub check_options {
 
     $realused = ($$resultat{$nets_ram_total} - ($$resultat{$nets_ram_free} + $totalcachedbuffered))
         / $$resultat{$nets_ram_total};
-
     if ($$resultat{$nets_ram_total} == 0) { $realused = 0; }
-
     $swapused
         = ($$resultat{$nets_swap_total} == 0)
         ? 0
@@ -605,12 +603,11 @@ sub check_options {
     }
     $n_output .= " ; " . $n_status;
     if (defined($o_perf)) {
-        if (defined($o_cache)) {
-            $n_output .= " | ram_used=" . ($$resultat{$nets_ram_total} - $$resultat{$nets_ram_free}) . ";";
-        } else {
-            $n_output .= " | ram_used="
-                . ($$resultat{$nets_ram_total} - $$resultat{$nets_ram_free} - $$resultat{$nets_ram_cache}) . ";";
-        }
+        my $perf_ramused = ($$resultat{$nets_ram_total} - $$resultat{$nets_ram_free});
+        $perf_ramused -= $$resultat{$nets_ram_cache}  if $o_perf > 1 or !defined($o_cache);
+        $perf_ramused -= $$resultat{$nets_ram_buffer} if $o_perf > 1 or defined($o_buffer);
+
+        $n_output .= " | ram_used=$perf_ramused;";
         $n_output .= ($o_warnR == 0) ? ";" : round($o_warnR * $$resultat{$nets_ram_total} / 100, 0) . ";";
         $n_output .= ($o_critR == 0) ? ";" : round($o_critR * $$resultat{$nets_ram_total} / 100, 0) . ";";
         $n_output .= "0;" . $$resultat{$nets_ram_total} . " ";
@@ -618,6 +615,11 @@ sub check_options {
         $n_output .= ($o_warnS == 0) ? ";" : round($o_warnS * $$resultat{$nets_swap_total} / 100, 0) . ";";
         $n_output .= ($o_critS == 0) ? ";" : round($o_critS * $$resultat{$nets_swap_total} / 100, 0) . ";";
         $n_output .= "0;" . $$resultat{$nets_swap_total};
+
+        if ($o_perf > 1) {
+            $n_output .= " buffered=" . $$resultat{$nets_ram_buffer} . ';;;0;' . $$resultat{$nets_ram_total};
+            $n_output .= " cached="   . $$resultat{$nets_ram_cache}  . ';;;0;' . $$resultat{$nets_ram_total};
+        }
     }
     $session->close;
     print "$n_output \n";