aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPim van den Berg2009-11-04 23:04:30 +0100
committerPim van den Berg2009-11-04 23:04:30 +0100
commit488b4f3ea230b0bc3cb7fa7e5264691e21067cd0 (patch)
tree67944684b708f9e2998eeeb0200e608f1f451a36
parentremove path_format from plugins (diff)
downloadapt-panopticon_cgp-488b4f3ea230b0bc3cb7fa7e5264691e21067cd0.zip
apt-panopticon_cgp-488b4f3ea230b0bc3cb7fa7e5264691e21067cd0.tar.gz
apt-panopticon_cgp-488b4f3ea230b0bc3cb7fa7e5264691e21067cd0.tar.bz2
apt-panopticon_cgp-488b4f3ea230b0bc3cb7fa7e5264691e21067cd0.tar.xz
add unixsock flush support
Via the UnixSock plugin of Collectd it is possible to send commands to the Collectd daemon. One of the commands is the FLUSH command, which you can use to let the daemon write cached data to the rrd files. CGP uses the FLUSH command before generating a graph and before showing the load information on the main page. In this case the information shown via CGP is always up-to-date. Also when you have configured the RRDTool plugin of Collectd with a CacheTimeout setting. This commit includes code based on functions collectd_identifier and collectd_flush from php-collection by Bruno Prémont.
-rw-r--r--conf/config.php5
-rw-r--r--inc/collectd.inc.php81
-rw-r--r--inc/html.inc.php2
-rw-r--r--plugin/cpu.php2
-rw-r--r--plugin/df.php3
-rw-r--r--plugin/disk.php3
-rw-r--r--plugin/entropy.php3
-rw-r--r--plugin/interface.php3
-rw-r--r--plugin/irq.php2
-rw-r--r--plugin/load.php3
-rw-r--r--plugin/memory.php2
-rw-r--r--plugin/processes.php2
-rw-r--r--plugin/sensors.php2
-rw-r--r--plugin/swap.php2
-rw-r--r--plugin/users.php3
15 files changed, 118 insertions, 0 deletions
diff --git a/conf/config.php b/conf/config.php
index cbddfb3..751bcf1 100644
--- a/conf/config.php
+++ b/conf/config.php
@@ -21,6 +21,11 @@ $CONFIG['groupby'] = array(
21 'sensors' => 'type', 21 'sensors' => 'type',
22); 22);
23 23
24# collectd's unix socket (unixsock plugin)
25# enabled: 'unix:///var/run/collectd-unixsock'
26# disabled: NULL
27$CONFIG['socket'] = NULL;
28
24if (file_exists(dirname(__FILE__).'/config.local.php')) 29if (file_exists(dirname(__FILE__).'/config.local.php'))
25 include 'config.local.php'; 30 include 'config.local.php';
26 31
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;
diff --git a/plugin/cpu.php b/plugin/cpu.php
index d750a3a..5f688f4 100644
--- a/plugin/cpu.php
+++ b/plugin/cpu.php
@@ -59,6 +59,8 @@ $obj->rrd_title = "CPU-$pinstance usage on $host";
59$obj->rrd_vertical = 'Jiffies'; 59$obj->rrd_vertical = 'Jiffies';
60$obj->rrd_format = '%5.2lf'; 60$obj->rrd_format = '%5.2lf';
61 61
62collectd_flush(ident_from_args($obj->args));
63
62$obj->rrd_graph(); 64$obj->rrd_graph();
63 65
64?> 66?>
diff --git a/plugin/df.php b/plugin/df.php
index f4dd5d6..6d7414b 100644
--- a/plugin/df.php
+++ b/plugin/df.php
@@ -4,6 +4,7 @@
4 4
5require_once 'conf/common.inc.php'; 5require_once 'conf/common.inc.php';
6require_once 'type/GenericStacked.class.php'; 6require_once 'type/GenericStacked.class.php';
7require_once 'inc/collectd.inc.php';
7 8
8# LAYOUT 9# LAYOUT
9# 10#
@@ -36,6 +37,8 @@ $obj->rrd_title = "Free space ($tinstance) on $host";
36$obj->rrd_vertical = 'Bytes'; 37$obj->rrd_vertical = 'Bytes';
37$obj->rrd_format = '%5.1lf%sB'; 38$obj->rrd_format = '%5.1lf%sB';
38 39
40collectd_flush(ident_from_args($obj->args));
41
39$obj->rrd_graph(); 42$obj->rrd_graph();
40 43
41?> 44?>
diff --git a/plugin/disk.php b/plugin/disk.php
index 0c18016..45b5fde 100644
--- a/plugin/disk.php
+++ b/plugin/disk.php
@@ -4,6 +4,7 @@
4 4
5require_once 'conf/common.inc.php'; 5require_once 'conf/common.inc.php';
6require_once 'type/GenericIO.class.php'; 6require_once 'type/GenericIO.class.php';
7require_once 'inc/collectd.inc.php';
7 8
8## LAYOUT 9## LAYOUT
9# disk-XXXX/ 10# disk-XXXX/
@@ -57,6 +58,8 @@ switch($type) {
57 break; 58 break;
58} 59}
59 60
61collectd_flush(ident_from_args($obj->args));
62
60$obj->rrd_graph(); 63$obj->rrd_graph();
61 64
62?> 65?>
diff --git a/plugin/entropy.php b/plugin/entropy.php
index dcfbcc8..a4379ab 100644
--- a/plugin/entropy.php
+++ b/plugin/entropy.php
@@ -4,6 +4,7 @@
4 4
5require_once 'conf/common.inc.php'; 5require_once 'conf/common.inc.php';
6require_once 'type/Default.class.php'; 6require_once 'type/Default.class.php';
7require_once 'inc/collectd.inc.php';
7 8
8## LAYOUT 9## LAYOUT
9# entropy/entropy.rrd 10# entropy/entropy.rrd
@@ -31,6 +32,8 @@ $obj->rrd_title = "Available entropy on $host";
31$obj->rrd_vertical = 'Bits'; 32$obj->rrd_vertical = 'Bits';
32$obj->rrd_format = '%4.0lf'; 33$obj->rrd_format = '%4.0lf';
33 34
35collectd_flush(ident_from_args($obj->args));
36
34$obj->rrd_graph(); 37$obj->rrd_graph();
35 38
36?> 39?>
diff --git a/plugin/interface.php b/plugin/interface.php
index a3fca3d..45e3cf1 100644
--- a/plugin/interface.php
+++ b/plugin/interface.php
@@ -4,6 +4,7 @@
4 4
5require_once 'conf/common.inc.php'; 5require_once 'conf/common.inc.php';
6require_once 'type/GenericIO.class.php'; 6require_once 'type/GenericIO.class.php';
7require_once 'inc/collectd.inc.php';
7 8
8# LAYOUT 9# LAYOUT
9# interface/ 10# interface/
@@ -48,6 +49,8 @@ switch($type) {
48 break; 49 break;
49} 50}
50 51
52collectd_flush(ident_from_args($obj->args));
53
51$obj->rrd_graph(); 54$obj->rrd_graph();
52 55
53?> 56?>
diff --git a/plugin/irq.php b/plugin/irq.php
index e442142..95bddcf 100644
--- a/plugin/irq.php
+++ b/plugin/irq.php
@@ -34,6 +34,8 @@ $obj->rrd_title = "Interrupts on $host";
34$obj->rrd_vertical = 'IRQs/s'; 34$obj->rrd_vertical = 'IRQs/s';
35$obj->rrd_format = '%6.1lf'; 35$obj->rrd_format = '%6.1lf';
36 36
37collectd_flush(ident_from_args($obj->args));
38
37$obj->rrd_graph(); 39$obj->rrd_graph();
38 40
39?> 41?>
diff --git a/plugin/load.php b/plugin/load.php
index 0246edc..83fcf19 100644
--- a/plugin/load.php
+++ b/plugin/load.php
@@ -4,6 +4,7 @@
4 4
5require_once 'conf/common.inc.php'; 5require_once 'conf/common.inc.php';
6require_once 'type/Default.class.php'; 6require_once 'type/Default.class.php';
7require_once 'inc/collectd.inc.php';
7 8
8## LAYOUT 9## LAYOUT
9# load/load.rrd 10# load/load.rrd
@@ -35,6 +36,8 @@ $obj->rrd_title = "System load on $host";
35$obj->rrd_vertical = 'System load'; 36$obj->rrd_vertical = 'System load';
36$obj->rrd_format = '%.2lf'; 37$obj->rrd_format = '%.2lf';
37 38
39collectd_flush(ident_from_args($obj->args));
40
38$obj->rrd_graph(); 41$obj->rrd_graph();
39 42
40?> 43?>
diff --git a/plugin/memory.php b/plugin/memory.php
index 6cef5a7..7b0e47b 100644
--- a/plugin/memory.php
+++ b/plugin/memory.php
@@ -47,6 +47,8 @@ $obj->rrd_title = "Physical memory utilization on $host";
47$obj->rrd_vertical = 'Bytes'; 47$obj->rrd_vertical = 'Bytes';
48$obj->rrd_format = '%5.1lf%s'; 48$obj->rrd_format = '%5.1lf%s';
49 49
50collectd_flush(ident_from_args($obj->args));
51
50$obj->rrd_graph(); 52$obj->rrd_graph();
51 53
52?> 54?>
diff --git a/plugin/processes.php b/plugin/processes.php
index c741a02..5fc0463 100644
--- a/plugin/processes.php
+++ b/plugin/processes.php
@@ -52,6 +52,8 @@ $obj->rrd_title = "Processes on $host";
52$obj->rrd_vertical = 'Processes'; 52$obj->rrd_vertical = 'Processes';
53$obj->rrd_format = '%5.1lf%s'; 53$obj->rrd_format = '%5.1lf%s';
54 54
55collectd_flush(ident_from_args($obj->args));
56
55$obj->rrd_graph(); 57$obj->rrd_graph();
56 58
57?> 59?>
diff --git a/plugin/sensors.php b/plugin/sensors.php
index b1b15cf..ddcf521 100644
--- a/plugin/sensors.php
+++ b/plugin/sensors.php
@@ -52,6 +52,8 @@ switch($type) {
52 break; 52 break;
53} 53}
54 54
55collectd_flush(ident_from_args($obj->args));
56
55$obj->rrd_graph(); 57$obj->rrd_graph();
56 58
57?> 59?>
diff --git a/plugin/swap.php b/plugin/swap.php
index 7160ec3..18c6242 100644
--- a/plugin/swap.php
+++ b/plugin/swap.php
@@ -44,6 +44,8 @@ $obj->rrd_title = "Swap utilization on $host";
44$obj->rrd_vertical = 'Bytes'; 44$obj->rrd_vertical = 'Bytes';
45$obj->rrd_format = '%5.1lf%s'; 45$obj->rrd_format = '%5.1lf%s';
46 46
47collectd_flush(ident_from_args($obj->args));
48
47$obj->rrd_graph(); 49$obj->rrd_graph();
48 50
49?> 51?>
diff --git a/plugin/users.php b/plugin/users.php
index 00874a2..1555927 100644
--- a/plugin/users.php
+++ b/plugin/users.php
@@ -4,6 +4,7 @@
4 4
5require_once 'conf/common.inc.php'; 5require_once 'conf/common.inc.php';
6require_once 'type/Default.class.php'; 6require_once 'type/Default.class.php';
7require_once 'inc/collectd.inc.php';
7 8
8## LAYOUT 9## LAYOUT
9# users/users.rrd 10# users/users.rrd
@@ -31,6 +32,8 @@ $obj->rrd_title = "Users on $host";
31$obj->rrd_vertical = 'Users'; 32$obj->rrd_vertical = 'Users';
32$obj->rrd_format = '%.1lf'; 33$obj->rrd_format = '%.1lf';
33 34
35collectd_flush(ident_from_args($obj->args));
36
34$obj->rrd_graph(); 37$obj->rrd_graph();
35 38
36?> 39?>