File: router-stats.pl

package info (click to toggle)
libsnmp-session-perl 1.14~git20221124T101957-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,104 kB
  • sloc: perl: 11,920; ansic: 25; makefile: 15
file content (161 lines) | stat: -rwxr-xr-x 4,731 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
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
#!/usr/local/bin/perl

use BER;
require 'SNMP_Session.pm';

# Set $host to the name of the host whose SNMP agent you want
# to talk to.  Set $community to the community name under
# which you want to talk to the agent.	Set port to the UDP
# port on which the agent listens (usually 161).

my $routerfile = 'test/routers';
my @routers = qw(swiEG1.switch.ch swiEZ1.switch.ch swiEZ2.switch.ch swiCS1.switch.ch swiCS2.switch.ch);
my $redline=10;
my $yellowline=5;

my $redball = "<table bgcolor=red><tr><td>&nbsp;</td></tr></table>";
my $yelball = "<table bgcolor=yellow><tr><td>&nbsp;</td></tr></table>";
my $greenball = "<table bgcolor=green><tr><td>&nbsp;</td></tr></table>";

print <<"TEXT";    
 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
 <html>
 <head>
 <meta http-equiv="Content-Type"
 content="text/html; charset=iso-8859-1">
 <title>WAN Routers at a glance</title>
 </head>

 <body background="/images/background.gif">
<table border="0" cellpadding="0" cellspacing="1" width="80%">
    <tr>
        <td align="right" rowspan="2">
<!-- <img src="../images/pushme2.gif" width="150" height="159"></td> -->
        <td colspan="2"><p align="center"><font size="5"
        face="Palatino"><strong>WAN Routers at a Glance<br>
        </strong></font><font size="4" face="Palatino"><strong>Technical
        Team Only</strong></font></p>
        </td>
    </tr>
</table>
<br>
TEXT
@results2=();
for ($currouter=0; $currouter < $#routers; $currouter++) {

$host=@routers[$currouter];
$community = "public";
$port = "161";
$path = 'test/stats/';

$session = SNMP_Session->open ($host, $community, $port)
    || die "couldn't open SNMP session to $host";

# Set $oid1, $oid2... to the BER-encoded OIDs of the MIB
# variables you want to get.

$oid1 = encode_oid (1, 3, 6, 1, 2, 1, 2, 1, 0);
$oid2 = encode_oid (1, 3, 6, 1, 2, 1, 1, 5, 0);
# Cisco CPU OID
$oid3 = encode_oid (1, 3, 6, 1, 4,1,9,2,1,58,0);
if ($session->get_request_response ($oid1,$oid2,$oid3)) {
    ($bindings) = $session->decode_get_response ($session->{pdu_buffer});
    while ($bindings ne '') {
	($binding,$bindings) = &decode_sequence ($bindings);
	($oid,$value) = &decode_by_template ($binding, "%O%@");
	$interfaces=pretty_print ($value);
	($binding,$bindings) = &decode_sequence ($bindings);
	($oid,$value) = &decode_by_template ($binding, "%O%@");
	$sysname=pretty_print ($value);
	($binding,$bindings) = &decode_sequence ($bindings);
	($oid,$value) = &decode_by_template ($binding, "%O%@");
	$cpupercent=pretty_print ($value);
    }
} else {
    die "No response from agent on $host";
}
print <<TEXT;

 <div align="left"><left>

 <table border="1" cellpadding="0" cellspacing="1" width=80%>
<TR> <td colspan=
TEXT
@results = ();
@outhead=();
@outvalue=();
@outhead[0]="CPU";
if ($cpupercent>$redline){
	    $graphic=$redball;
	  } elsif ($cpupercent>$yellowline) {
	    $graphic=$yelball;
	  } else {
	    $graphic=$greenball;
	  }
@outvalue[0]=$graphic;
$a=1;
for ($i=1; $i <= $interfaces; $i++) {
$oid1=encode_oid(1,3, 6, 1, 2, 1, 2, 2, 1 ,2, $i);
$oid2=encode_oid(1,3, 6, 1, 2, 1, 2, 2, 1 ,8, $i);
$oid3=encode_oid(1,3, 6, 1, 2, 1, 2, 2, 1 ,5, $i);
if ($session->get_request_response ($oid1,$oid2,$oid3)) {
    ($bindings) = $session->decode_get_response ($session->{pdu_buffer});
    while ($bindings ne '') {
	($binding,$bindings) = &decode_sequence ($bindings);
	($oid,$value) = &decode_by_template ($binding, "%O%@");
	$name=pretty_print ($value);
	($binding,$bindings) = &decode_sequence ($bindings);
	($oid,$value) = &decode_by_template ($binding, "%O%@");
	$status=pretty_print ($value);
	($binding,$bindings) = &decode_sequence ($bindings);
	($oid,$value) = &decode_by_template ($binding, "%O%@");
	$maxspeed=(pretty_print ($value)/8);
	
	if ($status=="1") {
	  $file = $path.$host.".".$i.".log";
	  @temp=split(/\n/,$file);
	  $file=@temp[0].@temp[1];
	  #print $file,"\n"; 
	  open(INFO, $file);
	  @lines = <INFO>;
	  @elements=split(/ /,@lines[1]);
	  $curtot=@elements[1]+@elements[2];
	  if ($maxspeed == 0) {
	    $graphic="";
	  } else {
	    $percentage=($curtot/$maxspeed)*100;
	    if ($percentage>$redline){
	      $graphic=$redball;
	    } elsif ($percentage>$yellowline) {
	      $graphic=$yelball;
	    } else {
	      $graphic=$greenball;
	    }
	  }
	  @outhead[$a]=$name;
	  @outvalue[$a]=$graphic;
	  $a++;
	}} 
} else {
    die "No response from agent on $host";
}
	
}
print $#outhead+1,">Utilisation statistics for ",$sysname," </TD></tr>";
for ($x=0; $x <= $#outhead; $x++) {
print "<td>",@outhead[$x],"</td>\n";
}
print "<tr>\n";
for ($x=0; $x <= $#outvalue; $x++) {
print "<td>",@outvalue[$x],"</td>\n";
}
@outhead=();
@outvalue=();
print <<"TEXT";
</tr>
</table><br>
</left></div>
TEXT
     
}
print "</body></html>";