diff options
author | Pim van den Berg | 2009-11-04 23:04:30 +0100 |
---|---|---|
committer | Pim van den Berg | 2009-11-04 23:04:30 +0100 |
commit | 488b4f3ea230b0bc3cb7fa7e5264691e21067cd0 (patch) | |
tree | 67944684b708f9e2998eeeb0200e608f1f451a36 /inc/collectd.inc.php | |
parent | remove path_format from plugins (diff) | |
download | apt-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.
Diffstat (limited to '')
-rw-r--r-- | inc/collectd.inc.php | 81 |
1 files changed, 81 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 | ||
161 | function 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) | ||
175 | function 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 | ||
225 | function 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 | ?> |