]> git.sesse.net Git - nms/blob - web/sshow.pl
initial import
[nms] / web / sshow.pl
1 #!/usr/bin/perl
2 #
3 #
4
5 my $username = '';
6 my $password = 'removed';
7
8 # Seconds to wait for connection
9 my $timeout = 15;
10
11 use warnings;
12 use strict;
13 use Switch;
14 use CGI;
15 use Net::Telnet;
16 use DBI;
17
18 # Grab from .htaccess-authentication
19 my $user = $ENV{'REMOTE_USER'};
20
21 my $dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org",
22                        "snmpfetch", "removed")
23         or die "Couldn't connect to database";
24 $dbh->{AutoCommit} = 0;
25
26 my $sgetdone = $dbh->prepare(
27 "SELECT * 
28 FROM  squeue 
29 WHERE processed = 't' 
30 ORDER BY updated DESC, sysname
31 LIMIT ?::text::int")
32         or die "Could not prepare sgetdone";
33
34 my $sgetdonegid = $dbh->prepare(
35 "SELECT * 
36 FROM  squeue 
37 WHERE processed = 't' AND gid = ?::text::int 
38 ORDER BY updated DESC, sysname")
39         or die "Could not prepare sgetdonegid";
40
41 my $slistdonegid = $dbh->prepare(
42 "SELECT DISTINCT gid, cmd, author, added
43 FROM squeue
44 WHERE processed = 't'
45 ORDER BY gid")
46         or die "Could not prepare slistdonegid";
47
48 my $slistprocgid = $dbh->prepare(
49 "SELECT DISTINCT gid, cmd, author, added
50 FROM squeue
51 WHERE processed = 'f'
52 ORDER BY gid")
53         or die "Could not prepare slistdonegid";
54
55 my $sgetgid = $dbh->prepare(
56 "SELECT *
57 FROM squeue
58 WHERE gid = ?")
59         or die "Could not prepare sgetgid";
60
61 my $sgetprocessing = $dbh->prepare(
62 "SELECT *
63 FROM  squeue
64 WHERE processed = 'f'
65 ORDER BY updated DESC, gid, sysname")
66         or die "Could not prepare sgetdone";
67
68 my $sgetnoconnect = $dbh->prepare(
69 "SELECT *
70 FROM squeue
71 WHERE result = 'Could not connect to switch, delaying...'")
72         or die "Could not prepare sgetnoconnect";
73
74 my $sdisablegid = $dbh->prepare("
75 UPDATE squeue SET disabled = 't'
76 WHERE gid = ?::text::int")
77         or die "Could not prepare sdisablegid";
78 my $senablegid = $dbh->prepare("
79 UPDATE squeue SET disabled = 'f'
80 WHERE gid = ?::text::int")
81         or die "Could not prepare sdisablegid";
82
83
84 my $cgi = new CGI;
85
86 print $cgi->header(-type=>'text/html');
87
88 print << "EOF";
89 <html>
90   <head>
91     <title>Switch managment</title>
92   </head>
93   <body>
94   <p>Du er logget inn som: $user</p>
95     <form method="POST" action="sshow.pl">
96     <p>
97       Vis <input type="text" name="count" size="4" value="10" /> siste<br />
98       Vis: <select name="action" />
99        <option value="listgid">Grupper</option>
100        <option value="done">Ferdige</option>
101        <option value="processing">I kø</option>
102       </select>
103       <input type="submit" value="Vis" /><br />
104     </p>
105     </form>
106     <br />
107 EOF
108
109 my $limit = $cgi->param('count');
110 if (!defined($limit)) {
111         $limit = 10;
112 }
113 my $action = $cgi->param('action');
114 if (!defined($action)) {
115         $action = 'listgid';
116 }
117
118 if (defined($cgi->param('agid'))) {
119         my $gid = $cgi->param('gid');
120         if (!defined($gid)) {
121                 print "<font color=\"red\">Du har ikke valgt en gid å slette.</font>\n";
122         }
123         if ($cgi->param('agid') eq 'Disable') {
124                 $sdisablegid->execute($gid);
125                 print "<p>gid: ".$cgi->param('gid')." har blitt disablet.\n";
126         }
127         else {
128                 $senablegid->execute($gid);
129                 print "<p>gid: ".$cgi->param('gid')." har blitt enablet.\n";
130         }
131         $dbh->commit();
132 }
133
134 if ($action eq 'noconnect') {
135         print "<h3>Kunne ikke koble til disse switchene:</h3>\n";
136         $sgetnoconnect->execute();
137         print "<pre>\n";
138         while ((my $row = $sgetnoconnect->fetchrow_hashref())) {
139                 print "$row->{'sysname'} : $row->{'cmd'} : Added: $row->{'added'} : Updated: $row->{'updated'}\n";
140         }
141         print "</pre>\n";
142 }
143
144 if ($action eq 'listgid') {
145         print "<pre>\n";
146         print "<a href=\"sshow.pl?action=noconnect\" />Kunne ikke koble til</a>\n\n\n";
147         print "<b>Ferdige:</b>\n";
148         $slistdonegid->execute();
149         my ($gid, $author);
150         $gid = -1;
151         while ((my $row = $slistdonegid->fetchrow_hashref())) {
152                 $author = $row->{author};
153                 if ($gid != $row->{gid}) {
154                         $gid = $row->{gid};
155                         print "GID: <a href=\"sshow.pl?action=showgid&gid=$gid\">$gid</a>\n";
156                         print "Author: $author\n";
157                         print "Added: ".$row->{added}."\n";
158                 }
159                 my $cmd = $row->{cmd};
160                 print "\t$cmd\n";
161         }
162         print "\n\n";
163         print "<b>I kø:</b>\n";
164         $slistprocgid->execute();
165         $gid = -1;
166         while ((my $row = $slistprocgid->fetchrow_hashref())) {
167                 $author = $row->{author};
168                 if ($gid != $row->{gid}) {
169                         $gid = $row->{gid};
170                         print "GID: <a href=\"sshow.pl?action=showgid&gid=$gid\">$gid</a>\n";
171                         print "Author: $author\n";
172                         print "Added: ".$row->{added}."\n";
173                 }
174                 my $cmd = $row->{cmd};
175                 print "\t$cmd\n";
176         }
177         $dbh->commit();
178         print "</pre>\n";
179 }
180
181 if ($action eq 'showgid') {
182         print "<pre>\n";
183         $sgetgid->execute($cgi->param('gid'));
184         my $row = $sgetgid->fetchrow_hashref();
185         print "GID: ".$row->{gid}."\n";
186         print "Author: ".$row->{author}."\n";
187         do {
188                 print "    <b>Name: ".$row->{sysname}." Addr: ".$row->{addr}."</b>\n";
189                 print "    `<b>".$row->{cmd}."`</b>\n";
190                 print "    <i>Added: ".$row->{added}." executed ".$row->{updated}."</i>\n";
191                 my $data = $row->{result};
192                 if (!defined($data)) {
193                         $data = "Not executed yet!";
194                 }
195                 my @lines = split(/[\n\r]+/, $data);
196                 foreach my $line (@lines) {
197                         print "\t$line\n";
198                 }
199         } while (($row = $sgetgid->fetchrow_hashref()));
200         print "</pre>\n";
201 }
202
203 if ($action eq 'done') {
204         print "<h3>Done</h3>\n";
205         print "<pre>\n";
206
207         my $squery;
208         if (defined($cgi->param('gid'))) {
209                 my $gid = $cgi->param('gid');
210                 $sgetdonegid->execute($gid);
211                 $squery = $sgetdonegid;
212         }
213         else {
214                 $sgetdone->execute($limit);
215                 $squery = $sgetdone;
216         }
217         my $sysname = '';
218         while (my $row = $squery->fetchrow_hashref()) {
219                 if ($sysname ne $row->{'sysname'}) {
220                         $sysname = $row->{'sysname'};
221                         print "$sysname (".$row->{addr}."):\n";
222                 }
223                 print "   Author: ".$row->{author}."\n";
224                 print "   Cmd: ".$row->{cmd}."\n";
225                 print "   Added: ".$row->{added}." Updated: ".$row->{updated}."\n";
226                 print "   gID: ".$row->{gid}."\n";
227                 my @result = split(/[\n\r]+/, $row->{result});
228                 foreach (@result) {
229                         print "\t".$_."\n";
230                 }
231                 print "\n";
232         }
233         $dbh->commit();
234         print "</pre>\n";
235 }
236 elsif ($action eq 'processing') {
237         print "<h3>Processing</h3>\n";
238         print "<pre>\n";
239         $sgetprocessing->execute();
240         while (my $row = $sgetprocessing->fetchrow_hashref()) {
241                 my $sysname = $row->{'sysname'};
242                 print "$sysname (".$row->{addr}."):\n";
243                 print "   Author: ".$row->{author}."\n";
244                 print "   Cmd: ".$row->{cmd}."\n";
245                 my $updated;
246                 if (defined($row->{updated})) { $updated = $row->{updated}; }
247                 else { $updated = 'never'; }
248                 print "   Added: ".$row->{added}." Updated: ".$updated."\n";
249                 print "   Disabled: ".$row->{disabled}."\n";
250                 print "   Locked: ".$row->{locked}."\n";
251                 print "   gID: ".$row->{gid};
252                 print "   <form action=\"sshow.pl\" methos=\"POST\">";
253                 print "<input type=\"hidden\" name=\"gid\" value=\"".$row->{gid}."\">";
254                 print "<input type=\"hidden\" name=\"action\" value=\"processing\">";
255                 if ($row->{disabled} == 0) {
256                         print "<input type=\"submit\" name=\"agid\" value=\"Disable\">\n";
257                 }
258                 else {
259                         print "<input type=\"submit\" name=\"agid\" value=\"Enable\">\n";
260                 }
261         }
262         $dbh->commit();
263         print "</pre>\n";
264 }
265
266 print << "EOF";
267   </body>
268 </html>
269 EOF