File: lambda-monitor.pl

package info (click to toggle)
libsnmp-session-perl 1.14~git20201002.0dedded-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,080 kB
  • sloc: perl: 11,603; ansic: 25; makefile: 13
file content (169 lines) | stat: -rw-r--r-- 5,042 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
162
163
164
165
166
167
168
169
#!/usr/local/bin/perl -w

use strict;

use SNMP_Session;
use SNMP_util;

sub show_light_trail (@);
sub show_bitmap ($$);
sub print_html_header ();
sub print_html_trailer ();

my $html_file = "/opt/www/htdocs/lan/switchlambda/status.html";

my $inverse_video = 0;

snmpmapOID (qw(amplifierOperStatus 1.3.6.1.4.1.2522.1.5.1.1.0
	       amplifierLastChange 1.3.6.1.4.1.2522.1.5.1.2.0
	       amplifierGain-dB 1.3.6.1.4.1.2522.1.5.1.3.0
	       inputPowerStatus 1.3.6.1.4.1.2522.1.5.1.4.0
	       inputPowerLevel 1.3.6.1.4.1.2522.1.5.1.5.0
	       outputPowerStatus 1.3.6.1.4.1.2522.1.5.1.6.0
	       outputPowerLevel 1.3.6.1.4.1.2522.1.5.1.7.0));

my @eastbound_amps = qw(public@muxCE1-A8  
	      public@muxLS1-A1  
	      public@muxLS1-A8  
	      public@muxBE1-A1
	      public@muxBE1-A8
	      public@muxBA1-A1  
	      public@muxBA1-A8
	      public@muxEZ1-A1);


my @westbound_amps = qw(
	      public@muxEZ1-A2
	      public@muxBA1-A7  
	      public@muxBA1-A2  
	      public@muxBE1-A7
	      public@muxBE1-A2
	      public@muxLS1-A7  
	      public@muxLS1-A2  
			);

my @amps = (@eastbound_amps, @westbound_amps);

for (;;) {
    open (HTML, ">$html_file.new");
    print_html_header ();
    my $localtime = localtime();
    print "",$localtime,"\n";

    print HTML "<p> Last updated: $localtime </p>\n";

    print "\nEastbound:\n";
    print HTML "<table width=\"100%\"><tr><td align=\"left\" valign=\"top\" width=\"45%\"><h2> Eastbound </h2>\n";
    show_light_trail (@eastbound_amps);

    print HTML "</td><td align=\"right\" valign=\"top\" width=\"45%\"><h2> Westbound </h2>";
    print "\nWestbound:\n";
    show_light_trail (@westbound_amps);

    print HTML "</td></tr></table>\n";
    print "-"x 75,"\n";
    print_html_trailer ();
    close (HTML);
    rename ($html_file.".new",$html_file);
    sleep (292);
}
1;

sub show_light_trail (@) {
    my @amps = @_;
    printf "%-16s%-8s%-8s\n", "node", "  in pwr", " out pwr";
    printf "%-16s%-8s%-8s\n", "name", "   dBm", "   dBm";

    print HTML "<table>\n <tr>\n  <th>Amplifier</th>\n  <th>Input<br>Power<br>(dBm)</th>\n  <th>Output<br>Power<br>(dBm)</th>\n </tr>\n";
    foreach my $amp (@amps) {
	my ($community,$nodename) = split (/@/,$amp);
	my ($amp_status,
	    $in_status, $in,
	    $out_status, $out)
	    = snmpget ($amp, qw(amplifierOperStatus
					   inputPowerStatus inputPowerLevel
					   outputPowerStatus outputPowerLevel));
	printf "%-16s%8.2f%8.2f\t%-8s%-8s%-8s\n",
	$nodename, $in, $out,
	show_bitmap ($amp_status, 5),
	pretty_power_status ($in_status),
	pretty_power_status ($out_status);
	my ($amp_class, $in_class, $out_class)
	    = (class_for_amp_status ($amp_status),
	       class_for_level_status ($in_status),
	       class_for_level_status ($out_status));
	print HTML "<tr><td class=\"$amp_class\">$nodename</td><td class=\"$in_class\">$in</td><td class=\"$out_class\">$out</td></tr>\n";
    }
    print HTML "</table>\n";
}

sub show_bitmap ($$) {
    my ($bits,$n) = @_;
    my ($k,$result);
    for ($k = 0, $result = ''; $k < $n; ++$k) {
	$result .= (($bits & (1<<$k)) ? '*' : ' ');
    }
    return $result;
}

sub pretty_power_status ($) {
    return (qw(???? ---- minr majr CRIT))[$_[0]];
}

sub class_for_level_status ($) {
    return (qw(weird normal minor major critical))[$_[0]];
}

sub class_for_amp_status ($) {
    return 'failed' if $_[0] & 1<<4;
    return 'critical' if $_[0] & 1<<3;
    return 'major' if $_[0] & 1<<2;
    return 'minor' if $_[0] & 1<<1;
    return 'normal' if $_[0] & 1<<0;
    return 'normal';
}

sub print_html_header () {
    my $expires = time + 300;
    my $expire_string = http_date_string ($expires);
    my ($c_bg, $c_normal, $c_minor, $c_major, $c_critical, $c_failed)
	= $inverse_video
	    ? qw(#000000 #ffffff #ff8080 #ff4040 #ff0000 #ff8000)
		 : qw(#ffffff #000000 #804040 #ff4040 #ff0000 #ff8000);
    print HTML <<EOM;
<html><head>
<title>SWITCHlambda Amplifier Status</title>
<meta http-equiv="Refresh" content="300">
<meta http-equiv="Expires" content="$expire_string">
<style type="text/css">
.normal {color: $c_normal}
.minor {color: $c_minor}
.major {color: $c_major}
.critical {color: $c_critical; font-weight: bold}
.failed {color: $c_failed; font-weight: bold; font-style: italic}
</style>
</head><body bgcolor="$c_bg" text="$c_normal">\n<h1>SWITCHlambda Amplifier Status</h1>
EOM
}

sub print_html_trailer () {
    print HTML <<EOM;
<h3> Color Legend </h3>
<p><span class="normal">normal</span>
- <span class="minor">minor</span>
- <span class="major">major</span>
- <span class="critical">critical</span>
- <span class="failed">failed</span></p>
</body></html>
EOM
}

sub http_date_string ($) {
    my ($time) = @_;
    my @gmtime = gmtime $time;
    my ($wday) = (qw(Sun Mon Tue Wed Thu Fri Sat))[$gmtime[6]];
    my ($month) = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$gmtime[4]];
    my ($mday, $year, $hour, $min, $sec) = @gmtime[3,5,2,1,0];
    return sprintf ("%s, %02d %s %04d %02d:%02d:%02d GMT",
		    $wday, $mday, $month, $year+1900, $hour, $min, $sec);
}