14 my $password = 'removed';
17 my $poll_frequency = 60;
19 my $dbh = db_connect();
20 $dbh->{AutoCommit} = 0;
22 my $spoll = $dbh->prepare("
31 (locked='f' OR now() - updated > '3 minutes'::interval) AND
32 (delay IS NULL OR delay - now() < 0)
37 my $sgetallpoll = $dbh->prepare("
54 my $slock = $dbh->prepare("UPDATE squeue SET locked = 't', updated=now() WHERE sysname = ?")
55 or die "Unable to prepare slock";
56 my $sunlock = $dbh->prepare("UPDATE squeue SET locked = 'f', updated=now() WHERE sysname = ?")
57 or die "Unable to prepare sunlock";
58 my $sresult = $dbh->prepare("UPDATE squeue SET updated = now(), result = ?,
59 processed = 't' WHERE id = ?")
60 or die "Unable to prepare sresult";
61 my $sdelay = $dbh->prepare("UPDATE squeue SET delay = now() + delaytime, updated=now(), result = ? WHERE sysname = ?")
62 or die "Unable to prepae sdelay";
64 # Send a command to switch and return the data recvied from the switch
66 my ($cmd, $conn) = @_;
68 # Send the command and get data from switch
69 my @data = $conn->cmd($cmd);
71 foreach my $line (@data) {
72 # Remove escape-7 sequence
73 $line =~ s/\x1b\x37//g;
80 sub switch_connect($) {
83 my $conn = new Net::Telnet( Timeout => $timeout,
84 Dump_Log => '/tmp/dumplog-queue',
86 Prompt => '/es(\-)?3024|e\d+\-\dsw>/i');
87 my $ret = $conn->open( Host => $ip);
88 if (!$ret || $ret != 1) {
91 # XXX: Just send the password as text, I did not figure out how to
92 # handle authentication with only password through $conn->login().
93 #$conn->login(»·Prompt => '/password[: ]*$/i',
95 # Password => $password);
96 $conn->cmd($password);
104 my $time = POSIX::ctime(time);
106 printf STDERR "[%s] %s\n", $time, $msg;
110 $spoll->execute() or die "Could not execute spoll";
111 my $switch = $spoll->fetchrow_hashref();
112 if (!defined($switch)) {
114 mylog("No available switches in pool, sleeping.");
118 $slock->execute($switch->{sysname});
121 if ($switch->{'locked'}) {
122 mylog("WARNING: Lock timed out on $switch->{'ip'}, breaking lock");
125 my $conn = switch_connect($switch->{addr});
126 if (!defined($conn)) {
127 mylog("Could not connect to ".$switch->{sysname})."(".$switch->{addr}.")";
128 $sdelay->execute("Could not connect to switch, delaying...", $switch->{sysname});
129 $sunlock->execute($switch->{sysname});
134 $error = $sgetallpoll->execute($switch->{sysname});
136 print "Could not execute sgetallpoll\n".$dbh->errstr();
140 while (my $row = $sgetallpoll->fetchrow_hashref()) {
141 print "sysname: ".$row->{sysname}." cmd: ".$row->{cmd}."\n";
142 my @data = switch_exec($row->{cmd}, $conn);
143 my $result = join("\n", @data);
144 $sresult->execute($result, $row->{id});
147 $sunlock->execute($switch->{sysname});