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