aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc')
-rw-r--r--inc/collectd.inc.php176
-rw-r--r--inc/html.inc.php45
-rw-r--r--inc/rrdtool.class.php25
3 files changed, 246 insertions, 0 deletions
diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php
new file mode 100644
index 0000000..2724c05
--- /dev/null
+++ b/inc/collectd.inc.php
@@ -0,0 +1,176 @@
1<?php
2
3require_once 'conf/common.inc.php';
4
5# returns an array of all collectd hosts
6function collectd_hosts() {
7 global $CONFIG;
8
9 if (!is_dir($CONFIG['datadir']))
10 return false;
11
12 $dir = scandir($CONFIG['datadir']);
13 foreach($dir as $k => $v) {
14 if(!is_dir($CONFIG['datadir'].'/'.$v) || $v == '.' || $v == '..')
15 unset($dir[$k]);
16 }
17 return($dir);
18}
19
20# returns an array of plugins/pinstances/types/tinstances
21function collectd_plugindata($host) {
22 global $CONFIG;
23
24 if (!is_dir($CONFIG['datadir'].'/'.$host))
25 return false;
26
27 chdir($CONFIG['datadir'].'/'.$host);
28 $files = glob("*/*.rrd");
29 if (!$files)
30 return false;
31
32 $data;
33 $i = 0;
34 foreach($files as $item) {
35 unset($part);
36
37 # split item by plugin/type
38 $part = explode('/', $item);
39 $part[1] = preg_replace('/\.rrd/', '', $part[1]);
40
41 # plugin
42 $data[$i]['p'] = preg_replace('/-.+/', '', $part[0]);
43
44 # plugin instance
45 if(preg_match('/-/', $part[0]))
46 $data[$i]['pi'] = preg_replace('/^[a-z_]+\-/', '', $part[0]);
47
48 # type
49 $data[$i]['t'] = preg_replace('/-.+/', '', $part[1]);
50
51 # type instance
52 if(preg_match('/-/', $part[1]))
53 $data[$i]['ti'] = preg_replace('/^[a-z_]+\-/', '', $part[1]);
54
55 $i++;
56 }
57 return($data);
58}
59
60# returns an array of all plugins of a host
61function collectd_plugins($host) {
62 $plugindata = collectd_plugindata($host);
63
64 $plugins = array();
65 foreach ($plugindata as $item) {
66 if (!in_array($item['p'], $plugins))
67 $plugins[] = $item['p'];
68 }
69
70 return $plugins;
71}
72
73# returns an array of all pi/t/ti of an plugin
74function collectd_plugindetail($host, $plugin, $detail, $where=NULL) {
75 $details = array('pi', 't', 'ti');
76 if (!in_array($detail, $details))
77 return false;
78
79 $plugindata = collectd_plugindata($host);
80
81 $return = array();
82 foreach ($plugindata as $item) {
83 if ($item['p'] == $plugin && !in_array($item[$detail], $return) && isset($item[$detail])) {
84 if ($where) {
85 $add = true;
86 # add detail to returnvalue if all where is true
87 foreach($where as $key => $value) {
88 if ($item[$key] != $value)
89 $add = false;
90 }
91 if ($add)
92 $return[] = $item[$detail];
93 } else {
94 $return[] = $item[$detail];
95 }
96 }
97 }
98
99 if (empty($return))
100 return false;
101
102 return $return;
103}
104
105# generate graph url's for a plugin of a host
106function graphs_from_plugin($host, $plugin) {
107 global $CONFIG;
108
109 $pis = collectd_plugindetail($host, $plugin, 'pi');
110 $ts = collectd_plugindetail($host, $plugin, 't');
111 $tis = collectd_plugindetail($host, $plugin, 'ti');
112 if (!$pis) $pis = array('NULL');
113 if (!$tis) $tis = array('NULL');
114
115 foreach($pis as $pi) {
116 if ($CONFIG['groupby'][$plugin] == 'type') {
117 foreach ($ts as $t) {
118 $items = array(
119 'h' => $host,
120 'p' => $plugin,
121 'pi' => $pi,
122 't' => $t
123 );
124 printf('<a href="%s/%s"><img src="%s/%s"></a>'."\n",
125 $CONFIG['weburl'],
126 build_url('detail.php', $items).'&x=800&y=350',
127 $CONFIG['weburl'],
128 build_url('graph.php', $items)
129 );
130 }
131 } else {
132 foreach ($tis as $ti) {
133 foreach ($ts as $t) {
134 $items = array(
135 'h' => $host,
136 'p' => $plugin,
137 'pi' => $pi,
138 't' => $t,
139 'ti' => $ti
140 );
141 printf('<a href="%s/%s"><img src="%s/%s"></a>'."\n",
142 $CONFIG['weburl'],
143 build_url('detail.php', $items).'&x=800&y=350',
144 $CONFIG['weburl'],
145 build_url('graph.php', $items)
146 );
147 }
148 }
149 }
150 }
151}
152
153# generate an url with GET values from $items
154function build_url($base, $items, $s=86400) {
155 if (!is_array($items))
156 return false;
157
158 if (!is_numeric($s))
159 return false;
160
161 $i=0;
162 foreach ($items as $key => $value) {
163 # don't include empty values
164 if ($value == 'NULL')
165 continue;
166
167 $base .= sprintf('%s%s=%s', $i==0 ? '?' : '&', $key, $value);
168 $i++;
169 }
170 if (!isset($items['s']))
171 $base .= '&s='.$s;
172
173 return $base;
174}
175
176?>
diff --git a/inc/html.inc.php b/inc/html.inc.php
new file mode 100644
index 0000000..d1d2a65
--- /dev/null
+++ b/inc/html.inc.php
@@ -0,0 +1,45 @@
1<?php
2
3function html_start() {
4 echo <<<EOT
5<!DOCTYPE HTML>
6<html>
7<head>
8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
9 <title>Collectd Graph Panel</title>
10 <link rel="stylesheet" href="layout/style.css" type="text/css">
11</head>
12<body>
13
14EOT;
15}
16
17function html_end() {
18 echo <<<EOT
19</body>
20</html>
21EOT;
22}
23
24require_once 'conf/common.inc.php';
25require_once 'inc/rrdtool.class.php';
26function host_summary($hosts) {
27 global $CONFIG;
28
29 $rrd = new RRDTool;
30
31 echo "<table class=\"summary\">\n";
32
33 foreach($hosts as $host) {
34 $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd');
35 if (!$rrd_info)
36 continue;
37 printf('<tr><th><a href="%s/host.php?h=%s">%s</a></th><td>%.2f</td><td>%.2f</td><td>%.2f</td></tr>'."\n",
38 $CONFIG['weburl'],$host, $host,
39 $rrd_info["ds[shortterm].last_ds"], $rrd_info["ds[midterm].last_ds"], $rrd_info["ds[longterm].last_ds"]);
40 }
41
42 echo "</table>\n";
43}
44
45?>
diff --git a/inc/rrdtool.class.php b/inc/rrdtool.class.php
new file mode 100644
index 0000000..e1a7b4b
--- /dev/null
+++ b/inc/rrdtool.class.php
@@ -0,0 +1,25 @@
1<?php
2
3class RRDTool {
4 var $width = 175;
5 var $height = 125;
6
7 function rrd_info($rrdfile) {
8 if (file_exists($rrdfile)) {
9 $raw_info = shell_exec('/usr/bin/rrdtool info '.$rrdfile);
10 $raw_array = explode("\n", $raw_info);
11 foreach ($raw_array as $key => $info) {
12 if ($info != "") {
13 $item_info = explode(" = ", $info);
14 $item_info[1] = preg_replace('/"/', '', $item_info[1]);
15 $info_array[$item_info[0]] = $item_info[1];
16 }
17 }
18 return($info_array);
19 } else {
20 return false;
21 }
22 }
23}
24
25?>