-
Notifications
You must be signed in to change notification settings - Fork 3
/
rack_maint.inc.php
2481 lines (1897 loc) · 87.3 KB
/
rack_maint.inc.php
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Lets do some initial install related stuff
if (file_exists(dirname(__FILE__)."/install.php")) {
printmsg("DEBUG => Found install file for rack_maint plugin.", 1);
include(dirname(__FILE__)."/install.php");
} else {
// TODO: MP: does having a rack list like this truely scale?? I guess it will do for now..
// Check permissions
if (! (auth('rack_add') or auth('advanced'))){
$window['js'] = "alert('Permission denied!'); removeElement('{$window_name}');";
return;
}
// Set the window title:
$window['title'] = "Rack List";
// Load some html into $window['html']
$form_id = "{$window_name}_filter_form";
$tab = 'racks';
$submit_window = $window_name;
$content_id = "{$window_name}_list";
$window['html'] .= <<<EOL
<!-- Tabs & Quick Filter -->
<table width="100%" cellspacing="0" border="0" cellpadding="0" >
<tr>
<td id="{$form_id}_{$tab}_tab" nowrap="true" class="table-tab-active">
Racks <span id="{$form_id}_{$tab}_count"></span>
</td>
<td id="{$form_id}_quick_filter" class="padding" align="right" width="100%">
<form id="{$form_id}" onSubmit="return false;">
<input id="{$form_id}_page" name="page" value="1" type="hidden">
<input name="content_id" value="{$content_id}" type="hidden">
<input name="form_id" value="{$form_id}" type="hidden">
<div id="{$form_id}_filter_overlay"
style="position: relative;
display: inline;
color: #CACACA;
cursor: text;"
onClick="this.style.display = 'none'; el('{$form_id}_filter').focus();"
>Name</div>
<input
id="{$form_id}_filter"
name="filter"
class="filter"
type="text"
value=""
size="10"
maxlength="20"
alt="Quick Filter"
onFocus="el('{$form_id}_filter_overlay').style.display = 'none';"
onBlur="if (this.value == '') el('{$form_id}_filter_overlay').style.display = 'inline';"
onKeyUp="
if (typeof(timer) != 'undefined') clearTimeout(timer);
code = 'if ({$form_id}_last_search != el(\'{$form_id}_filter\').value) {' +
' {$form_id}_last_search = el(\'{$form_id}_filter\').value;' +
' document.getElementById(\'{$form_id}_page\').value = 1;' +
' xajax_window_submit(\'{$submit_window}\', xajax.getFormValues(\'{$form_id}\'), \'display_list\');' +
'}';
timer = setTimeout(code, 700);"
>
</form>
</td>
</tr>
</table>
<!-- Item List -->
<div id='{$content_id}'>
{$conf['loading_icon']}
</div>
EOL;
// Define javascript to run after the window is created
$window['js'] = <<<EOL
/* Put a minimize icon in the title bar */
el('{$window_name}_title_r').innerHTML =
' <a onClick="toggle_window(\'{$window_name}\');" title="Minimize window" style="cursor: pointer;"><img src="{$images}/icon_minimize.gif" border="0" /></a>' +
el('{$window_name}_title_r').innerHTML;
/* Put a help icon in the title bar */
el('{$window_name}_title_r').innerHTML =
' <a href="{$_ENV['help_url']}{$window_name}" target="null" title="Help" style="cursor: pointer;"><img src="{$images}/silk/help.png" border="0" /></a>' +
el('{$window_name}_title_r').innerHTML;
/* Setup the quick filter */
el('{$form_id}_filter_overlay').style.left = (el('{$form_id}_filter_overlay').offsetWidth + 10) + 'px';
{$form_id}_last_search = '';
/* Tell the browser to load/display the list */
xajax_window_submit('{$submit_window}', xajax.getFormValues('{$form_id}'), 'display_list');
EOL;
}
// This function displays a list (all?) racks in the DB
function ws_display_list($window_name, $form) {
global $conf, $self, $onadb;
global $font_family, $color, $style, $images;
// Check permissions
if (! (auth('rack_add') or auth('advanced'))){
$response = new xajaxResponse();
$response->addScript("alert('Permission denied!');");
return($response->getXML());
}
// If the group supplied an array in a string, build the array and store it in $form
$form = parse_options_string($form);
// Find out what page we're on
$page = 1;
if ($form['page'] and is_numeric($form['page'])) { $page = $form['page']; }
$html = <<<EOL
<!-- Results Table -->
<table cellspacing="0" border="0" cellpadding="0" width="100%" class="list-box">
<!-- Table Header -->
<tr>
<td class="list-header" align="center" style="{$style['borderR']};">Name</td>
<td class="list-header" align="center" style="{$style['borderR']};">Size</td>
<td class="list-header" align="center" style="{$style['borderR']};">Location</td>
<td class="list-header" align="center" style="{$style['borderR']};">Description</td>
<td class="list-header" align="center"> </td>
</tr>
EOL;
$where = '`id` > 0';
if (is_array($form) and $form['filter']) {
$where = '`name` LIKE ' . $onadb->qstr('%'.$form['filter'].'%');
}
// Offset for SQL query
$offset = ($conf['search_results_per_page'] * ($page - 1));
if ($offset == 0) { $offset = -1; }
// Get our groups
list($status, $rows, $records) = db_get_records($onadb, 'racks', $where, 'name', $conf['search_results_per_page'], $offset);
// If we got less than serach_results_per_page, add the current offset to it
// so that if we're on the last page $rows still has the right number in it.
if ($rows > 0 and $rows < $conf['search_results_per_page']) {
$rows += ($conf['search_results_per_page'] * ($page - 1));
}
// If there were more than $conf['search_results_per_page'] find out how many records there really are
else if ($rows >= $conf['search_results_per_page']) {
list ($status, $rows, $tmp) = db_get_records($onadb, 'racks', $where, '', 0);
}
$count = $rows;
// Loop through and display the groups
foreach ($records as $record) {
list ($status, $rows, $loc) = db_get_record($onadb, 'locations', "id={$record['location_id']}");
// Escape data for display in html
foreach(array_keys($record) as $key) {
$record[$key] = htmlentities($record[$key], ENT_QUOTES);
}
$html .= <<<EOL
<tr onMouseOver="this.className='row-highlight'" onMouseOut="this.className='row-normal'">
<td class="list-row">
<a title="View rack. ID: {$record['id']}"
class="act"
onClick="xajax_window_submit('work_space','xajax_window_submit(\'rack_maint\', \'id=>{$record['id']}\', \'display\')');toggle_window('{$window_name}');"
>{$record['name']}</a>
</td>
<td class="list-row">
{$record['size']} Units
</td>
<td class="list-row">
{$loc['reference']}
</td>
<td class="list-row">
{$record['description']}
</td>
<td align="right" class="list-row" nowrap="true">
<a title="Edit rack. ID: {$record['id']}"
class="act"
onClick="xajax_window_submit('rack_maint', 'id=>{$record['id']}', 'rack_editor');"
><img src="{$images}/silk/page_edit.png" border="0"></a>
<a title="Delete rack: ID: {$record['id']}"
class="act"
onClick="var doit=confirm('Are you sure you want to delete this rack?');
if (doit == true)
xajax_window_submit('{$window_name}', 'id=>{$record['id']}', 'rack_delete');"
><img src="{$images}/silk/delete.png" border="0"></a>
</td>
</tr>
EOL;
}
$html .= <<<EOL
</table>
<!-- Add a new record -->
<div class="act-box" style="padding: 2px 4px; border-top: 1px solid {$color['border']}; border-bottom: 1px solid {$color['border']};">
<!-- ADD LINK -->
<a title="New rack"
class="act"
onClick="xajax_window_submit('rack_maint', ' ', 'rack_editor');"
><img src="{$images}/silk/page_add.png" border="0"></a>
<a title="New rack"
class="act"
onClick="xajax_window_submit('rack_maint', ' ', 'rack_editor');"
>Add new rack</a>
</div>
EOL;
// Build page links if there are any
$html .= get_page_links($page, $conf['search_results_per_page'], $count, $window_name, $form['form_id']);
// Insert the new table into the window
// Instantiate the xajaxResponse object
$response = new xajaxResponse();
$response->addAssign("{$form['form_id']}_racks_count", "innerHTML", "({$count})");
$response->addAssign("{$form['content_id']}", "innerHTML", $html);
// $response->addScript($js);
return($response->getXML());
}
///////////////////////////////////////////////////////////////////////
// Function: rack_add (string $options='')
//
// Input Options:
// $options = key=value pairs of options for this function.
// multiple sets of key=value pairs should be separated
// by an "&" symbol.
//
// Output:
// Returns a two part list:
// 1. The exit status of the function. 0 on success, non-zero on
// error. All errors messages are stored in $self['error'].
// 2. A textual message for display on the console or web interface.
//
// Example: list($status, $result) = rack_add('host=test&type=something');
///////////////////////////////////////////////////////////////////////
function rack_add($options="") {
global $conf, $self, $onadb;
// Version - UPDATE on every edit!
$version = '1.10';
printmsg("DEBUG => rack_add({$options}) called", 3);
// Parse incoming options string to an array
$options = parse_options($options);
// Return the usage summary if we need to
if ($options['help'] or !($options['name'] and $options['size']) ) {
// NOTE: Help message lines should not exceed 80 characters for proper display on a console
$self['error'] = 'ERROR => Insufficient parameters';
return(array(1,
<<<EOM
rack_add-v{$version}
Add a new rack
Synopsis: rack_add [KEY=VALUE] ...
Required:
name=NAME Name of the rack
size=INT Size of rack in units
description=STRING Description
location=STRING|id Location of rack
numbering=STRING Numbering direction of rack
EOM
));
}
// Check permissions
if (! (auth('rack_add') or auth('advanced'))){
$self['error'] = "Permission denied!";
printmsg($self['error'], 0);
return(array(10, $self['error'] . "\n"));
}
// sanitize the numeric values
if (!is_numeric($options['size'])) {
$self['error'] = "ERROR => The specified size was not numeric!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
// Standardize names to be in upper case and spaces are converted to -'s.
$options['name'] = trim($options['name']);
$options['name'] = preg_replace('/\s+/', '-', $options['name']);
$options['name'] = strtoupper($options['name']);
// check the rack name
list($status, $rows, $rackcheck) = db_get_records($onadb, 'racks', "name='{$options['name']}'");
if ($status or $rows) {
printmsg("ERROR => There is already a rack with that name!",3);
$self['error'] = "ERROR => There is already a rack with that name!";
return(array(30, $self['error'] . "\n"));
}
// check the rack name
if ($options['location']) {
list($status, $rows, $loc) = ona_find_location($options['location']);
if ($status or !$rows) {
printmsg("ERROR => Unable to find specified location: {$options['location']}!",3);
$self['error'] = "ERROR => Unable to find specified location: {$options['location']}!";
return(array(31, $self['error'] . "\n"));
}
$options['location'] = $loc['id'];
}
// Check the numbering plan. Assume descending if invalid.
if ( ($options['numbering'] != 'ASC') && ($options['numbering'] != 'DESC') ) {
$options['numbering'] = 'DESC';
}
// Get the next ID for the new rack record
$id = ona_get_next_id('racks');
if (!$id) {
$self['error'] = "ERROR => The ona_get_next_id('racks') call failed!";
printmsg($self['error'], 0);
return(array(7, $self['error'] . "\n"));
}
printmsg("DEBUG => ID for new rack record: $id", 3);
// There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
$options['description'] = str_replace('\\=','=',$options['description']);
$options['description'] = str_replace('\\&','&',$options['description']);
// Add the rack record
list($status, $rows) = db_insert_record(
$onadb,
'racks',
array(
'id' => $id,
'size' => $options['size'],
'description' => $options['description'],
'location_id' => $options['location'],
'numbering' => $options['numbering'],
'name' => $options['name']
)
);
if ($status or !$rows) {
$self['error'] = "ERROR => rack_add() SQL Query failed adding record: " . $self['error'];
printmsg($self['error'], 0);
return(array(6, $self['error'] . "\n"));
}
// Else start an output message
$text = "INFO => Added ".strtolower($options['numbering'])." rack '{$options['name']}' with a size of: {$options['size']}.";
printmsg($text,0);
$text .= "\n";
// Return the success notice
return(array(0, $text));
}
///////////////////////////////////////////////////////////////////////
// Function: rack_assignment_add (string $options='')
//
// Input Options:
// $options = key=value pairs of options for this function.
// multiple sets of key=value pairs should be separated
// by an "&" symbol.
//
// Output:
// Returns a two part list:
// 1. The exit status of the function. 0 on success, non-zero on
// error. All errors messages are stored in $self['error'].
// 2. A textual message for display on the console or web interface.
//
// Example: list($status, $result) = rack_assignment_add('host=test&type=something');
///////////////////////////////////////////////////////////////////////
function rack_assignment_add($options="") {
global $conf, $self, $onadb;
// Version - UPDATE on every edit!
$version = '1.00';
printmsg("DEBUG => rack_assignment_add({$options}) called", 3);
// Parse incoming options string to an array
$options = parse_options($options);
// Return the usage summary if we need to
if ($options['help'] or !($options['rack'] and $options['position']) ) {
// NOTE: Help message lines should not exceed 80 characters for proper display on a console
$self['error'] = 'ERROR => Insufficient parameters';
return(array(1,
<<<EOM
rack_assignment_add-v{$version}
Add a new rack unit record
Synopsis: rack_assignment_add [KEY=VALUE] ...
Required:
device=NAME|ID ONA device name or ID
OR
alt_name=NAME If no ONA device, provide a name (I.E. Patch Panel)
rack=NAME|ID Name or id of the rack this device is in
position=INT Position of the first rack unit this device uses from top
mounted_from=front|back Device is inserted in rack from front or back
size=INT Size of device in rack units
depth=1|2|3|4|half|full Depth of device in quarters
EOM
));
}
// Check permissions
if (! (auth('rack_add') or auth('advanced'))){
$self['error'] = "Permission denied!";
printmsg($self['error'], 0);
return(array(10, $self['error'] . "\n"));
}
// Find the rack
list($status, $rows, $rack) = ona_get_record(array('id' => $options['rack']), 'racks');
if ($status or !$rows) {
$self['error'] = "ERROR => Unable to find rack: {$options['rack']}!";
printmsg($self['error'],3);
return(array(20, $self['error'] . "\n"));
}
// sanitize the numeric values
if (!is_numeric($options['size'])) {
$self['error'] = "ERROR => The specified size was not numeric!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
if (!is_numeric($options['position'])) {
$self['error'] = "ERROR => The specified position was not numeric!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
// size cant be more than rack total size
if ($options['size'] > $rack['size']) {
$self['error'] = "ERROR => Your allocation size is larger than the rack itself!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
// the position we start in the rack plus the size we are adding cant exceed rack total size
if ($options['position']+$options['size'] > $rack['size']+1) {
$self['error'] = "ERROR => Your allocation size and position extends beyond the rack itself!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
// Sanitize the mounted from values and get the opposite value
$mnt = strtolower($options['mounted_from']);
if ($mnt == 'front' or $mnt == 'f' or $mnt == '1') {
$options['mounted_from'] = '1';
$mnt_opposite = '2';
}
else if ($mnt == 'back' or $mnt == 'b' or $mnt == '2') {
$options['mounted_from'] = '2';
$mnt_opposite = '1';
}
else {
$self['error'] = "ERROR => Invalid mounted_from type '{$options['mounted_from']}'!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
// Sanitize the depth values and get the opposite value // depth must be 1-4
$d = strtolower($options['depth']);
if ($d == 'full' or $d == 'f' or $d == '4') {
$options['depth'] = '4';
}
else if ($d == '3') {
$options['depth'] = '3';
}
else if ($d == 'half' or $d == 'h' or $d == '2') {
$options['depth'] = '2';
}
else if ($d == '1') {
$options['depth'] = '1';
}
else {
$self['error'] = "ERROR => Invalid depth type '{$options['depth']}'!";
printmsg($self['error'],3);
return(array(3, $self['error'] . "\n"));
}
// Check that our device does not overlap with any other devices
// Generate SQL for each position
for ($i=$options['position']; $i <= $options['position']+$options['size']-1; $i++) {
if ($possql) $possql = ' '.$possql.' or ';
$possql .= "{$i} between position and position+size-1";
}
// check the positions
list($status, $rows, $rackcheck) = db_get_records($onadb, 'rack_assignments', "rack_id={$rack['id']} and mounted_from={$options['mounted_from']} and ({$possql})");
if ($status or $rows) {
printmsg("ERROR => This device overlaps an existing rack assignment due to its position or size!",3);
$self['error'] = "ERROR => This device overlaps an existing rack assignment due to its position or size!";
return(array(30, $self['error'] . "\n"));
}
// check the depth
list($status, $rows, $tmp) = db_get_records($onadb, 'rack_assignments', "rack_id={$rack['id']} and mounted_from={$mnt_opposite} and {$options['depth']} > (4-depth) and ({$possql})");
if ($status or $rows) {
printmsg("ERROR => This device overlaps an existing rack assignment due to its position and depth!",3);
$self['error'] = "ERROR => This device overlaps an existing rack assignment due to its position and depth!";
return(array(40, $self['error'] . "\n"));
}
// Deal with devices or alt names
if ($options['alt_name']) {
$device['id'] = 0;
}
// find the device and use it.
if ($options['device']) {
list($status, $rows, $device) = ona_find_device($options['device']);
if ($status or !$rows) {
$self['error'] = "ERROR => Unable to find device using: {$options['device']}!";
printmsg($self['error'],3);
return(array(40, $self['error'] . "\n"));
}
// check that this device is not already assigned to another rack location
list($status, $rows, $tmp) = db_get_records($onadb, 'rack_assignments', "device_id = {$device['id']}");
if ($status or $rows) {
$self['error'] = "ERROR => This device is already assigned to a rack: {$options['device']}!";
printmsg($self['error'],3);
return(array(41, $self['error'] . "\n"));
}
$options['alt_name'] = '';
}
// Get the next ID for the new rack record
$id = ona_get_next_id('rack_assignments');
if (!$id) {
$self['error'] = "ERROR => The ona_get_next_id('rack_assignments') call failed!";
printmsg($self['error'], 0);
return(array(7, $self['error'] . "\n"));
}
printmsg("DEBUG => ID for new rack assignment record: $id", 3);
// There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
//$options['notes'] = str_replace('\\=','=',$options['notes']);
//$options['notes'] = str_replace('\\&','&',$options['notes']);
// Add the rack record
list($status, $rows) = db_insert_record(
$onadb,
'rack_assignments',
array(
'id' => $id,
'rack_id' => $rack['id'],
'device_id' => $device['id'],
'position' => $options['position'],
'depth' => $options['depth'],
'size' => $options['size'],
'mounted_from' => $options['mounted_from'],
'alt_name' => $options['alt_name']
)
);
if ($status or !$rows) {
$self['error'] = "ERROR => rack_assignment_add() SQL Query failed adding record: " . $self['error'];
printmsg($self['error'], 0);
return(array(6, $self['error'] . "\n"));
}
// Else start an output message
$text = "INFO => Rack assignment of {$options['size']} unit(s) in rack {$rack['name']} at position {$options['position']} successfull.";
printmsg($text,0);
$text .= "\n";
// Return the success notice
return(array(0, $text));
}
///////////////////////////////////////////////////////////////////////
// Function: rack_modify (string $options='')
//
// $options = key=value pairs of options for this function.
// multiple sets of key=value pairs should be separated
// by an "&" symbol.
//
// Output:
// Updates an rack record in the IP database.
// Returns a two part list:
// 1. The exit status of the function. 0 on success, non-zero on
// error. All errors messages are stored in $self['error'].
// 2. A textual message for display on the console or web interface.
//
// Example: list($status, $result) = rack_modify('alias=test&host=q1234.something.com');
///////////////////////////////////////////////////////////////////////
function rack_modify($options="") {
global $conf, $self, $onadb;
printmsg("DEBUG => rack_modify({$options}) called", 3);
// Version - UPDATE on every edit!
$version = '1.10';
// Parse incoming options string to an array
$options = parse_options($options);
// Return the usage summary if we need to
if ($options['help'] or !(
($options['rack'])
and
($options['set_name'] or
$options['set_description'] or
$options['set_location'] or
$options['set_size'] or
$options['set_numbering'])
)
)
{
// NOTE: Help message lines should not exceed 80 characters for proper display on a console
$self['error'] = 'ERROR => Insufficient parameters';
return(array(1,
<<<EOM
rack_modify-v{$version}
Modifies a rack in the database
Synopsis: rack_modify [KEY=VALUE] ...
Where:
rack=NAME|ID Name or ID of rack
Optional:
set_name=NAME Name of rack
set_size=INT Size of device in rack units
set_description=STRING Description of rack
set_location=STRING|ID Location of rack
set_numbering=STRING Numbering direction of rack
EOM
));
}
// find the rack to modify
if (is_numeric($options['rack']))
list($status, $rows, $orig_rack) = db_get_record($onadb, 'racks', array('id' => $options['rack']));
else
list($status, $rows, $orig_rack) = db_get_record($onadb, 'racks', array('name' => $options['rack']));
// Test to see that we were able to find the specified record
if (!$orig_rack['id']) {
printmsg("DEBUG => Unable to find a rack record: {$options['rack']}!",3);
$self['error'] = "ERROR => Unable to find the rack record: {$options['rack']}!";
return(array(4, $self['error']. "\n"));
}
printmsg("DEBUG => rack_modify(): Found entry, {$orig_rack['name']}", 3);
// This variable will contain the updated info we'll insert into the DB
$SET = array();
// Deal with devices or alt names
if ($options['set_name'] and $options['set_name'] != $orig_rack['name']) {
// Standardize names to be in upper case and spaces are converted to -'s.
$options['set_name'] = trim($options['set_name']);
$options['set_name'] = preg_replace('/\s+/', '-', $options['set_name']);
$options['set_name'] = strtoupper($options['set_name']);
// check the rack name
list($status, $rows, $rackcheck) = db_get_records($onadb, 'racks', "name='{$options['set_name']}'");
if ($status or $rows) {
printmsg("ERROR => There is already a rack with that name!",3);
$self['error'] = "ERROR => There is already a rack with that name!";
return(array(30, $self['error'] . "\n"));
}
$SET['name'] = $options['set_name'];
}
if ($options['set_description'] and $options['set_description'] != $orig_rack['description']) {
// There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
$options['set_description'] = str_replace('\\=','=',$options['set_description']);
$options['set_description'] = str_replace('\\&','&',$options['set_description']);
$SET['description'] = $options['set_description'];
}
// define the remaining entries
if ($options['set_size'] and $options['set_size'] != $orig_rack['size']) {
// sanitize the numeric values
if (!is_numeric($options['set_size'])) {
$self['error'] = "ERROR => The specified size was not numeric!";
printmsg($self['error'],3);
return(array(2, $self['error'] . "\n"));
}
$SET['size'] = $options['set_size'];
}
// check the rack name
if ($options['set_location']) {
list($status, $rows, $loc) = ona_find_location($options['set_location']);
if ($status or !$rows) {
printmsg("ERROR => Unable to find specified location: {$options['set_location']}!",3);
$self['error'] = "ERROR => Unable to find specified location: {$options['set_location']}!";
return(array(31, $self['error'] . "\n"));
}
if ($loc['id'] != $orig_rack['location_id']) $SET['location_id'] = $loc['id'];
}
// Check the numbering plan. Assume descending if invalid.
if ( ($options['set_numbering'] != 'ASC') && ($options['set_numbering'] != 'DESC') ) {
$options['set_numbering'] = 'DESC';
}
if ($options['set_numbering'] != $orig_rack['numbering']) $SET['numbering'] = $options['set_numbering'];
// Check permissions
if (! (auth('rack_mod') or auth('advanced'))){
$self['error'] = "Permission denied!";
printmsg($self['error'], 0);
return(array(10, $self['error'] . "\n"));
}
// Update the record
list($status, $rows) = db_update_record($onadb, 'racks', array('id' => $orig_rack['id']), $SET);
if ($status or !$rows) {
$self['error'] = "ERROR => rack_modify() SQL Query failed: {$self['error']}";
printmsg($self['error'],0);
return(array(6, $self['error'] . "\n"));
}
// Get the entry again to display details
list($status, $rows, $new_rack) = db_get_record($onadb, 'racks', "id={$orig_rack['id']}");
// Return the success notice
$self['error'] = "INFO => Rack UPDATED: {$new_rack['name']}";
$log_msg = "INFO => Rack UPDATED:{$new_rack['id']}: ";
$more="";
$original_rack = $orig_rack;
foreach(array_keys($original_rack) as $key) {
if($original_rack[$key] != $new_rack[$key]) {
$log_msg .= $more . $key . "[" .$original_rack[$key] . "=>" . $new_rack[$key] . "]";
$more= ";";
}
}
// only print to logfile if a change has been made to the record
if($more != '') {
//printmsg($self['error'], 0);
printmsg($log_msg, 0);
}
return(array(0, $self['error'] . "\n"));
}
///////////////////////////////////////////////////////////////////////
// Function: rack_assignment_modify (string $options='')
//
// $options = key=value pairs of options for this function.
// multiple sets of key=value pairs should be separated
// by an "&" symbol.
//
// Output:
// Updates an rack_assignment record in the IP database.
// Returns a two part list:
// 1. The exit status of the function. 0 on success, non-zero on
// error. All errors messages are stored in $self['error'].
// 2. A textual message for display on the console or web interface.
//
// Example: list($status, $result) = rack_assignment_modify('alias=test&host=q1234.something.com');
///////////////////////////////////////////////////////////////////////
function rack_assignment_modify($options="") {
global $conf, $self, $onadb;
printmsg("DEBUG => rack_assignment_modify({$options}) called", 3);
// Version - UPDATE on every edit!
$version = '1.00';
// Parse incoming options string to an array
$options = parse_options($options);
// Return the usage summary if we need to
if ($options['help'] or !(
($options['rack_assignment'])
and
($options['set_device'] or
$options['set_alt_name'] or
$options['set_position'] or
$options['set_mounted_from'] or
$options['set_size'] or
$options['set_depth'])
)
)
{
// NOTE: Help message lines should not exceed 80 characters for proper display on a console
$self['error'] = 'ERROR => Insufficient parameters';
return(array(1,
<<<EOM
rack_assignment_modify-v{$version}
Modifies a rack_assignment in the database
Synopsis: rack_assignment_modify [KEY=VALUE] ...
Where:
rack_assignment=ID rack_assignment id
Optional:
set_device=NAME|ID ONA device name or ID
OR
set_alt_name=NAME If no ONA device, provide a name (I.E. Patch Panel)
set_position=INT Position of the first rack unit this device uses from top
set_mounted_from=front|back Device is inserted in rack from front or back
set_size=INT Size of device in rack units
set_depth=1|2|3|4|half|full Depth of device in quarters
EOM
));
}
// Determine the entry itself exists
list($status, $rows, $original_rack_assignment) = db_get_record($onadb, 'rack_assignments', "id={$options['rack_assignment']}");
// Test to see that we were able to find the specified record
if (!$original_rack_assignment['id']) {
printmsg("DEBUG => Unable to find a rack_assignment record using ID {$options['rack_assignment']}!",3);
$self['error'] = "ERROR => Unable to find the rack_assignment record using ID {$options['rack_assignment']}!";
return(array(4, $self['error']. "\n"));
}
printmsg("DEBUG => rack_assignment_modify(): Found entry, {$entry['id']}", 3);
// This variable will contain the updated info we'll insert into the DB
$SET = array();
// Deal with devices or alt names
if ($options['set_alt_name']) {
$SET['alt_name'] = $options['set_alt_name'];
$SET['device_id'] = 0;
}
// find the device and use it.
if ($options['set_device']) {
list($status, $rows, $device) = ona_find_device($options['set_device']);
if ($status or !$rows) {
$self['error'] = "ERROR => Unable to find device using: {$options['set_device']}!";
printmsg($self['error'],3);
return(array(40, $self['error'] . "\n"));
}
if ($device['id'] != $original_rack_assignment['device_id']) {
// check that this device is not already assigned to another rack location
list($status, $rows, $tmp) = db_get_records($onadb, 'rack_assignments', "device_id = {$device['id']}");
if ($status or $rows) {
$self['error'] = "ERROR => This device is already assigned to a rack: {$options['device']}!";
printmsg($self['error'],3);
return(array(41, $self['error'] . "\n"));
}
$SET['device_id'] = $device['id'];
$SET['alt_name'] = '';
}
}
// define the remaining entries
if ($options['set_size']) $SET['size'] = $options['set_size'];
if ($options['set_position']) $SET['position'] = $options['set_position'];
if ($options['set_depth']) $SET['depth'] = $options['set_depth'];
// Check permissions
if (! (auth('rack_mod') or auth('advanced'))){
$self['error'] = "Permission denied!";
printmsg($self['error'], 0);
return(array(10, $self['error'] . "\n"));
}
// Update the record
list($status, $rows) = db_update_record($onadb, 'rack_assignments', array('id' => $original_rack_assignment['id']), $SET);
if ($status or !$rows) {
$self['error'] = "ERROR => rack_assignment_modify() SQL Query failed: {$self['error']}";
printmsg($self['error'],0);
return(array(6, $self['error'] . "\n"));
}
// Get the entry again to display details
list($status, $rows, $new_rack_assignment) = db_get_record($onadb, 'rack_assignments', "id={$original_rack_assignment['id']}");
// Return the success notice
$self['error'] = "INFO => Rack assignment UPDATED: {$new_rack_assignment['name']}";
$log_msg = "INFO => Rack assignment UPDATED:{$new_rack_assignment['id']}: ";
$more="";
foreach(array_keys($original_rack_assignment) as $key) {
if($original_rack_assignment[$key] != $new_rack_assignment[$key]) {
$log_msg .= $more . $key . "[" .$original_rack_assignment[$key] . "=>" . $new_rack_assignment[$key] . "]";
$more= ";";
}
}
// only print to logfile if a change has been made to the record