-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconman.cgi
More file actions
executable file
·609 lines (564 loc) · 23.7 KB
/
conman.cgi
File metadata and controls
executable file
·609 lines (564 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Switch;
use HTML::Table;
use CGI qw(:standard);
# user editable variables begin
my $dbfile = './cables.db';
my $scriptname = 'conman.cgi';
my $readonlymode = 0;
# user editable variables end
my $query_interface_id_name = "SELECT interface.id, device.name || '.' || interface.name FROM interface INNER JOIN device ON device.id = interface.device_id ORDER BY device.name ASC, interface.name ASC";
my $query_interface_type_id_name = "select id, name from interface_type";
my $query_device_type_id_name = "select id, name from device_type";
my $query_room_id_name = "select id, name from room";
my $query_rack_id_name = "select id, name from rack";
my $query_list_room = "SELECT id, name, description, notes FROM room ORDER BY name ASC";
my $query_list_rack = "SELECT rack.id, rack.name, rack.description, room.name, rack.notes FROM rack INNER JOIN room ON rack.room_id=room.id ORDER BY rack.name ASC";
my $query_list_device = "SELECT device.id, device.name, device_type.name, rack.name, device.description, device.notes FROM device INNER JOIN device_type ON device.device_type_id=device_type.id INNER JOIN rack ON device.rack_id=rack.id ORDER BY device.name";
my $query_list_interface = "SELECT interface.id, device.name || '.' || interface.name, interface_type.name, interface.notes FROM interface INNER JOIN interface_type ON interface.interface_type_id=interface_type.id INNER JOIN device ON interface.device_id=device.id ORDER BY device.name ASC, interface.name ASC";
my $query_list_connection = "SELECT i1.id, d1.name || '.' || i1.name, d2.name || '.' || i2.name, linklist.seq FROM interface AS i1, interface AS i2, linklist, device AS d1, device AS d2 WHERE linklist.from_interface_id = i1.id AND linklist.interface_id=i2.id AND i1.device_id=d1.id AND i2.device_id=d2.id ORDER BY d1.name, i1.name, linklist.seq";
my $dbh;
sub init_db() {
if ($readonlymode == 1) {
return;
}
$dbh->do("CREATE TABLE device_type(id INTEGER PRIMARY KEY, name TEXT NOT NULL)");
$dbh->do("CREATE TABLE room(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL CHECK (NOT name = ''), description TEXT, notes TEXT)");
$dbh->do("CREATE TABLE rack(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL CHECK (NOT name = ''), description TEXT, room_id INT, notes TEXT, FOREIGN KEY(room_id) REFERENCES room(id))");
$dbh->do("CREATE TABLE device(id INTEGER PRIMARY KEY AUTOINCREMENT, device_type_id INT, name TEXT NOT NULL CHECK (NOT name = ''), description TEXT, rack_id INT, notes TEXT, FOREIGN KEY(rack_id) REFERENCES rack(id), FOREIGN KEY(device_type_id) REFERENCES device_type(id))");
$dbh->do("CREATE TABLE interface_type(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL CHECK (NOT name = ''))");
$dbh->do("CREATE TABLE interface(id INTEGER PRIMARY KEY AUTOINCREMENT, interface_type_id INT, name TEXT NOT NULL CHECK (NOT name = ''), device_id INT, notes TEXT, FOREIGN KEY(interface_type_id) REFERENCES interface_type(id), FOREIGN KEY(device_id) REFERENCES device(id))");
$dbh->do("CREATE TABLE linklist(".
"id INTEGER PRIMARY KEY AUTOINCREMENT, from_interface_id INT, interface_id INT UNIQUE, seq INT NOT NULL, ".
"FOREIGN KEY(from_interface_id) REFERENCES interface(id), ".
"FOREIGN KEY(interface_id) REFERENCES interface(id), ".
"CHECK (NOT from_interface_id = interface_id))");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'switch')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'computer')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'patch panel')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'wall sockets')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'KVM switch')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'blade chassis')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'webcam')");
$dbh->do("INSERT INTO device_type (id, name) VALUES (null, 'terminal')");
$dbh->do("INSERT INTO interface_type (id, name) VALUES (null, 'Ethernet (copper)')");
$dbh->do("INSERT INTO interface_type (id, name) VALUES (null, 'Ethernet (fiber)')");
$dbh->do("INSERT INTO interface_type (id, name) VALUES (null, 'Serial console')");
$dbh->do("INSERT INTO interface_type (id, name) VALUES (null, 'KVM')");
$dbh->do("INSERT INTO interface_type (id, name) VALUES (null, 'misc')");
}
sub print_header() {
print <<EOF;
Content-Type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head><title>ConnectionManager</title></head>
<body>
EOF
}
sub print_navigation() {
print "<center>\n";
print "<table>\n";
print " <tr>\n";
print " <td><a href=\"".$scriptname."?command=list&subcommand=room\">Rooms</a></td>\n";
print " <td><a href=\"".$scriptname."?command=list&subcommand=rack\">Racks</a></td>\n";
print " <td><a href=\"".$scriptname."?command=list&subcommand=device\">Devices</a></td>\n";
print " <td><a href=\"".$scriptname."?command=list&subcommand=interface\">Interfaces</a></td>\n";
print " <td><a href=\"".$scriptname."?command=list&subcommand=connection\">Connections</a></td>\n";
print " </tr>\n";
print "</table>\n";
print "</center>\n";
}
sub print_refresh_javascript($) {
my ($params) = @_;
print "<script type=\"text/javascript\">\n".
"<!--\n".
"window.location = \"$scriptname?$params\"\n".
"//-->\n".
"</script>\n";
}
sub print_footer() {
print <<EOF;
</body>
</html>
EOF
}
sub get_connection_list_for_interface($) {
my @connlist;
my ($id) = @_;
my $connsth;
$connsth = $dbh->prepare("select device.name || '.' || interface.name from interface INNER JOIN device ON interface.device_id=device.id WHERE interface.id = (SELECT DISTINCT from_interface_id FROM linklist WHERE interface_id = ? OR from_interface_id = ?)");
$connsth->execute($id,$id);
my $row;
while ($row = $connsth->fetchrow_arrayref()) {
push(@connlist, $row->[0]);
}
$connsth->finish();
$connsth = $dbh->prepare("select device.name || '.' || interface.name from linklist INNER JOIN device ON interface.device_id=device.id INNER JOIN interface ON interface.id=linklist.interface_id WHERE linklist.from_interface_id = (SELECT DISTINCT from_interface_id FROM linklist WHERE interface_id = ? OR from_interface_id = ?) ORDER BY seq ASC");
$connsth->execute($id,$id);
while ($row = $connsth->fetchrow_arrayref()) {
push(@connlist, $row->[0]);
}
$connsth->finish();
return @connlist;
}
sub select_id_name($$$) {
my ($query, $selectname, $default) = @_;
my $tointidsth = $dbh->prepare($query);
$tointidsth->execute();
my $row;
my %labels;
my @values;
push(@values, '');
while ($row = $tointidsth->fetchrow_arrayref()) {
push(@values, $row->[0]);
$labels{$row->[0]} = $row->[1];
}
$tointidsth->finish();
if (!$default) {
$default=$values[0];
}
return popup_menu($selectname, \@values, $default, \%labels);
}
sub delete_connection($) {
my ($id) = @_;
if ($readonlymode == 0) {
my $delconnsth = $dbh->prepare("DELETE FROM linklist WHERE from_interface_id = (SELECT DISTINCT from_interface_id FROM linklist WHERE from_interface_id = ? OR interface_id = ?)");
$delconnsth->execute($id,$id);
$delconnsth->finish();
}
}
sub edit_room($) {
my ($id) = @_;
my ($name, $notes, $description) = (param('name'), param('notes'), param('description'));
if ($id && $name) {
my $query = "UPDATE room SET name='$name'";
if ($notes) {
$query = $query.", notes='$notes'";
} else {
$query = $query.", notes=''";
}
if ($description) {
$query = $query.", description='$description'";
} else {
$query = $query.", description=''";
}
$query = $query." WHERE id = $id";
if ($readonlymode == 0) {
my $updateroomsth = $dbh->prepare($query);
$updateroomsth->execute();
$updateroomsth->finish();
}
}
my $roomsth = $dbh->prepare("SELECT id, name, description, notes FROM room WHERE id = ?");
$roomsth->execute($id);
my $row;
print start_form(-method=>'get', -action=>"$scriptname");
if ($row = $roomsth->fetchrow_arrayref()) {
print "<h2>Room: $row->[1]</h2>\n";
print "Name: ".textfield('name',$row->[1], 40, 80)."<br/>\n";
print "Description: ".textfield('description',$row->[2], 40, 80)."<br/>\n";
print "Notes: ".textfield('notes',$row->[3], 40, 80)."<br/>\n";
}
$roomsth->finish();
print "<input type=\"hidden\" name=\"id\" value=\"$id\"/>\n";
print '<input type="hidden" name="command" value="edit"/><input type="hidden" name="subcommand" value="room"/>'.submit('submit', 'commit changes');
print end_form();
print "<h3>Racks</h3>\n";
my $racktable = new HTML::Table();
$racktable->addRow("ID", "Name", "Description", "Notes");
my $racksth = $dbh->prepare("SELECT id, name, description, notes FROM rack WHERE room_id = ? ORDER BY name");
$racksth->execute($id);
while ($row = $racksth->fetchrow_arrayref()) {
my $rackid = $row->[0];
$row->[1] = "<a href=\"$scriptname?command=edit&subcommand=rack&id=$rackid\">$row->[1]</a>";
$racktable->addRow(@$row, "<a href=\"$scriptname?command=remove&subcommand=rack&id=$rackid\">delete rack</a>");
}
$racksth->finish();
$racktable->print;
}
sub edit_rack($) {
my ($id) = @_;
my ($name, $notes, $roomid, $description) = (param('name'), param('notes'), param('roomid'), param('description'));
if ($name && $roomid) {
my $query = "UPDATE rack SET room_id=$roomid, name='$name'";
if ($notes) {
$query = $query.", notes='$notes'";
} else {
$query = $query.", notes=''";
}
if ($description) {
$query = $query.", description='$description'";
} else {
$query = $query.", description=''";
}
$query = $query." WHERE id = $id";
if ($readonlymode == 0) {
my $updateracksth = $dbh->prepare($query);
$updateracksth->execute();
$updateracksth->finish();
}
}
my $racksth = $dbh->prepare("SELECT id, name, room_id, description, notes FROM rack WHERE id = ?");
$racksth->execute($id);
my $row;
print start_form(-method=>'get', -action=>"$scriptname");
if ($row = $racksth->fetchrow_arrayref()) {
print "<h2>Rack: $row->[1]</h2>\n";
print "Name: ".textfield('name',$row->[1], 40, 80)."<br/>\n";
print "Room: ".select_id_name($query_room_id_name,'roomid',$row->[2])."<br/>\n";
print "Description: ".textfield('description',$row->[3], 40, 80)."<br/>\n";
print "Notes: ".textfield('notes',$row->[4], 40, 80)."<br/>\n";
}
$racksth->finish();
print "<input type=\"hidden\" name=\"id\" value=\"$id\"/>\n";
print '<input type="hidden" name="command" value="edit"/><input type="hidden" name="subcommand" value="rack"/>'.submit('submit', 'commit changes');
print end_form();
print "<h3>Devices</h3>\n";
my $devtable = new HTML::Table();
$devtable->addRow("ID", "Name", "Type", "Description", "Notes");
my $devsth = $dbh->prepare("SELECT device.id, device.name, device_type.name, device.description, device.notes FROM device INNER JOIN device_type ON device.device_type_id=device_type.id WHERE device.rack_id = ? ORDER BY device.name");
$devsth->execute($id);
while ($row = $devsth->fetchrow_arrayref()) {
my $devid = $row->[0];
$row->[1] = "<a href=\"$scriptname?command=edit&subcommand=device&id=$devid\">$row->[1]</a>";
$devtable->addRow(@$row, "<a href=\"$scriptname?command=remove&subcommand=device&id=$devid\">delete device</a>");
}
$devsth->finish();
$devtable->print;
}
sub edit_device($) {
my ($id) = @_;
my ($name, $notes, $rackid, $devtypeid, $description) = (param('name'), param('notes'), param('rackid'), param('devtypeid'), param('description'));
if ($name && $rackid && $devtypeid) {
my $query = "UPDATE device SET rack_id=$rackid, device_type_id=$devtypeid, name='$name'";
if ($notes) {
$query = $query.", notes='$notes'";
} else {
$query = $query.", notes=''";
}
if ($description) {
$query = $query.", description='$description'";
} else {
$query = $query.", description=''";
}
$query = $query." WHERE id = $id";
if ($readonlymode == 0) {
my $updatedevsth = $dbh->prepare($query);
$updatedevsth->execute();
$updatedevsth->finish();
}
}
my $devsth = $dbh->prepare("SELECT device.id, device_type.name, device.name, rack.name, device.description, device.notes, device.rack_id, device.device_type_id FROM device INNER JOIN device_type ON device.device_type_id=device_type.id INNER JOIN rack ON device.rack_id=rack.id WHERE device.id = ?");
$devsth->execute($id);
my $row;
print start_form(-method=>'get', -action=>"$scriptname");
if ($row = $devsth->fetchrow_arrayref()) {
print "<h2>Device: $row->[2]</h2>\n";
print "Name: ".textfield('name',$row->[2], 40, 80)."<br/>\n";
print "Type: ".select_id_name($query_device_type_id_name,'devtypeid',$row->[7])."<br/>\n";
print "Rack: ".select_id_name($query_rack_id_name,'rackid',$row->[6])."<br/>\n";
print "Description: ".textfield('description',$row->[4], 40, 80)."<br/>\n";
print "Notes: ".textfield('notes',$row->[5], 40, 80)."<br/>\n";
}
$devsth->finish();
print "<input type=\"hidden\" name=\"id\" value=\"$id\"/>\n";
print '<input type="hidden" name="command" value="edit"/><input type="hidden" name="subcommand" value="device"/>'.submit('submit', 'commit changes');
print end_form();
print "<h3>Interfaces</h3>\n";
print start_form(-method=>'get', -action=>"$scriptname");
my $inttable = new HTML::Table();
$inttable->addRow("ID", "Name", "Type", "Notes", "Connections");
my $devintsth = $dbh->prepare("SELECT interface.id, interface.name, interface_type.name, interface.notes FROM interface INNER JOIN interface_type ON interface.interface_type_id=interface_type.id WHERE interface.device_id = ? ORDER BY interface.name");
$devintsth->execute($id);
while ($row = $devintsth->fetchrow_arrayref()) {
my @connections;
@connections = get_connection_list_for_interface($row->[0]);
if (@connections > 1) {
$inttable->addRow(@$row, @connections, "<a href=\"$scriptname?command=remove&subcommand=connection&id=$row->[0]\">delete connection</a>");
} else {
$inttable->addRow(@$row, "<a href=\"$scriptname?command=remove&subcommand=interface&id=$row->[0]\">delete interface</a>");
}
}
$devintsth->finish();
$inttable->addRow((start_form(-method=>'get', -action=>"$scriptname")));
my @addconnectionform;
@addconnectionform = ('', '', '', '', select_id_name($query_interface_id_name,'fromintid',0), select_id_name($query_interface_id_name,'link1',0), select_id_name($query_interface_id_name,'link2',0), select_id_name($query_interface_id_name,'link3',0).hidden('fromintid',$row->[0]).'<input type="hidden" name="command" value="add"/><input type="hidden" name="subcommand" value="connection"/>', submit('submit','add conn'));
$inttable->addRow(@addconnectionform);
$inttable->addRow((end_form()));
$inttable->print;
print "<h4>Add a new interface</h4>\n";
print start_form(-method=>'get', -action=>"$scriptname");
$inttable = new HTML::Table();
$inttable->addRow(textfield('name','name',10,20), select_id_name($query_interface_type_id_name,'typeid',0), textfield('notes','', 20,40), '<input type="hidden" name="command" value="add"/><input type="hidden" name="subcommand" value="interface"/>'.hidden('deviceid',"$id").submit('submit', 'add'));
$inttable->print;
print end_form();
}
sub device_id_for_interface($) {
my ($id) = @_;
my $devid;
my $row;
my $devsth = $dbh->prepare("SELECT device_id FROM interface WHERE id = ?");
$devsth->execute($id);
while ($row = $devsth->fetchrow_arrayref()) {
$devid = $row->[0];
}
$devsth->finish();
return $devid;
}
##################################################################3
# sub-routines end, main begins
##################################################################3
my $dsn = "dbi:SQLite:dbname=$dbfile";
my $doinit = 0;
if (! -f $dbfile) {
$doinit = 1;
}
$dbh = DBI->connect($dsn, "", "", { RaiseError => 1 })
or die $DBI::errstr;
$dbh->do("PRAGMA foreign_keys = ON");
if ($doinit == 1) {
init_db();
}
print_header();
print_navigation();
my $sth;
my $command = param('command');
my $subcommand = param('subcommand');
if ($command && $subcommand) {
switch ($command) {
case "list" {
my $table = new HTML::Table();
my @addNewItemRow;
switch ($subcommand) {
case "room" {
$sth = $dbh->prepare($query_list_room);
$table->addRow("ID", "Name", "Description", "Notes");
@addNewItemRow = ("", textfield('name','name',20,80), textfield('description','description', 40, 80), textfield('notes', 'notes', 40, 80), '<input type="hidden" name="command" value="add"/><input type="hidden" name="subcommand" value="room"/>'.submit('submit', 'add'));
}
case "rack" {
$sth = $dbh->prepare($query_list_rack);
$table->addRow("ID", "Name", "Description", "Room", "Notes");
@addNewItemRow = ("", textfield('name','name',20,80), textfield('description','description', 40, 80), select_id_name($query_room_id_name,'roomid',0), textfield('notes', 'notes', 40, 80), '<input type="hidden" name="command" value="add"/><input type="hidden" name="subcommand" value="rack"/>'.submit('submit', 'add'));
}
case "device" {
$sth = $dbh->prepare($query_list_device);
$table->addRow("ID", "Name", "Type", "Rack", "Description", "Notes");
@addNewItemRow = ("", textfield('name','name',20,80), select_id_name($query_device_type_id_name,'devtypeid',0), select_id_name($query_rack_id_name,'rackid',0), textfield('description','description', 40, 80), textfield('notes', 'notes', 40, 80), '<input type="hidden" name="command" value="add"/><input type="hidden" name="subcommand" value="device"/>'.submit('submit', 'add'));
}
case "interface" {
$sth = $dbh->prepare($query_list_interface);
$table->addRow("ID", "Name", "Type", "Notes");
}
case "connection" {
$sth = $dbh->prepare($query_list_connection);
$table->addRow("Start if ID", "From", "Hops", "Seq");
}
}
if ($sth) {
$sth->execute();
my $row;
while ($row = $sth->fetchrow_arrayref()) {
my $id=$row->[0];
$row->[1] = "<a href=\"$scriptname?command=edit&subcommand=$subcommand&id=$id\">$row->[1]</a>";
$table->addRow((@$row, "<a href=\"$scriptname?command=remove&subcommand=$subcommand&id=$id\">delete</a>"));
}
print start_form(-method=>'get', -action=>"$scriptname");
$table->setRowHead(1);
if (@addNewItemRow) {
$table->addRow(@addNewItemRow);
}
$table->print;
print end_form();
$sth->finish();
}
}
case "add" {
switch ($subcommand) {
# add room <name> [<description> [notes]]
case "room" {
my $name = param('name');
if ($name) {
my $description = param('description');
my $notes = param('notes');
my $query = "INSERT INTO room (name";
my $values = "('".$name."'";
if ($description) {
$query = $query.",description";
$values = $values.",'".$description."'";
}
if ($notes) {
$query = $query.",notes";
$values = $values.",'".$notes."'";
}
$query = $query.") VALUES ".$values.")";
if ($readonlymode == 0) {
$sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
}
}
print_refresh_javascript("command=list&subcommand=room");
}
# add rack <name> <roomid> [<description> [notes]]
case "rack" {
my $name = param('name');
my $roomid = param('roomid');
if ($name && $roomid) {
my $description = param('description');
my $notes = param('notes');
my $query = "INSERT INTO rack (name, room_id";
my $values = "('".$name."',".$roomid;
if ($description) {
$query = $query.",description";
$values = $values.",'".$description."'";
}
if ($notes) {
$query = $query.",notes";
$values = $values.",'".$notes."'";
}
$query = $query.") VALUES ".$values.")";
if ($readonlymode == 0) {
$sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
}
}
print_refresh_javascript("command=list&subcommand=rack");
}
# add device <name> <devtypeid> <rackid> [<description> [notes]]
case "device" {
my $name = param('name');
my $typeid = param('devtypeid');
my $rackid = param('rackid');
if ($name && $typeid && $rackid) {
my $description = param('description');
my $notes = param('notes');
my $query = "INSERT INTO device (name, device_type_id, rack_id";
my $values = "('".$name."',".$typeid.",".$rackid;
if ($description) {
$query = $query.",description";
$values = $values.",'".$description."'";
}
if ($notes) {
$query = $query.",notes";
$values = $values.",'".$notes."'";
}
$query = $query.") VALUES ".$values.")";
if ($readonlymode == 0) {
$sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
}
}
print_refresh_javascript("command=list&subcommand=device");
}
# add interface <name> <typeid> <deviceid> [notes]
case "interface" {
my $name = param('name');
my $typeid = param('typeid');
my $deviceid = param('deviceid');
if ($name && $typeid && $deviceid) {
my $notes = param('notes');
my $query = "INSERT INTO interface (name, interface_type_id, device_id";
my $values = "('".$name."',".$typeid.",".$deviceid;
if ($notes) {
$query = $query.",notes";
$values = $values.",'".$notes."'";
}
$query = $query.") VALUES ".$values.")";
if ($readonlymode == 0) {
$sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
}
}
print_refresh_javascript("command=edit&subcommand=device&id=$deviceid");
}
# add connection <fromintid> <tointid> <conntypeid> [notes]
case "connection" {
my $fromintid = param('fromintid');
if ($fromintid) {
for (my $i = 1;$i < 4;$i++) {
my $link = param("link$i");
if ($link) {
my $query = "INSERT INTO linklist (from_interface_id, interface_id, seq";
my $values = "('".$fromintid."',".$link.",".$i;
$query = $query.") VALUES ".$values.")";
if ($readonlymode == 0) {
$sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
}
}
}
}
print_refresh_javascript("command=edit&subcommand=device&id=".device_id_for_interface($fromintid));
}
}
}
case "remove" {
my $id = param('id');
if ($subcommand && $id && ($readonlymode == 0)) {
switch ($subcommand) {
case "room" {
$sth = $dbh->prepare("DELETE FROM room where id = ?");
$sth->execute($id);
$sth->finish();
print_refresh_javascript("command=list&subcommand=room");
}
case "rack" {
$sth = $dbh->prepare("DELETE FROM rack where id = ?");
$sth->execute($id);
$sth->finish();
print_refresh_javascript("command=list&subcommand=rack");
}
case "device" {
$sth = $dbh->prepare("DELETE FROM device where id = ?");
$sth->execute($id);
$sth->finish();
print_refresh_javascript("command=list&subcommand=device");
}
case "interface" {
my $devid = device_id_for_interface($id);
$sth = $dbh->prepare("DELETE FROM interface where id = ?");
$sth->execute($id);
$sth->finish();
print_refresh_javascript("command=edit&subcommand=device&id=$devid");
}
case "connection" {
delete_connection($id);
print "Click 'Back' and 'Refresh'<br/>\n";
}
}
}
}
case "edit" {
my $id = param('id');
if ($subcommand && $id) {
switch ($subcommand) {
case "room" {
edit_room($id);
}
case "rack" {
edit_rack($id);
}
case "device" {
edit_device($id);
}
case "interface" {
edit_device(device_id_for_interface($id));
}
case "connection" {
edit_device(device_id_for_interface($id));
}
}
}
}
}
}
$dbh->disconnect();
print_footer();