1 | <?php |
---|
2 | /* |
---|
3 | (c)2006 Joe Presbrey <presbrey@mit.edu> |
---|
4 | inspired by parts of nagiosgraph in Perl |
---|
5 | */ |
---|
6 | |
---|
7 | function hashcolor($x) { |
---|
8 | $x .= 'x'; $c=1; |
---|
9 | for($i = 0; $i < strlen($x); $i++) { $c=(51*$c+ord($x{$i}))%216; } |
---|
10 | $h = array(51*floor($c/36), 51*floor($c/6%6), 51*($c%6)); |
---|
11 | $i = $n = $m = 0; |
---|
12 | for($i = 0; $i <= 2; $i++) { |
---|
13 | if ($h[$i] < $h[$m]) $m = $i; |
---|
14 | if ($h[$i] > $h[$n]) $n = $i; |
---|
15 | } |
---|
16 | if ($h[$m]>102) $h[$m] = 102; |
---|
17 | if ($h[$n]<153) $h[$n] = 153; |
---|
18 | $n = ($h[2])+($h[1]*256)+$h[0]*256*256; |
---|
19 | $c = sprintf("%06X", ($h[2])+($h[1]*256)+$h[0]*256*256); |
---|
20 | return $c; |
---|
21 | } |
---|
22 | |
---|
23 | function findRRD($host, $service) { |
---|
24 | if (isset($GLOBALS['RRD_PATH'])) { |
---|
25 | $rrd = $GLOBALS['RRD_PATH']; |
---|
26 | } else { |
---|
27 | $rrd = dirname(__FILE__); |
---|
28 | } |
---|
29 | $f = glob("$rrd/{$host}_{$service}_*.rrd"); |
---|
30 | if (count($f)) { |
---|
31 | $o = array_shift($f); |
---|
32 | } else { |
---|
33 | $host = str_replace('-','%2D',rawurlencode($host)); |
---|
34 | $service = str_replace('-','%2D',rawurlencode($service)); |
---|
35 | $f = glob("$rrd/{$host}_{$service}_*.rrd"); |
---|
36 | if (count($f)) { |
---|
37 | $o = array_shift($f); |
---|
38 | } else { |
---|
39 | $f = glob("$rrd/{$host}_{$service}*.rrd"); |
---|
40 | if (count($f)) |
---|
41 | $o = array_shift($f); |
---|
42 | } |
---|
43 | } |
---|
44 | $p = realpath($o); |
---|
45 | if (strlen($p)>strlen($host)+strlen($service)) { |
---|
46 | if (preg_match_all('/([^_]+)_([^_]+)_(.+).rrd/iU', basename($p), $m)) { |
---|
47 | return array($p, $m[1][0], $m[2][0], $m[3][0]); |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | function graphInfo($file) { |
---|
53 | $rrdinfo = `rrdtool info $file`; |
---|
54 | preg_match_all('/ds\[([^\]]*)\]\./',$rrdinfo,$ds); |
---|
55 | $lines = array_unique($ds[1]); |
---|
56 | //sort($lines); |
---|
57 | return $lines; |
---|
58 | } |
---|
59 | |
---|
60 | function makeDefs($file, $ignores=array(), $oneHost=true) { |
---|
61 | $info = graphInfo($file[0]); |
---|
62 | $defs = array(); |
---|
63 | $def = 'DEF:$dj=$file:$di:AVERAGE' . |
---|
64 | ' LINE2:$dj#$c:$dj' . |
---|
65 | ' GPRINT:$dj:MAX:Max\\\\:\\ %6.2lf%s' . |
---|
66 | ' GPRINT:$dj:AVERAGE:Avg\\\\:\\ %6.2lf%s' . |
---|
67 | ' GPRINT:$dj:MIN:Min\\\\:\\ %6.2lf%s' . |
---|
68 | ' GPRINT:$dj:LAST:Cur\\\\:\\ %6.2lf%s\\\\n'; |
---|
69 | foreach($info as $sv) { |
---|
70 | if (in_array(strtolower($sv), $ignores)) continue; |
---|
71 | $d = str_replace('$di',$sv,$def); |
---|
72 | if ($oneHost) { |
---|
73 | $d = str_replace('$dj',$sv,$d); |
---|
74 | $d = str_replace('$c',hashcolor($sv),$d); |
---|
75 | } else { |
---|
76 | $d = str_replace('$dj',urldecode($file[1]).'_'.$sv,$d); |
---|
77 | $d = str_replace('$c',hashcolor(md5($file[0].$sv)),$d); |
---|
78 | } |
---|
79 | $d = str_replace('$file',$file[0],$d); |
---|
80 | $defs[] = $d; |
---|
81 | } |
---|
82 | return implode(' ',$defs); |
---|
83 | } |
---|
84 | |
---|
85 | function outputGraph($hosts, $service, $time, $opts = array()) { |
---|
86 | if (!is_array($hosts)) $hosts = array($hosts); |
---|
87 | $oneHost = count($hosts)<=1; |
---|
88 | // if (!is_array($services)) $services = array($services); |
---|
89 | $defs = array(); |
---|
90 | $args = array(); |
---|
91 | $files = array(); |
---|
92 | foreach($hosts as $host) { |
---|
93 | $file = findRRD($host, $service); |
---|
94 | if (is_array($file) && strlen($file[0])) $files[] = $file; |
---|
95 | } |
---|
96 | foreach($files as $file) { |
---|
97 | if (isset($GLOBALS['RRD_IGNORE']) |
---|
98 | && isset($GLOBALS['RRD_IGNORE'][strtolower($service)])) { |
---|
99 | $def = makeDefs($file, $GLOBALS['RRD_IGNORE'][strtolower($service)], $oneHost); |
---|
100 | } else { |
---|
101 | $def = makeDefs($file, array(), $oneHost); |
---|
102 | } |
---|
103 | if (strlen($def)) $defs[] = $def; |
---|
104 | } |
---|
105 | |
---|
106 | if (count($opts)) |
---|
107 | extract($opts); |
---|
108 | if (isset($geom)) { |
---|
109 | if (isset($geom[0])) |
---|
110 | $args[] = '-w '.$geom[0]; |
---|
111 | if (isset($geom[1])) |
---|
112 | $args[] = '-h '.$geom[1]; |
---|
113 | } |
---|
114 | if (isset($legend) && !$legend) { |
---|
115 | $args[] = '-g'; |
---|
116 | } |
---|
117 | if (isset($title)) { |
---|
118 | if (count($files)) { |
---|
119 | list($fhost, $fservice, $fdb) = array_slice(explode('_',basename($files[0][0])),0,3); |
---|
120 | if ($oneHost) { |
---|
121 | $title = str_replace('%h', urldecode($fhost), $title); |
---|
122 | $title = str_replace('%s', urldecode($fservice), $title); |
---|
123 | } else { |
---|
124 | $title = str_replace('%h', implode(',',$hosts), $title); |
---|
125 | $title = str_replace('%s', urldecode($service), $title); |
---|
126 | } |
---|
127 | } |
---|
128 | $title = escapeshellarg($title); |
---|
129 | if (strlen($title)) $args[] = "-v$title"; |
---|
130 | } |
---|
131 | |
---|
132 | if (count($defs)) { |
---|
133 | $defs = implode(' ', $defs); |
---|
134 | if (count($args)) |
---|
135 | $argstr = implode(' ', $args); |
---|
136 | $cmd = "rrdtool graph - -a PNG --start -$time $defs $argstr"; |
---|
137 | $data = `$cmd`; |
---|
138 | if (strlen($data)>0) { |
---|
139 | header('Content-Type: image/png'); |
---|
140 | echo $data; |
---|
141 | exit; |
---|
142 | } else { |
---|
143 | echo "failed: $cmd"; |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | //outputGraph('better-mousetrap', 'DISK: /', 192000); |
---|