]> git.sesse.net Git - nms/blob - web/smanagement.pl
ed783b2fa915b245f821f92c9d19675416df061c
[nms] / web / smanagement.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use CGI;
5 use DBI;
6 use Data::Dumper;
7 use lib '../include';
8 use nms;
9
10 # Grab from .htaccess-authentication
11 my $user = $ENV{'REMOTE_USER'};
12
13 my $dbh = nms::db_connect();
14 $dbh->{AutoCommit} = 0;
15
16 # Ugly casting, found not other way
17 my $sinsert = $dbh->prepare(    "INSERT INTO squeue 
18                                 (gid, added, priority, addr, sysname, cmd, author)
19                                 VALUES(?::text::int, now(), ?::text::int, ?::text::inet, ?, ?, ?)")
20         or die "Could not prepare sinsert";
21 my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
22         or die "Could not prepare sgetip";
23 my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
24
25 sub parse_range($) {
26         my $switches = $_;
27         my @range;
28
29         my @rangecomma = split(/\s*,\s*/, $switches);
30         foreach (@rangecomma) {
31                 my ($first, $drop1, $last, $drop2) = $_ =~ /(e\d+\-[123456])(sw)?\s*\-\s*(e\d+\-[123456])?(sw)?/;
32                 if (!defined($first) && $_ =~ /e\d+\-[123456]/) {
33                         $first = $_;
34                 }
35                 if (!defined($first)) {
36                         print "<font color=\"red\">Parse error in: $_</font><br>\n";
37                         next;
38                 }
39                 my ($rowstart, $placestart) = $first =~ /e(\d+)\-([123456])/;
40                 if (!defined($rowstart) || !defined($placestart)) {
41                         print "<font color=\"red\">Parse error in: $_</font><br>\n";
42                         next;
43                 }
44                 my ($rowend, $placeend);
45                 if (!defined($last)) {
46                         $rowend = $rowstart;
47                         $placeend = $placestart;
48                 }
49                 else {
50                         ($rowend, $placeend) = $last =~ /e(\d+)\-([123456])/;
51                 }
52                 if (!defined($rowend) || !defined($placeend)) {
53                         print "<font color=\"red\">Parse error in: $_</font><br>\n";
54                         next;
55                 }
56                 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
57                 for (my $i = $rowstart; $i <= $rowend; $i++) {
58                         my $dostart;
59                         if ($rowstart != $i) {
60                                 $dostart = 1;
61                         }
62                         else {
63                                 $dostart = $placestart;
64                         }
65                         for (my $j = $dostart; $j <= 6; $j++) {
66                                 last if ($i == $rowend && $j > $placeend);
67                                 push(@range, "e$i-$j");
68                         }
69                 }
70         }
71 #       foreach (@range) {
72 #               print ":: $_<br>\n";
73 #       }
74         return @range;
75 }
76
77 sub get_addr_from_switchnum($) {
78         my ($sysname) = @_;
79
80         $sgetip->execute($sysname."sw");
81         if ($sgetip->rows() < 1) {
82                 print "Could not get the ip for: ".$sysname."sw";
83                 return undef;
84         }
85         my $row = $sgetip->fetchrow_hashref();
86         return $row->{'ip'};
87 }
88
89 my $cgi = new CGI;
90
91 print $cgi->header(-type=>'text/html');
92
93 print << "EOF";
94 <html>
95   <head>
96     <title>Switch managment</title>
97   </head>
98   <body>
99   <p>Du er logget inn som: $user</p>
100     <form method="POST" action="smanagement.pl">
101     <table>
102       <tr>
103         <td>Alle switchene</td>
104         <td><input type="radio" name="rangetype" value="all" /></td>
105         <td></td>
106         <td>Disabled</td>
107       </tr>
108       <tr>
109         <td>Switch</td>
110         <td><input type="radio" checked name="rangetype" value="switch" /></td>
111         <td><input type="text" name="range" /></td>
112         <td>e1-2, e3-3 - e10-2</td>
113       </tr>
114       <tr>
115         <td>Rad</td>
116         <td><input type="radio" name="rangetype" value="row" /></td>
117         <td><input type="text" name="range" /></td>
118         <td>1,3-5 (Disabled)</td>
119       </tr>
120        <tr>
121         <td><hr /></td>
122         <td><hr /></td>
123         <td><hr /></td>
124         <td><hr /></td>
125       </tr>
126       <tr>
127         <td>Prioritet</td>
128         <td></td>
129         <td>
130           <select name="priority">
131             <option value="1">1 (lavest)</option>
132             <option value="2">2</option>
133             <option selected value="3">3</option>
134             <option value="4">4</option>
135             <option value="5">5 (høyest)</option>
136           </select>
137         </td>
138       </tr>
139       <tr>
140         <td>Kommando(er):</td>
141         <td></td>
142         <td><textarea name="cmd"></textarea></td>
143         <td>En kommando per linje</td>
144       </td>
145       <tr>
146         <td><hr /></td>
147         <td><hr /></td>
148         <td><hr /></td>
149         <td><hr /></td>
150       </tr>
151     </table>
152     <input type="submit" value="Execute!" /><br />
153     </form>
154 EOF
155
156 print "<br />\n";
157
158 my @switches = ();
159 switch ($cgi->param('rangetype')) {
160         case 'all' {
161 #               print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
162                 @switches = ();
163                 print "<font color=\"red\">Slått av!</font>\n";
164         }
165         case 'switch' {
166 #               print "Sender `".$cgi->param('cmd')."` til switchene `"
167 #                     .$cgi->param('range')."`.<br />";
168                 $_ = $cgi->param('range');
169                 @switches = parse_range($_);
170         }
171         case 'row' {
172 #               print "Sender `".$cgi->param('cmd')."` til radene `"
173 #                     .$cgi->param('range')."`.<br />";
174 #               print "This function does not work yet.";
175 #               $_ = $cgi->param('range');
176 #               @switches = &parse_row_range($_);
177                 @switches = ();
178                 print "<font color=\"red\">Slått av!</font>\n";
179         }
180 }
181
182 my $gid;
183 if (@switches > 0) {
184         $sgid->execute();
185         my $row = $sgid->fetchrow_hashref();
186         $gid = $row->{gid};
187 }
188
189 my $pri = $cgi->param('priority');
190
191 print "<pre>\n";
192 foreach my $switch (@switches) {
193         my $addr = get_addr_from_switchnum($switch);
194         if (!defined($addr)) {
195                 next;
196         }
197         print "$switch got addr $addr <br>\n";
198         my @cmds = split(/[\n\r]+/, $cgi->param('cmd'));
199         print "Queuing commands for $switch:\n";
200         foreach my $cmd (@cmds) {
201                 my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
202         #       my $result = 1;
203                 if (!$result) {
204                         print "\t<font color=\"red\">"
205                                ."Could not execute query."
206                                ."</font>\n";
207                         print "\t".$dbh->errstr."\n";
208                 }
209                 else {
210                         print "\tQueued: $cmd\n";
211                 }
212         }
213         print "\n";
214 }
215 $dbh->commit;
216 if (defined($gid)) {
217         print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";
218 }
219 print "</pre>\n";
220
221 print << "EOF";
222   </body>
223 </html>
224 EOF