File: cisco-env.monitor

package info (click to toggle)
mon-contrib 1.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 896 kB
  • sloc: perl: 5,510; sh: 391; python: 118; makefile: 72
file content (214 lines) | stat: -rwxr-xr-x 7,258 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl
#
# Monitor Cisco device for certain environmental conditions via snmp
# On devices that export this data, this script will detect and report:
#  - power supply failures
#  - fan blade failures
#  - temperature alarms
#  - chassis minor/major alarms
#
# Can parse monitor-auth.cf for snmp community string, or read from command line
#
# Arguments are:
#
# [-C monitor-auth.cf] [-c community] host [host ...]
#
#
# cisco-env.monitor written by
# Carnegie Mellon University, Computing Services
#
#
# Copyright (c) 2004 Carnegie Mellon University. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# 3. The name "Carnegie Mellon University" must not be used to endorse or
#    promote products derived from this software without prior written
#    permission. For permission or any legal details, please contact:
#      Office of Technology Transfer
#      Carnegie Mellon University
#      5000 Forbes Avenue
#      Pittsburgh, PA 15213-3890
#      (412) 268-4387, fax: (412) 268-7395
#      tech-transfer@andrew.cmu.edu
#
# 4. Redistributions of any form whatsoever must retain the following
#    acknowledgment: "This product includes software developed by Computing
#    Services at Carnegie Mellon University (http://www.cmu.edu/computing/)."
#
# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
# IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL,
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.


#
# $Id: cisco-env.monitor,v 1.1 2005/08/20 18:14:32 vitroth Exp $
#

use SNMP;
use Getopt::Std;
use IO::File;

use strict;

my ($community, $timeout, %failures, %opts);
getopts("c:t:C:", \%opts);
$community = $opts{'c'};
$timeout = ($opts{'t'} || 3) * 1000 * 1000;
my $RETVAL = 0;

my %communities;
my $group=$ENV{MON_GROUP};
my $service=$ENV{MON_SERVICE};
my $file = $ENV{MON_CFBASEDIR}."/monitor-auth.cf";
my $cf;
if ($cf = new IO::File "<$file") {
  while (<$cf>) {
    chomp;
    if (/^(\S+):readcommunity\s*=\s*(\S+)$/) {
      $communities{$1}=$2;
    }
  }
  $community ||= $communities{"$group:$service"};
  $community ||= $communities{"$group:*"};
  $community ||= $communities{"*:$service"};
  $community ||= $communities{"*:*"};

}

$community ||= 'public';

foreach my $host (@ARGV) {
    my $session = new SNMP::Session(DestHost => $host,
                                 Community => $community, 
                                 Timeout => $timeout,
				 Retries => 2,
				 UseNumeric => 1,
				 UseLongNames => 2);
    if (!defined ($session)) {
    	$RETVAL = ($RETVAL == 1) ? 1 : 2;
	$failures{$host} = "$host: could not get SNMP session";
	next;
    }

    my $desc = $session->get([".1.3.6.1.2.1.1.1.0"]);
    if (!$desc) {
      $RETVAL = 1;
      $failures{$host} = "$host: cannot contact snmpd";
      next;
    }

    my %Capture = ('ps1status' => {'oid' => '.1.3.6.1.4.1.9.5.1.2.4.0',
				  'desc' => 'Power Supply 1',
				  'res' => {'1' => 'ok', '2' => 'ok', 
				  	    '3' => 'minor fault', '4' => 'major fault'},
				  },
		   'ps2status' => {'oid' =>'.1.3.6.1.4.1.9.5.1.2.7.0',
				  'desc' => 'Power Supply 2',
                                  'res' => {'1' => 'ok', '2' => 'ok', 
                                            '3' => 'minor fault', '4' => 'major fault'},
				  },
		   'fanstatus' => {'oid' => '.1.3.6.1.4.1.9.5.1.2.9.0',
				  'desc' => 'Fan Tray',
                                  'res' => {'1' => 'ok', '2' => 'ok', 
                                            '3' => 'minor fault', '4' => 'major fault'},
				  },
		   'tempstatus' => {'oid' => '.1.3.6.1.4.1.9.5.1.2.13.0',
				    'desc' => 'Temperature Status',
                                  'res' => {'1' => 'ok', '2' => 'ok', 
                                            '3' => 'minor fault', '4' => 'major fault'},
				   },
		   'minoralarm' => {'oid' => '.1.3.6.1.4.1.9.5.1.2.11.0',
				    'desc' => 'Chassis Minor Alarm',
				    'res' => {'1' => 'ok', '2' => 'activated'},
				   },
		   'majoralarm' => {'oid' => '.1.3.6.1.4.1.9.5.1.2.12.0',
				    'desc' => 'Chassis Major Alarm',
				    'res' => {'1' => 'ok', '2' => 'activated'},
				   },

			# 1- normal, 5- notPresent
		   'env-tempstatus' => {'oid' => '.1.3.6.1.4.1.9.9.13.1.3.1.6',
					'desc' => 'Temperature State',
					'res' => {'1' => 'ok', '2' => 'warning', '3' => 'critical',
						  '4' => 'shutdown', '5' => 'ok', '6' => 'notFunctioning'},
					'type' => 'walk',
					},
		   'env-fanstate' => {'oid' => '.1.3.6.1.4.1.9.9.13.1.4.1.3',
				      'desc' => 'Fan State',
				      'res' => {'1' => 'ok', '2' => 'warning', '3' => 'critical',
						'4' => 'shutdown', '5' => 'ok', '6' => 'notFunctioning'},
				      'type' => 'walk',
				     },
		   'env-psstate' => {'oid' => '.1.3.6.1.4.1.9.9.13.1.5.1.3',
			 	     'desc' => 'Power Supply State',
				     'res' => {'1' => 'ok', '2' => 'warning', '3' => 'critical',
                                                '4' => 'shutdown', '5' => 'ok', '6' => 'notFunctioning'},
                                      'type' => 'walk',
                                     },
		);

    foreach my $K (keys %Capture) {
      my @Results;
      if ($Capture{$K}->{'type'} eq 'walk') {
        @Results = l_snmpwalk_values($session, $Capture{$K}->{'oid'});
      }else{
        push(@Results, $session->get($Capture{$K}->{'oid'}));
      }
      foreach my $Res (@Results) {
        if (defined $Capture{$K}->{'res'}->{$Res}) {
  	  if ($Capture{$K}->{'res'}->{$Res} ne 'ok') {
	    $RETVAL = 1;
   	    $failures{$host} .= '; ' unless ($failures{$host} eq '');
	    $failures{$host} .= $Capture{$K}->{'desc'}." ".
			        $Capture{$K}->{'res'}->{$Res};
          }
        }
      }
    }

}

print join (" ", sort keys %failures), "\n\n" if (scalar keys %failures);
foreach my $host (sort keys %failures) {
    print $host.': '.$failures{$host}, "\n";
}

exit $RETVAL;

sub l_snmpwalk_values {
  my ($session, $oid) = @_;

  my $rootlen = length($oid);

  $session->{ErrorStr} = '';
  
  my $var = new SNMP::Varbind(["$oid"]);
  my $val = $session->getnext($var);
  my $name = $var->[$SNMP::Varbind::tag_f].".".$var->[$SNMP::Varbind::iid_f];
  my %walk;

  while (!$session->{ErrorStr} && substr($name, 0, $rootlen) eq $oid){
    my $value=$var->[$SNMP::Varbind::val_f];

    $walk{"$name"} = $value;
    $val = $session->getnext($var);
    $name=$var->[$SNMP::Varbind::tag_f];
    $name.=".$var->[$SNMP::Varbind::iid_f]" if $var->[$SNMP::Varbind::iid_f];
  }  #while

  return values %walk;
}