]> git.sesse.net Git - nms/blob - web/tempfetch.pl
Put Zyxel password in config.pm.
[nms] / web / tempfetch.pl
1 #! /usr/bin/perl
2 #use BER;
3 use Data::Dumper;
4 use DBI;
5 use Net::Telnet;
6 use POSIX;
7 use Time::HiRes;
8 use lib '../include';
9 use nms;
10 use strict;
11 use warnings;
12
13 # Tweak timeouit og sjekk med :support||:net hva passord vil bli
14 my $timeout = 25;
15 my $location = 'skipet';
16
17 my $dbh = nms::db_connect();
18 $dbh->{AutoCommit} = 0;
19
20 my $qswitch = $dbh->prepare(<<"EOF")
21 SELECT 
22   *
23 FROM
24   switches
25   NATURAL LEFT JOIN switchtypes
26 WHERE
27   (locked='f' OR now() - last_updated > '15 minutes'::interval)
28 LIMIT 1
29 FOR UPDATE OF switches
30 EOF
31         or die "Couldn't prepare qswitch";
32 my $qlock = $dbh->prepare("UPDATE switches SET locked='t', last_updated=now() WHERE switch=?")
33         or die "Couldn't prepare qlock";
34 my $qunlock = $dbh->prepare("UPDATE switches SET locked='f', last_updated=now() WHERE switch=?")
35         or die "Couldn't prepare qunlock";
36 my $qpoll = $dbh->prepare("INSERT INTO temppoll (time, switch, temp) VALUES (timeofday()::timestamp,?::text::int,?::text::float)")
37         or die "Couldn't prepare qpoll";
38
39 while (1) {
40         # Find a switch to grab
41         $qswitch->execute()
42                 or die "Couldn't get switch";
43         my $switch = $qswitch->fetchrow_hashref();
44
45         if (!defined($switch)) {
46                 $dbh->commit;
47                 mylog("No available switches in pool, sleeping.");
48                 sleep 60;
49                 next;
50         }
51
52         $qlock->execute($switch->{'switch'})
53                 or die "Couldn't lock switch";
54         $dbh->commit;
55
56         if ($switch->{'locked'}) {
57                 mylog("WARNING: Lock timed out on $switch->{'ip'}, breaking lock");
58         }
59
60         my $msg;
61         if (defined($switch->{'overdue'})) {
62                 $msg = sprintf "Polling temp on %s (%s), %s overdue.",
63                         $switch->{'ip'}, $switch->{'sysname'}, $switch->{'overdue'};
64         } else {
65                 $msg = sprintf "Polling temp on %s (%s), never polled before.",
66                         $switch->{'ip'}, $switch->{'sysname'};
67         }
68         mylog($msg);
69
70         my $ip = $switch->{'ip'};
71         my $start = [Time::HiRes::gettimeofday];
72         eval {
73                 my $conn = switch_connect($ip);
74                 if (!defined($conn)) {
75                         print "Could not connect to switch ".$switch->{'switch'}."\n";
76                 }
77                 my @data = switch_exec('sys monitor status', $conn);
78                 my @fields = split(/\s+/, $data[2]);
79                 # The temp fields are 6, 7, 8
80                 my $avgtemp = ($fields[6] + $fields[7] + $fields[8]) / 3;
81                 print $avgtemp." avgtemp\n";
82                 $qpoll->execute($switch->{'switch'},
83                                 $avgtemp) or die "Could not exec qpoll";
84         };
85         my $elapsed = Time::HiRes::tv_interval($start);
86         $msg = sprintf "Polled $switch->{'ip'} in %5.3f seconds.", $elapsed;
87         mylog($msg);
88
89         $qunlock->execute($switch->{'switch'})
90                 or die "Couldn't unlock switch";
91         sleep 1;
92         $dbh->commit;
93 }
94
95 sub switch_exec {
96         my ($cmd, $conn) = @_;
97
98         # Send the command and get data from switch
99         my @data = $conn->cmd($cmd);
100         my @lines = ();
101         foreach my $line (@data) {
102                 # Remove escape-7 sequence
103                 $line =~ s/\x1b\x37//g;
104                 push (@lines, $line);
105                 }
106
107         return @lines;
108 }
109
110 sub switch_connect {
111         my ($ip) = @_;
112
113         my $conn = new Net::Telnet(     Timeout => $timeout,
114                                         Dump_Log => '/tmp/dumplog',
115                                         Errmode => 'return',
116                                         Prompt => '/(es3024|e\d+\-\dsw)>/i');
117         my $ret = $conn->open(  Host => $ip);
118         if (!$ret || $ret != 1) {
119                 return (0);
120         }
121         # XXX: Just send the password as text, I did not figure out how to
122         # handle authentication with only password through $conn->login().
123         #$conn->login(  Prompt => '/password[: ]*$/i',
124         #               Name => $password,
125         #               Password => $password);
126         my @data = $conn->cmd($nms::config::zyxel_password);
127         # Get rid of banner
128         $conn->get;
129         return ($conn);
130 }
131
132
133 sub mylog {
134         my $msg = shift;
135         my $time = POSIX::ctime(time);
136         $time =~ s/\n.*$//;
137         printf STDERR "[%s] %s\n", $time, $msg;
138 }