13 # Tweak timeouit og sjekk med :support||:net hva passord vil bli
15 my $location = 'skipet';
17 my $dbh = nms::db_connect();
18 $dbh->{AutoCommit} = 0;
20 my $qswitch = $dbh->prepare(<<"EOF")
25 NATURAL LEFT JOIN switchtypes
27 (locked='f' OR now() - last_updated > '15 minutes'::interval)
29 FOR UPDATE OF switches
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";
40 # Find a switch to grab
42 or die "Couldn't get switch";
43 my $switch = $qswitch->fetchrow_hashref();
45 if (!defined($switch)) {
47 mylog("No available switches in pool, sleeping.");
52 $qlock->execute($switch->{'switch'})
53 or die "Couldn't lock switch";
56 if ($switch->{'locked'}) {
57 mylog("WARNING: Lock timed out on $switch->{'ip'}, breaking lock");
61 if (defined($switch->{'overdue'})) {
62 $msg = sprintf "Polling temp on %s (%s), %s overdue.",
63 $switch->{'ip'}, $switch->{'sysname'}, $switch->{'overdue'};
65 $msg = sprintf "Polling temp on %s (%s), never polled before.",
66 $switch->{'ip'}, $switch->{'sysname'};
70 my $ip = $switch->{'ip'};
71 my $start = [Time::HiRes::gettimeofday];
73 my $conn = switch_connect($ip);
74 if (!defined($conn)) {
75 print "Could not connect to switch ".$switch->{'switch'}."\n";
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";
85 my $elapsed = Time::HiRes::tv_interval($start);
86 $msg = sprintf "Polled $switch->{'ip'} in %5.3f seconds.", $elapsed;
89 $qunlock->execute($switch->{'switch'})
90 or die "Couldn't unlock switch";
96 my ($cmd, $conn) = @_;
98 # Send the command and get data from switch
99 my @data = $conn->cmd($cmd);
101 foreach my $line (@data) {
102 # Remove escape-7 sequence
103 $line =~ s/\x1b\x37//g;
104 push (@lines, $line);
113 my $conn = new Net::Telnet( Timeout => $timeout,
114 Dump_Log => '/tmp/dumplog',
116 Prompt => '/(es3024|e\d+\-\dsw)>/i');
117 my $ret = $conn->open( Host => $ip);
118 if (!$ret || $ret != 1) {
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',
125 # Password => $password);
126 my @data = $conn->cmd($nms::config::zyxel_password);
135 my $time = POSIX::ctime(time);
137 printf STDERR "[%s] %s\n", $time, $msg;