aboutsummaryrefslogtreecommitdiffstats
path: root/web/nms.gathering.org/sshow.pl
blob: 64663a225cd91ec71a707d3a7fa7c908ae1604df (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/perl
use lib '../../include';
use nms;

use warnings;
use strict;
use Switch;
use CGI;
use DBI;
use HTML::Entities;

# Grab from .htaccess-authentication
my $user = $ENV{'REMOTE_USER'};

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

my $sgetdone = $dbh->prepare(
"SELECT * 
FROM  squeue 
WHERE processed = 't' 
ORDER BY updated DESC, sysname
LIMIT ?::text::int")
	or die "Could not prepare sgetdone";

my $sgetdonegid = $dbh->prepare(
"SELECT * 
FROM  squeue 
WHERE processed = 't' AND gid = ?::text::int 
ORDER BY updated DESC, sysname")
	or die "Could not prepare sgetdonegid";

my $slistdonegid = $dbh->prepare(
"SELECT DISTINCT gid, cmd, author, added
FROM squeue
WHERE processed = 't'
ORDER BY gid DESC
LIMIT ?::text::int")
	or die "Could not prepare slistdonegid";

my $slistprocgid = $dbh->prepare(
"SELECT DISTINCT gid, cmd, author, added
FROM squeue
WHERE processed = 'f'
ORDER BY gid")
	or die "Could not prepare slistprocgid";

my $sgetgid = $dbh->prepare(
"SELECT *
FROM squeue
WHERE gid = ?")
	or die "Could not prepare sgetgid";

my $sgetprocessing = $dbh->prepare(
"SELECT *
FROM  squeue
WHERE processed = 'f'
ORDER BY updated DESC, gid, sysname")
	or die "Could not prepare sgetprocessing";

my $sgetnoconnect = $dbh->prepare(
"SELECT *
FROM squeue
WHERE result = 'Could not connect to switch, delaying...'")
	or die "Could not prepare sgetnoconnect";

my $sdisablegid = $dbh->prepare("
UPDATE squeue SET disabled = 't'
WHERE gid = ?::text::int")
	or die "Could not prepare sdisablegid";
my $senablegid = $dbh->prepare("
UPDATE squeue SET disabled = 'f'
WHERE gid = ?::text::int")
	or die "Could not prepare sdisablegid";


my $cgi = new CGI;

print $cgi->header(-type=>'text/html; charset=utf-8');

print << "EOF";
<html>
  <head>
    <title>Switch managment</title>
  </head>
  <body>
  <p>Du er logget inn som: $user</p>
    <form method="POST" action="sshow.pl">
    <p>
      Vis <input type="text" name="count" size="4" value="10" /> siste<br />
      Vis: <select name="action" />
       <option value="listgid">Grupper</option>
       <option value="done">Ferdige</option>
       <option value="processing">I </option>
      </select>
      <input type="submit" value="Vis" /><br />
    </p>
    </form>
    <br />
EOF

my $limit = $cgi->param('count');
if (!defined($limit)) {
	$limit = 10;
}
my $action = $cgi->param('action');
if (!defined($action)) {
	$action = 'listgid';
}

if (defined($cgi->param('agid'))) {
	my $gid = $cgi->param('gid');
	if (!defined($gid)) {
		print "<font color=\"red\">Du har ikke valgt en gid å slette.</font>\n";
		print "<p>gid: ".$cgi->param('gid')." har blitt disablet.\n";
	}
	else {
		$senablegid->execute($gid);
		print "<p>gid: ".$cgi->param('gid')." har blitt enablet.\n";
	}
	$dbh->commit();
}

if ($action eq 'noconnect') {
	print "<h3>Kunne ikke koble til disse switchene:</h3>\n";
	$sgetnoconnect->execute();
	print "<pre>\n";
	while ((my $row = $sgetnoconnect->fetchrow_hashref())) {
		print "$row->{'sysname'} : $row->{'cmd'} : Added: $row->{'added'} : Updated: $row->{'updated'}\n";
	}
	print "</pre>\n";
}

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($limit);
	my ($gid, $author);
	$gid = -1;
	while ((my $row = $slistdonegid->fetchrow_hashref())) {
		$author = $row->{author};
		if ($gid != $row->{gid}) {
			$gid = $row->{gid};
			print "GID: <a href=\"sshow.pl?action=showgid&gid=$gid\">$gid</a>\n";
			print "Author: $author\n";
			print "Added: ".$row->{added}."\n";
		}
		my $cmd = $row->{cmd};
		print "$cmd\n\n";
	}
	print "\n\n";
	print "<b>I kø:</b>\n";
	$slistprocgid->execute();
	$gid = -1;
	while ((my $row = $slistprocgid->fetchrow_hashref())) {
		$author = $row->{author};
		if ($gid != $row->{gid}) {
			$gid = $row->{gid};
			print "GID: <a href=\"sshow.pl?action=showgid&gid=$gid\">$gid</a>\n";
			print "Author: $author\n";
			print "Added: ".$row->{added}."\n";
		}
		my $cmd = $row->{cmd};
		print "$cmd\n\n";
	}
	$dbh->commit();
	print "</pre>\n";
}

if ($action eq 'showgid') {
	print "<pre>\n";
	$sgetgid->execute($cgi->param('gid'));
	my $row = $sgetgid->fetchrow_hashref();
	print "GID: ".$row->{gid}."\n";
	print "Author: ".$row->{author}."\n";
	do {
		print "    <b>Name: ".$row->{sysname}." Addr: ".$row->{addr}."</b>\n";
		print "    `<b>".$row->{cmd}."`</b>\n";
		print "    <i>Added: ".$row->{added}." executed ".$row->{updated}."</i>\n";
		my $data = $row->{result};
		if (!defined($data)) {
			$data = "Not executed yet!";
		}
		my @lines = split(/[\n\r]+/, $data);
		foreach my $line (@lines) {
			print "\t", encode_entities($line), "\n";
		}
	} while (($row = $sgetgid->fetchrow_hashref()));
	print "</pre>\n";
}

if ($action eq 'done') {
	print "<h3>Done</h3>\n";
	print "<pre>\n";

	my $squery;
	if (defined($cgi->param('gid'))) {
		my $gid = $cgi->param('gid');
		$sgetdonegid->execute($gid);
		$squery = $sgetdonegid;
	} else {
		$sgetdone->execute($limit);
		$squery = $sgetdone;
	}
	my $sysname = '';
	while (my $row = $squery->fetchrow_hashref()) {
		if ($sysname ne $row->{'sysname'}) {
			$sysname = $row->{'sysname'};
			print "$sysname (".$row->{addr}."):\n";
		}
		print "   Author: ".$row->{author}."\n";
		print "   Cmd: ".$row->{cmd}."\n";
		print "   Added: ".$row->{added}." Updated: ".$row->{updated}."\n";
		print "   GID: ".$row->{gid}."\n";
		my @result = split(/[\n\r]+/, $row->{result});
		foreach (@result) {
			print "\t", encode_entities($_), "\n";
		}
		print "\n";
	}
	$dbh->commit();
	print "</pre>\n";
}
elsif ($action eq 'processing') {
	print "<h3>Processing</h3>\n";
	print "<pre>\n";
	$sgetprocessing->execute();
	while (my $row = $sgetprocessing->fetchrow_hashref()) {
		my $sysname = $row->{'sysname'};
		print "$sysname (".$row->{addr}."):\n";
		print "   Author: ".$row->{author}."\n";
		print "   Cmd: ".$row->{cmd}."\n";
		my $updated;
		if (defined($row->{updated})) { $updated = $row->{updated}; }
		else { $updated = 'never'; }
		print "   Added: ".$row->{added}." Updated: ".$updated."\n";
		print "   Disabled: ".$row->{disabled}."\n";
		print "   Locked: ".$row->{locked}."\n";
		print "   gID: ".$row->{gid};
		print "   <form action=\"sshow.pl\" methos=\"POST\">";
		print "<input type=\"hidden\" name=\"gid\" value=\"".$row->{gid}."\">";
		print "<input type=\"hidden\" name=\"action\" value=\"processing\">";
		if ($row->{disabled} == 0) {
			print "<input type=\"submit\" name=\"agid\" value=\"Disable\">\n";
		}
		else {
			print "<input type=\"submit\" name=\"agid\" value=\"Enable\">\n";
		}
	}
	$dbh->commit();
	print "</pre>\n";
}

print << "EOF";
  </body>
</html>
EOF