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