]> git.sesse.net Git - nms/blob - web/sshow.pl
Merge.
[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; charset=utf-8');
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                 print "<p>gid: ".$cgi->param('gid')." har blitt disablet.\n";
114         }
115         else {
116                 $senablegid->execute($gid);
117                 print "<p>gid: ".$cgi->param('gid')." har blitt enablet.\n";
118         }
119         $dbh->commit();
120 }
121
122 if ($action eq 'noconnect') {
123         print "<h3>Kunne ikke koble til disse switchene:</h3>\n";
124         $sgetnoconnect->execute();
125         print "<pre>\n";
126         while ((my $row = $sgetnoconnect->fetchrow_hashref())) {
127                 print "$row->{'sysname'} : $row->{'cmd'} : Added: $row->{'added'} : Updated: $row->{'updated'}\n";
128         }
129         print "</pre>\n";
130 }
131
132 if ($action eq 'listgid') {
133         print "<pre>\n";
134         print "<a href=\"sshow.pl?action=noconnect\" />Kunne ikke koble til</a>\n\n\n";
135         print "<b>Ferdige:</b>\n";
136         $slistdonegid->execute();
137         my ($gid, $author);
138         $gid = -1;
139         while ((my $row = $slistdonegid->fetchrow_hashref())) {
140                 $author = $row->{author};
141                 if ($gid != $row->{gid}) {
142                         $gid = $row->{gid};
143                         print "GID: <a href=\"sshow.pl?action=showgid&gid=$gid\">$gid</a>\n";
144                         print "Author: $author\n";
145                         print "Added: ".$row->{added}."\n";
146                 }
147                 my $cmd = $row->{cmd};
148                 print "\t$cmd\n";
149         }
150         print "\n\n";
151         print "<b>I kø:</b>\n";
152         $slistprocgid->execute();
153         $gid = -1;
154         while ((my $row = $slistprocgid->fetchrow_hashref())) {
155                 $author = $row->{author};
156                 if ($gid != $row->{gid}) {
157                         $gid = $row->{gid};
158                         print "GID: <a href=\"sshow.pl?action=showgid&gid=$gid\">$gid</a>\n";
159                         print "Author: $author\n";
160                         print "Added: ".$row->{added}."\n";
161                 }
162                 my $cmd = $row->{cmd};
163                 print "\t$cmd\n";
164         }
165         $dbh->commit();
166         print "</pre>\n";
167 }
168
169 if ($action eq 'showgid') {
170         print "<pre>\n";
171         $sgetgid->execute($cgi->param('gid'));
172         my $row = $sgetgid->fetchrow_hashref();
173         print "GID: ".$row->{gid}."\n";
174         print "Author: ".$row->{author}."\n";
175         do {
176                 print "    <b>Name: ".$row->{sysname}." Addr: ".$row->{addr}."</b>\n";
177                 print "    `<b>".$row->{cmd}."`</b>\n";
178                 print "    <i>Added: ".$row->{added}." executed ".$row->{updated}."</i>\n";
179                 my $data = $row->{result};
180                 if (!defined($data)) {
181                         $data = "Not executed yet!";
182                 }
183                 my @lines = split(/[\n\r]+/, $data);
184                 foreach my $line (@lines) {
185                         print "\t$line\n";
186                 }
187         } while (($row = $sgetgid->fetchrow_hashref()));
188         print "</pre>\n";
189 }
190
191 if ($action eq 'done') {
192         print "<h3>Done</h3>\n";
193         print "<pre>\n";
194
195         my $squery;
196         if (defined($cgi->param('gid'))) {
197                 my $gid = $cgi->param('gid');
198                 $sgetdonegid->execute($gid);
199                 $squery = $sgetdonegid;
200         }
201         else {
202                 $sgetdone->execute($limit);
203                 $squery = $sgetdone;
204         }
205         my $sysname = '';
206         while (my $row = $squery->fetchrow_hashref()) {
207                 if ($sysname ne $row->{'sysname'}) {
208                         $sysname = $row->{'sysname'};
209                         print "$sysname (".$row->{addr}."):\n";
210                 }
211                 print "   Author: ".$row->{author}."\n";
212                 print "   Cmd: ".$row->{cmd}."\n";
213                 print "   Added: ".$row->{added}." Updated: ".$row->{updated}."\n";
214                 print "   gID: ".$row->{gid}."\n";
215                 my @result = split(/[\n\r]+/, $row->{result});
216                 foreach (@result) {
217                         print "\t".$_."\n";
218                 }
219                 print "\n";
220         }
221         $dbh->commit();
222         print "</pre>\n";
223 }
224 elsif ($action eq 'processing') {
225         print "<h3>Processing</h3>\n";
226         print "<pre>\n";
227         $sgetprocessing->execute();
228         while (my $row = $sgetprocessing->fetchrow_hashref()) {
229                 my $sysname = $row->{'sysname'};
230                 print "$sysname (".$row->{addr}."):\n";
231                 print "   Author: ".$row->{author}."\n";
232                 print "   Cmd: ".$row->{cmd}."\n";
233                 my $updated;
234                 if (defined($row->{updated})) { $updated = $row->{updated}; }
235                 else { $updated = 'never'; }
236                 print "   Added: ".$row->{added}." Updated: ".$updated."\n";
237                 print "   Disabled: ".$row->{disabled}."\n";
238                 print "   Locked: ".$row->{locked}."\n";
239                 print "   gID: ".$row->{gid};
240                 print "   <form action=\"sshow.pl\" methos=\"POST\">";
241                 print "<input type=\"hidden\" name=\"gid\" value=\"".$row->{gid}."\">";
242                 print "<input type=\"hidden\" name=\"action\" value=\"processing\">";
243                 if ($row->{disabled} == 0) {
244                         print "<input type=\"submit\" name=\"agid\" value=\"Disable\">\n";
245                 }
246                 else {
247                         print "<input type=\"submit\" name=\"agid\" value=\"Enable\">\n";
248                 }
249         }
250         $dbh->commit();
251         print "</pre>\n";
252 }
253
254 print << "EOF";
255   </body>
256 </html>
257 EOF