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