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