aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc')
-rw-r--r--inc/collectd.inc.php81
-rw-r--r--inc/html.inc.php2
2 files changed, 83 insertions, 0 deletions
diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php
index 525b931..3ef9102 100644
--- a/inc/collectd.inc.php
+++ b/inc/collectd.inc.php
@@ -157,4 +157,85 @@ function build_url($base, $items, $s=86400) {
157 return $base; 157 return $base;
158} 158}
159 159
160# generate identifier that collectd's FLUSH command understands
161function collectd_identifier($host, $plugin, $pinst, $type, $tinst) {
162 global $CONFIG;
163
164 $identifier = sprintf('%s/%s%s%s/%s%s%s', $host,
165 $plugin, strlen($pinst) ? '-' : '', $pinst,
166 $type, strlen($tinst) ? '-' : '', $tinst);
167
168 if (is_file($CONFIG['datadir'].'/'.$identifier.'.rrd'))
169 return $identifier;
170 else
171 return FALSE;
172}
173
174# tell collectd to FLUSH all data of the identifier(s)
175function collectd_flush($identifier) {
176 global $CONFIG;
177
178 if (!$CONFIG['socket'])
179 return FALSE;
180
181 if (!$identifier || (is_array($identifier) && count($identifier) == 0) ||
182 !(is_string($identifier) || is_array($identifier)))
183 return FALSE;
184
185 $u_errno = 0;
186 $u_errmsg = '';
187 if ($socket = @fsockopen($CONFIG['socket'], 0, $u_errno, $u_errmsg)) {
188 $cmd = 'FLUSH plugin=rrdtool';
189 if (is_array($identifier)) {
190 foreach ($identifier as $val)
191 $cmd .= sprintf(' identifier="%s"', $val);
192 } else
193 $cmd .= sprintf(' identifier="%s"', $identifier);
194 $cmd .= "\n";
195
196 $r = fwrite($socket, $cmd, strlen($cmd));
197 if ($r === false || $r != strlen($cmd)) {
198 printf('ERROR: Failed to write full command to unix-socket: %d out of %d written',
199 $r === false ? -1 : $r, strlen($cmd));
200 return FALSE;
201 }
202
203 $resp = fgets($socket);
204 if ($resp === false) {
205 printf('ERROR: Failed to read response from collectd for command: %s',
206 trim($cmd));
207 return FALSE;
208 }
209
210 $n = (int)$resp;
211 while ($n-- > 0)
212 fgets($socket);
213
214 fclose($socket);
215
216 return TRUE;
217 } else {
218 printf('ERROR: Failed to open unix-socket to collectd: %d: %s',
219 $u_errno, $u_errmsg);
220 return FALSE;
221 }
222}
223
224# generate identifiers from args
225function ident_from_args($args) {
226 if (is_array($args['tinstance'])) {
227 foreach($args['tinstance'] as $ti) {
228 $instances[] = collectd_identifier($args['host'],
229 $args['plugin'], $args['pinstance'],
230 $args['type'], $ti);
231 }
232 } else {
233 $instances[] = collectd_identifier($args['host'],
234 $args['plugin'], $args['pinstance'],
235 $args['type'], $args['tinstance']);
236 }
237
238 return $instances;
239}
240
160?> 241?>
diff --git a/inc/html.inc.php b/inc/html.inc.php
index cbe3ad2..09da518 100644
--- a/inc/html.inc.php
+++ b/inc/html.inc.php
@@ -2,6 +2,7 @@
2 2
3require_once 'conf/common.inc.php'; 3require_once 'conf/common.inc.php';
4require_once 'inc/rrdtool.class.php'; 4require_once 'inc/rrdtool.class.php';
5require_once 'inc/collectd.inc.php';
5 6
6function html_start() { 7function html_start() {
7 global $CONFIG; 8 global $CONFIG;
@@ -46,6 +47,7 @@ function host_summary($hosts) {
46 echo "<table class=\"summary\">\n"; 47 echo "<table class=\"summary\">\n";
47 48
48 foreach($hosts as $host) { 49 foreach($hosts as $host) {
50 collectd_flush(sprintf('%s/load/load', $host));
49 $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); 51 $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd');
50 if (!$rrd_info) 52 if (!$rrd_info)
51 continue; 53 continue;