File: vm-cpu-all.php

package info (click to toggle)
dtc 0.35.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 18,824 kB
  • sloc: php: 50,739; sh: 8,596; makefile: 572; perl: 148; xml: 25
file content (133 lines) | stat: -rw-r--r-- 3,746 bytes parent folder | download
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
<?php

$panel_type = "none";
require_once("../shared/autoSQLconfig.php");
require_once("$dtcshared_path/dtc_lib.php");

if(!isHostnameOrIP($_REQUEST["vps_server_hostname"])){
	die("VPS node name has wrong format: dying.");
}
if(isset($_REQUEST["vps_name"])){
	if(!checkSubdomainFormat($_REQUEST["vps_name"])){
		die("VPS name has wrong format: dying.");
	}
}

if( $_SERVER["SCRIPT_NAME"] != "/dtc/vm-cpu-all.php"){
	require_once("authme.php");
}else{
	checkLoginPass($adm_login,$adm_pass);
	$q = "SELECT * FROM $pro_mysql_vps_table WHERE owner='$adm_login' AND vps_server_hostname='".$_REQUEST["vps_server_hostname"]."' AND vps_xen_name='".$_REQUEST["vps_name"]."'";
	$r = mysql_query($q)or die();
	$n = mysql_num_rows($r);
	if($n != 1){
		die( _("Access not granted line ") .__LINE__. _(" file ") .__FILE__ );
	}
}

// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

$xpoints = 320;
$ypoints = 100;
$vert_label = "CPU usage";

if( isset($_REQUEST["graph"]) ){

	switch($_REQUEST["graph"]){
		case "hour":
			$title = 'Hour graph';
			$steps = 3600;
			break;
		case "day":
			$title = 'Day Graph';
			$steps = 3600*24;
			break;
		case "week":
			$title = 'Week Graph';
			$steps = 3600*24*7;
			break;
		case "month":
			$title = 'Month Graph';
			$steps = 3600*24*31;
			break;
		case "year":
			$title = 'Year Graph';
			$steps = 3600*24*365;
			break;
		default:
			die("Nothing to do here...");
			break;
	}
	$range = - $steps;

	// List all rrd files in the folder
	$all_rrd_files = array();
	$xen_vps_numbers = array();
	if(is_dir("/var/lib/dtc/dtc-xenservers-rrds/".$_REQUEST["vps_server_hostname"])){
		if ($handle = opendir("/var/lib/dtc/dtc-xenservers-rrds/".$_REQUEST["vps_server_hostname"])) {
			while (false !== ($file = readdir($handle))) {
				if(preg_match("/^xen([0-9])([0-9])-cpu.rrd\$/",$file)){
					$all_rrd_files[] = $file;
					$xen_vps_numbers[] = substr($file,3,2);
				}
			}
		}
	}
	sort($all_rrd_files);
	$num_rrd = sizeof($all_rrd_files);

	$filename = tempnam("/tmp","dtc_cpugraph");
	$cmd = "rrdtool graph $filename --imgformat PNG --width $xpoints --height $ypoints --start $range --end now --vertical-label '$vert_label' --title '$title' --lazy --interlaced ";

	$colors = array("000000", "FF0000", "00FF00", "0000FF", "DEDE00", "00FFFF", "FF90FF", "FF8040", "C040A0", "A0A0A0", "40A0A0", "40A0FF", "FFA040");
	$num_cols = sizeof($colors);

	for($i=0;$i<$num_rrd;$i++){
		$rrd = "/var/lib/dtc/dtc-xenservers-rrds/".$_REQUEST["vps_server_hostname"]."/".$all_rrd_files[$i];
		$cmd .= "DEF:cpu$i=$rrd:cpuseconds:AVERAGE ";
		$cmd .= "CDEF:percentcpu$i=cpu$i,1.5,* ";
		$cmd .= "CDEF:percentcpuzero$i=percentcpu$i,DUP,UN,EXC,0,EXC,IF ";
	}
	for($i=0;$i<$num_rrd;$i++){
		if($i == 0){
			$plop = "AREA";
		}else{
			$plop = "STACK";
		}
		$color = $colors[ $i % $num_cols ];
		$cmd .= "$plop:percentcpuzero$i#$color:xen".substr($all_rrd_files[$i],3,2)." ";
		$cmd .= "GPRINT:percentcpu$i:'AVERAGE:CPU\:%02.0lf%%\\j' ";
	}
//	echo $cmd;
	exec($cmd,$output);

	$filesize = filesize($filename);

	if( ($fp = fopen($filename,"rb")) != NULL ){
		header("Content-Type: image/png");
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: $filesize");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: public", false);
		header("Expires: 0");
		while(!feof($fp) && connection_status() == 0){
			print(fread($fp,1024*8));
			flush();
		}
		fclose($fp);
	}
	unlink($filename);
}

?>