File: 21_check_snmp_load_abstract_snmp_version_check

package info (click to toggle)
nagios-snmp-plugins 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,008 kB
  • ctags: 381
  • sloc: perl: 7,178; sh: 309; makefile: 50
file content (121 lines) | stat: -rw-r--r-- 4,229 bytes parent folder | download | duplicates (2)
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
From 846165c880793a97a2e727f4d13e23df40e8f1a4 Mon Sep 17 00:00:00 2001
From: morgajel <morgajel@gmail.com>
Date: Wed, 13 May 2015 12:33:21 -0400
Subject: [PATCH] Abstracted snmp version check to circumvent error and bug

There are two issues:
1) Net::SNMP changed it's VERSION to be a quoted string rather than a bare mess. This caused
Argument "v6.0.1" isn't numeric in numeric lt (<) at /opt/manubulon/plugins/check_snmp_load.pl line 368.

2) the rest of the file used lt rather than <, which means they were using ascii sorting rather than digit comparison

Both of these issues have now been resolved; This fix has only been applied to this one script, but it may
need to be implemented in the other scripts.
---
 check_snmp_load.pl | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/check_snmp_load.pl b/check_snmp_load.pl
index c8661aa..751c6ec 100755
--- a/check_snmp_load.pl
+++ b/check_snmp_load.pl
@@ -280,6 +280,18 @@ sub check_options {
 	}
 }
 
+# This is required to get around all of the silly historical methods of
+# versioning with Net::SNMP.
+sub is_legacy_snmp_version {
+    my $version=Net::SNMP->VERSION; #using a variable for easier testing
+    if ($version=~/^\D*(\d)/ and $1 < 4){
+        print "$1 wee";
+        return 1;
+    }else{
+        return 0;
+    }
+}
+
 ########## MAIN #######
 
 check_options();
@@ -365,7 +377,7 @@ sub check_options {
 verb("Checking linux load");
 
 # Get number of CPUs
-my $resultat =  (Net::SNMP->VERSION < 4) ?
+my $resultat =  (is_legacy_snmp_version()) ?
 	  $session->get_table($proc_id)
 	: $session->get_table(Baseoid => $proc_id);
 
@@ -378,7 +390,7 @@ sub check_options {
 my $ncpu = keys %$resultat;
 
 # Get load table
-$resultat = (Net::SNMP->VERSION lt 4) ?
+$resultat = (is_legacy_snmp_version()) ?
 		  $session->get_table($linload_table)
 		: $session->get_table(Baseoid => $linload_table);
 
@@ -445,7 +457,7 @@ sub check_options {
 
 if ($o_check_type eq "cisco") {
 my @oidlists = ($cisco_cpu_5m, $cisco_cpu_1m, $cisco_cpu_5s);
-my $resultat = (Net::SNMP->VERSION lt 4) ?
+my $resultat = (is_legacy_snmp_version()) ?
 	  $session->get_request(@oidlists)
 	: $session->get_request(-varbindlist => \@oidlists);
 
@@ -499,7 +511,7 @@ sub check_options {
 ############## Cisco N5K CPU Check ###################
 if ($o_check_type eq "n5k") {
 my @oidlists = ($n5k_cpu);
-my $resultat = (Net::SNMP->VERSION lt 4) ?
+my $resultat = (is_legacy_snmp_version()) ?
           $session->get_request(@oidlists)
         : $session->get_request(-varbindlist => \@oidlists);
 if (!defined($resultat)) {
@@ -542,7 +554,7 @@ sub check_options {
 
 if ($o_check_type eq "cata") {
 my @oidlists = ($ciscocata_cpu_5m, $ciscocata_cpu_1m, $ciscocata_cpu_5s);
-my $resultat = (Net::SNMP->VERSION lt 4) ?
+my $resultat = (is_legacy_snmp_version()) ?
 	  $session->get_request(@oidlists)
 	: $session->get_request(-varbindlist => \@oidlists);
 
@@ -597,7 +609,7 @@ sub check_options {
 
 if ($o_check_type eq "nsc") {
 my @oidlists = ($nsc_cpu_5m, $nsc_cpu_1m, $nsc_cpu_5s);
-my $resultat = (Net::SNMP->VERSION lt 4) ?
+my $resultat = (is_legacy_snmp_version()) ?
 	  $session->get_request(@oidlists)
 	: $session->get_request(-varbindlist => \@oidlists);
 
@@ -654,7 +666,7 @@ sub check_options {
 # Get load table
 my @oidlist = $cpu_oid{$o_check_type}; 
 verb("Checking OID : @oidlist");
-my $resultat = (Net::SNMP->VERSION lt 4) ? 
+my $resultat = (is_legacy_snmp_version()) ?
 	  $session->get_request(@oidlist)
 	: $session->get_request(-varbindlist => \@oidlist);
 if (!defined($resultat)) {
@@ -702,7 +714,7 @@ sub check_options {
 verb("Checking hpux load");
 
 my @oidlists = ($hpux_load_1_min, $hpux_load_5_min, $hpux_load_15_min);
-my $resultat = (Net::SNMP->VERSION lt 4) ?
+my $resultat = (is_legacy_snmp_version()) ?
 	  $session->get_request(@oidlists)
 	: $session->get_request(-varbindlist => \@oidlists);
 
@@ -755,7 +767,7 @@ sub check_options {
 
 ########## Standard cpu usage check ############
 # Get desctiption table
-my $resultat =  (Net::SNMP->VERSION lt 4) ?
+my $resultat =  (is_legacy_snmp_version()) ?
 	  $session->get_table($base_proc)
 	: $session->get_table(Baseoid => $base_proc);