aboutsummaryrefslogtreecommitdiffstats
path: root/clients/smanagrun.pl
blob: 4d03696b18bab1da04c28775f2f528c8f57244eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl
#
#

use warnings;
use strict;
use Net::Telnet;
use DBI;
use POSIX;
use lib '../include';
use nms;
use Data::Dumper::Simple;

BEGIN {
	require "../include/config.pm";
	eval {
		require "../include/config.local.pm";
	};
}
# Tweak and check
my $password = '';
my $timeout = 15;
my $delaytime = 30;
my $poll_frequency = 60;

my $dbh = db_connect();
$dbh->{AutoCommit} = 0;

my $spoll = $dbh->prepare("
SELECT
  addr,
  sysname
FROM
  squeue
WHERE
  processed = 'f' AND
  disabled = 'f' AND
  (locked='f' OR now() - updated > '10 minutes'::interval) AND
  (delay IS NULL OR delay < now())
ORDER BY
  priority DESC,
  added
LIMIT 1");
my $sgetallpoll = $dbh->prepare("
SELECT
  id,
  gid,
  addr,
  sysname,
  cmd
FROM
  squeue
WHERE
  sysname = ? AND
  disabled = 'f' AND
  processed = 'f'
ORDER BY
  priority DESC,
  added");

my $slock = $dbh->prepare("UPDATE squeue SET locked = 't', updated=now() WHERE sysname = ?")
	or die "Unable to prepare slock";
my $sunlock = $dbh->prepare("UPDATE squeue SET locked = 'f', updated=now() WHERE sysname = ?")
	or die "Unable to prepare sunlock";
my $sresult = $dbh->prepare("UPDATE squeue SET updated = now(), result = ?,
                            processed = 't' WHERE id = ?")
	or die "Unable to prepare sresult";
my $sdelay = $dbh->prepare("UPDATE squeue SET delay = now() + delaytime, updated=now(), result = ? WHERE sysname = ?")
	or die "Unable to prepae sdelay";

## Send a command to switch and return the data recvied from the switch
#sub switch_exec($$) {
#	my ($cmd, $conn) = @_;
#
#	# Send the command and get data from switch
#	my @data = $conn->cmd($cmd);
#	my @lines = ();
#	foreach my $line (@data) {
#		# Remove escape-7 sequence
#		$line =~ s/\x1b\x37//g;
#		push (@lines, $line);
#	}
#
#	return @data;
#}
#
#sub switch_connect($) {
#	my ($ip) = @_;
#
#	my $conn = new Net::Telnet(	Timeout => $timeout,
#					Dump_Log => '/tmp/dumplog-queue',
#					Errmode => 'return',
#					Prompt => '/DGS-3100\#/i');
#	my $ret = $conn->open(	Host => $ip);
#	if (!$ret || $ret != 1) {
#		return (undef);
#	}
#	# XXX: Just send the password as text, I did not figure out how to
#	# handle authentication with only password through $conn->login().
#	#$conn->login(��Prompt => '/password[: ]*$/i',
#	#		Name => $password,
#	#		Password => $password);
#	$conn->cmd($password);
#	# Get rid of banner
#	$conn->get;
#	return ($conn);
#}
#
sub mylog {
	my $msg = shift;
	my $time = POSIX::ctime(time);
	$time =~ s/\n.*$//;
	printf STDERR "[%s] %s\n", $time, $msg;
}

while (1) {
	$spoll->execute() or die "Could not execute spoll";
	my $switch = $spoll->fetchrow_hashref();
	if (!defined($switch)) {
		$dbh->commit;
		mylog("No available switches in pool, sleeping.");
		sleep 10;
		next;
	}
	$slock->execute($switch->{sysname});
	$dbh->commit();

	if ($switch->{'locked'}) {
		mylog("WARNING: Lock timed out on $switch->{'ip'}, breaking lock");
	}

	mylog("Connecting to $switch->{sysname} on $switch->{addr}");
	my $conn = switch_connect($switch->{addr});
	if (!defined($conn)) {
		mylog("Could not connect to ".$switch->{sysname}."(".$switch->{addr}.")");
		$sdelay->execute("Could not connect to switch, delaying...", $switch->{sysname});
		$sunlock->execute($switch->{sysname});
		$dbh->commit();
		next;
	}
	my $error;
	$error = $sgetallpoll->execute($switch->{sysname});
	if (!$error) {
		print "Could not execute sgetallpoll\n".$dbh->errstr();
		$conn->close;
		next;
	}
	while (my $row = $sgetallpoll->fetchrow_hashref()) {
		print "sysname: ".$row->{sysname}." cmd: ".$row->{cmd}."\n";
		my @data;
		my @commands = split(/[\r\n\000]+/, $row->{cmd});
		for my $cmd (@commands) {
			next unless $cmd =~ /\S/; # ignorer linjer med kun whitespace
			push @data, "# $cmd";
			if ($cmd =~ s/^!//) {
				push @data, switch_exec($cmd, $conn, 1);
			} else {
				push @data, switch_exec($cmd, $conn);
			}
		}
		my $result = join("\n", @data);
		$sresult->execute($result, $row->{id});
	}
	$conn->close();
	$sunlock->execute($switch->{sysname});
}