1 /**
2 * collectd - src/varnish.c
3 * Copyright (C) 2010 Jerome Renard
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 * Jerome Renard <jerome.renard@gmail.com>
21 **/
23 /**
24 * Current list of what is monitored and what is not monitored (yet)
25 * {{{
26 * Field name Description Monitored
27 * ---------- ----------- ---------
28 * uptime Child uptime N
29 * client_conn Client connections accepted Y
30 * client_drop Connection dropped, no sess Y
31 * client_req Client requests received Y
32 * cache_hit Cache hits Y
33 * cache_hitpass Cache hits for pass Y
34 * cache_miss Cache misses Y
35 * backend_conn Backend conn. success Y
36 * backend_unhealthy Backend conn. not attempted Y
37 * backend_busy Backend conn. too many Y
38 * backend_fail Backend conn. failures Y
39 * backend_reuse Backend conn. reuses Y
40 * backend_toolate Backend conn. was closed Y
41 * backend_recycle Backend conn. recycles Y
42 * backend_unused Backend conn. unused Y
43 * fetch_head Fetch head Y
44 * fetch_length Fetch with Length Y
45 * fetch_chunked Fetch chunked Y
46 * fetch_eof Fetch EOF Y
47 * fetch_bad Fetch had bad headers Y
48 * fetch_close Fetch wanted close Y
49 * fetch_oldhttp Fetch pre HTTP/1.1 closed Y
50 * fetch_zero Fetch zero len Y
51 * fetch_failed Fetch failed Y
52 * n_sess_mem N struct sess_mem N
53 * n_sess N struct sess N
54 * n_object N struct object N
55 * n_vampireobject N unresurrected objects N
56 * n_objectcore N struct objectcore N
57 * n_objecthead N struct objecthead N
58 * n_smf N struct smf N
59 * n_smf_frag N small free smf N
60 * n_smf_large N large free smf N
61 * n_vbe_conn N struct vbe_conn N
62 * n_wrk N worker threads Y
63 * n_wrk_create N worker threads created Y
64 * n_wrk_failed N worker threads not created Y
65 * n_wrk_max N worker threads limited Y
66 * n_wrk_queue N queued work requests Y
67 * n_wrk_overflow N overflowed work requests Y
68 * n_wrk_drop N dropped work requests Y
69 * n_backend N backends N
70 * n_expired N expired objects N
71 * n_lru_nuked N LRU nuked objects N
72 * n_lru_saved N LRU saved objects N
73 * n_lru_moved N LRU moved objects N
74 * n_deathrow N objects on deathrow N
75 * losthdr HTTP header overflows N
76 * n_objsendfile Objects sent with sendfile N
77 * n_objwrite Objects sent with write N
78 * n_objoverflow Objects overflowing workspace N
79 * s_sess Total Sessions Y
80 * s_req Total Requests Y
81 * s_pipe Total pipe Y
82 * s_pass Total pass Y
83 * s_fetch Total fetch Y
84 * s_hdrbytes Total header bytes Y
85 * s_bodybytes Total body bytes Y
86 * sess_closed Session Closed N
87 * sess_pipeline Session Pipeline N
88 * sess_readahead Session Read Ahead N
89 * sess_linger Session Linger N
90 * sess_herd Session herd N
91 * shm_records SHM records Y
92 * shm_writes SHM writes Y
93 * shm_flushes SHM flushes due to overflow Y
94 * shm_cont SHM MTX contention Y
95 * shm_cycles SHM cycles through buffer Y
96 * sm_nreq allocator requests Y
97 * sm_nobj outstanding allocations Y
98 * sm_balloc bytes allocated Y
99 * sm_bfree bytes free Y
100 * sma_nreq SMA allocator requests Y
101 * sma_nobj SMA outstanding allocations Y
102 * sma_nbytes SMA outstanding bytes Y
103 * sma_balloc SMA bytes allocated Y
104 * sma_bfree SMA bytes free Y
105 * sms_nreq SMS allocator requests Y
106 * sms_nobj SMS outstanding allocations Y
107 * sms_nbytes SMS outstanding bytes Y
108 * sms_balloc SMS bytes allocated Y
109 * sms_bfree SMS bytes freed Y
110 * backend_req Backend requests made N
111 * n_vcl N vcl total N
112 * n_vcl_avail N vcl available N
113 * n_vcl_discard N vcl discarded N
114 * n_purge N total active purges N
115 * n_purge_add N new purges added N
116 * n_purge_retire N old purges deleted N
117 * n_purge_obj_test N objects tested N
118 * n_purge_re_test N regexps tested against N
119 * n_purge_dups N duplicate purges removed N
120 * hcb_nolock HCB Lookups without lock Y
121 * hcb_lock HCB Lookups with lock Y
122 * hcb_insert HCB Inserts Y
123 * esi_parse Objects ESI parsed (unlock) Y
124 * esi_errors ESI parse errors (unlock) Y
125 * }}}
126 */
127 #include "collectd.h"
128 #include "common.h"
129 #include "plugin.h"
130 #include "configfile.h"
132 #include <varnish/varnishapi.h>
134 /* {{{ user_config_s */
135 struct user_config_s {
136 char *instance;
138 _Bool collect_cache;
139 _Bool collect_connections;
140 _Bool collect_esi;
141 _Bool collect_backend;
142 _Bool collect_fetch;
143 _Bool collect_hcb;
144 _Bool collect_shm;
145 _Bool collect_sma;
146 _Bool collect_sms;
147 _Bool collect_sm;
148 _Bool collect_totals;
149 _Bool collect_workers;
150 };
151 typedef struct user_config_s user_config_t; /* }}} */
153 static _Bool have_instance = 0;
155 static int varnish_submit (const char *plugin_instance, /* {{{ */
156 const char *type, const char *type_instance, value_t value)
157 {
158 value_list_t vl = VALUE_LIST_INIT;
160 vl.values = &value;
161 vl.values_len = 1;
163 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
165 sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
166 if (plugin_instance != NULL)
167 sstrncpy (vl.plugin_instance, plugin_instance,
168 sizeof (vl.plugin_instance));
170 sstrncpy(vl.type, type, sizeof(vl.type));
171 if (type_instance != NULL)
172 sstrncpy(vl.type_instance, type_instance,
173 sizeof(vl.type_instance));
175 return (plugin_dispatch_values(&vl));
176 } /* }}} int varnish_submit */
178 static int varnish_submit_gauge (const char *plugin_instance, /* {{{ */
179 const char *type, const char *type_instance,
180 uint64_t gauge_value)
181 {
182 value_t value;
184 value.gauge = (gauge_t) gauge_value;
186 return (varnish_submit (plugin_instance, type, type_instance, value));
187 } /* }}} int varnish_submit_gauge */
189 static int varnish_submit_derive (const char *plugin_instance, /* {{{ */
190 const char *type, const char *type_instance,
191 uint64_t derive_value)
192 {
193 value_t value;
195 value.derive = (derive_t) derive_value;
197 return (varnish_submit (plugin_instance, type, type_instance, value));
198 } /* }}} int varnish_submit_derive */
200 static void varnish_monitor(const user_config_t *conf, struct varnish_stats *VSL_stats) /* {{{ */
201 {
202 if(conf->collect_cache)
203 {
204 /* Cache hits */
205 varnish_submit_derive (conf->instance, "cache_result", "hit" , VSL_stats->cache_hit);
206 /* Cache misses */
207 varnish_submit_derive (conf->instance, "cache_result", "miss" , VSL_stats->cache_miss);
208 /* Cache hits for pass */
209 varnish_submit_derive (conf->instance, "cache_result", "hitpass", VSL_stats->cache_hitpass);
210 }
212 if(conf->collect_connections)
213 {
214 /* Client connections accepted */
215 varnish_submit_derive (conf->instance, "connections", "client-accepted", VSL_stats->client_conn);
216 /* Connection dropped, no sess */
217 varnish_submit_derive (conf->instance, "connections", "client-dropped" , VSL_stats->client_drop);
218 /* Client requests received */
219 varnish_submit_derive (conf->instance, "connections", "client-received", VSL_stats->client_req);
220 }
222 if(conf->collect_esi)
223 {
224 /* Objects ESI parsed (unlock) */
225 varnish_submit_derive (conf->instance, "total_operations", "esi-parsed", VSL_stats->esi_parse);
226 /* ESI parse errors (unlock) */
227 varnish_submit_derive (conf->instance, "total_operations", "esi-error", VSL_stats->esi_errors);
228 }
230 if(conf->collect_backend)
231 {
232 /* Backend conn. success */
233 varnish_submit_derive (conf->instance, "connections", "backend-success" , VSL_stats->backend_conn);
234 /* Backend conn. not attempted */
235 varnish_submit_derive (conf->instance, "connections", "backend-not-attempted", VSL_stats->backend_unhealthy);
236 /* Backend conn. too many */
237 varnish_submit_derive (conf->instance, "connections", "backend-too-many" , VSL_stats->backend_busy);
238 /* Backend conn. failures */
239 varnish_submit_derive (conf->instance, "connections", "backend-failures" , VSL_stats->backend_fail);
240 /* Backend conn. reuses */
241 varnish_submit_derive (conf->instance, "connections", "backend-reuses" , VSL_stats->backend_reuse);
242 /* Backend conn. was closed */
243 varnish_submit_derive (conf->instance, "connections", "backend-was-closed" , VSL_stats->backend_toolate);
244 /* Backend conn. recycles */
245 varnish_submit_derive (conf->instance, "connections", "backend-recycled" , VSL_stats->backend_recycle);
246 /* Backend conn. unused */
247 varnish_submit_derive (conf->instance, "connections", "backend-unused" , VSL_stats->backend_unused);
248 }
250 if(conf->collect_fetch)
251 {
252 /* Fetch head */
253 varnish_submit_derive (conf->instance, "http_requests", "fetch-head" , VSL_stats->fetch_head);
254 /* Fetch with length */
255 varnish_submit_derive (conf->instance, "http_requests", "fetch-length" , VSL_stats->fetch_length);
256 /* Fetch chunked */
257 varnish_submit_derive (conf->instance, "http_requests", "fetch-chunked" , VSL_stats->fetch_chunked);
258 /* Fetch EOF */
259 varnish_submit_derive (conf->instance, "http_requests", "fetch-eof" , VSL_stats->fetch_eof);
260 /* Fetch bad headers */
261 varnish_submit_derive (conf->instance, "http_requests", "fetch-bad_headers", VSL_stats->fetch_bad);
262 /* Fetch wanted close */
263 varnish_submit_derive (conf->instance, "http_requests", "fetch-close" , VSL_stats->fetch_close);
264 /* Fetch pre HTTP/1.1 closed */
265 varnish_submit_derive (conf->instance, "http_requests", "fetch-oldhttp" , VSL_stats->fetch_oldhttp);
266 /* Fetch zero len */
267 varnish_submit_derive (conf->instance, "http_requests", "fetch-zero" , VSL_stats->fetch_zero);
268 /* Fetch failed */
269 varnish_submit_derive (conf->instance, "http_requests", "fetch-failed" , VSL_stats->fetch_failed);
270 }
272 if(conf->collect_hcb)
273 {
274 /* HCB Lookups without lock */
275 varnish_submit_derive (conf->instance, "cache_operation", "lookup_nolock", VSL_stats->hcb_nolock);
276 /* HCB Lookups with lock */
277 varnish_submit_derive (conf->instance, "cache_operation", "lookup_lock", VSL_stats->hcb_lock);
278 /* HCB Inserts */
279 varnish_submit_derive (conf->instance, "cache_operation", "insert", VSL_stats->hcb_insert);
280 }
282 if(conf->collect_shm)
283 {
284 /* SHM records */
285 varnish_submit_derive (conf->instance, "total_operations", "shmlog-records" , VSL_stats->shm_records);
286 /* SHM writes */
287 varnish_submit_derive (conf->instance, "total_operations", "shmlog-writes" , VSL_stats->shm_writes);
288 /* SHM flushes due to overflow */
289 varnish_submit_derive (conf->instance, "total_operations", "shmlog-flushes" , VSL_stats->shm_flushes);
290 /* SHM MTX contention */
291 varnish_submit_derive (conf->instance, "total_operations", "shmlog-contention", VSL_stats->shm_cont);
292 /* SHM cycles through buffer */
293 varnish_submit_derive (conf->instance, "total_operations", "shmlog-cycles" , VSL_stats->shm_cycles);
294 }
296 if(conf->collect_sm)
297 {
298 /* allocator requests */
299 varnish_submit_derive (conf->instance, "total_requests", "storage-file", VSL_stats->sm_nreq);
300 /* outstanding allocations */
301 varnish_submit_gauge (conf->instance, "requests", "storage-file-outstanding", VSL_stats->sm_nobj);
302 /* bytes allocated */
303 varnish_submit_gauge (conf->instance, "bytes", "storage-file-allocated", VSL_stats->sm_balloc);
304 /* bytes free */
305 varnish_submit_gauge (conf->instance, "bytes", "storage-file-free", VSL_stats->sm_bfree);
306 }
308 if(conf->collect_sma)
309 {
310 /* SMA allocator requests */
311 varnish_submit_derive (conf->instance, "total_requests", "storage-mem", VSL_stats->sma_nreq);
312 /* SMA outstanding allocations */
313 varnish_submit_gauge (conf->instance, "requests", "storage-mem-outstanding", VSL_stats->sma_nobj);
314 /* SMA outstanding bytes */
315 varnish_submit_gauge (conf->instance, "bytes", "storage-mem-outstanding", VSL_stats->sma_nbytes);
316 /* SMA bytes allocated */
317 varnish_submit_gauge (conf->instance, "bytes", "storage-mem-allocated", VSL_stats->sma_balloc);
318 /* SMA bytes free */
319 varnish_submit_gauge (conf->instance, "bytes", "storage-mem-free" , VSL_stats->sma_bfree);
320 }
322 if(conf->collect_sms)
323 {
324 /* SMS allocator requests */
325 varnish_submit_derive (conf->instance, "total_requests", "storage-synth", VSL_stats->sms_nreq);
326 /* SMS outstanding allocations */
327 varnish_submit_gauge (conf->instance, "requests", "storage-synth-outstanding", VSL_stats->sms_nobj);
328 /* SMS outstanding bytes */
329 varnish_submit_gauge (conf->instance, "bytes", "storage-synth-outstanding", VSL_stats->sms_nbytes);
330 /* SMS bytes allocated */
331 varnish_submit_gauge (conf->instance, "bytes", "storage-synth-allocated", VSL_stats->sms_balloc);
332 /* SMS bytes freed */
333 varnish_submit_gauge (conf->instance, "bytes", "storage-synth-free", VSL_stats->sms_bfree);
334 }
336 if(conf->collect_totals)
337 {
338 /* Total Sessions */
339 varnish_submit_derive (conf->instance, "total_counters", "sessions", VSL_stats->s_sess);
340 /* Total Requests */
341 varnish_submit_derive (conf->instance, "total_requests", "requests", VSL_stats->s_req);
342 /* Total pipe */
343 varnish_submit_derive (conf->instance, "total_operations", "pipe", VSL_stats->s_pipe);
344 /* Total pass */
345 varnish_submit_derive (conf->instance, "total_operations", "pass", VSL_stats->s_pass);
346 /* Total fetch */
347 varnish_submit_derive (conf->instance, "total_operations", "fetches", VSL_stats->s_fetch);
348 /* Total header bytes */
349 varnish_submit_derive (conf->instance, "total_bytes", "header-bytes", VSL_stats->s_hdrbytes);
350 /* Total body byte */
351 varnish_submit_derive (conf->instance, "total_bytes", "body-bytes", VSL_stats->s_bodybytes);
352 }
354 if(conf->collect_workers)
355 {
356 /* worker threads */
357 varnish_submit_gauge ( conf->instance, "threads", "threads", VSL_stats-> n_wrk );
358 /* worker threads created */
359 varnish_submit_gauge ( conf->instance , "threads", "threads-created", VSL_stats-> n_wrk_create );
360 /* worker threads not created */
361 varnish_submit_gauge ( conf->instance, "threads", "threads-failed", VSL_stats-> n_wrk_failed );
362 /* worker threads limited */
363 varnish_submit_gauge ( conf->instance, "threads", "threads-limited", VSL_stats-> n_wrk_max );
364 /* queued work requests */
365 varnish_submit_gauge ( conf->instance, "threads", "queued-requests", VSL_stats-> n_wrk_queue );
366 /* overflowed work requests */
367 varnish_submit_gauge ( conf->instance, "threads", "overflowed-requests", VSL_stats-> n_wrk_overflow );
368 /* dropped work requests */
369 varnish_submit_gauge ( conf->instance, "threads", "dropped-requests", VSL_stats-> n_wrk_drop );
370 }
371 } /* }}} void varnish_monitor */
373 static int varnish_read(user_data_t *ud) /* {{{ */
374 {
375 struct varnish_stats *VSL_stats;
376 user_config_t *conf;
378 if ((ud == NULL) || (ud->data == NULL))
379 return (EINVAL);
381 conf = ud->data;
383 VSL_stats = VSL_OpenStats(conf->instance);
384 if (VSL_stats == NULL)
385 {
386 ERROR("Varnish plugin : unable to load statistics");
388 return (-1);
389 }
391 varnish_monitor(conf, VSL_stats);
393 return (0);
394 } /* }}} */
396 static void varnish_config_free (void *ptr) /* {{{ */
397 {
398 user_config_t *conf = ptr;
400 if (conf == NULL)
401 return;
403 sfree (conf->instance);
404 sfree (conf);
405 } /* }}} */
407 static int varnish_init (void) /* {{{ */
408 {
409 user_config_t *conf;
410 user_data_t ud;
412 if (have_instance)
413 return (0);
415 conf = malloc (sizeof (*conf));
416 if (conf == NULL)
417 return (ENOMEM);
418 memset (conf, 0, sizeof (*conf));
420 /* Default settings: */
421 conf->instance = NULL;
422 conf->collect_cache = 1;
423 conf->collect_backend = 1;
424 conf->collect_connections = 1;
425 conf->collect_shm = 1;
427 ud.data = conf;
428 ud.free_func = varnish_config_free;
430 plugin_register_complex_read (/* group = */ "varnish",
431 /* name = */ "varnish/localhost",
432 /* callback = */ varnish_read,
433 /* interval = */ NULL,
434 /* user data = */ &ud);
436 return (0);
437 } /* }}} int varnish_init */
439 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
440 {
441 user_config_t *conf;
442 user_data_t ud;
443 char callback_name[DATA_MAX_NAME_LEN];
444 int i;
446 conf = malloc (sizeof (*conf));
447 if (conf == NULL)
448 return (ENOMEM);
449 memset (conf, 0, sizeof (*conf));
450 conf->instance = NULL;
452 if (ci->values_num == 1)
453 {
454 int status;
456 status = cf_util_get_string (ci, &conf->instance);
457 if (status != 0)
458 {
459 sfree (conf);
460 return (status);
461 }
462 assert (conf->instance != NULL);
464 if (strcmp ("localhost", conf->instance) == 0)
465 {
466 sfree (conf->instance);
467 conf->instance = NULL;
468 }
469 }
470 else if (ci->values_num > 1)
471 {
472 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
473 "one argument.");
474 return (EINVAL);
475 }
477 for (i = 0; i < ci->children_num; i++)
478 {
479 oconfig_item_t *child = ci->children + i;
481 if (strcasecmp ("CollectCache", child->key) == 0)
482 cf_util_get_boolean (child, &conf->collect_cache);
483 else if (strcasecmp ("CollectConnections", child->key) == 0)
484 cf_util_get_boolean (child, &conf->collect_connections);
485 else if (strcasecmp ("CollectESI", child->key) == 0)
486 cf_util_get_boolean (child, &conf->collect_esi);
487 else if (strcasecmp ("CollectBackend", child->key) == 0)
488 cf_util_get_boolean (child, &conf->collect_backend);
489 else if (strcasecmp ("CollectFetch", child->key) == 0)
490 cf_util_get_boolean (child, &conf->collect_fetch);
491 else if (strcasecmp ("CollectHCB", child->key) == 0)
492 cf_util_get_boolean (child, &conf->collect_hcb);
493 else if (strcasecmp ("CollectSHM", child->key) == 0)
494 cf_util_get_boolean (child, &conf->collect_shm);
495 else if (strcasecmp ("CollectSMA", child->key) == 0)
496 cf_util_get_boolean (child, &conf->collect_sma);
497 else if (strcasecmp ("CollectSMS", child->key) == 0)
498 cf_util_get_boolean (child, &conf->collect_sms);
499 else if (strcasecmp ("CollectSM", child->key) == 0)
500 cf_util_get_boolean (child, &conf->collect_sm);
501 else if (strcasecmp ("CollectTotals", child->key) == 0)
502 cf_util_get_boolean (child, &conf->collect_totals);
503 else if (strcasecmp ("CollectWorkers", child->key) == 0)
504 cf_util_get_boolean (child, &conf->collect_workers);
505 else
506 {
507 WARNING ("Varnish plugin: Ignoring unknown "
508 "configuration option: \"%s\"",
509 child->key);
510 }
511 }
513 if (!conf->collect_cache
514 && !conf->collect_connections
515 && !conf->collect_esi
516 && !conf->collect_backend
517 && !conf->collect_fetch
518 && !conf->collect_hcb
519 && !conf->collect_shm
520 && !conf->collect_sma
521 && !conf->collect_sms
522 && !conf->collect_sm
523 && !conf->collect_totals
524 && !conf->collect_workers)
525 {
526 WARNING ("Varnish plugin: No metric has been configured for "
527 "instance \"%s\". Disabling this instance.",
528 (conf->instance == NULL) ? "localhost" : conf->instance);
529 return (EINVAL);
530 }
532 ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
533 (conf->instance == NULL) ? "localhost" : conf->instance);
535 ud.data = conf;
536 ud.free_func = varnish_config_free;
538 plugin_register_complex_read (/* group = */ "varnish",
539 /* name = */ callback_name,
540 /* callback = */ varnish_read,
541 /* interval = */ NULL,
542 /* user data = */ &ud);
544 have_instance = 1;
546 return (0);
547 } /* }}} int varnish_config_instance */
549 static int varnish_config (oconfig_item_t *ci) /* {{{ */
550 {
551 int i;
553 for (i = 0; i < ci->children_num; i++)
554 {
555 oconfig_item_t *child = ci->children + i;
557 if (strcasecmp ("Instance", child->key) == 0)
558 varnish_config_instance (child);
559 else
560 {
561 WARNING ("Varnish plugin: Ignoring unknown "
562 "configuration option: \"%s\"",
563 child->key);
564 }
565 }
567 return (0);
568 } /* }}} int varnish_config */
570 void module_register (void) /* {{{ */
571 {
572 plugin_register_complex_config("varnish", varnish_config);
573 plugin_register_init ("varnish", varnish_init);
574 } /* }}} */
576 /* vim: set sw=8 noet fdm=marker : */