forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.inc.php
4781 lines (3907 loc) · 152 KB
/
assets.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
/*
openDCIM
This is the main class library for the openDCIM application, which
is a PHP/Web based data center infrastructure management system.
This application was originally written by Scott A. Milliken while
employed at Vanderbilt University in Nashville, TN, as the
Data Center Manager, and released under the GNU GPL.
Copyright (C) 2011 Scott A. Milliken
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For further details on the license, see http://www.gnu.org/licenses
*/
class Cabinet {
/* Cabinet: The workhorse logical container for DCIM. This can be a 2-post rack, a 4-post open rack,
or an enclosed cabinet. The height is variable. Devices are attached to cabinets, and
cabinets are attached to data centers. PDU's are associated with cabinets, and metrics
are reported on cabinets for power, space, and weight.
*/
var $CabinetID;
var $DataCenterID;
var $Location;
var $AssignedTo;
var $ZoneID;
var $CabRowID; //JMGA: Row of this cabinet
var $CabinetHeight;
var $Model;
var $Keylock;
var $MaxKW;
var $MaxWeight;
var $InstallationDate;
var $SensorIPAddress;
var $SensorCommunity;
var $SensorTemplateID;
var $MapX1;
var $MapY1;
var $MapX2;
var $MapY2;
var $FrontEdge;
var $Notes;
function MakeSafe() {
$this->CabinetID=intval($this->CabinetID);
$this->DataCenterID=intval($this->DataCenterID);
$this->Location=sanitize($this->Location);
$this->AssignedTo=intval($this->AssignedTo);
$this->ZoneID=intval($this->ZoneID);
$this->CabRowID=intval($this->CabRowID);
$this->CabinetHeight=intval($this->CabinetHeight);
$this->Model=sanitize($this->Model);
$this->Keylock=sanitize($this->Keylock);
$this->MaxKW=floatval($this->MaxKW);
$this->MaxWeight=intval($this->MaxWeight);
$this->InstallationDate=date("Y-m-d", strtotime($this->InstallationDate));
$this->SensorIPAddress=sanitize($this->SensorIPAddress);
$this->SensorCommunity=sanitize($this->SensorCommunity);
$this->SensorTemplateID=intval($this->SensorTemplateID);
$this->MapX1=abs($this->MapX1);
$this->MapY1=abs($this->MapY1);
$this->MapX2=abs($this->MapX2);
$this->MapY2=abs($this->MapY2);
$this->FrontEdge=in_array($this->FrontEdge, array("Top","Right","Left","Bottom"))?$this->FrontEdge:"Top";
$this->Notes=sanitize($this->Notes,false);
}
static function RowToObject($dbRow){
/*
* Generic function that will take any row returned from the fac_Cabinet
* table and convert it to an object for use in array or other
*/
$cab=new Cabinet();
$cab->CabinetID=$dbRow["CabinetID"];
$cab->DataCenterID=$dbRow["DataCenterID"];
$cab->Location=$dbRow["Location"];
$cab->AssignedTo=$dbRow["AssignedTo"];
$cab->ZoneID=$dbRow["ZoneID"];
$cab->CabRowID=$dbRow["CabRowID"];
$cab->CabinetHeight=$dbRow["CabinetHeight"];
$cab->Model=$dbRow["Model"];
$cab->Keylock=$dbRow["Keylock"];
$cab->MaxKW=$dbRow["MaxKW"];
$cab->MaxWeight=$dbRow["MaxWeight"];
$cab->InstallationDate=$dbRow["InstallationDate"];
$cab->SensorIPAddress=$dbRow["SensorIPAddress"];
$cab->SensorCommunity=$dbRow["SensorCommunity"];
$cab->SensorTemplateID=$dbRow["SensorTemplateID"];
$cab->MapX1=$dbRow["MapX1"];
$cab->MapY1=$dbRow["MapY1"];
$cab->MapX2=$dbRow["MapX2"];
$cab->MapY2=$dbRow["MapY2"];
$cab->FrontEdge=$dbRow["FrontEdge"];
$cab->Notes=$dbRow["Notes"];
return $cab;
}
function CreateCabinet(){
global $dbh;
$this->MakeSafe();
$sql="INSERT INTO fac_Cabinet SET DataCenterID=$this->DataCenterID,
Location=\"$this->Location\", AssignedTo=$this->AssignedTo,
ZoneID=$this->ZoneID, CabRowID=$this->CabRowID,
CabinetHeight=$this->CabinetHeight, Model=\"$this->Model\",
Keylock=\"$this->Keylock\", MaxKW=\"$this->MaxKW\", MaxWeight=$this->MaxWeight,
InstallationDate=\"$this->InstallationDate\",
SensorIPAddress=\"$this->SensorIPAddress\",
SensorCommunity=\"$this->SensorCommunity\",
SensorTemplateID=$this->SensorTemplateID, MapX1=$this->MapX1,
MapY1=$this->MapY1, MapX2=$this->MapX2, MapY2=$this->MapY2, FrontEdge=\"$this->FrontEdge\",
Notes=\"$this->Notes\";";
if(!$dbh->exec($sql)){
$info=$dbh->errorInfo();
error_log("CreateCabinet::PDO Error: {$info[2]} SQL=$sql");
return false;
}else{
$this->CabinetID=$dbh->lastInsertID();
}
(class_exists('LogActions'))?LogActions::LogThis($this):'';
return $this->CabinetID;
}
function UpdateCabinet(){
global $dbh;
$this->MakeSafe();
$old=new Cabinet();
$old->CabinetID=$this->CabinetID;
$old->GetCabinet();
$sql="UPDATE fac_Cabinet SET DataCenterID=$this->DataCenterID,
Location=\"$this->Location\", AssignedTo=$this->AssignedTo,
ZoneID=$this->ZoneID, CabRowID=$this->CabRowID,
CabinetHeight=$this->CabinetHeight, Model=\"$this->Model\",
Keylock=\"$this->Keylock\", MaxKW=$this->MaxKW, MaxWeight=$this->MaxWeight,
InstallationDate=\"".date("Y-m-d", strtotime($this->InstallationDate))."\",
SensorIPAddress=\"$this->SensorIPAddress\",
SensorCommunity=\"$this->SensorCommunity\",
SensorTemplateID=$this->SensorTemplateID,
MapX1=$this->MapX1, MapY1=$this->MapY1, MapX2=$this->MapX2, MapY2=$this->MapY2, FrontEdge=\"$this->FrontEdge\",
Notes=\"$this->Notes\" WHERE CabinetID=$this->CabinetID;";
if(!$dbh->query($sql)){
$info=$dbh->errorInfo();
error_log("UpdateCabinet::PDO Error: {$info[2]} SQL=$sql" );
return false;
}
(class_exists('LogActions'))?LogActions::LogThis($this,$old):'';
return true;
}
function GetCabinet(){
global $dbh;
$this->MakeSafe();
$sql="SELECT * FROM fac_Cabinet WHERE CabinetID=$this->CabinetID;";
if($cabinetRow=$dbh->query($sql)->fetch()){
foreach(Cabinet::RowToObject($cabinetRow) as $prop => $value){
$this->$prop=$value;
}
return true;
}else{
return false;
}
}
static function ListCabinets($deptid=null) {
global $dbh;
$cabinetList=array();
$dept=(!is_null($deptid))?" WHERE AssignedTo=".intval($deptid):'';
$sql="SELECT * FROM fac_Cabinet$dept ORDER BY DataCenterID, Location;";
foreach($dbh->query($sql) as $cabinetRow){
$cabinetList[]=Cabinet::RowToObject($cabinetRow);
}
return $cabinetList;
}
function ListCabinetsByDC($limit=false,$limitzone=false){
global $dbh;
$this->MakeSafe();
$hascoords=($limit)?'AND MapX1!=MapX2 AND MapY1!=MapY2':'';
$limitzone=($limitzone && $this->ZoneID>0)?" AND ZoneID=$this->ZoneID":'';
$sql="SELECT * FROM fac_Cabinet WHERE DataCenterID=$this->DataCenterID $hascoords$limitzone ORDER BY Location;";
$cabinetList=array();
foreach($dbh->query($sql) as $cabinetRow){
$cabinetList[]=Cabinet::RowToObject($cabinetRow);
}
return $cabinetList;
}
function CabinetOccupancy($CabinetID){
global $dbh;
$CabinetID=intval($CabinetID);
//$sql="SELECT SUM(Height) AS Occupancy FROM fac_Device WHERE Cabinet=$CabinetID;";
//JMGA halfdepth height calculation
$sql = "select sum(if(HalfDepth,Height/2,Height)) as Occupancy from fac_Device where Cabinet=$CabinetID";
if(!$row=$dbh->query($sql)->fetch()){
$info=$dbh->errorInfo();
error_log("CabinetOccupancy::PDO Error: {$info[2]} SQL=$sql");
return false;
}
return $row["Occupancy"];
}
static function GetOccupants($CabinetID){
global $dbh;
$sql="SELECT Owner FROM fac_Device WHERE Cabinet=".intval($CabinetID)." Group By Owner;";
$occupants=array();
foreach($dbh->query($sql) as $row){
$occupants[]=$row[0];
}
return $occupants;
}
function GetDCSelectList(){
global $dbh;
$sql="SELECT * FROM fac_DataCenter ORDER BY Name";
$selectList='<select name="datacenterid" id="datacenterid">';
foreach($dbh->query($sql) as $selectRow){
$selected=($selectRow["DataCenterID"]==$this->DataCenterID)?' selected':'';
$selectList.="<option value=\"{$selectRow["DataCenterID"]}\"$selected>{$selectRow["Name"]}</option>";
}
$selectList.='</select>';
return $selectList;
}
function GetDCSelectListSubmit(){
global $dbh;
$sql="SELECT * FROM fac_DataCenter ORDER BY Name;";
$selectList='<select name="datacenterid" id="datacenterid" onChange="form.submit()">';
foreach($dbh->query($sql) as $selectRow){
$selected=($selectRow[ "DataCenterID"]==$this->DataCenterID)?' selected':'';
$selectList.="<option value={$selectRow["DataCenterID"]}$selected>{$selectRow["Name"]}</option>";
}
$selectList.='</select>';
return $selectList;
}
function GetZoneSelectList(){
global $dbh;
$this->MakeSafe();
$sql="SELECT * FROM fac_Zone WHERE DataCenterID=$this->DataCenterID ORDER BY Description;";
$selectList='<select name="zoneid" id="zoneid">';
$selectList.='<option value=0>'.__("None").'</option>';
foreach($dbh->query($sql) as $selectRow){
$selected=($selectRow["ZoneID"]==$this->ZoneID)?' selected':'';
$selectList.="<option value=\"{$selectRow["ZoneID"]}\"$selected>{$selectRow["Description"]}</option>";
}
$selectList.='</select>';
return $selectList;
}
function GetCabinetsByRow($rear=false){
global $dbh;
$this->MakeSafe();
$cabrow=new CabRow();
$cabrow->CabRowID=$this->CabRowID;
$sql="SELECT MIN(MapX1) AS MapX1, MAX(MapX2) AS MapX2, MIN(MapY1) AS MapY1,
MAX(MapY2) AS MapY2, AVG(MapX1) AS AvgX1, AVG(MapX2) AS AvgX2, COUNT(*) AS
CabCount FROM fac_Cabinet WHERE CabRowID=$cabrow->CabRowID AND MapX1>0
AND MapX2>0 AND MapY1>0 and MapY2>0;";
$shape=$dbh->query($sql)->fetch();
// size of average cabinet
$sX=$shape["AvgX2"]-$shape["AvgX1"];
// change in x and y to give overall shape of row
$cX=$shape["MapX2"]-$shape["MapX1"];
$cY=$shape["MapY2"]-$shape["MapY1"];
/*
* In rows with more than one cabinet we can determine the layout based on
* their size. The side of a row will be close to the change in x or y while
* the front/rear of a row will be equal to the average of the sides
* multiplied by the number of objects in the set
*
* change = size * number of cabinets
*/
$layout=($cX==$sX*$shape["CabCount"] || $cX>$cY)?"Horizontal":"Vertical";
$order=($layout=="Horizontal")?"MapX1,":"MapY1,";
$frontedge=$cabrow->GetCabRowFrontEdge($layout);
// Order first by row layout then by natural sort
$sql="SELECT * FROM fac_Cabinet WHERE CabRowID=$cabrow->CabRowID ORDER BY $order
length(Location), Location ASC;";
$cabinetList=array();
foreach($dbh->query($sql) as $cabinetRow){
$cabinetList[]=Cabinet::RowToObject($cabinetRow);
}
if($frontedge=="Right" || $frontedge=="Top"){
$cabinetList=array_reverse($cabinetList);
}
return $cabinetList;
}
function GetCabinetsByZone(){
global $dbh;
$this->MakeSafe();
$sql="SELECT * FROM fac_Cabinet WHERE ZoneID=$this->ZoneID;";
$cabinetList=array();
foreach($dbh->query($sql) as $cabinetRow){
$cabinetList[]=Cabinet::RowToObject($cabinetRow);
}
return $cabinetList;
}
function GetCabRowSelectList(){
global $dbh;
$this->MakeSafe();
$sql="SELECT * FROM fac_CabRow WHERE ZoneID=$this->ZoneID ORDER BY Name;";
$selectList='<select name="cabrowid" id="cabrowid">';
$selectList.='<option value=0>'.__("None").'</option>';
foreach($dbh->query($sql) as $selectRow){
$selected=($selectRow["CabRowID"]==$this->CabRowID)?' selected':'';
$selectList.="<option value=\"{$selectRow["CabRowID"]}\"$selected>{$selectRow["Name"]}</option>";
}
$selectList.='</select>';
return $selectList;
}
function GetCabinetSelectList(){
global $dbh;
$sql="SELECT Name, CabinetID, Location, AssignedTo FROM fac_DataCenter, fac_Cabinet WHERE
fac_DataCenter.DataCenterID=fac_Cabinet.DataCenterID ORDER BY Name ASC,
Location ASC;";
$selectList="<select name=\"cabinetid\" id=\"cabinetid\"><option value=\"-1\">Storage Room</option>";
foreach($dbh->query($sql) as $selectRow){
if($selectRow["CabinetID"]==$this->CabinetID || User::Current()->canWrite($selectRow["AssignedTo"])){
$selected=($selectRow["CabinetID"]==$this->CabinetID)?' selected':'';
$selectList.="<option value=\"{$selectRow["CabinetID"]}\"$selected>{$selectRow["Name"]} / {$selectRow["Location"]}</option>";
}
}
$selectList .= "</select>";
return $selectList;
}
function BuildCabinetTree(){
global $dbh;
$dc=new DataCenter();
$dept=new Department();
$dcList=$dc->GetDCList();
if(count($dcList) >0){
$tree="<ul class=\"mktree\" id=\"datacenters\">\n";
$zoneInfo=new Zone();
while(list($dcID,$datacenter)=each($dcList)){
if($dcID==$this->DataCenterID){
$classType = "liOpen";
}else{
$classType = "liClosed";
}
$tree.="\t<li class=\"$classType\" id=\"dc$dcID\"><a href=\"dc_stats.php?dc=$datacenter->DataCenterID\">$datacenter->Name</a>/\n\t\t<ul>\n";
$sql="SELECT * FROM fac_Cabinet WHERE DataCenterID=\"$dcID\" ORDER BY Location ASC;";
foreach($dbh->query($sql) as $cabRow){
$dept->DeptID = $cabRow["AssignedTo"];
if($dept->DeptID==0){
$dept->Name = "General Use";
}else{
$dept->GetDeptByID();
}
$tree.="\t\t\t<li id=\"cab{$cabRow['CabinetID']}\"><a href=\"cabnavigator.php?cabinetid={$cabRow['CabinetID']}\">{$cabRow['Location']} [$dept->Name]</a></li>\n";
}
$tree.="\t\t</ul>\n </li>\n";
}
$tree.="<li class=\"liOpen\" id=\"dc-1\"><a href=\"storageroom.php\">Storage Room</a></li>";
$tree.="</ul>";
}
return $tree;
}
function DeleteCabinet(){
global $dbh;
/* Need to delete all devices and CDUs first */
$tmpDev=new Device();
$tmpCDU=new PowerDistribution();
$tmpDev->Cabinet=$this->CabinetID;
$devList=$tmpDev->ViewDevicesByCabinet();
foreach($devList as &$delDev){
$delDev->DeleteDevice();
}
$tmpCDU->CabinetID=$this->CabinetID;
$cduList=$tmpCDU->GetPDUbyCabinet();
foreach($cduList as &$delCDU){
$delCDU->DeletePDU();
}
$sql="DELETE FROM fac_Cabinet WHERE CabinetID=$this->CabinetID;";
if(!$dbh->exec($sql)){
$info=$dbh->errorInfo();
error_log("PDO Error::DeleteCabinet: {$info[2]} SQL=$sql");
return false;
}
(class_exists('LogActions'))?LogActions::LogThis($this):'';
return true;
}
function SearchByCabinetName( $db = null ) {
global $dbh;
$sql="select * from fac_Cabinet where ucase(Location) like \"%" . transform($this->Location) . "%\" order by Location;";
$cabinetList=array();
foreach ( $dbh->query( $sql ) as $cabinetRow ){
$cabID=$cabinetRow["CabinetID"];
$cabinetList[$cabID]=Cabinet::RowToObject($cabinetRow);
}
return $cabinetList;
}
function SearchByOwner( $db = null ) {
global $dbh;
$sql="select * from fac_Cabinet WHERE AssignedTo=".intval($this->AssignedTo)." ORDER BY Location;";
$cabinetList=array();
foreach ( $dbh->query( $sql ) as $cabinetRow ) {
$cabID=$cabinetRow["CabinetID"];
$cabinetList[$cabID]=Cabinet::RowToObject($cabinetRow);
}
return $cabinetList;
}
function SearchByCustomTag( $tag=null ) {
global $dbh;
$sql="SELECT a.* from fac_Cabinet a, fac_CabinetTags b, fac_Tags c WHERE a.CabinetID=b.CabinetID AND b.TagID=c.TagID AND UCASE(c.Name) LIKE UCASE('%".sanitize($tag)."%');";
$cabinetList=array();
foreach ( $dbh->query( $sql ) as $cabinetRow ) {
$cabID=$cabinetRow["CabinetID"];
$cabinetList[$cabID]=Cabinet::RowToObject($cabinetRow);
}
return $cabinetList;
}
function GetTags() {
global $dbh;
$sql = "SELECT TagID FROM fac_CabinetTags WHERE CabinetID=".intval($this->CabinetID).";";
$tags = array();
foreach ( $dbh->query( $sql ) as $row ) {
$tags[]=Tags::FindName($row[0]);
}
return $tags;
}
function SetTags( $tags=array() ) {
global $dbh;
if(count($tags)>0){
//Clear existing tags
$this->SetTags();
foreach($tags as $tag){
$t=Tags::FindID($tag);
if($t==0){
$t=Tags::CreateTag($tag);
}
$sql="INSERT INTO fac_CabinetTags (CabinetID, TagID) VALUES (".intval($this->CabinetID).",$t);";
if ( ! $dbh->exec($sql) ) {
$info = $dbh->errorInfo();
error_log( "PDO Error: " . $info[2] . " SQL=" . $sql );
return false;
}
}
}else{
//If no array is passed then clear all the tags
$delsql="DELETE FROM fac_CabinetTags WHERE CabinetID=".intval($this->CabinetID).";";
$dbh->exec($delsql);
}
return 0;
}
static function UpdateSensors( $CabinetID = null ) {
global $dbh;
global $config;
if ( ! function_exists( "snmpget" ) ) {
return;
}
if ( $CabinetID != null ) {
$sql = sprintf( "select a.CabinetID, a.SensorIPAddress, a.SensorCommunity, b.* from fac_Cabinet a, fac_SensorTemplate b where a.SensorTemplateID=b.TemplateID and a.CabinetID=%d and a.SensorIPAddress>'' and a.SensorTemplateID>0", $CabinetID );
} else {
$sql = "select a.CabinetID, a.SensorIPAddress, a.SensorCommunity, b.* from fac_Cabinet a, fac_SensorTemplate b where a.SensorTemplateID=b.TemplateID and a.SensorIPAddress>'' and a.SensorTemplateID>0";
}
$sensors = $dbh->prepare( "insert into fac_CabinetTemps values (:cabinetid, now(), :temp, :humidity ) on duplicate key update LastRead=now(), Temp=:temp, Humidity=:humidity" );
foreach ( $dbh->query( $sql ) as $row ) {
if ( $row["SensorCommunity"] == "" ) {
$Community = $config->ParameterArray["SNMPCommunity"];
} else {
$Community = $row["SensorCommunity"];
}
if ( $row["SNMPVersion"] == "2c" ) {
@list( $trash, $temp ) = explode( ":", @snmp2_get( $row["SensorIPAddress"], $Community, $row["TemperatureOID"] ) );
@list( $trash, $humid ) = explode( ":", @snmp2_get( $row["SensorIPAddress"], $Community, $row["HumidityOID"] ) );
} else {
@list( $trash, $temp ) = explode( ":", @snmpget( $row["SensorIPAddress"], $Community, $row["TemperatureOID"] ) );
@list( $trash, $humid ) = explode( ":", @snmpget( $row["SensorIPAddress"], $Community, $row["HumidityOID"] ) );
}
$temp = preg_replace( "/[^0-9.,+]/", "", $temp );
$humid = preg_replace( "/[^0-9.'+]/", "", $humid );
if (($row["mUnits"] == "english") && ($config->ParameterArray["mUnits"] == "metric")) {
$temp = (($temp-32)*5/9);
} elseif (($row["mUnits"] == "metric") && ($config->ParameterArray["mUnits"] == "english")) {
$temp = (($temp*9/5)+32);
}
if ( $row["TempMultiplier"] != 0 ) {
$temp *= $row["TempMultiplier"];
}
if ( $row["HumidityMultiplier"] != 0 ) {
$humid *= $row["HumidityMultiplier"];
}
$sensors->execute( array( "cabinetid"=>$row["CabinetID"], "temp"=>$temp, "humidity"=>$humid ) );
}
}
}
class CabinetAudit {
/* CabinetAudit: A perpetual audit trail for how often a cabinet has been audited, and by what user.
*/
var $CabinetID;
var $UserID;
var $AuditStamp;
var $Comments;
function CertifyAudit( $db = null ) {
global $dbh;
if($this->Comments){
$tmpAudit=new CabinetAudit();
$tmpAudit->CabinetID=$this->CabinetID;
(class_exists('LogActions'))?LogActions::LogThis($this,$tmpAudit):'';
}else{
(class_exists('LogActions'))?LogActions::LogThis($this):'';
}
if ( ! $dbh->exec( $sql ) ) {
$info = $dbh->errorInfo();
error_log( "PDO Error: " . $info[2] . " SQL=" . $sql );
return false;
}
return;
}
function GetLastAudit( $db = null ) {
global $dbh;
$sql = "select * from fac_GenericLog where ObjectID=\"" . intval( $this->CabinetID ) . "\" and Class=\"CabinetAudit\" order by Time DESC Limit 1";
if($row=$dbh->query($sql)->fetch()){
$this->CabinetID=$row["ObjectID"];
$this->UserID=$row["UserID"];
$this->AuditStamp=date("M d, Y H:i", strtotime($row["Time"]));
return true;
} else {
// No sense in logging an error for something that's never been done
return false;
}
}
function GetLastAuditByUser( $db = null ) {
global $dbh;
$sql = "select * from fac_GenericLog where UserID=\"" . addslashes( $this->UserID ) . "\" and Class=\"CabinetAudit\" order by Time DESC Limit 1";
if ( $row = $dbh->query( $sql )->fetch() ) {
$this->CabinetID = $row["ObjectID"];
$this->UserID = $row["UserID"];
$this->AuditStamp = date( "M d, Y H:i", strtotime( $row["Time"] ) );
} else {
$info = $dbh->errorInfo();
error_log( "PDO Error: " . $info[2] . " SQL=" . $sql );
return false;
}
return;
}
}
class SensorTemplate {
/* Sensor Template - Information about how to get temperature/humidity from various types of devices */
var $TemplateID;
var $ManufacturerID;
var $Name;
var $SNMPVersion;
var $TemperatureOID;
var $HumidityOID;
var $TempMultiplier;
var $HumidityMultiplier;
static function getTemplate( $templateID = null ) {
global $dbh;
if ( $templateID != null ) {
$sql = sprintf( "select * from fac_SensorTemplate where TemplateID=%d", $templateID );
} else {
$sql = "select * from fac_SensorTemplate order by Name ASC";
}
$tempList = array();
foreach ( $dbh->query( $sql ) as $row ) {
$n = sizeof ( $tempList );
$tempList[$n] = new SensorTemplate();
$tempList[$n]->TemplateID = $row["TemplateID"];
$tempList[$n]->ManufacturerID = $row["ManufacturerID"];
$tempList[$n]->Name = $row["Name"];
$tempList[$n]->SNMPVersion = $row["SNMPVersion"];
$tempList[$n]->TemperatureOID = $row["TemperatureOID"];
$tempList[$n]->HumidityOID = $row["HumidityOID"];
$tempList[$n]->TempMultiplier = $row["TempMultiplier"];
$tempList[$n]->HumidityMultiplier = $row["HumidityMultiplier"];
$tempList[$n]->mUnits = $row["mUnits"];
}
if ( $templateID != null ) {
return array_pop($tempList);
} else {
return $tempList;
}
}
function CreateTemplate() {
global $dbh;
$sql = $dbh->prepare( "insert into fac_SensorTemplate values ( 0, :ManufacturerID, :Name, :SNMPVersion, :TemperatureOID, :HumidityOID, :TempMultiplier, :HumidityMultiplier, :mUnits )" );
$args = array( "ManufacturerID" => $this->ManufacturerID,
"Name" => $this->Name,
"SNMPVersion" => $this->SNMPVersion,
"TemperatureOID" => $this->TemperatureOID,
"HumidityOID" => $this->HumidityOID,
"TempMultiplier" => $this->TempMultiplier,
"HumidityMultiplier" => $this->HumidityMultiplier,
"mUnits" => $this->mUnits );
$sql->execute( $args );
$this->TemplateID = $dbh->lastInsertId();
(class_exists('LogActions'))?LogActions::LogThis($this):'';
}
function UpdateTemplate() {
global $dbh;
$old=SensorTemplate::getTemplate($this->TemplateID);
$sql = $dbh->prepare( "update fac_SensorTemplate set ManufacturerID=:ManufacturerID, Name=:Name, SNMPVersion=:SNMPVersion, TemperatureOID=:TemperatureOID, HumidityOID=:HumidityOID, TempMultiplier=:TempMultiplier, HumidityMultiplier=:HumidityMultiplier, mUnits=:mUnits where TemplateID=:TemplateID" );
$args = array( "ManufacturerID" => $this->ManufacturerID,
"Name" => $this->Name,
"SNMPVersion" => $this->SNMPVersion,
"TemperatureOID" => $this->TemperatureOID,
"HumidityOID" => $this->HumidityOID,
"TempMultiplier" => $this->TempMultiplier,
"HumidityMultiplier" => $this->HumidityMultiplier,
"mUnits" => $this->mUnits,
"TemplateID" => $this->TemplateID );
$sql->execute( $args );
(class_exists('LogActions'))?LogActions::LogThis($this,$old):'';
}
function DeleteTemplate() {
global $dbh;
// Set any sensors using this template back to the default "no template" value
$sql = "update fac_Cabinet set SensorTemplateID=0 where SensorTemplateID=" . intval( $this->TemplateID );
$dbh->exec( $sql );
// Now it is "safe" to delete the record as it will leave no orphans
$sql = "delete from fac_SensorTemplate where TemplateID=" . intval( $this->TemplateID );
$dbh->exec( $sql );
(class_exists('LogActions'))?LogActions::LogThis($this):'';
}
}
class CabinetTemps {
/* CabinetTemps: Temperature sensor readings from intelligent, SNMP readable temperature sensors */
var $CabinetID;
var $LastRead;
var $Temp;
var $Humidity;
function GetReading() {
global $dbh;
$sql = sprintf( "select * from fac_CabinetTemps where CabinetID=%d", $this->CabinetID );
if ( $row = $dbh->query( $sql )->fetch() ) {
$this->LastRead = date( "m-d-Y H:i:s", strtotime($row["LastRead"]) );
$Temp = $row["Temp"];
$Humidity = $row["Humidity"];
} else {
$info = $dbh->errorInfo();
error_log( "PDO Error: " . $info[2] . " SQL=" . $sql );
return false;
}
return;
}
}
class ColorCoding {
var $ColorID;
var $Name;
var $DefaultNote;
function CreateCode() {
global $dbh;
$sql="INSERT INTO fac_ColorCoding SET Name=\"".sanitize($this->Name)."\",
DefaultNote=\"".sanitize($this->DefaultNote)."\"";
if($dbh->exec($sql)){
$this->ColorID=$dbh->lastInsertId();
}else{
$info=$dbh->errorInfo();
error_log("PDO Error::CreateCode {$info[2]}");
return false;
}
return $this->ColorID;
}
function UpdateCode() {
global $dbh;
$sql="UPDATE fac_ColorCoding SET Name=\"".sanitize($this->Name)."\",
DefaultNote=\"".sanitize($this->DefaultNote)."\" WHERE ColorID=".intval($this->ColorID).";";
if(!$dbh->query($sql)){
$info=$dbh->errorInfo();
error_log("PDO Error: {$info[2]}");
return false;
}else{
return true;
}
}
function DeleteCode() {
/* If you call this, the upstream application should be checking to see if it is used already - you don't want to
create orphan connetions that reference this color code! */
global $dbh;
$sql="DELETE FROM fac_ColorCoding WHERE ColorID=".intval($this->ColorID);
if(!$dbh->exec($sql)){
$info=$dbh->errorInfo();
error_log("PDO Error: {$info[2]}");
return false;
}
return true;
}
function GetCode() {
global $dbh;
$sql="SELECT * FROM fac_ColorCoding WHERE ColorID=".intval($this->ColorID);
if($row=$dbh->query($sql)->fetch()){
$this->Name=$row["Name"];
$this->DefaultNote=$row["DefaultNote"];
}else{
return false;
}
return true;
}
function GetCodeByName() {
global $dbh;
$sql="SELECT * FROM fac_ColorCoding WHERE Name='".transform($this->Name)."';";
if($row=$dbh->query($sql)->fetch()){
$this->ColorID=$row["ColorID"];
$this->DefaultNote=$row["DefaultNote"];
}else{
return false;
}
return true;
}
static function GetCodeList() {
global $dbh;
$sql="SELECT * FROM fac_ColorCoding ORDER BY Name ASC";
$codeList=array();
foreach($dbh->query($sql) as $row){
$n=$row["ColorID"]; // index array by id
$codeList[$n]=new ColorCoding();
$codeList[$n]->ColorID=$row["ColorID"];
$codeList[$n]->Name=$row["Name"];
$codeList[$n]->DefaultNote=$row["DefaultNote"];
}
return $codeList;
}
static function ResetCode($colorid,$tocolorid=0){
/*
* This probably shouldn't be a function here since it will only be used in one
* place. This function will remove a color code from any device ports or will
* set it to another via an optional second color id
*
*/
global $dbh;
$colorid=intval($colorid);
$tocolorid=intval($tocolorid); // it will always be 0 unless otherwise set
$sql="UPDATE fac_DevicePorts SET ColorID='$tocolorid' WHERE ColorID='$colorid';";
if(!$dbh->query($sql)){
$info=$dbh->errorInfo();
error_log("PDO Error: {$info[2]}");
return false;
}else{
return true;
}
}
static function TimesUsed($colorid){
global $dbh;
$colorid=intval($colorid);
// get a count of the number of times this color is in use both on ports or assigned
// to a template.
$sql="SELECT COUNT(*) + (SELECT COUNT(*) FROM fac_MediaTypes WHERE ColorID=$colorid)
AS Result FROM fac_DevicePorts WHERE ColorID=$colorid";
$count=$dbh->prepare($sql);
$count->execute();
return $count->fetchColumn();
}
}
class ConnectionPath {
/* ConnectionPath: Display connection path between two endpoint devices through DC infrastructure.
Initial info are DeviceID and PortNumber.
Then locates one end of the connection path with "GotoHeadDevice" method.
Walk the path to the other end with "GotoNextDevice" method.
Contribution of Jose Miguel Gomez Apesteguia (June 2013)
*/
var $DeviceID;
var $PortNumber; //The sign of PortNumber indicate if the path continue by front port (>0) or rear port (<0)
private $PathAux; //loops control
function MakeSafe(){
$this->DeviceID=intval($this->DeviceID);
$this->PortNumber=intval($this->PortNumber);
}
private function AddDeviceToPathAux () {
$i=count($this->PathAux);
$this->PathAux[$i]["DeviceID"]=$this->DeviceID;
$this->PathAux[$i]["PortNumber"]=$this->PortNumber;
}
private function ClearPathAux(){
$this->PathAux=array();
}
private function IsDeviceInPathAux () {
$ret=false;
for ($i=0; $i<count($this->PathAux); $i++){
if ($this->PathAux[$i]["DeviceID"]==$this->DeviceID && $this->PathAux[$i]["PortNumber"]=$this->PortNumber) {
$ret=true;
break;
}
}
return $ret;
}