#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use CGI;
use DBI;
use Data::Dumper;
use lib '../../include';
use nms;
# Grab from .htaccess-authentication
my $user = $ENV{'REMOTE_USER'};
my $dbh = nms::db_connect();
$dbh->{AutoCommit} = 0;
# Ugly casting, found no other way
my $sinsert = $dbh->prepare( "INSERT INTO squeue
(gid, added, priority, addr, sysname, cmd, author)
VALUES(?::text::int, timeofday()::timestamptz, ?::text::int, ?::text::inet, ?, ?, ?)")
or die "Could not prepare sinsert";
my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
or die "Could not prepare sgetip";
my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
my $all_switches = $dbh->prepare("SELECT sysname FROM switches ORDER BY sysname");
sub parse_range($) {
my $switches = $_;
my @range;
my @rangecomma = split(/\s*,\s*/, $switches);
foreach (@rangecomma) {
my ($first, $last) = $_ =~ /(e\d+\-(?:sw)?[123456])\s*\-\s*(e\d+\-(?:sw)?[123456])?/;
if (!defined($first) && $_ =~ /e\d+\-(sw)?[123456]/) {
$first = $_;
}
if (!defined($first)) {
print "Parse error in: $_
\n";
next;
}
my ($rowstart, $placestart) = $first =~ /e(\d+)\-(?:sw)?([123456])/;
if (!defined($rowstart) || !defined($placestart)) {
print "Parse error in: $_
\n";
next;
}
my ($rowend, $placeend);
if (!defined($last)) {
$rowend = $rowstart;
$placeend = $placestart;
}
else {
($rowend, $placeend) = $last =~ /e(\d+)\-(?:sw)?([123456])/;
}
if (!defined($rowend) || !defined($placeend)) {
print "Parse error in: $_
\n";
next;
}
#print "e $rowstart - $placestart to e $rowend - $placeend
\n";
for (my $i = $rowstart; $i <= $rowend; $i++) {
my $dostart;
if ($rowstart != $i) {
$dostart = 1;
}
else {
$dostart = $placestart;
}
for (my $j = $dostart; $j <= 6; $j++) {
last if ($i == $rowend && $j > $placeend);
push(@range, "e$i-$j");
}
}
}
# foreach (@range) {
# print ":: $_
\n";
# }
return @range;
}
sub get_addr_from_switchnum($) {
my ($sysname) = @_;
$sgetip->execute($sysname);
if ($sgetip->rows() < 1) {
print "Could not get the ip for: ".$sysname;
return undef;
}
my $row = $sgetip->fetchrow_hashref();
return $row->{'ip'};
}
my $cgi = new CGI;
print $cgi->header(-type=>'text/html; charset=utf-8');
print << "EOF";
Du er logget inn som: $user
EOF print "\n"; foreach my $switch (@switches) { my $addr = get_addr_from_switchnum($switch); if (!defined($addr)) { next; } my $cmd = $cgi->param('cmd'); print "$switch got addr $addr\n"; print << "EOF"; EOF
\n"; print "Queuing commands for $switch:\n"; my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user); if (!$result) { print "\t" ."Could not execute query." ."\n"; print "\t".$dbh->errstr."\n"; } else { print "\tQueued: $cmd\n"; } print "\n"; } $dbh->commit; if (defined($gid)) { print "Vis resultat\n"; } print "