aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKnut Auvor Grythe <knut@auvor.no>2015-04-01 01:21:15 +0200
committerroot <root@einstein.tg15.gathering.org>2015-04-01 01:21:15 +0200
commit8ce0c22a42189aa9411d26d1c5a320da919e1286 (patch)
tree44773a028e2e704b433b84d95004b5750c1046b9
parent2c44d754c0f7693e414bf57148772b4bc8cc023b (diff)
Add support for #require-version and %SYSNAME% in smanagrun, UI fixes
-rwxr-xr-xclients/smanagrun.pl22
-rwxr-xr-xinclude/nms.pm15
-rwxr-xr-xweb/nms.gathering.org/sshow.pl18
3 files changed, 39 insertions, 16 deletions
diff --git a/clients/smanagrun.pl b/clients/smanagrun.pl
index 30a702d..52f627e 100755
--- a/clients/smanagrun.pl
+++ b/clients/smanagrun.pl
@@ -7,9 +7,9 @@ use strict;
use Net::Telnet;
use DBI;
use POSIX;
+use Data::Dumper::Simple;
use lib '../include';
use nms;
-use Data::Dumper::Simple;
BEGIN {
require "../include/config.pm";
@@ -108,13 +108,26 @@ while (1) {
next;
}
while (my $row = $sgetallpoll->fetchrow_hashref()) {
- print "sysname: ".$row->{sysname}." cmd: ".$row->{cmd}."\n";
+ my $sysname = $row->{sysname};
my @data;
my @commands = split(/[\r\n\000]+/, $row->{cmd});
for my $cmd (@commands) {
+ print "sysname: $sysname cmd: $cmd\n";
next unless $cmd =~ /\S/; # ignorer linjer med kun whitespace
push @data, "# $cmd";
- if ($cmd =~ s/^!//) {
+ $cmd =~ s/%SYSNAME%/$sysname/g;
+ if ($cmd =~ /^#\s*require-version\s+(.*\S)\s*$/) {
+ my $required_version = $1;
+ my $versions;
+ $versions = switch_exec_json("show version", $conn);
+ my $version = $versions->{'multi-routing-engine-results'}[0]{'multi-routing-engine-item'}[0]{'software-information'}[0]{'junos-version'}[0]{'data'};
+ if ($version ne $required_version) {
+ push @data, "# '$version' != '$required_version', aborting script";
+ last;
+ } else {
+ push @data, "# Version matches";
+ }
+ } elsif ($cmd =~ s/^!//) {
push @data, switch_exec($cmd, $conn, 1);
} else {
push @data, switch_exec($cmd, $conn);
@@ -129,7 +142,8 @@ while (1) {
};
if ($@) {
warn $@;
- $sdelay->execute($@ . ", delaying...", $switch->{sysname});
+ chomp(my $err = $@);
+ $sdelay->execute($err . ", delaying...", $switch->{sysname});
$sunlock->execute($switch->{sysname});
$dbh->commit();
}
diff --git a/include/nms.pm b/include/nms.pm
index e532a05..1ade743 100755
--- a/include/nms.pm
+++ b/include/nms.pm
@@ -7,10 +7,11 @@ use Net::Telnet;
use Data::Dumper;
use FixedSNMP;
use FileHandle;
+use JSON;
package nms;
use base 'Exporter';
-our @EXPORT = qw(switch_disconnect switch_connect_ssh switch_connect_dlink switch_exec switch_timeout db_connect);
+our @EXPORT = qw(switch_disconnect switch_connect_ssh switch_connect_dlink switch_exec switch_exec_json switch_timeout db_connect);
BEGIN {
require "config.pm";
@@ -61,9 +62,10 @@ sub switch_connect_ssh($) {
#$inputlog->print("\n\nConnecting to " . $ip . "\n\n");
my $telnet = Net::Telnet->new(-fhopen => $pty,
+ -timeout => $nms::config::telnet_timeout,
-dump_log => $dumplog,
-input_log => $inputlog,
- -prompt => '/.*\@e\d\d-\d> /',
+ -prompt => '/.*\@e\d+-\d+[>#] /',
-telnetmode => 0,
-cmd_remove_mode => 1,
-output_record_separator => "\r");
@@ -131,7 +133,14 @@ sub switch_exec {
}
return @data;
}
-
+
+sub switch_exec_json($$) {
+ my ($cmd, $conn) = @_;
+ my @json = switch_exec("$cmd | display json", $conn);
+ pop @json; # Remove the banner at the end of the output
+ return ::decode_json(join("", @json));
+}
+
sub switch_timeout {
my ($timeout, $conn) = @_;
diff --git a/web/nms.gathering.org/sshow.pl b/web/nms.gathering.org/sshow.pl
index 1b310af..64663a2 100755
--- a/web/nms.gathering.org/sshow.pl
+++ b/web/nms.gathering.org/sshow.pl
@@ -34,7 +34,8 @@ my $slistdonegid = $dbh->prepare(
"SELECT DISTINCT gid, cmd, author, added
FROM squeue
WHERE processed = 't'
-ORDER BY gid")
+ORDER BY gid DESC
+LIMIT ?::text::int")
or die "Could not prepare slistdonegid";
my $slistprocgid = $dbh->prepare(
@@ -42,7 +43,7 @@ my $slistprocgid = $dbh->prepare(
FROM squeue
WHERE processed = 'f'
ORDER BY gid")
- or die "Could not prepare slistdonegid";
+ or die "Could not prepare slistprocgid";
my $sgetgid = $dbh->prepare(
"SELECT *
@@ -55,7 +56,7 @@ my $sgetprocessing = $dbh->prepare(
FROM squeue
WHERE processed = 'f'
ORDER BY updated DESC, gid, sysname")
- or die "Could not prepare sgetdone";
+ or die "Could not prepare sgetprocessing";
my $sgetnoconnect = $dbh->prepare(
"SELECT *
@@ -134,7 +135,7 @@ if ($action eq 'listgid') {
print "<pre>\n";
print "<a href=\"sshow.pl?action=noconnect\" />Kunne ikke koble til</a>\n\n\n";
print "<b>Ferdige:</b>\n";
- $slistdonegid->execute();
+ $slistdonegid->execute($limit);
my ($gid, $author);
$gid = -1;
while ((my $row = $slistdonegid->fetchrow_hashref())) {
@@ -146,7 +147,7 @@ if ($action eq 'listgid') {
print "Added: ".$row->{added}."\n";
}
my $cmd = $row->{cmd};
- print "\t$cmd\n";
+ print "$cmd\n\n";
}
print "\n\n";
print "<b>I kø:</b>\n";
@@ -161,7 +162,7 @@ if ($action eq 'listgid') {
print "Added: ".$row->{added}."\n";
}
my $cmd = $row->{cmd};
- print "\t$cmd\n";
+ print "$cmd\n\n";
}
$dbh->commit();
print "</pre>\n";
@@ -198,8 +199,7 @@ if ($action eq 'done') {
my $gid = $cgi->param('gid');
$sgetdonegid->execute($gid);
$squery = $sgetdonegid;
- }
- else {
+ } else {
$sgetdone->execute($limit);
$squery = $sgetdone;
}
@@ -212,7 +212,7 @@ if ($action eq 'done') {
print " Author: ".$row->{author}."\n";
print " Cmd: ".$row->{cmd}."\n";
print " Added: ".$row->{added}." Updated: ".$row->{updated}."\n";
- print " gID: ".$row->{gid}."\n";
+ print " GID: ".$row->{gid}."\n";
my @result = split(/[\n\r]+/, $row->{result});
foreach (@result) {
print "\t", encode_entities($_), "\n";