PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/debs/ganglia/ganglia-3.1.2/web/functions.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 464 lines | 309 code | 65 blank | 90 comment | 74 complexity | 10d45f12d73dad72a651a67e14074963 MD5 | raw file
  1. <?php
  2. #
  3. # Some common functions for the Ganglia PHP website.
  4. # Assumes the Gmeta XML tree has already been parsed,
  5. # and the global variables $metrics, $clusters, and $hosts
  6. # have been set.
  7. #
  8. #-------------------------------------------------------------------------------
  9. # Allows a form of inheritance for template files.
  10. # If a file does not exist in the chosen template, the
  11. # default is used. Cuts down on code duplication.
  12. function template ($name)
  13. {
  14. global $template_name;
  15. $fn = "./templates/$template_name/$name";
  16. $default = "./templates/default/$name";
  17. if (file_exists($fn)) {
  18. return $fn;
  19. }
  20. else {
  21. return $default;
  22. }
  23. }
  24. #-------------------------------------------------------------------------------
  25. # Creates a hidden input field in a form. Used to save CGI variables.
  26. function hiddenvar ($name, $var)
  27. {
  28. $hidden = "";
  29. if ($var) {
  30. #$url = rawurlencode($var);
  31. $hidden = "<input type=\"hidden\" name=\"$name\" value=\"$var\">\n";
  32. }
  33. return $hidden;
  34. }
  35. #-------------------------------------------------------------------------------
  36. # Gives a readable time string, from a "number of seconds" integer.
  37. # Often used to compute uptime.
  38. function uptime($uptimeS)
  39. {
  40. $uptimeD=intval($uptimeS/86400);
  41. $uptimeS=$uptimeD ? $uptimeS % ($uptimeD*86400) : $uptimeS;
  42. $uptimeH=intval($uptimeS/3600);
  43. $uptimeS=$uptimeH ? $uptimeS % ($uptimeH*3600) : $uptimeS;
  44. $uptimeM=intval($uptimeS/60);
  45. $uptimeS=$uptimeM ? $uptimeS % ($uptimeM*60) : $uptimeS;
  46. $s = ($uptimeD!=1) ? "s" : "";
  47. return sprintf("$uptimeD day$s, %d:%02d:%02d",$uptimeH,$uptimeM,$uptimeS);
  48. }
  49. #-------------------------------------------------------------------------------
  50. # Try to determine a nodes location in the cluster. Attempts to find the
  51. # LOCATION attribute first. Requires the host attribute array from
  52. # $hosts[$cluster][$name], where $name is the hostname.
  53. # Returns [-1,-1,-1] if we could not determine location.
  54. #
  55. function findlocation($attrs)
  56. {
  57. $rack=$rank=$plane=-1;
  58. $loc=$attrs['LOCATION'];
  59. if ($loc) {
  60. sscanf($loc, "%d,%d,%d", $rack, $rank, $plane);
  61. #echo "Found LOCATION: $rack, $rank, $plane.<br>";
  62. }
  63. if ($rack<0 or $rank<0) {
  64. # Try to parse the host name. Assumes a compute-<rack>-<rank>
  65. # naming scheme.
  66. $n=sscanf($attrs['NAME'], "compute-%d-%d", $rack, $rank);
  67. $plane=0;
  68. }
  69. return array($rack,$rank,$plane);
  70. }
  71. #-------------------------------------------------------------------------------
  72. function cluster_sum($name, $metrics)
  73. {
  74. $sum = 0;
  75. foreach ($metrics as $host => $val)
  76. {
  77. if(isset($val[$name]['VAL'])) $sum += $val[$name]['VAL'];
  78. }
  79. return $sum;
  80. }
  81. #-------------------------------------------------------------------------------
  82. function cluster_min($name, $metrics)
  83. {
  84. $min = "";
  85. foreach ($metrics as $host => $val)
  86. {
  87. $v = $val[$name]['VAL'];
  88. if (!is_numeric($min) or $min < $v)
  89. {
  90. $min = $v;
  91. $minhost = $host;
  92. }
  93. }
  94. return array($min, $minhost);
  95. }
  96. #-------------------------------------------------------------------------------
  97. #
  98. # A useful function for giving the correct picture for a given
  99. # load. Scope is "node | cluster | grid". Value is 0 <= v <= 1.
  100. function load_image ($scope, $value)
  101. {
  102. global $load_scale;
  103. $scaled_load = $value / $load_scale;
  104. if ($scaled_load>1.00) {
  105. $image = template("images/${scope}_overloaded.jpg");
  106. }
  107. else if ($scaled_load>=0.75) {
  108. $image = template("images/${scope}_75-100.jpg");
  109. }
  110. else if ($scaled_load >= 0.50) {
  111. $image = template("images/${scope}_50-74.jpg");
  112. }
  113. else if ($scaled_load>=0.25) {
  114. $image = template("images/${scope}_25-49.jpg");
  115. }
  116. else {
  117. $image = template("images/${scope}_0-24.jpg");
  118. }
  119. return $image;
  120. }
  121. #-------------------------------------------------------------------------------
  122. # A similar function that specifies the background color for a graph
  123. # based on load. Quantizes the load figure into 6 sets.
  124. function load_color ($value)
  125. {
  126. global $load_colors;
  127. global $load_scale;
  128. $scaled_load = $value / $load_scale;
  129. if ($scaled_load>1.00) {
  130. $color = $load_colors["100+"];
  131. }
  132. else if ($scaled_load>=0.75) {
  133. $color = $load_colors["75-100"];
  134. }
  135. else if ($scaled_load >= 0.50) {
  136. $color = $load_colors["50-75"];
  137. }
  138. else if ($scaled_load>=0.25) {
  139. $color = $load_colors["25-50"];
  140. }
  141. else if ($scaled_load < 0.0)
  142. $color = $load_colors["down"];
  143. else {
  144. $color = $load_colors["0-25"];
  145. }
  146. return $color;
  147. }
  148. #-------------------------------------------------------------------------------
  149. #
  150. # Just a useful function to print the HTML for
  151. # the load/death of a cluster node
  152. function node_image ($metrics)
  153. {
  154. global $hosts_down;
  155. $cpu_num = $metrics["cpu_num"]['VAL'];
  156. if(!$cpu_num || $cpu_num == 0)
  157. {
  158. $cpu_num = 1;
  159. }
  160. $load_one = $metrics["load_one"]['VAL'];
  161. $value = $load_one / $cpu_num;
  162. # Check if the host is down
  163. # RFM - Added isset() check to eliminate error messages in ssl_error_log
  164. if (isset($hosts_down) and $hosts_down)
  165. $image = template("images/node_dead.jpg");
  166. else
  167. $image = load_image("node", $value);
  168. return $image;
  169. }
  170. #-------------------------------------------------------------------------------
  171. #
  172. # Finds the min/max over a set of metric graphs. Nodes is
  173. # an array keyed by host names.
  174. #
  175. function find_limits($nodes, $metricname)
  176. {
  177. global $metrics, $clustername, $rrds, $rrd_dir, $start, $end;
  178. if (!count($metrics))
  179. return array(0, 0);
  180. $firsthost = key($metrics);
  181. if (array_key_exists($metricname,$metrics[$firsthost])) {
  182. if ($metrics[$firsthost][$metricname]['TYPE'] == "string"
  183. or $metrics[$firsthost][$metricname]['SLOPE'] == "zero")
  184. return array(0,0);
  185. }
  186. else {
  187. return array(0,0);
  188. }
  189. $max=0;
  190. $min=0;
  191. foreach ( $nodes as $host => $value )
  192. {
  193. $out = array();
  194. $rrd_dir = "$rrds/$clustername/$host";
  195. if (file_exists("$rrd_dir/$metricname.rrd")) {
  196. $command = RRDTOOL . " graph '' --start $start --end $end ".
  197. "DEF:limits='$rrd_dir/$metricname.rrd':'sum':AVERAGE ".
  198. "PRINT:limits:MAX:%.2lf ".
  199. "PRINT:limits:MIN:%.2lf";
  200. exec($command, $out);
  201. if(isset($out[1])) {
  202. $thismax = $out[1];
  203. } else {
  204. $thismax = NULL;
  205. }
  206. if (!is_numeric($thismax)) continue;
  207. if ($max < $thismax) $max = $thismax;
  208. $thismin=$out[2];
  209. if (!is_numeric($thismin)) continue;
  210. if ($min > $thismin) $min = $thismin;
  211. #echo "$host: $thismin - $thismax (now $value)<br>\n";
  212. }
  213. }
  214. return array($min, $max);
  215. }
  216. #-------------------------------------------------------------------------------
  217. #
  218. # Generates the colored Node cell HTML. Used in Physical
  219. # view and others. Intended to be used to build a table, output
  220. # begins with "<tr><td>" and ends the same.
  221. function nodebox($hostname, $verbose, $title="", $extrarow="")
  222. {
  223. global $cluster, $clustername, $metrics, $hosts_up, $GHOME;
  224. if (!$title) $title = $hostname;
  225. # Scalar helps choose a load color. The lower it is, the easier to get red.
  226. # The highest level occurs at a load of (loadscalar*10).
  227. $loadscalar=0.2;
  228. # An array of [NAME|VAL|TYPE|UNITS|SOURCE].
  229. $m=$metrics[$hostname];
  230. $up = $hosts_up[$hostname] ? 1 : 0;
  231. # The metrics we need for this node.
  232. # Give memory in Gigabytes. 1GB = 2^20 bytes.
  233. $mem_total_gb = $m['mem_total']['VAL']/1048576;
  234. $load_one=$m['load_one']['VAL'];
  235. $cpu_speed=$m['cpu_speed']['VAL']/1024;
  236. $cpu_num= $m['cpu_num']['VAL'];
  237. #
  238. # The nested tables are to get the formatting. Insane.
  239. # We have three levels of verbosity. At L3 we show
  240. # everything; at L1 we only show name and load.
  241. #
  242. $rowclass = $up ? rowStyle() : "down";
  243. $host_url=rawurlencode($hostname);
  244. $cluster_url=rawurlencode($clustername);
  245. $row1 = "<tr><td class=$rowclass>\n".
  246. "<table width=\"100%\" cellpadding=1 cellspacing=0 border=0><tr>".
  247. "<td><a href=\"$GHOME/?p=$verbose&amp;c=$cluster_url&amp;h=$host_url\">".
  248. "$title</a>&nbsp;<br>\n";
  249. $cpus = $cpu_num > 1 ? "($cpu_num)" : "";
  250. if ($up)
  251. $hardware =
  252. sprintf("<em>cpu: </em>%.2f<small>G</small> %s ", $cpu_speed, $cpus) .
  253. sprintf("<em>mem: </em>%.2f<small>G</small>",$mem_total_gb);
  254. else $hardware = "&nbsp;";
  255. $row2 = "<tr><td colspan=2>";
  256. if ($verbose==2)
  257. $row2 .= $hardware;
  258. else if ($verbose > 2) {
  259. $hostattrs = $up ? $hosts_up : $hosts_down;
  260. $last_heartbeat = $hostattrs[$hostname]['TN'];
  261. $age = $last_heartbeat > 3600 ? uptime($last_heartbeat) :
  262. "${last_heartbeat}s";
  263. $row2 .= "<font size=-2>Last heartbeat $age</font>";
  264. $row3 = $hardware;
  265. }
  266. #
  267. # Load box.
  268. #
  269. if (!$cpu_num) $cpu_num=1;
  270. $loadindex = intval($load_one / ($loadscalar*$cpu_num)) + 1;
  271. # 10 is currently the highest allowed load index.
  272. $load_class = $loadindex > 10 ? "L10" : "L$loadindex";
  273. $row1 .= "</td><td align=right valign=top>".
  274. "<table cellspacing=1 cellpadding=3 border=0><tr>".
  275. "<td class=$load_class align=right><small>$load_one</small>".
  276. "</td></tr></table>".
  277. "</td></tr>\n";
  278. # Construct cell.
  279. $cell = $row1;
  280. if ($extrarow)
  281. $cell .= $extrarow;
  282. if ($verbose>1)
  283. $cell .= $row2;
  284. $cell .= "</td></tr></table>\n";
  285. # Tricky.
  286. if ($verbose>2)
  287. $cell .= $row3;
  288. $cell .= "</td></tr>\n";
  289. return $cell;
  290. }
  291. #-------------------------------------------------------------------------------
  292. # Alternate between even and odd row styles.
  293. function rowstyle()
  294. {
  295. static $style;
  296. if ($style == "even") { $style = "odd"; }
  297. else { $style = "even"; }
  298. return $style;
  299. }
  300. #-------------------------------------------------------------------------------
  301. # Organize hosts by rack locations.
  302. # Works with or without "location" host attributes.
  303. function physical_racks()
  304. {
  305. global $hosts_up, $hosts_down;
  306. # 2Key = "Rack ID / Rank (order in rack)" = [hostname, UP|DOWN]
  307. $rack = NULL;
  308. # If we don't know a node's location, it goes in a negative ID rack.
  309. $i=1;
  310. $unknownID= -1;
  311. if (is_array($hosts_up)) {
  312. foreach ($hosts_up as $host=>$v) {
  313. # Try to find the node's location in the cluster.
  314. list($rack, $rank, $plane) = findlocation($v);
  315. if ($rack>=0 and $rank>=0 and $plane>=0) {
  316. $racks[$rack][]=$v['NAME'];
  317. continue;
  318. }
  319. else {
  320. $i++;
  321. if (! ($i % 25)) {
  322. $unknownID--;
  323. }
  324. $racks[$unknownID][] = $v['NAME'];
  325. }
  326. }
  327. }
  328. if (is_array($hosts_down)) {
  329. foreach ($hosts_down as $host=>$v) {
  330. list($rack, $rank, $plane) = findlocation($v);
  331. if ($rack>=0 and $rank>=0 and $plane>=0) {
  332. $racks[$rack][]=$v['NAME'];
  333. continue;
  334. }
  335. else {
  336. $i++;
  337. if (! ($i % 25)) {
  338. $unknownID--;
  339. }
  340. $racks[$unknownID][] = $v['NAME'];
  341. }
  342. }
  343. }
  344. # Sort the racks array.
  345. if ($unknownID<-1) { krsort($racks); }
  346. else {
  347. ksort($racks);
  348. reset($racks);
  349. while (list($rack,) = each($racks)) {
  350. # In our convention, y=0 is close to the floor. (Easier to wire up)
  351. krsort($racks[$rack]);
  352. }
  353. }
  354. return $racks;
  355. }
  356. #-------------------------------------------------------------------------------
  357. # Return a version of the string which is safe for display on a web page.
  358. # Potentially dangerous characters are converted to HTML entities.
  359. # Resulting string is not URL-encoded.
  360. function clean_string( $string )
  361. {
  362. return htmlentities( $string );
  363. }
  364. #-------------------------------------------------------------------------------
  365. function sanitize ( $string ) {
  366. return escapeshellcmd( clean_string( rawurldecode( $string ) ) ) ;
  367. }
  368. #-------------------------------------------------------------------------------
  369. # If arg is a valid number, return it. Otherwise, return null.
  370. function clean_number( $value )
  371. {
  372. return is_numeric( $value ) ? $value : null;
  373. }
  374. #-------------------------------------------------------------------------------
  375. # Return true if string is a 3 or 6 character hex color. Return false otherwise.
  376. function is_valid_hex_color( $string )
  377. {
  378. $return_value = false;
  379. if( strlen( $string ) == 6 || strlen( $string ) == 3 ) {
  380. if( preg_match( '/^[0-9a-fA-F]+$/', $string ) ) {
  381. $return_value = true;
  382. }
  383. }
  384. return $return_value;
  385. }
  386. #-------------------------------------------------------------------------------
  387. # Return a shortened version of a FQDN
  388. # if "hostname" is numeric only, assume it is an IP instead
  389. #
  390. function strip_domainname( $hostname ) {
  391. $postition = strpos($hostname, '.');
  392. $name = substr( $hostname , 0, $postition );
  393. if ( FALSE === $postition || is_numeric($name) ) {
  394. return $hostname;
  395. } else {
  396. return $name;
  397. }
  398. }
  399. ?>