Import the IP history plugin from the demo server.
[racktables-contribs] / wattage_consumption.php
CommitLineData
fba27978
CB
1<?php
2
3//-----------------------------------------------------------------------------------
4// Wattage Consumption - written by curtisb
b2fa3593
MW
5//
6// Updated for Racktables-0.20.4 by Mark Wilkinson
7// bonus of Rack Wattage portlet in Rack Page if portlet patch applied (bugs.racktables.org ID 691)
8//
9//
10// Usage - Create attribute 'Wattage consumption' and map to Servers, Chassis, etc
11// Set power values for servers,etc.
12// View Report page (or Rack page)
13//
fba27978
CB
14$tab['reports']['watts_per_row'] = 'Enviromental Totals';
15$tabhandler['reports']['watts_per_row'] = 'getWattsPerRow';
b2fa3593 16if ( function_exists('registerPortletHandler') )
b2fa3593 17 registerPortletHandler( 'rack', 'default', 'left', 'Wattage Consumption', 'renderPortletWattConsumption');
fba27978
CB
18
19function getWattsPerRow ()
20{
b62db262
DO
21 // assertions
22 // find the needed attributes
fba27978 23
b62db262 24 global $nextorder;
fba27978 25
b62db262
DO
26 // Was this function called with a specific row_id?
27 if (isset ($_REQUEST['row_id']))
28 {
29 assertStringArg ('row_id');
30 $row_toshow = $_REQUEST['row_id'];
31 }
32 else
b2fa3593 33 $row_toshow = -1;
fba27978 34
b62db262
DO
35 //from renderRackspace(), interface.php:151
36 $found_racks = array();
37 $rows = array();
38 $cellfilter = getCellFilter();
39 $rackCount = 0;
40 $order = 'odd';
41 // get rackspace information
b2fa3593
MW
42 foreach (getAllRows() as $row_id => $rowInfo)
43 {
b62db262 44 $rackList = filterCellList (listCells ('rack', $row_id), $cellfilter['expression']);
b2fa3593 45 $found_racks = array_merge ($found_racks, $rackList);
bcbc8921
DO
46 $rows[] = array
47 (
b2fa3593
MW
48 'location_id' => $rowInfo['location_id'],
49 'location_name' => $rowInfo['location_name'],
b62db262 50 'row_id' => $row_id,
b2fa3593 51 'row_name' => $rowInfo['name'],
b62db262
DO
52 'racks' => $rackList
53 );
bcbc8921 54 $rackCount += count ($rackList);
b62db262 55 }
fba27978 56
b62db262
DO
57 // Main layout starts.
58 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
fba27978 59
b62db262 60 // Left portlet with list of rows.
b2fa3593 61 echo "<tr><td class=pcleft width='50%'>";
b62db262
DO
62 startPortlet ('Rack Rows (' . count ($rows) . ')');
63 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
64 foreach ($rows as $row)
65 {
66 $row_id = $row['row_id'];
67 $row_name = $row['row_name'];
b2fa3593 68 $row_location = $row['location_name'];
b62db262 69 $rackList = $row['racks'];
fba27978 70
b62db262 71 echo "<tr class=row_${order}><td width='20%'></td><td class=tdleft>";
bcbc8921 72 if (! count ($rackList))
b2fa3593 73 echo "${row_location} - ${row_name} (empty row)";
b62db262 74 else
bcbc8921 75 echo "<a href='" . makeHref (array ('page' => 'reports', 'tab' => 'watts_per_row', 'row_id' => $row_id)) . "'>${row_location} - ${row_name}</a>";
b62db262
DO
76 echo "<td><tr>\n";
77 $order = $nextorder[$order];
78 }
fba27978 79
b62db262
DO
80 echo "</td></tr>\n";
81 echo "</table><br>\n";
82 finishPortlet();
fba27978 83
b62db262 84 echo "</td><td class=pcright>";
fba27978 85
b62db262 86 // Right Portlet: Draw the racks in the selected row
bcbc8921 87 if ($row_toshow > -1)
b62db262 88 {
b2fa3593
MW
89 $rowInfo = getRowInfo ($row_toshow);
90 $cellfilter = getCellFilter();
91 $rackList = filterCellList (listCells ('rack', $row_toshow), $cellfilter['expression']);
92
b2fa3593
MW
93 $rackwidth = getRackImageWidth() * getConfigVar ('ROW_SCALE');
94 // Maximum number of racks per row is proportionally less, but at least 1.
95 $maxPerRow = max (floor (getConfigVar ('RACKS_PER_ROW') / getConfigVar ('ROW_SCALE')), 1);
96 $rackListIdx = 0;
97 $rowTotalWattage = 0;
98 $order = 'odd';
bcbc8921 99 startPortlet ('Racks within ' . $rowInfo['name'] . ' (' . count($rackList) . ')' );
b2fa3593
MW
100 echo "<table border=0 cellspacing=5 align='center'><tr>";
101 foreach ($rackList as $rack)
b62db262 102 {
b2fa3593
MW
103 $rackTotalWattage = 0;
104 // see renderRack(), interface.php:311
105 $rackData = spotEntity ('rack', $rack['id']);
106 amplifyCell ($rackData);
107 $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
108 foreach ($rackData['mountedObjects'] as $object)
b62db262 109 {
b2fa3593
MW
110 $objectData = spotEntity ('object', $object);
111 amplifyCell ($objectData);
112 foreach (getAttrValues ($objectData['id']) as $record)
b2fa3593 113 if ($record['name'] == 'Wattage consumption')
b2fa3593 114 $rackTotalWattage += $record['value'];
b62db262 115 }
b2fa3593 116 if ($rackListIdx % $maxPerRow == 0)
bcbc8921
DO
117 echo $rackListIdx > 0 ? '</tr><tr>' : '<tr>';
118 echo "<td align=center class=row_${order}><a href='" . makeHref (array( 'page' => 'rack', 'rack_id' => $rack['id'])) . "'>";
119
b2fa3593
MW
120 echo "<img border=0 width=${rackwidth} height=" . (getRackImageHeight ($rack['height']) * getConfigVar ('ROW_SCALE'));
121 echo " title='${rack['height']} units'";
122 echo "src='?module=image&img=minirack&rack_id=${rack['id']}'>";
123 echo "<br>${rack['name']} ($rackTotalWattage)</a></td>";
124 $order = $nextorder[$order];
125 $rackListIdx++;
126 $rowTotalWattage += $rackTotalWattage;
b62db262 127 }
b2fa3593
MW
128
129 echo "</tr><tr><td align=center colspan=";
bcbc8921 130 print (count ($rackList));
b2fa3593
MW
131 echo "><br><b>The row total for attribute Wattage consuption is: $rowTotalWattage</b></td>\n";
132
133 echo "</tr></table>\n";
134 finishPortlet();
135 }
136 echo "</td></tr></table>";
137}
138
bcbc8921 139function renderPortletWattConsumption ($info)
b2fa3593
MW
140{
141 $rackTotalWattage = 0;
142 $rackData = spotEntity ('rack', $info['id']);
143 amplifyCell ($rackData);
144 $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
145 foreach ($rackData['mountedObjects'] as $object)
146 {
147 $objectData = spotEntity ('object', $object);
148 amplifyCell ($objectData);
149 foreach (getAttrValues ($objectData['id']) as $record)
b2fa3593 150 if ($record['name'] == 'Wattage consumption')
b2fa3593 151 $rackTotalWattage += $record['value'];
b62db262 152 }
b2fa3593
MW
153 startPortlet ('Wattage Consumption' );
154 echo "<table border=0 cellspacing=5 align='center'><tr>";
155 echo "<td>The total for attribute Wattage consuption is: <b>$rackTotalWattage</b></td>\n";
b62db262
DO
156 echo "</tr></table>\n";
157 finishPortlet();
fba27978
CB
158}
159
160?>