diff options
author | Pim van den Berg | 2009-09-20 13:36:25 +0200 |
---|---|---|
committer | Pim van den Berg | 2009-09-20 13:45:26 +0200 |
commit | cd94ef34134d8f06af83ec97119018b4e80346a0 (patch) | |
tree | b01a57617cb5528554b1909583bdf110d8acb631 /inc/collectd.inc.php | |
download | apt-panopticon_cgp-cd94ef34134d8f06af83ec97119018b4e80346a0.zip apt-panopticon_cgp-cd94ef34134d8f06af83ec97119018b4e80346a0.tar.gz apt-panopticon_cgp-cd94ef34134d8f06af83ec97119018b4e80346a0.tar.bz2 apt-panopticon_cgp-cd94ef34134d8f06af83ec97119018b4e80346a0.tar.xz |
initial import of cgp
Collectd Graph Panel is a frontend for Collectd written in PHP.
The goal of CGP is to provide an easy-to-use frontend for Collectd,
starting with page that shows an overview of all the hosts you are
managing with Collectd.
In this initial import there is support for the plugins that are default
enabled in Collectd. The supported plugins are located in the plugin
directory.
Diffstat (limited to 'inc/collectd.inc.php')
-rw-r--r-- | inc/collectd.inc.php | 176 |
1 files changed, 176 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 | |||
3 | require_once 'conf/common.inc.php'; | ||
4 | |||
5 | # returns an array of all collectd hosts | ||
6 | function 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 | ||
21 | function 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 | ||
61 | function 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 | ||
74 | function 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 | ||
106 | function 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 | ||
154 | function 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 | ?> | ||