diff options
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 | ?> |