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 *category, 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));
167 if (plugin_instance == NULL)
168 plugin_instance = "default";
170 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
171 "%s-%s", plugin_instance, category);
173 sstrncpy (vl.type, type, sizeof (vl.type));
175 if (type_instance != NULL)
176 sstrncpy (vl.type_instance, type_instance,
177 sizeof (vl.type_instance));
179 return (plugin_dispatch_values (&vl));
180 } /* }}} int varnish_submit */
182 static int varnish_submit_gauge (const char *plugin_instance, /* {{{ */
183 const char *category, const char *type, const char *type_instance,
184 uint64_t gauge_value)
185 {
186 value_t value;
188 value.gauge = (gauge_t) gauge_value;
190 return (varnish_submit (plugin_instance, category, type, type_instance, value));
191 } /* }}} int varnish_submit_gauge */
193 static int varnish_submit_derive (const char *plugin_instance, /* {{{ */
194 const char *category, const char *type, const char *type_instance,
195 uint64_t derive_value)
196 {
197 value_t value;
199 value.derive = (derive_t) derive_value;
201 return (varnish_submit (plugin_instance, category, type, type_instance, value));
202 } /* }}} int varnish_submit_derive */
204 static void varnish_monitor (const user_config_t *conf, struct varnish_stats *VSL_stats) /* {{{ */
205 {
206 if (conf->collect_cache)
207 {
208 /* Cache hits */
209 varnish_submit_derive (conf->instance, "cache", "cache_result", "hit", VSL_stats->cache_hit);
210 /* Cache misses */
211 varnish_submit_derive (conf->instance, "cache", "cache_result", "miss", VSL_stats->cache_miss);
212 /* Cache hits for pass */
213 varnish_submit_derive (conf->instance, "cache", "cache_result", "hitpass", VSL_stats->cache_hitpass);
214 }
216 if (conf->collect_connections)
217 {
218 /* Client connections accepted */
219 varnish_submit_derive (conf->instance, "connections", "connections", "accepted", VSL_stats->client_conn);
220 /* Connection dropped, no sess */
221 varnish_submit_derive (conf->instance, "connections", "connections", "dropped" , VSL_stats->client_drop);
222 /* Client requests received */
223 varnish_submit_derive (conf->instance, "connections", "connections", "received", VSL_stats->client_req);
224 }
226 if (conf->collect_esi)
227 {
228 /* Objects ESI parsed (unlock) */
229 varnish_submit_derive (conf->instance, "esi", "total_operations", "parsed", VSL_stats->esi_parse);
230 /* ESI parse errors (unlock) */
231 varnish_submit_derive (conf->instance, "esi", "total_operations", "error", VSL_stats->esi_errors);
232 }
234 if (conf->collect_backend)
235 {
236 /* Backend conn. success */
237 varnish_submit_derive (conf->instance, "backend", "connections", "success" , VSL_stats->backend_conn);
238 /* Backend conn. not attempted */
239 varnish_submit_derive (conf->instance, "backend", "connections", "not-attempted", VSL_stats->backend_unhealthy);
240 /* Backend conn. too many */
241 varnish_submit_derive (conf->instance, "backend", "connections", "too-many" , VSL_stats->backend_busy);
242 /* Backend conn. failures */
243 varnish_submit_derive (conf->instance, "backend", "connections", "failures" , VSL_stats->backend_fail);
244 /* Backend conn. reuses */
245 varnish_submit_derive (conf->instance, "backend", "connections", "reuses" , VSL_stats->backend_reuse);
246 /* Backend conn. was closed */
247 varnish_submit_derive (conf->instance, "backend", "connections", "was-closed" , VSL_stats->backend_toolate);
248 /* Backend conn. recycles */
249 varnish_submit_derive (conf->instance, "backend", "connections", "recycled" , VSL_stats->backend_recycle);
250 /* Backend conn. unused */
251 varnish_submit_derive (conf->instance, "backend", "connections", "unused" , VSL_stats->backend_unused);
252 }
254 if (conf->collect_fetch)
255 {
256 /* Fetch head */
257 varnish_submit_derive (conf->instance, "fetch", "http_requests", "head" , VSL_stats->fetch_head);
258 /* Fetch with length */
259 varnish_submit_derive (conf->instance, "fetch", "http_requests", "length" , VSL_stats->fetch_length);
260 /* Fetch chunked */
261 varnish_submit_derive (conf->instance, "fetch", "http_requests", "chunked" , VSL_stats->fetch_chunked);
262 /* Fetch EOF */
263 varnish_submit_derive (conf->instance, "fetch", "http_requests", "eof" , VSL_stats->fetch_eof);
264 /* Fetch bad headers */
265 varnish_submit_derive (conf->instance, "fetch", "http_requests", "bad_headers", VSL_stats->fetch_bad);
266 /* Fetch wanted close */
267 varnish_submit_derive (conf->instance, "fetch", "http_requests", "close" , VSL_stats->fetch_close);
268 /* Fetch pre HTTP/1.1 closed */
269 varnish_submit_derive (conf->instance, "fetch", "http_requests", "oldhttp" , VSL_stats->fetch_oldhttp);
270 /* Fetch zero len */
271 varnish_submit_derive (conf->instance, "fetch", "http_requests", "zero" , VSL_stats->fetch_zero);
272 /* Fetch failed */
273 varnish_submit_derive (conf->instance, "fetch", "http_requests", "failed" , VSL_stats->fetch_failed);
274 }
276 if (conf->collect_hcb)
277 {
278 /* HCB Lookups without lock */
279 varnish_submit_derive (conf->instance, "hcb", "cache_operation", "lookup_nolock", VSL_stats->hcb_nolock);
280 /* HCB Lookups with lock */
281 varnish_submit_derive (conf->instance, "hcb", "cache_operation", "lookup_lock", VSL_stats->hcb_lock);
282 /* HCB Inserts */
283 varnish_submit_derive (conf->instance, "hcb", "cache_operation", "insert", VSL_stats->hcb_insert);
284 }
286 if (conf->collect_shm)
287 {
288 /* SHM records */
289 varnish_submit_derive (conf->instance, "shm", "total_operations", "records" , VSL_stats->shm_records);
290 /* SHM writes */
291 varnish_submit_derive (conf->instance, "shm", "total_operations", "writes" , VSL_stats->shm_writes);
292 /* SHM flushes due to overflow */
293 varnish_submit_derive (conf->instance, "shm", "total_operations", "flushes" , VSL_stats->shm_flushes);
294 /* SHM MTX contention */
295 varnish_submit_derive (conf->instance, "shm", "total_operations", "contention", VSL_stats->shm_cont);
296 /* SHM cycles through buffer */
297 varnish_submit_derive (conf->instance, "shm", "total_operations", "cycles" , VSL_stats->shm_cycles);
298 }
300 if (conf->collect_sm)
301 {
302 /* allocator requests */
303 varnish_submit_derive (conf->instance, "sm", "total_requests", "nreq", VSL_stats->sm_nreq);
304 /* outstanding allocations */
305 varnish_submit_gauge (conf->instance, "sm", "requests", "outstanding", VSL_stats->sm_nobj);
306 /* bytes allocated */
307 varnish_submit_gauge (conf->instance, "sm", "bytes", "allocated", VSL_stats->sm_balloc);
308 /* bytes free */
309 varnish_submit_gauge (conf->instance, "sm", "bytes", "free", VSL_stats->sm_bfree);
310 }
312 if (conf->collect_sma)
313 {
314 /* SMA allocator requests */
315 varnish_submit_derive (conf->instance, "sma", "total_requests", "nreq", VSL_stats->sma_nreq);
316 /* SMA outstanding allocations */
317 varnish_submit_gauge (conf->instance, "sma", "requests", "outstanding", VSL_stats->sma_nobj);
318 /* SMA outstanding bytes */
319 varnish_submit_gauge (conf->instance, "sma", "bytes", "outstanding", VSL_stats->sma_nbytes);
320 /* SMA bytes allocated */
321 varnish_submit_gauge (conf->instance, "sma", "bytes", "allocated", VSL_stats->sma_balloc);
322 /* SMA bytes free */
323 varnish_submit_gauge (conf->instance, "sma", "bytes", "free" , VSL_stats->sma_bfree);
324 }
326 if (conf->collect_sms)
327 {
328 /* SMS allocator requests */
329 varnish_submit_derive (conf->instance, "sms", "total_requests", "allocator", VSL_stats->sms_nreq);
330 /* SMS outstanding allocations */
331 varnish_submit_gauge (conf->instance, "sms", "requests", "outstanding", VSL_stats->sms_nobj);
332 /* SMS outstanding bytes */
333 varnish_submit_gauge (conf->instance, "sms", "bytes", "outstanding", VSL_stats->sms_nbytes);
334 /* SMS bytes allocated */
335 varnish_submit_gauge (conf->instance, "sms", "bytes", "allocated", VSL_stats->sms_balloc);
336 /* SMS bytes freed */
337 varnish_submit_gauge (conf->instance, "sms", "bytes", "free", VSL_stats->sms_bfree);
338 }
340 if (conf->collect_totals)
341 {
342 /* Total Sessions */
343 varnish_submit_derive (conf->instance, "totals", "total_sessions", "sessions", VSL_stats->s_sess);
344 /* Total Requests */
345 varnish_submit_derive (conf->instance, "totals", "total_requests", "requests", VSL_stats->s_req);
346 /* Total pipe */
347 varnish_submit_derive (conf->instance, "totals", "total_operations", "pipe", VSL_stats->s_pipe);
348 /* Total pass */
349 varnish_submit_derive (conf->instance, "totals", "total_operations", "pass", VSL_stats->s_pass);
350 /* Total fetch */
351 varnish_submit_derive (conf->instance, "totals", "total_operations", "fetches", VSL_stats->s_fetch);
352 /* Total header bytes */
353 varnish_submit_derive (conf->instance, "totals", "total_bytes", "header-bytes", VSL_stats->s_hdrbytes);
354 /* Total body byte */
355 varnish_submit_derive (conf->instance, "totals", "total_bytes", "body-bytes", VSL_stats->s_bodybytes);
356 }
358 if (conf->collect_workers)
359 {
360 /* worker threads */
361 varnish_submit_gauge (conf->instance, "workers", "threads", "worker", VSL_stats->n_wrk);
362 /* worker threads created */
363 varnish_submit_gauge (conf->instance, "workers", "total_threads", "created", VSL_stats->n_wrk_create);
364 /* worker threads not created */
365 varnish_submit_gauge (conf->instance, "workers", "total_threads", "failed", VSL_stats->n_wrk_failed);
366 /* worker threads limited */
367 varnish_submit_gauge (conf->instance, "workers", "total_threads", "limited", VSL_stats->n_wrk_max);
368 /* queued work requests */
369 varnish_submit_gauge (conf->instance, "workers", "total_requests", "queued", VSL_stats->n_wrk_queue);
370 /* overflowed work requests */
371 varnish_submit_gauge (conf->instance, "workers", "total_requests", "overflowed", VSL_stats->n_wrk_overflow);
372 /* dropped work requests */
373 varnish_submit_gauge (conf->instance, "workers", "total_requests", "dropped", VSL_stats->n_wrk_drop);
374 }
375 } /* }}} void varnish_monitor */
377 static int varnish_read (user_data_t *ud) /* {{{ */
378 {
379 struct varnish_stats *VSL_stats;
380 user_config_t *conf;
382 if ((ud == NULL) || (ud->data == NULL))
383 return (EINVAL);
385 conf = ud->data;
387 VSL_stats = VSL_OpenStats (conf->instance);
388 if (VSL_stats == NULL)
389 {
390 ERROR ("Varnish plugin : unable to load statistics");
392 return (-1);
393 }
395 varnish_monitor (conf, VSL_stats);
397 return (0);
398 } /* }}} */
400 static void varnish_config_free (void *ptr) /* {{{ */
401 {
402 user_config_t *conf = ptr;
404 if (conf == NULL)
405 return;
407 sfree (conf->instance);
408 sfree (conf);
409 } /* }}} */
411 static int varnish_config_apply_default (user_config_t *conf) /* {{{ */
412 {
413 if (conf == NULL)
414 return (EINVAL);
416 conf->collect_backend = 1;
417 conf->collect_cache = 1;
418 conf->collect_connections = 1;
419 conf->collect_esi = 0;
420 conf->collect_fetch = 0;
421 conf->collect_hcb = 0;
422 conf->collect_shm = 1;
423 conf->collect_sm = 0;
424 conf->collect_sma = 0;
425 conf->collect_sms = 0;
426 conf->collect_totals = 0;
428 return (0);
429 } /* }}} int varnish_config_apply_default */
431 static int varnish_init (void) /* {{{ */
432 {
433 user_config_t *conf;
434 user_data_t ud;
436 if (have_instance)
437 return (0);
439 conf = malloc (sizeof (*conf));
440 if (conf == NULL)
441 return (ENOMEM);
442 memset (conf, 0, sizeof (*conf));
444 /* Default settings: */
445 conf->instance = NULL;
447 varnish_config_apply_default (conf);
449 ud.data = conf;
450 ud.free_func = varnish_config_free;
452 plugin_register_complex_read (/* group = */ "varnish",
453 /* name = */ "varnish/localhost",
454 /* callback = */ varnish_read,
455 /* interval = */ NULL,
456 /* user data = */ &ud);
458 return (0);
459 } /* }}} int varnish_init */
461 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
462 {
463 user_config_t *conf;
464 user_data_t ud;
465 char callback_name[DATA_MAX_NAME_LEN];
466 int i;
468 conf = malloc (sizeof (*conf));
469 if (conf == NULL)
470 return (ENOMEM);
471 memset (conf, 0, sizeof (*conf));
472 conf->instance = NULL;
474 varnish_config_apply_default (conf);
476 if (ci->values_num == 1)
477 {
478 int status;
480 status = cf_util_get_string (ci, &conf->instance);
481 if (status != 0)
482 {
483 sfree (conf);
484 return (status);
485 }
486 assert (conf->instance != NULL);
488 if (strcmp ("localhost", conf->instance) == 0)
489 {
490 sfree (conf->instance);
491 conf->instance = NULL;
492 }
493 }
494 else if (ci->values_num > 1)
495 {
496 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
497 "one argument.");
498 return (EINVAL);
499 }
501 for (i = 0; i < ci->children_num; i++)
502 {
503 oconfig_item_t *child = ci->children + i;
505 if (strcasecmp ("CollectCache", child->key) == 0)
506 cf_util_get_boolean (child, &conf->collect_cache);
507 else if (strcasecmp ("CollectConnections", child->key) == 0)
508 cf_util_get_boolean (child, &conf->collect_connections);
509 else if (strcasecmp ("CollectESI", child->key) == 0)
510 cf_util_get_boolean (child, &conf->collect_esi);
511 else if (strcasecmp ("CollectBackend", child->key) == 0)
512 cf_util_get_boolean (child, &conf->collect_backend);
513 else if (strcasecmp ("CollectFetch", child->key) == 0)
514 cf_util_get_boolean (child, &conf->collect_fetch);
515 else if (strcasecmp ("CollectHCB", child->key) == 0)
516 cf_util_get_boolean (child, &conf->collect_hcb);
517 else if (strcasecmp ("CollectSHM", child->key) == 0)
518 cf_util_get_boolean (child, &conf->collect_shm);
519 else if (strcasecmp ("CollectSMA", child->key) == 0)
520 cf_util_get_boolean (child, &conf->collect_sma);
521 else if (strcasecmp ("CollectSMS", child->key) == 0)
522 cf_util_get_boolean (child, &conf->collect_sms);
523 else if (strcasecmp ("CollectSM", child->key) == 0)
524 cf_util_get_boolean (child, &conf->collect_sm);
525 else if (strcasecmp ("CollectTotals", child->key) == 0)
526 cf_util_get_boolean (child, &conf->collect_totals);
527 else if (strcasecmp ("CollectWorkers", child->key) == 0)
528 cf_util_get_boolean (child, &conf->collect_workers);
529 else
530 {
531 WARNING ("Varnish plugin: Ignoring unknown "
532 "configuration option: \"%s\"",
533 child->key);
534 }
535 }
537 if (!conf->collect_cache
538 && !conf->collect_connections
539 && !conf->collect_esi
540 && !conf->collect_backend
541 && !conf->collect_fetch
542 && !conf->collect_hcb
543 && !conf->collect_shm
544 && !conf->collect_sma
545 && !conf->collect_sms
546 && !conf->collect_sm
547 && !conf->collect_totals
548 && !conf->collect_workers)
549 {
550 WARNING ("Varnish plugin: No metric has been configured for "
551 "instance \"%s\". Disabling this instance.",
552 (conf->instance == NULL) ? "localhost" : conf->instance);
553 return (EINVAL);
554 }
556 ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
557 (conf->instance == NULL) ? "localhost" : conf->instance);
559 ud.data = conf;
560 ud.free_func = varnish_config_free;
562 plugin_register_complex_read (/* group = */ "varnish",
563 /* name = */ callback_name,
564 /* callback = */ varnish_read,
565 /* interval = */ NULL,
566 /* user data = */ &ud);
568 have_instance = 1;
570 return (0);
571 } /* }}} int varnish_config_instance */
573 static int varnish_config (oconfig_item_t *ci) /* {{{ */
574 {
575 int i;
577 for (i = 0; i < ci->children_num; i++)
578 {
579 oconfig_item_t *child = ci->children + i;
581 if (strcasecmp ("Instance", child->key) == 0)
582 varnish_config_instance (child);
583 else
584 {
585 WARNING ("Varnish plugin: Ignoring unknown "
586 "configuration option: \"%s\"",
587 child->key);
588 }
589 }
591 return (0);
592 } /* }}} int varnish_config */
594 void module_register (void) /* {{{ */
595 {
596 plugin_register_complex_config ("varnish", varnish_config);
597 plugin_register_init ("varnish", varnish_init);
598 } /* }}} */
600 /* vim: set sw=8 noet fdm=marker : */