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