forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfrastructure.inc.php
3297 lines (2728 loc) · 94.8 KB
/
infrastructure.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
Classes Defined Here:
DataCenter: A logical/physical container for assets. This may be a room, or
even just a portion of a room. It does need to be a contiguous
space for mapping purposes. Large data centers may want to break
up the space into quadrants for easier management, but it can
easily be handled as a whole (in terms of the software and database).
Any mappings for larger than approximately 2500 SF become difficult
to see on a laptop screen, because each cabinet takes up such a small
portion of the overall map.
DeviceTemplate: A template with default values for height, wattage, and weight.
Height and wattage values can be overridden at the device level.
Templates are completely optional, but are a very good way to
manage the power capacity of the data center.
Manufacturer: Used only in DeviceTemplate, so if you choose not to utilize
templates, there is no need to enter Manufacturers.
Zone: A logical grouping of DataCenter components, so that if a large data
center has been broken down into smaller components, it can be
reported on as a single entity. For example, Building A may have
data centers in Room 100, Room 109, and Room 205. All three can
be placed in a single zone for reporting on Building A data center
metrics.
NOT YET IMPLEMENTED
*/
class BinAudits {
var $BinID;
var $UserID;
var $AuditStamp;
function MakeSafe(){
$this->BinID=intval($this->BinID);
$this->UserID=sanitize($this->UserID);
$this->AuditStamp=sanitize($this->AuditStamp);
}
function MakeDisplay(){
$this->UserID=stripslashes($this->UserID);
$this->AuditStamp=stripslashes($this->AuditStamp);
}
function exec($sql){
global $dbh;
return $dbh->exec($sql);
}
function AddAudit(){
$this->AuditStamp=date("Y-m-d",strtotime($this->AuditStamp));
$this->MakeSafe();
$sql="INSERT INTO fac_BinAudits SET BinID=$this->BinID, UserID=\"$this->UserID\", AuditStamp=\"$this->AuditStamp\";";
$this->exec($sql);
}
}
class BinContents {
var $BinID;
var $SupplyID;
var $Count;
function MakeSafe(){
$this->BinID=intval($this->BinID);
$this->SupplyID=intval($this->SupplyID);
$this->Count=intval($this->Count);
}
static function RowToObject($row){
$bin=new BinContents();
$bin->BinID=$row["BinID"];
$bin->SupplyID=$row["SupplyID"];
$bin->Count=$row["Count"];
return $bin;
}
function query($sql){
global $dbh;
return $dbh->query($sql);
}
function exec($sql){
global $dbh;
return $dbh->exec($sql);
}
function AddContents(){
$sql="INSERT INTO fac_BinContents SET BinID=$this->BinID, SupplyID=$this->SupplyID, Count=$this->Count;";
return $this->exec($sql);
}
function GetBinContents(){
$this->MakeSafe();
/* Return all of the supplies found in this bin */
$sql="SELECT * FROM fac_BinContents WHERE BinID=$this->BinID;";
$binList=array();
foreach($this->query($sql) as $row){
$binList[]=BinContents::RowToObject($row);
}
return $binList;
}
function FindSupplies(){
$this->MakeSafe();
/* Return all of the bins where this SupplyID is found */
$sql="SELECT a.* FROM fac_BinContents a, fac_SupplyBin b WHERE
a.SupplyID=$this->SupplyID AND a.BinID=b.BinID ORDER BY b.Location ASC;";
$binList=array();
foreach($this->query($sql) as $row){
$binList[]=BinContents::RowToObject($row);
}
return $binList;
}
function UpdateCount(){
$this->MakeSafe();
$sql="UPDATE fac_BinContents SET Count=$this->Count WHERE BinID=$this->BinID
AND SupplyID=$this->SupplyID;";
return $this->query($sql);
}
function RemoveContents(){
$this->MakeSafe();
$sql="DELETE FROM fac_BinContents WHERE BinID=$this->BinID AND
SupplyID=$this->SupplyID;";
return $this->exec($sql);
}
function EmptyBin(){
$this->MakeSafe();
$sql="DELETE FROM fac_BinContents WHERE BinID=$this->BinID;";
return $this->exec($sql);
}
}
class DataCenter {
var $DataCenterID;
var $Name;
var $SquareFootage;
var $DeliveryAddress;
var $Administrator;
var $MaxkW;
var $DrawingFileName;
var $EntryLogging;
var $dcconfig;
var $ContainerID;
var $MapX;
var $MapY;
var $U1Position;
function MakeSafe(){
$this->DataCenterID=intval($this->DataCenterID);
$this->Name=sanitize($this->Name);
$this->SquareFootage=intval($this->SquareFootage);
$this->DeliveryAddress=sanitize($this->DeliveryAddress);
$this->Administrator=sanitize($this->Administrator);
$this->MaxkW=intval($this->MaxkW);
$this->DrawingFileName=sanitize($this->DrawingFileName);
$this->EntryLogging=intval($this->EntryLogging);
$this->ContainerID=intval($this->ContainerID);
$this->MapX=abs($this->MapX);
$this->MapY=abs($this->MapY);
$this->U1Position=in_array($this->U1Position, array("Top","Bottom","Default"))?$this->U1Position:"Default";
}
function MakeDisplay(){
$this->Name=stripslashes($this->Name);
$this->DeliveryAddress=stripslashes($this->DeliveryAddress);
$this->Administrator=stripslashes($this->Administrator);
$this->DrawingFileName=stripslashes($this->DrawingFileName);
}
public function __construct($dcid=false){
if($dcid){
$this->DataCenterID=intval($dcid);
}
return $this;
}
static function RowToObject($row){
$dc=New DataCenter();
$dc->DataCenterID=$row["DataCenterID"];
$dc->Name=$row["Name"];
$dc->SquareFootage=$row["SquareFootage"];
$dc->DeliveryAddress=$row["DeliveryAddress"];
$dc->Administrator=$row["Administrator"];
$dc->MaxkW=$row["MaxkW"];
$dc->DrawingFileName=$row["DrawingFileName"];
$dc->EntryLogging=$row["EntryLogging"];
$dc->ContainerID=$row["ContainerID"];
$dc->MapX=$row["MapX"];
$dc->MapY=$row["MapY"];
$dc->U1Position=$row["U1Position"];
$dc->MakeDisplay();
return $dc;
}
function query($sql){
global $dbh;
return $dbh->query($sql);
}
function exec($sql){
global $dbh;
return $dbh->exec($sql);
}
function CreateDataCenter(){
global $dbh;
$this->MakeSafe();
$sql="INSERT INTO fac_DataCenter SET Name=\"$this->Name\",
SquareFootage=$this->SquareFootage, DeliveryAddress=\"$this->DeliveryAddress\",
Administrator=\"$this->Administrator\", MaxkW=$this->MaxkW,
DrawingFileName=\"$this->DrawingFileName\", EntryLogging=0,
ContainerID=$this->ContainerID, MapX=$this->MapX, MapY=$this->MapY,
U1Position=\"$this->U1Position\";";
if(!$dbh->exec($sql)){
$info=$dbh->errorInfo();
error_log("PDO Error::DataCenter:CreateDataCenter {$info[2]} SQL=$sql");
return false;
}
$this->DataCenterID=$dbh->lastInsertId();
(class_exists('LogActions'))?LogActions::LogThis($this):'';
return true;
}
function DeleteDataCenter($junkremoval=true) {
$this->MakeSafe();
// Have to make sure that we delete EVERYTHING and not create orphans
// Also, if we are down to the last data center, refuse to delete it
$sql = "SELECT COUNT(*) AS Total FROM fac_DataCenter;";
if ( ! $row = $this->query($sql)->fetch() ) {
return false;
}
if ( $row["Total"] < 2 ) {
return false;
}
// The Cabinet delete function already deletes all children within it, first, so just delete all of them
$cab = new Cabinet();
$cab->DataCenterID = $this->DataCenterID;
$cabList = $cab->ListCabinetsByDC();
foreach( $cabList as $c ) {
$c->DeleteCabinet();
}
// Now delete any Zones or Rows that are attached to this data center
$zn = new Zone();
$zn->DataCenterID = $this->DataCenterID;
$zoneList = $zn->GetZonesByDC();
foreach ( $zoneList as $z ) {
// This function already deletes any rows within the zone
$z->DeleteZone();
}
// Time to deal with the crap in storage
// Get a list of all the devices that are in this data center's storage room
$sql="SELECT * FROM fac_Device WHERE Cabinet=-1 AND Position=$this->DataCenterID;";
$devices=array();
foreach($this->query($sql) as $row){
$devices[]=Device::RowToObject($row);
}
// Default action is just to delete them
if($junkremoval){
foreach($devices as $dev){
$dev->DeleteDevice();
}
}else{ // move it to the general storage room
foreach($devices as $dev){
$dev->Position=0;
$dev->UpdateDevice();
}
}
// Finally, delete the data center itself
$sql="DELETE FROM fac_DataCenter WHERE DataCenterID=$this->DataCenterID;";
$this->exec($sql);
(class_exists('LogActions'))?LogActions::LogThis($this):'';
return true;
}
function UpdateDataCenter(){
$this->MakeSafe();
$sql="UPDATE fac_DataCenter SET Name=\"$this->Name\",
SquareFootage=$this->SquareFootage, DeliveryAddress=\"$this->DeliveryAddress\",
Administrator=\"$this->Administrator\", MaxkW=$this->MaxkW,
DrawingFileName=\"$this->DrawingFileName\", EntryLogging=0,
ContainerID=$this->ContainerID, MapX=$this->MapX, MapY=$this->MapY,
U1Position=\"$this->U1Position\" WHERE DataCenterID=$this->DataCenterID;";
$this->MakeDisplay();
$old=new DataCenter();
$old->DataCenterID=$this->DataCenterID;
$old->GetDataCenter();
(class_exists('LogActions'))?LogActions::LogThis($this,$old):'';
return $this->query($sql);
}
function GetDataCenter(){
$this->MakeSafe();
$sql="SELECT * FROM fac_DataCenter WHERE DataCenterID=$this->DataCenterID;";
if($row=$this->query($sql)->fetch()){
foreach(DataCenter::RowToObject($row) as $prop => $value){
$this->$prop=$value;
}
return true;
}else{
return false;
}
}
static function GetDCList($indexedbyid=false){
global $dbh;
$sql="SELECT * FROM fac_DataCenter ORDER BY Name ASC;";
$datacenterList=array();
foreach($dbh->query($sql) as $row){
if($indexedbyid){
$datacenterList[$row['DataCenterID']]=DataCenter::RowToObject($row);
}else{
$datacenterList[]=DataCenter::RowToObject($row);
}
}
return $datacenterList;
}
function GetDataCenterbyID(){
// Not sure why this was duplicated but this will do til we clear up the references
return $this->GetDataCenter();
}
// Return an array of the immediate children
function GetChildren(){
$children=array();
$zone=new Zone();
$zone->DataCenterID=$this->DataCenterID;
foreach($zone->GetZonesByDC() as $child){
$children[]=$child;
}
$row=new CabRow();
$row->DataCenterID=$this->DataCenterID;
foreach($row->GetCabRowsByDC(true) as $child){
$children[]=$child;
}
$cab=new Cabinet();
$cab->DataCenterID=$this->DataCenterID;
foreach($cab->ListCabinetsByDC() as $child){
if($child->ZoneID>0 || $child->CabRowID>0 || $child->DataCenterID!=$this->DataCenterID){
}else{
$children[]=$child;
}
}
return $children;
}
/**
* Returns an array with all the hierarchy of containers the data center
* belongs to.
* @param type $containerList
* @return type
*/
public function getContainerList($containerID = 0)
{
$container = new Container();
if ($containerID == 0) {
$container->ContainerID = $this->ContainerID;
} else {
$container->ContainerID = $containerID;
}
$container->GetContainer();
$containerList[] = $container->Name;
if ($container->ParentID > 0) {
$childContainerList = $this->getContainerList($container->ParentID);
$containerList = array_merge($childContainerList, $containerList);
}
return $containerList;
}
function GetOverview(){
$this->MakeSafe();
$statusarray=array();
// check to see if map was set
if(strlen($this->DrawingFileName)){
$mapfile="drawings".DIRECTORY_SEPARATOR.$this->DrawingFileName;
$overview=array();
$space=array();
$weight=array();
$power=array();
$temperature=array();
$humidity=array();
$realpower=array();
$colors=array();
// map was set in config check to ensure a file exists before we attempt to use it
if(file_exists($mapfile)){
$this->dcconfig=new Config();
$dev=new Device();
$templ=new DeviceTemplate();
$cab=new Cabinet();
// get all color codes and limits for use with loop below
$CriticalColor=html2rgb($this->dcconfig->ParameterArray["CriticalColor"]);
$CautionColor=html2rgb($this->dcconfig->ParameterArray["CautionColor"]);
$GoodColor=html2rgb($this->dcconfig->ParameterArray["GoodColor"]);
$SpaceRed=intval($this->dcconfig->ParameterArray["SpaceRed"]);
$SpaceYellow=intval($this->dcconfig->ParameterArray["SpaceYellow"]);
$WeightRed=intval($this->dcconfig->ParameterArray["WeightRed"]);
$WeightYellow=intval($this->dcconfig->ParameterArray["WeightYellow"]);
$PowerRed=intval($this->dcconfig->ParameterArray["PowerRed"]);
$PowerYellow=intval($this->dcconfig->ParameterArray["PowerYellow"]);
$unknown=html2rgb('FFFFFF');
// Copy all colors into an array to export
$color['unk']=array('r' => $unknown[0], 'g' => $unknown[1], 'b' => $unknown[2]);
$color['bad']=array('r' => $CriticalColor[0], 'g' => $CriticalColor[1], 'b' => $CriticalColor[2]);
$color['med']=array('r' => $CautionColor[0], 'g' => $CautionColor[1], 'b' => $CautionColor[2]);
$color['low']=array('r' => $GoodColor[0], 'g' => $GoodColor[1], 'b' => $GoodColor[2]);
$colors=$color;
// Assign color variables
$CriticalColor='bad';
$CautionColor='med';
$GoodColor='low';
$unknownColor='unk';
// Temperature
$TemperatureYellow=intval($this->dcconfig->ParameterArray["TemperatureYellow"]);
$TemperatureRed=intval($this->dcconfig->ParameterArray["TemperatureRed"]);
// Humidity
$HumidityMin=intval($this->dcconfig->ParameterArray["HumidityRedLow"]);
$HumidityMedMin=intval($this->dcconfig->ParameterArray["HumidityYellowLow"]);
$HumidityMedMax=intval($this->dcconfig->ParameterArray["HumidityYellowHigh"]);
$HumidityMax=intval($this->dcconfig->ParameterArray["HumidityRedHigh"]);
//Real Power
$RealPowerRed=intval($this->dcconfig->ParameterArray["PowerRed"]);
$RealPowerYellow=intval($this->dcconfig->ParameterArray["PowerYellow"]);
// get image file attributes and type
list($width, $height, $type, $attr)=getimagesize($mapfile);
$cdus=array();
$sql = "select c.CabinetID, P.RealPower, P.BreakerSize, P.InputAmperage*PP.PanelVoltage as VoltAmp from
(fac_Cabinet c left join (select CabinetID, Wattage as RealPower, BreakerSize, InputAmperage, PanelID from fac_PowerDistribution PD
left join fac_PDUStats PS on PD.PDUID=PS.PDUID) P on c.CabinetID=P.CabinetID)
left join (select PanelVoltage, PanelID from fac_PowerPanel) PP on PP.PanelID=P.PanelID
where PanelVoltage is not null and RealPower is not null and c.DataCenterID=".intval($this->DataCenterID);
$rpvalues=$this->query($sql);
foreach($rpvalues as $cduRow){
$cabid=$cduRow['CabinetID'];
$voltamp=$cduRow['VoltAmp'];
$rp=$cduRow['RealPower'];
$bs=$cduRow['BreakerSize'];
if($bs==1){
$maxDraw=$voltamp / 1.732;
}elseif($bs==2){
$maxDraw=$voltamp;
}else{
$maxDraw=$voltamp * 1.732;
}
// De-rate all breakers to 80% sustained load
$maxDraw*=0.8;
// Only keep the highest percentage of any single CDU in a cabinet
if ( $rp > 0 ) {
$pp=intval($rp / $maxDraw * 100);
} else {
$pp = 0;
}
$cdus[$cabid]=(isset($cdus[$cabid]) && $cdus[$cabid]>$pp)?$cdus[$cabid]:$pp;
}
$cab->DataCenterID = $this->DataCenterID;
$cabList = $cab->ListCabinetsByDC();
$titletemp=0;
$titlerp=0;
// read all cabinets and calculate the color to display on the cabinet
foreach($cabList as $cabRow){
if ($cabRow->MapX1==$cabRow->MapX2 || $cabRow->MapY1==$cabRow->MapY2){
continue;
}
$currentHeight=$cabRow->CabinetHeight;
$metrics = CabinetMetrics::getMetrics( $cabRow->CabinetID );
$currentTemperature=$metrics->IntakeTemperature;
$currentHumidity=$metrics->IntakeHumidity;
$currentRealPower=$metrics->MeasuredPower;
$used=$metrics->SpaceUsed;
// check to make sure the cabinet height is set to keep errors out of the logs
if(!isset($cabRow->CabinetHeight)||$cabRow->CabinetHeight==0){$SpacePercent=100;}else{$SpacePercent=number_format($metrics->SpaceUsed /$cabRow->CabinetHeight *100,0);}
// check to make sure there is a weight limit set to keep errors out of logs
if(!isset($cabRow->MaxWeight)||$cabRow->MaxWeight==0){$WeightPercent=0;}else{$WeightPercent=number_format($metrics->CalculatedWeight /$cabRow->MaxWeight *100,0);}
// check to make sure there is a kilowatt limit set to keep errors out of logs
if(!isset($cabRow->MaxKW)||$cabRow->MaxKW==0){$PowerPercent=0;}else{$PowerPercent=number_format(($metrics->CalculatedPower /1000 ) /$cabRow->MaxKW *100,0);}
if(!isset($cabRow->MaxKW)||$cabRow->MaxKW==0){$RealPowerPercent=0;}else{$RealPowerPercent=number_format(($metrics->MeasuredPower /1000 ) /$cabRow->MaxKW *100,0, ",", ".");}
// check for individual cdu's being weird
if(isset($cdus[$cab->CabinetID])){$RealPowerPercent=($RealPowerPercent>$cdus[$cab->CabinetID])?$RealPowerPercent:$cdus[$cab->CabinetID];}
//Decide which color to paint on the canvas depending on the thresholds
if($SpacePercent>$SpaceRed){$scolor=$CriticalColor;}elseif($SpacePercent>$SpaceYellow){$scolor=$CautionColor;}else{$scolor=$GoodColor;}
if($WeightPercent>$WeightRed){$wcolor=$CriticalColor;}elseif($WeightPercent>$WeightYellow){$wcolor=$CautionColor;}else{$wcolor=$GoodColor;}
if($PowerPercent>$PowerRed){$pcolor=$CriticalColor;}elseif($PowerPercent>$PowerYellow){$pcolor=$CautionColor;}else{$pcolor=$GoodColor;}
if($RealPowerPercent>$RealPowerRed){$rpcolor=$CriticalColor;}elseif($RealPowerPercent>$RealPowerYellow){$rpcolor=$CautionColor;}else{$rpcolor=$GoodColor;}
if($currentTemperature==0){$tcolor=$unknownColor;}
elseif($currentTemperature>$TemperatureRed){$tcolor=$CriticalColor;}
elseif($currentTemperature>$TemperatureYellow){$tcolor=$CautionColor;}
else{$tcolor=$GoodColor;}
if($currentHumidity==0){$hcolor=$unknownColor;}
elseif($currentHumidity>$HumidityMax || $currentHumidity<$HumidityMin){$hcolor=$CriticalColor;}
elseif($currentHumidity>$HumidityMedMax || $currentHumidity<$HumidityMedMin) {$hcolor=$CautionColor;}
else{$hcolor=$GoodColor;}
foreach(array($scolor,$wcolor,$pcolor,$tcolor,$hcolor,$rpcolor) as $cc){
if($cc=='bad'){
$color='bad';break;
}elseif($cc=='med'){
$color='med';break;
}else{
$color='low';
}
}
$overview[$cabRow->CabinetID]=$color;
$space[$cabRow->CabinetID]=$scolor;
$weight[$cabRow->CabinetID]=$wcolor;
$power[$cabRow->CabinetID]=$pcolor;
$temperature[$cabRow->CabinetID]=$tcolor;
$humidity[$cabRow->CabinetID]=$hcolor;
$realpower[$cabRow->CabinetID]=$rpcolor;
$airflow[$cabRow->CabinetID]=$cabRow->FrontEdge;
}
}
$tempSQL = "select max(LastRead) as ReadingTime from fac_SensorReadings where DeviceID in (select DeviceID from fac_Device where DeviceType='Sensor' and Cabinet in (select CabinetID from fac_Cabinet where DataCenterID=" . $this->DataCenterID . "))";
$tempRes = $this->query( $tempSQL );
$tempRow = $tempRes->fetch();
$pwrSQL = "select max(LastRead) as ReadingTime from fac_PDUStats where PDUID in (select DeviceID from fac_Device where DeviceType='CDU' and Cabinet in (select CabinetID from fac_Cabinet where DataCenterID=" . $this->DataCenterID . "))";
$pwrRes = $this->query( $pwrSQL );
$pwrRow = $pwrRes->fetch();
//Key
$overview['title']=__("Composite View of Cabinets");
$space['title']=__("Occupied Space");
$weight['title']=__("Calculated Weight");
$power['title']=__("Calculated Power Usage");
$temperature['title']=($tempRow["ReadingTime"]>0)?__("Measured on")." ".date( 'c', strtotime( $tempRow["ReadingTime"])):__("no data");
$humidity['title']=($tempRow["ReadingTime"]>0)?__("Measured on")." ".date( 'c', strtotime( $tempRow["ReadingTime"])):__("no data");
$realpower['title']=($pwrRow["ReadingTime"]>0)?__("Measured on")." ".date( 'c', strtotime( $pwrRow["ReadingTime"])):__("no data");
$airflow['title']=__("Air Flow");
$statusarray=array('overview' => $overview,
'space' => $space,
'weight' => $weight,
'power' => $power,
'humidity' => $humidity,
'temperature' => $temperature,
'realpower' => $realpower,
'airflow' => $airflow,
'colors' => $colors
);
}
return $statusarray;
}
function GetDCStatistics(){
$this->GetDataCenter();
$sql="SELECT SUM(CabinetHeight) as TotalU FROM fac_Cabinet WHERE
DataCenterID=$this->DataCenterID;";
$dcStats["TotalU"]=($test=$this->query($sql)->fetchColumn())?$test:0;
$sql="SELECT SUM(a.Height) as TotalU FROM fac_Device a,fac_Cabinet b WHERE
a.Cabinet=b.CabinetID AND b.DataCenterID=$this->DataCenterID AND
a.DeviceType NOT IN ('Server','Storage Array');";
$dcStats["Infrastructure"]=($test=$this->query($sql)->fetchColumn())?$test:0;
$sql="SELECT SUM(a.Height) as TotalU FROM fac_Device a,fac_Cabinet b WHERE
a.Cabinet=b.CabinetID AND b.DataCenterID=$this->DataCenterID AND
a.Reservation=false AND a.DeviceType IN ('Server', 'Storage Array');";
$dcStats["Occupied"]=($test=$this->query($sql)->fetchColumn())?$test:0;
$sql="SELECT SUM(a.Height) FROM fac_Device a,fac_Cabinet b WHERE
a.Cabinet=b.CabinetID AND a.Reservation=true AND b.DataCenterID=$this->DataCenterID;";
$dcStats["Allocated"]=($test=$this->query($sql)->fetchColumn())?$test:0;
$dcStats["Available"]=$dcStats["TotalU"] - $dcStats["Occupied"] - $dcStats["Infrastructure"] - $dcStats["Allocated"];
// Perform two queries - one is for the wattage overrides (where NominalWatts > 0) and one for the template (default) values
$sql="SELECT SUM(NominalWatts) as TotalWatts FROM fac_Device a,fac_Cabinet b WHERE
a.Cabinet=b.CabinetID AND a.NominalWatts>0 AND
b.DataCenterID=$this->DataCenterID;";
$dcStats["ComputedWatts"]=($test=$this->query($sql)->fetchColumn())?$test:0;
$sql="SELECT SUM(c.Wattage) as TotalWatts FROM fac_Device a, fac_Cabinet b,
fac_DeviceTemplate c WHERE a.Cabinet=b.CabinetID AND
a.TemplateID=c.TemplateID AND a.NominalWatts=0 AND
b.DataCenterID=$this->DataCenterID;";
$dcStats["ComputedWatts"]+=($test=$this->query($sql)->fetchColumn())?$test:0;
$sql="SELECT AVG(NULLIF(a.Temperature, 0)) as AvgTemp FROM fac_SensorReadings a, fac_Cabinet b, fac_Device c
WHERE a.DeviceID=c.DeviceID and c.Cabinet=b.CabinetID AND c.BackSide=0 AND
b.DataCenterID=$this->DataCenterID;";
$dcStats["AvgTemp"]=($test=round($this->query($sql)->fetchColumn()))?$test:0;
$sql="SELECT AVG(NULLIF(a.Humidity, 0)) as AvgHumidity FROM fac_SensorReadings a, fac_Cabinet b, fac_Device c
WHERE a.DeviceID=c.DeviceID and c.BackSide=0 and c.Cabinet=b.CabinetID AND
b.DataCenterID=$this->DataCenterID;";
$dcStats["AvgHumidity"]=($test=round($this->query($sql)->fetchColumn()))?$test:0;
$pdu=new PowerDistribution();
$dcStats["MeasuredWatts"]=$pdu->GetWattageByDC($this->DataCenterID);
return $dcStats;
}
function AddDCToTree($lev=0) {
$dept=new Department();
$zone=new Zone();
$classType = "liClosed";
$tree=str_repeat(" ",$lev+1)."<li class=\"$classType\" id=\"dc$this->DataCenterID\"><a class=\"DC\" href=\"dc_stats.php?dc="
."$this->DataCenterID\">$this->Name</a>\n";
$tree.=str_repeat(" ",$lev+2)."<ul>\n";
$zone->DataCenterID=$this->DataCenterID;
$zoneList=$zone->GetZonesByDC();
while(list($zoneNum,$myzone)=each($zoneList)){
$tree.=str_repeat(" ",$lev+3)."<li class=\"liClosed\" id=\"zone$myzone->ZoneID\"><a class=\"ZONE\" href=\"zone_stats.php?zone="
."$myzone->ZoneID\">$myzone->Description</a>\n";
$tree.=str_repeat(" ",$lev+4)."<ul>\n";
//Rows
$sql="SELECT CabRowID, Name AS Fila FROM fac_CabRow WHERE
ZoneID=$myzone->ZoneID ORDER BY Fila;";
foreach($this->query($sql) as $filaRow){
$tree.=str_repeat(" ",$lev+5)."<li class=\"liClosed\">".
"<a class=\"CABROW\" href=\"rowview.php?row={$filaRow['CabRowID']}\">".__("Row ")."{$filaRow['Fila']}</a>\n";
$tree.=str_repeat(" ",$lev+6)."<ul>\n";
// DataCenterID and ZoneID are redundant if fac_cabrow is defined and is CabrowID set in fac_cabinet
$cabsql="SELECT * FROM fac_Cabinet WHERE DataCenterID=$this->DataCenterID
AND ZoneID=$myzone->ZoneID AND CabRowID={$filaRow['CabRowID']} ORDER
BY Location REGEXP '^[A-Za-z]+$', CAST(Location as SIGNED INTEGER),
Location;";
foreach($this->query($cabsql) as $cabRow){
$tree.=str_repeat(" ",$lev+7)."<li id=\"cab{$cabRow['CabinetID']}\"><a class=\"RACK\" href=\"cabnavigator.php?cabinetid={$cabRow['CabinetID']}\">{$cabRow['Location']}</a></li>\n";
}
$tree.=str_repeat(" ",$lev+6)."</ul>\n";
$tree.=str_repeat(" ",$lev+5)."</li>\n";
}
//Cabinets without CabRowID
$cabsql="SELECT * FROM fac_Cabinet WHERE DataCenterID=$this->DataCenterID AND
ZoneID=$myzone->ZoneID AND CabRowID=0 ORDER BY Location ASC;";
foreach($this->query($cabsql) as $cabRow){
$tree.=str_repeat(" ",$lev+5)."<li id=\"cab{$cabRow['CabinetID']}\"><a class=\"RACK\" href=\"cabnavigator.php?cabinetid={$cabRow['CabinetID']}\">{$cabRow['Location']}</a></li>\n";
}
$tree.=str_repeat(" ",$lev+4)."</ul>\n";
$tree.=str_repeat(" ",$lev+3)."</li>\n";
} //zone
//Cabinets without ZoneID
$cabsql="SELECT * FROM fac_Cabinet WHERE DataCenterID=$this->DataCenterID AND
ZoneID=0 ORDER BY Location ASC;";
foreach($this->query($cabsql) as $cabRow){
$tree.=str_repeat(" ",$lev+3)."<li id=\"cab{$cabRow['CabinetID']}\"><a class=\"RACK\" href=\"cabnavigator.php?cabinetid={$cabRow['CabinetID']}\">{$cabRow['Location']}</a></li>\n";
}
//StorageRoom for this DC
$tree.=str_repeat(" ",$lev+3)."<li id=\"sr-$this->DataCenterID\"><a href=\"storageroom.php?dc=$this->DataCenterID\">".__("Storage Room")."</a></li>\n";
$tree.=str_repeat(" ",$lev+2)."</ul>\n";
$tree.=str_repeat(" ",$lev+1)."</li>\n";
return $tree;
}
}
class DeviceTemplate {
var $TemplateID;
var $ManufacturerID;
var $Model;
var $Height;
var $Weight;
var $Wattage;
var $DeviceType;
var $PSCount;
var $NumPorts;
var $Notes;
var $FrontPictureFile;
var $RearPictureFile;
var $ChassisSlots;
var $RearChassisSlots;
var $SNMPVersion;
var $CustomValues;
var $GlobalID;
var $ShareToRepo;
var $KeepLocal;
public function __construct($dtid=false){
if($dtid){
$this->TemplateID=intval($dtid);
}
return $this;
}
function MakeSafe(){
$validDeviceTypes=array('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure','CDU','Sensor');
$validSNMPVersions=array(1,'2c',3);
// Instead of defaulting to v2c for snmp we'll default to whatever the system default is
global $config;
$this->TemplateID=intval($this->TemplateID);
$this->ManufacturerID=intval($this->ManufacturerID);
$this->Model=sanitize($this->Model);
$this->Height=intval($this->Height);
$this->Weight=intval($this->Weight);
$this->Wattage=intval($this->Wattage);
$this->DeviceType=(in_array($this->DeviceType, $validDeviceTypes))?$this->DeviceType:'Server';
$this->PSCount=intval($this->PSCount);
$this->NumPorts=intval($this->NumPorts);
$this->Notes=sanitize($this->Notes,false);
$this->FrontPictureFile=sanitize($this->FrontPictureFile);
$this->RearPictureFile=sanitize($this->RearPictureFile);
$this->ChassisSlots=intval($this->ChassisSlots);
$this->RearChassisSlots=intval($this->RearChassisSlots);
$this->SNMPVersion=(in_array($this->SNMPVersion, $validSNMPVersions))?$this->SNMPVersion:$config->ParameterArray["SNMPVersion"];
$this->GlobalID=intval($this->GlobalID);
$this->ShareToRepo=intval($this->ShareToRepo);
$this->KeepLocal=intval($this->KeepLocal);
}
function MakeDisplay(){
$this->Model=stripslashes($this->Model);
$this->Notes=stripslashes($this->Notes);
$this->FrontPictureFile=stripslashes($this->FrontPictureFile);
$this->RearPictureFile=stripslashes($this->RearPictureFile);
}
static function RowToObject($row,$extendmodel=true){
$Template=new DeviceTemplate();
$Template->TemplateID=$row["TemplateID"];
$Template->ManufacturerID=$row["ManufacturerID"];
$Template->Model=$row["Model"];
$Template->Height=$row["Height"];
$Template->Weight=$row["Weight"];
$Template->Wattage=$row["Wattage"];
$Template->DeviceType=$row["DeviceType"];
$Template->PSCount=$row["PSCount"];
$Template->NumPorts=$row["NumPorts"];
$Template->Notes=$row["Notes"];
$Template->FrontPictureFile=$row["FrontPictureFile"];
$Template->RearPictureFile=$row["RearPictureFile"];
$Template->ChassisSlots=$row["ChassisSlots"];
$Template->RearChassisSlots=$row["RearChassisSlots"];
$Template->SNMPVersion=$row["SNMPVersion"];
$Template->GlobalID = $row["GlobalID"];
$Template->ShareToRepo = $row["ShareToRepo"];
$Template->KeepLocal = $row["KeepLocal"];
$Template->MakeDisplay();
$Template->GetCustomValues();
if($extendmodel){
// Extend our device model
if($Template->DeviceType=="CDU"){
$cdut=new CDUTemplate();
$cdut->TemplateID=$Template->TemplateID;
$cdut->GetTemplate();
foreach($cdut as $prop => $val){
$Template->$prop=$val;
}
}
if($Template->DeviceType=="Sensor"){
$st=new SensorTemplate();
$st->TemplateID=$Template->TemplateID;
$st->GetTemplate();
foreach($st as $prop => $val){
$Template->$prop=$val;
}
}
}
return $Template;
}
function query($sql){
global $dbh;
return $dbh->query($sql);
}
function exec($sql){
global $dbh;
return $dbh->exec($sql);
}
function prepare( $sql ) {
global $dbh;
return $dbh->prepare( $sql );
}
function clearShareFlag() {
$st = $this->prepare( "update fac_DeviceTemplate set ShareToRepo=0 where TemplateID=:TemplateID" );
$st->execute( array( ":TemplateID"=>$this->TemplateID ) );
}
function CreateTemplate(){
global $dbh;
$this->MakeSafe();
$sql="INSERT INTO fac_DeviceTemplate SET ManufacturerID=$this->ManufacturerID,
Model=\"$this->Model\", Height=$this->Height, Weight=$this->Weight,
Wattage=$this->Wattage, DeviceType=\"$this->DeviceType\",
PSCount=$this->PSCount, NumPorts=$this->NumPorts, Notes=\"$this->Notes\",
FrontPictureFile=\"$this->FrontPictureFile\", RearPictureFile=\"$this->RearPictureFile\",
ChassisSlots=$this->ChassisSlots, RearChassisSlots=$this->RearChassisSlots, SNMPVersion=\"$this->SNMPVersion\",
GlobalID=$this->GlobalID, ShareToRepo=$this->ShareToRepo, KeepLocal=$this->KeepLocal;";
if(!$dbh->exec($sql)){
error_log( "SQL Error: " . $sql );
return false;
}else{
$this->TemplateID=$dbh->lastInsertId();
if($this->DeviceType=="CDU"){
// If this is a cdu make the corresponding other hidden template
$cdut=new CDUTemplate();
foreach($cdut as $prop => $val){
if(isset($this->$prop)){
$cdut->$prop=$this->$prop;
}
}
$cdut->CreateTemplate($this->TemplateID);
}
if($this->DeviceType=="Sensor"){
// If this is a sensor make the corresponding other hidden template
$st=new SensorTemplate();
foreach($st as $prop => $val){
if(isset($this->$prop)){
$st->$prop=$this->$prop;
}
}
$st->CreateTemplate($this->TemplateID);
}
(class_exists('LogActions'))?LogActions::LogThis($this):'';
$this->MakeDisplay();
return true;
}
}
function UpdateTemplate(){
$this->MakeSafe();
$sql="UPDATE fac_DeviceTemplate SET ManufacturerID=$this->ManufacturerID,
Model=\"$this->Model\", Height=$this->Height, Weight=$this->Weight,
Wattage=$this->Wattage, DeviceType=\"$this->DeviceType\",
PSCount=$this->PSCount, NumPorts=$this->NumPorts, Notes=\"$this->Notes\",
FrontPictureFile=\"$this->FrontPictureFile\", RearPictureFile=\"$this->RearPictureFile\",
ChassisSlots=$this->ChassisSlots, RearChassisSlots=$this->RearChassisSlots, SNMPVersion=\"$this->SNMPVersion\",
GlobalID=$this->GlobalID, ShareToRepo=$this->ShareToRepo, KeepLocal=$this->KeepLocal
WHERE TemplateID=$this->TemplateID;";
$old=new DeviceTemplate();
$old->TemplateID=$this->TemplateID;
$old->GetTemplateByID();
if($old->DeviceType=="CDU" && $this->DeviceType!=$old->DeviceType){
// Template changed from CDU to something else, clean up the mess
$cdut=new CDUTemplate();
$cdut->TemplateID=$this->TemplateID;
$cdut->DeleteTemplate();
}elseif($this->DeviceType=="CDU" && $this->DeviceType!=$old->DeviceType){
// Template changed to CDU from something else, make the extra stuff
$cdut=new CDUTemplate();
$cdut->Model=$this->Model;
$cdut->ManufacturerID=$this->ManufacturerID;
$cdut->CreateTemplate($this->TemplateID);
}
if($old->DeviceType=="Sensor" && $this->DeviceType!=$old->DeviceType){
// Template changed from Sensor to something else, clean up the mess
$st=new SensorTemplate();
$st->TemplateID=$this->TemplateID;
$st->DeleteTemplate();
}elseif($this->DeviceType=="Sensor" && $this->DeviceType!=$old->DeviceType){
// Template changed to Sensor from something else, make the extra stuff
$st=new SensorTemplate();
$st->Model=$this->Model;
$st->ManufacturerID=$this->ManufacturerID;
$st->CreateTemplate($this->TemplateID);
}
if(!$this->query($sql)){
return false;
}else{
(class_exists('LogActions'))?LogActions::LogThis($this,$old):'';
$this->MakeDisplay();
return true;
}
}
function DeleteTemplate(){
$this->MakeSafe();
// If we're removing the template clean up the children
$this->DeleteSlots();
$this->DeletePorts();
$sql="DELETE FROM fac_DeviceTemplate WHERE TemplateID=$this->TemplateID;";
(class_exists('LogActions'))?LogActions::LogThis($this):'';
return $this->exec($sql);
}
function Search($indexedbyid=false,$loose=false){
$o=new stdClass();
// Store any values that have been added before we make them safe
foreach($this as $prop => $val){
if(isset($val)){
$o->$prop=$val;
}