2 * collectd - src/varnish.c
3 * Copyright (C) 2010 Jérôme Renard
4 * Copyright (C) 2010 Marc Fournier
5 * Copyright (C) 2010-2012 Florian Forster
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Jérôme Renard <jerome.renard at gmail.com>
22 * Marc Fournier <marc.fournier at camptocamp.com>
23 * Florian octo Forster <octo at collectd.org>
34 typedef struct VSC_C_main c_varnish_stats_t;
38 #include <varnishapi.h>
40 typedef struct VSC_C_main c_varnish_stats_t;
44 #include <varnishapi.h>
45 typedef struct varnish_stats c_varnish_stats_t;
48 /* {{{ user_config_s */
49 struct user_config_s {
53 _Bool collect_connections;
55 _Bool collect_backend;
56 #ifdef HAVE_VARNISH_V3
61 _Bool collect_objects;
67 _Bool collect_session;
76 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
80 _Bool collect_workers;
85 typedef struct user_config_s user_config_t; /* }}} */
87 static _Bool have_instance = 0;
89 static int varnish_submit(const char *plugin_instance, /* {{{ */
90 const char *category, const char *type,
91 const char *type_instance, value_t value) {
92 value_list_t vl = VALUE_LIST_INIT;
97 sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
99 if (plugin_instance == NULL)
100 plugin_instance = "default";
101 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
102 plugin_instance, category);
104 sstrncpy(vl.type, type, sizeof(vl.type));
106 if (type_instance != NULL)
107 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
109 return plugin_dispatch_values(&vl);
110 } /* }}} int varnish_submit */
112 static int varnish_submit_gauge(const char *plugin_instance, /* {{{ */
113 const char *category, const char *type,
114 const char *type_instance,
115 uint64_t gauge_value) {
116 return varnish_submit(plugin_instance, category, type, type_instance, (value_t){
117 .gauge=(gauge_t)gauge_value,
119 } /* }}} int varnish_submit_gauge */
121 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
122 const char *category, const char *type,
123 const char *type_instance,
124 uint64_t derive_value) {
125 return varnish_submit(plugin_instance, category, type, type_instance, (value_t){
126 .derive=(derive_t)derive_value,
128 } /* }}} int varnish_submit_derive */
130 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
131 static int varnish_monitor(void *priv,
132 const struct VSC_point *const pt) /* {{{ */
135 const user_config_t *conf;
145 class = pt->section->fantom->type;
146 name = pt->desc->name;
148 if (strcmp(class, "MAIN") != 0)
151 #elif HAVE_VARNISH_V3
155 if (strcmp(class, "") != 0)
159 val = *(const volatile uint64_t *)pt->ptr;
161 if (conf->collect_cache) {
162 if (strcmp(name, "cache_hit") == 0)
163 return varnish_submit_derive(conf->instance, "cache", "cache_result",
165 else if (strcmp(name, "cache_miss") == 0)
166 return varnish_submit_derive(conf->instance, "cache", "cache_result",
168 else if (strcmp(name, "cache_hitpass") == 0)
169 return varnish_submit_derive(conf->instance, "cache", "cache_result",
173 if (conf->collect_connections) {
174 if (strcmp(name, "client_conn") == 0)
175 return varnish_submit_derive(conf->instance, "connections",
176 "connections", "accepted", val);
177 else if (strcmp(name, "client_drop") == 0)
178 return varnish_submit_derive(conf->instance, "connections",
179 "connections", "dropped", val);
180 else if (strcmp(name, "client_req") == 0)
181 return varnish_submit_derive(conf->instance, "connections",
182 "connections", "received", val);
185 #ifdef HAVE_VARNISH_V3
186 if (conf->collect_dirdns) {
187 if (strcmp(name, "dir_dns_lookups") == 0)
188 return varnish_submit_derive(conf->instance, "dirdns",
189 "cache_operation", "lookups", val);
190 else if (strcmp(name, "dir_dns_failed") == 0)
191 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
193 else if (strcmp(name, "dir_dns_hit") == 0)
194 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
196 else if (strcmp(name, "dir_dns_cache_full") == 0)
197 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
202 if (conf->collect_esi) {
203 if (strcmp(name, "esi_errors") == 0)
204 return varnish_submit_derive(conf->instance, "esi", "total_operations",
206 else if (strcmp(name, "esi_parse") == 0)
207 return varnish_submit_derive(conf->instance, "esi", "total_operations",
209 else if (strcmp(name, "esi_warnings") == 0)
210 return varnish_submit_derive(conf->instance, "esi", "total_operations",
214 if (conf->collect_backend) {
215 if (strcmp(name, "backend_conn") == 0)
216 return varnish_submit_derive(conf->instance, "backend", "connections",
218 else if (strcmp(name, "backend_unhealthy") == 0)
219 return varnish_submit_derive(conf->instance, "backend", "connections",
220 "not-attempted", val);
221 else if (strcmp(name, "backend_busy") == 0)
222 return varnish_submit_derive(conf->instance, "backend", "connections",
224 else if (strcmp(name, "backend_fail") == 0)
225 return varnish_submit_derive(conf->instance, "backend", "connections",
227 else if (strcmp(name, "backend_reuse") == 0)
228 return varnish_submit_derive(conf->instance, "backend", "connections",
230 else if (strcmp(name, "backend_toolate") == 0)
231 return varnish_submit_derive(conf->instance, "backend", "connections",
233 else if (strcmp(name, "backend_recycle") == 0)
234 return varnish_submit_derive(conf->instance, "backend", "connections",
236 else if (strcmp(name, "backend_unused") == 0)
237 return varnish_submit_derive(conf->instance, "backend", "connections",
239 else if (strcmp(name, "backend_retry") == 0)
240 return varnish_submit_derive(conf->instance, "backend", "connections",
242 else if (strcmp(name, "backend_req") == 0)
243 return varnish_submit_derive(conf->instance, "backend", "http_requests",
245 else if (strcmp(name, "n_backend") == 0)
246 return varnish_submit_gauge(conf->instance, "backend", "backends",
250 if (conf->collect_fetch) {
251 if (strcmp(name, "fetch_head") == 0)
252 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
254 else if (strcmp(name, "fetch_length") == 0)
255 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
257 else if (strcmp(name, "fetch_chunked") == 0)
258 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
260 else if (strcmp(name, "fetch_eof") == 0)
261 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
263 else if (strcmp(name, "fetch_bad") == 0)
264 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
266 else if (strcmp(name, "fetch_close") == 0)
267 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
269 else if (strcmp(name, "fetch_oldhttp") == 0)
270 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
272 else if (strcmp(name, "fetch_zero") == 0)
273 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
275 else if (strcmp(name, "fetch_failed") == 0)
276 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
278 else if (strcmp(name, "fetch_1xx") == 0)
279 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
281 else if (strcmp(name, "fetch_204") == 0)
282 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
284 else if (strcmp(name, "fetch_304") == 0)
285 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
289 if (conf->collect_hcb) {
290 if (strcmp(name, "hcb_nolock") == 0)
291 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
292 "lookup_nolock", val);
293 else if (strcmp(name, "hcb_lock") == 0)
294 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
296 else if (strcmp(name, "hcb_insert") == 0)
297 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
301 if (conf->collect_objects) {
302 if (strcmp(name, "n_expired") == 0)
303 return varnish_submit_derive(conf->instance, "objects", "total_objects",
305 else if (strcmp(name, "n_lru_nuked") == 0)
306 return varnish_submit_derive(conf->instance, "objects", "total_objects",
308 else if (strcmp(name, "n_lru_saved") == 0)
309 return varnish_submit_derive(conf->instance, "objects", "total_objects",
311 else if (strcmp(name, "n_lru_moved") == 0)
312 return varnish_submit_derive(conf->instance, "objects", "total_objects",
314 else if (strcmp(name, "n_deathrow") == 0)
315 return varnish_submit_derive(conf->instance, "objects", "total_objects",
317 else if (strcmp(name, "losthdr") == 0)
318 return varnish_submit_derive(conf->instance, "objects", "total_objects",
319 "header_overflow", val);
320 else if (strcmp(name, "n_obj_purged") == 0)
321 return varnish_submit_derive(conf->instance, "objects", "total_objects",
323 else if (strcmp(name, "n_objsendfile") == 0)
324 return varnish_submit_derive(conf->instance, "objects", "total_objects",
325 "sent_sendfile", val);
326 else if (strcmp(name, "n_objwrite") == 0)
327 return varnish_submit_derive(conf->instance, "objects", "total_objects",
329 else if (strcmp(name, "n_objoverflow") == 0)
330 return varnish_submit_derive(conf->instance, "objects", "total_objects",
331 "workspace_overflow", val);
335 if (conf->collect_ban) {
336 if (strcmp(name, "n_ban") == 0)
337 return varnish_submit_derive(conf->instance, "ban", "total_operations",
339 else if (strcmp(name, "n_ban_add") == 0)
340 return varnish_submit_derive(conf->instance, "ban", "total_operations",
342 else if (strcmp(name, "n_ban_retire") == 0)
343 return varnish_submit_derive(conf->instance, "ban", "total_operations",
345 else if (strcmp(name, "n_ban_obj_test") == 0)
346 return varnish_submit_derive(conf->instance, "ban", "total_operations",
347 "objects_tested", val);
348 else if (strcmp(name, "n_ban_re_test") == 0)
349 return varnish_submit_derive(conf->instance, "ban", "total_operations",
350 "regexps_tested", val);
351 else if (strcmp(name, "n_ban_dups") == 0)
352 return varnish_submit_derive(conf->instance, "ban", "total_operations",
357 if (conf->collect_ban) {
358 if (strcmp(name, "bans") == 0)
359 return varnish_submit_derive(conf->instance, "ban", "total_operations",
361 else if (strcmp(name, "bans_added") == 0)
362 return varnish_submit_derive(conf->instance, "ban", "total_operations",
364 else if (strcmp(name, "bans_obj") == 0)
365 return varnish_submit_derive(conf->instance, "ban", "total_operations",
367 else if (strcmp(name, "bans_req") == 0)
368 return varnish_submit_derive(conf->instance, "ban", "total_operations",
370 else if (strcmp(name, "bans_completed") == 0)
371 return varnish_submit_derive(conf->instance, "ban", "total_operations",
373 else if (strcmp(name, "bans_deleted") == 0)
374 return varnish_submit_derive(conf->instance, "ban", "total_operations",
376 else if (strcmp(name, "bans_tested") == 0)
377 return varnish_submit_derive(conf->instance, "ban", "total_operations",
379 else if (strcmp(name, "bans_dups") == 0)
380 return varnish_submit_derive(conf->instance, "ban", "total_operations",
385 if (conf->collect_session) {
386 if (strcmp(name, "sess_closed") == 0)
387 return varnish_submit_derive(conf->instance, "session",
388 "total_operations", "closed", val);
389 else if (strcmp(name, "sess_pipeline") == 0)
390 return varnish_submit_derive(conf->instance, "session",
391 "total_operations", "pipeline", val);
392 else if (strcmp(name, "sess_readahead") == 0)
393 return varnish_submit_derive(conf->instance, "session",
394 "total_operations", "readahead", val);
395 else if (strcmp(name, "sess_conn") == 0)
396 return varnish_submit_derive(conf->instance, "session",
397 "total_operations", "accepted", val);
398 else if (strcmp(name, "sess_drop") == 0)
399 return varnish_submit_derive(conf->instance, "session",
400 "total_operations", "dropped", val);
401 else if (strcmp(name, "sess_fail") == 0)
402 return varnish_submit_derive(conf->instance, "session",
403 "total_operations", "failed", val);
404 else if (strcmp(name, "sess_pipe_overflow") == 0)
405 return varnish_submit_derive(conf->instance, "session",
406 "total_operations", "overflow", val);
407 else if (strcmp(name, "sess_queued") == 0)
408 return varnish_submit_derive(conf->instance, "session",
409 "total_operations", "queued", val);
410 else if (strcmp(name, "sess_linger") == 0)
411 return varnish_submit_derive(conf->instance, "session",
412 "total_operations", "linger", val);
413 else if (strcmp(name, "sess_herd") == 0)
414 return varnish_submit_derive(conf->instance, "session",
415 "total_operations", "herd", val);
418 if (conf->collect_shm) {
419 if (strcmp(name, "shm_records") == 0)
420 return varnish_submit_derive(conf->instance, "shm", "total_operations",
422 else if (strcmp(name, "shm_writes") == 0)
423 return varnish_submit_derive(conf->instance, "shm", "total_operations",
425 else if (strcmp(name, "shm_flushes") == 0)
426 return varnish_submit_derive(conf->instance, "shm", "total_operations",
428 else if (strcmp(name, "shm_cont") == 0)
429 return varnish_submit_derive(conf->instance, "shm", "total_operations",
431 else if (strcmp(name, "shm_cycles") == 0)
432 return varnish_submit_derive(conf->instance, "shm", "total_operations",
436 if (conf->collect_sms) {
437 if (strcmp(name, "sms_nreq") == 0)
438 return varnish_submit_derive(conf->instance, "sms", "total_requests",
440 else if (strcmp(name, "sms_nobj") == 0)
441 return varnish_submit_gauge(conf->instance, "sms", "requests",
443 else if (strcmp(name, "sms_nbytes") == 0)
444 return varnish_submit_gauge(conf->instance, "sms", "bytes",
446 else if (strcmp(name, "sms_balloc") == 0)
447 return varnish_submit_derive(conf->instance, "sms", "total_bytes",
449 else if (strcmp(name, "sms_bfree") == 0)
450 return varnish_submit_derive(conf->instance, "sms", "total_bytes",
454 if (conf->collect_struct) {
455 if (strcmp(name, "n_sess_mem") == 0)
456 return varnish_submit_gauge(conf->instance, "struct",
457 "current_sessions", "sess_mem", val);
458 else if (strcmp(name, "n_sess") == 0)
459 return varnish_submit_gauge(conf->instance, "struct",
460 "current_sessions", "sess", val);
461 else if (strcmp(name, "n_object") == 0)
462 return varnish_submit_gauge(conf->instance, "struct", "objects",
464 else if (strcmp(name, "n_vampireobject") == 0)
465 return varnish_submit_gauge(conf->instance, "struct", "objects",
466 "vampireobject", val);
467 else if (strcmp(name, "n_objectcore") == 0)
468 return varnish_submit_gauge(conf->instance, "struct", "objects",
470 else if (strcmp(name, "n_waitinglist") == 0)
471 return varnish_submit_gauge(conf->instance, "struct", "objects",
473 else if (strcmp(name, "n_objecthead") == 0)
474 return varnish_submit_gauge(conf->instance, "struct", "objects",
476 else if (strcmp(name, "n_smf") == 0)
477 return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
479 else if (strcmp(name, "n_smf_frag") == 0)
480 return varnish_submit_gauge(conf->instance, "struct", "objects",
482 else if (strcmp(name, "n_smf_large") == 0)
483 return varnish_submit_gauge(conf->instance, "struct", "objects",
485 else if (strcmp(name, "n_vbe_conn") == 0)
486 return varnish_submit_gauge(conf->instance, "struct", "objects",
490 if (conf->collect_totals) {
491 if (strcmp(name, "s_sess") == 0)
492 return varnish_submit_derive(conf->instance, "totals", "total_sessions",
494 else if (strcmp(name, "s_req") == 0)
495 return varnish_submit_derive(conf->instance, "totals", "total_requests",
497 else if (strcmp(name, "s_pipe") == 0)
498 return varnish_submit_derive(conf->instance, "totals",
499 "total_operations", "pipe", val);
500 else if (strcmp(name, "s_pass") == 0)
501 return varnish_submit_derive(conf->instance, "totals",
502 "total_operations", "pass", val);
503 else if (strcmp(name, "s_fetch") == 0)
504 return varnish_submit_derive(conf->instance, "totals",
505 "total_operations", "fetches", val);
506 else if (strcmp(name, "s_synth") == 0)
507 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
509 else if (strcmp(name, "s_req_hdrbytes") == 0)
510 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
512 else if (strcmp(name, "s_req_bodybytes") == 0)
513 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
515 else if (strcmp(name, "s_resp_hdrbytes") == 0)
516 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
518 else if (strcmp(name, "s_resp_bodybytes") == 0)
519 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
521 else if (strcmp(name, "s_pipe_hdrbytes") == 0)
522 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
524 else if (strcmp(name, "s_pipe_in") == 0)
525 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
527 else if (strcmp(name, "s_pipe_out") == 0)
528 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
530 else if (strcmp(name, "n_purges") == 0)
531 return varnish_submit_derive(conf->instance, "totals",
532 "total_operations", "purges", val);
533 else if (strcmp(name, "s_hdrbytes") == 0)
534 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
535 "header-bytes", val);
536 else if (strcmp(name, "s_bodybytes") == 0)
537 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
539 else if (strcmp(name, "n_gzip") == 0)
540 return varnish_submit_derive(conf->instance, "totals",
541 "total_operations", "gzip", val);
542 else if (strcmp(name, "n_gunzip") == 0)
543 return varnish_submit_derive(conf->instance, "totals",
544 "total_operations", "gunzip", val);
547 if (conf->collect_uptime) {
548 if (strcmp(name, "uptime") == 0)
549 return varnish_submit_gauge(conf->instance, "uptime", "uptime",
550 "client_uptime", val);
553 if (conf->collect_vcl) {
554 if (strcmp(name, "n_vcl") == 0)
555 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
557 else if (strcmp(name, "n_vcl_avail") == 0)
558 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
560 else if (strcmp(name, "n_vcl_discard") == 0)
561 return varnish_submit_gauge(conf->instance, "vcl", "vcl",
562 "discarded_vcl", val);
563 else if (strcmp(name, "vmods") == 0)
564 return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
568 if (conf->collect_workers) {
569 if (strcmp(name, "threads") == 0)
570 return varnish_submit_gauge(conf->instance, "workers", "threads",
572 else if (strcmp(name, "threads_created") == 0)
573 return varnish_submit_derive(conf->instance, "workers", "total_threads",
575 else if (strcmp(name, "threads_failed") == 0)
576 return varnish_submit_derive(conf->instance, "workers", "total_threads",
578 else if (strcmp(name, "threads_limited") == 0)
579 return varnish_submit_derive(conf->instance, "workers", "total_threads",
581 else if (strcmp(name, "threads_destroyed") == 0)
582 return varnish_submit_derive(conf->instance, "workers", "total_threads",
584 else if (strcmp(name, "thread_queue_len") == 0)
585 return varnish_submit_derive(conf->instance, "workers", "queue_length",
587 else if (strcmp(name, "n_wrk") == 0)
588 return varnish_submit_gauge(conf->instance, "workers", "threads",
590 else if (strcmp(name, "n_wrk_create") == 0)
591 return varnish_submit_derive(conf->instance, "workers", "total_threads",
593 else if (strcmp(name, "n_wrk_failed") == 0)
594 return varnish_submit_derive(conf->instance, "workers", "total_threads",
596 else if (strcmp(name, "n_wrk_max") == 0)
597 return varnish_submit_derive(conf->instance, "workers", "total_threads",
599 else if (strcmp(name, "n_wrk_drop") == 0)
600 return varnish_submit_derive(conf->instance, "workers", "total_threads",
602 else if (strcmp(name, "n_wrk_queue") == 0)
603 return varnish_submit_derive(conf->instance, "workers",
604 "total_requests", "queued", val);
605 else if (strcmp(name, "n_wrk_overflow") == 0)
606 return varnish_submit_derive(conf->instance, "workers",
607 "total_requests", "overflowed", val);
608 else if (strcmp(name, "n_wrk_queued") == 0)
609 return varnish_submit_derive(conf->instance, "workers",
610 "total_requests", "queued", val);
611 else if (strcmp(name, "n_wrk_lqueue") == 0)
612 return varnish_submit_derive(conf->instance, "workers",
613 "total_requests", "queue_length", val);
617 if (conf->collect_vsm) {
618 if (strcmp(name, "vsm_free") == 0)
619 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
620 else if (strcmp(name, "vsm_used") == 0)
621 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
622 else if (strcmp(name, "vsm_cooling") == 0)
623 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
625 else if (strcmp(name, "vsm_overflow") == 0)
626 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
628 else if (strcmp(name, "vsm_overflowed") == 0)
629 return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
636 } /* }}} static int varnish_monitor */
637 #else /* if HAVE_VARNISH_V2 */
638 static void varnish_monitor(const user_config_t *conf, /* {{{ */
639 const c_varnish_stats_t *stats) {
640 if (conf->collect_cache) {
642 varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
645 varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
647 /* Cache hits for pass */
648 varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
649 stats->cache_hitpass);
652 if (conf->collect_connections) {
653 /* Client connections accepted */
654 varnish_submit_derive(conf->instance, "connections", "connections",
655 "accepted", stats->client_conn);
656 /* Connection dropped, no sess */
657 varnish_submit_derive(conf->instance, "connections", "connections",
658 "dropped", stats->client_drop);
659 /* Client requests received */
660 varnish_submit_derive(conf->instance, "connections", "connections",
661 "received", stats->client_req);
664 if (conf->collect_esi) {
665 /* ESI parse errors (unlock) */
666 varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
668 /* Objects ESI parsed (unlock) */
669 varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
673 if (conf->collect_backend) {
674 /* Backend conn. success */
675 varnish_submit_derive(conf->instance, "backend", "connections", "success",
676 stats->backend_conn);
677 /* Backend conn. not attempted */
678 varnish_submit_derive(conf->instance, "backend", "connections",
679 "not-attempted", stats->backend_unhealthy);
680 /* Backend conn. too many */
681 varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
682 stats->backend_busy);
683 /* Backend conn. failures */
684 varnish_submit_derive(conf->instance, "backend", "connections", "failures",
685 stats->backend_fail);
686 /* Backend conn. reuses */
687 varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
688 stats->backend_reuse);
689 /* Backend conn. was closed */
690 varnish_submit_derive(conf->instance, "backend", "connections",
691 "was-closed", stats->backend_toolate);
692 /* Backend conn. recycles */
693 varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
694 stats->backend_recycle);
695 /* Backend conn. unused */
696 varnish_submit_derive(conf->instance, "backend", "connections", "unused",
697 stats->backend_unused);
698 /* Backend requests mades */
699 varnish_submit_derive(conf->instance, "backend", "http_requests",
700 "requests", stats->backend_req);
702 varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
706 if (conf->collect_fetch) {
708 varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
710 /* Fetch with length */
711 varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
712 stats->fetch_length);
714 varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
715 stats->fetch_chunked);
717 varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
719 /* Fetch bad headers */
720 varnish_submit_derive(conf->instance, "fetch", "http_requests",
721 "bad_headers", stats->fetch_bad);
722 /* Fetch wanted close */
723 varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
725 /* Fetch pre HTTP/1.1 closed */
726 varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
727 stats->fetch_oldhttp);
729 varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
732 varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
733 stats->fetch_failed);
736 if (conf->collect_hcb) {
737 /* HCB Lookups without lock */
738 varnish_submit_derive(conf->instance, "hcb", "cache_operation",
739 "lookup_nolock", stats->hcb_nolock);
740 /* HCB Lookups with lock */
741 varnish_submit_derive(conf->instance, "hcb", "cache_operation",
742 "lookup_lock", stats->hcb_lock);
744 varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
748 if (conf->collect_objects) {
749 /* N expired objects */
750 varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
752 /* N LRU nuked objects */
753 varnish_submit_derive(conf->instance, "objects", "total_objects",
754 "lru_nuked", stats->n_lru_nuked);
755 /* N LRU saved objects */
756 varnish_submit_derive(conf->instance, "objects", "total_objects",
757 "lru_saved", stats->n_lru_saved);
758 /* N LRU moved objects */
759 varnish_submit_derive(conf->instance, "objects", "total_objects",
760 "lru_moved", stats->n_lru_moved);
761 /* N objects on deathrow */
762 varnish_submit_derive(conf->instance, "objects", "total_objects",
763 "deathrow", stats->n_deathrow);
764 /* HTTP header overflows */
765 varnish_submit_derive(conf->instance, "objects", "total_objects",
766 "header_overflow", stats->losthdr);
767 /* Objects sent with sendfile */
768 varnish_submit_derive(conf->instance, "objects", "total_objects",
769 "sent_sendfile", stats->n_objsendfile);
770 /* Objects sent with write */
771 varnish_submit_derive(conf->instance, "objects", "total_objects",
772 "sent_write", stats->n_objwrite);
773 /* Objects overflowing workspace */
774 varnish_submit_derive(conf->instance, "objects", "total_objects",
775 "workspace_overflow", stats->n_objoverflow);
778 if (conf->collect_purge) {
779 /* N total active purges */
780 varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
782 /* N new purges added */
783 varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
785 /* N old purges deleted */
786 varnish_submit_derive(conf->instance, "purge", "total_operations",
787 "deleted", stats->n_purge_retire);
788 /* N objects tested */
789 varnish_submit_derive(conf->instance, "purge", "total_operations",
790 "objects_tested", stats->n_purge_obj_test);
791 /* N regexps tested against */
792 varnish_submit_derive(conf->instance, "purge", "total_operations",
793 "regexps_tested", stats->n_purge_re_test);
794 /* N duplicate purges removed */
795 varnish_submit_derive(conf->instance, "purge", "total_operations",
796 "duplicate", stats->n_purge_dups);
799 if (conf->collect_session) {
801 varnish_submit_derive(conf->instance, "session", "total_operations",
802 "closed", stats->sess_closed);
803 /* Session Pipeline */
804 varnish_submit_derive(conf->instance, "session", "total_operations",
805 "pipeline", stats->sess_pipeline);
806 /* Session Read Ahead */
807 varnish_submit_derive(conf->instance, "session", "total_operations",
808 "readahead", stats->sess_readahead);
810 varnish_submit_derive(conf->instance, "session", "total_operations",
811 "linger", stats->sess_linger);
813 varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
817 if (conf->collect_shm) {
819 varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
822 varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
824 /* SHM flushes due to overflow */
825 varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
827 /* SHM MTX contention */
828 varnish_submit_derive(conf->instance, "shm", "total_operations",
829 "contention", stats->shm_cont);
830 /* SHM cycles through buffer */
831 varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
835 if (conf->collect_sm) {
836 /* allocator requests */
837 varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
839 /* outstanding allocations */
840 varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
842 /* bytes allocated */
843 varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
846 varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
850 if (conf->collect_sma) {
851 /* SMA allocator requests */
852 varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
854 /* SMA outstanding allocations */
855 varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
857 /* SMA outstanding bytes */
858 varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
860 /* SMA bytes allocated */
861 varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
864 varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
868 if (conf->collect_sms) {
869 /* SMS allocator requests */
870 varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
872 /* SMS outstanding allocations */
873 varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
875 /* SMS outstanding bytes */
876 varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
878 /* SMS bytes allocated */
879 varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
881 /* SMS bytes freed */
882 varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
886 if (conf->collect_struct) {
887 /* N struct sess_mem */
888 varnish_submit_gauge(conf->instance, "struct", "current_sessions",
889 "sess_mem", stats->n_sess_mem);
891 varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
893 /* N struct object */
894 varnish_submit_gauge(conf->instance, "struct", "objects", "object",
896 /* N struct objecthead */
897 varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
898 stats->n_objecthead);
900 varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
902 /* N small free smf */
903 varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
905 /* N large free smf */
906 varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
908 /* N struct vbe_conn */
909 varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
913 if (conf->collect_totals) {
915 varnish_submit_derive(conf->instance, "totals", "total_sessions",
916 "sessions", stats->s_sess);
918 varnish_submit_derive(conf->instance, "totals", "total_requests",
919 "requests", stats->s_req);
921 varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
924 varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
927 varnish_submit_derive(conf->instance, "totals", "total_operations",
928 "fetches", stats->s_fetch);
929 /* Total header bytes */
930 varnish_submit_derive(conf->instance, "totals", "total_bytes",
931 "header-bytes", stats->s_hdrbytes);
932 /* Total body byte */
933 varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
937 if (conf->collect_vcl) {
939 varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
941 /* N vcl available */
942 varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
944 /* N vcl discarded */
945 varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
946 stats->n_vcl_discard);
949 if (conf->collect_workers) {
951 varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
953 /* worker threads created */
954 varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
955 stats->n_wrk_create);
956 /* worker threads not created */
957 varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
958 stats->n_wrk_failed);
959 /* worker threads limited */
960 varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
962 /* dropped work requests */
963 varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
965 /* queued work requests */
966 varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
968 /* overflowed work requests */
969 varnish_submit_derive(conf->instance, "workers", "total_requests",
970 "overflowed", stats->n_wrk_overflow);
973 } /* }}} void varnish_monitor */
976 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
977 static int varnish_read(user_data_t *ud) /* {{{ */
980 const c_varnish_stats_t *stats;
985 if ((ud == NULL) || (ud->data == NULL))
995 if (conf->instance != NULL) {
998 status = VSM_n_Arg(vd, conf->instance);
1001 ERROR("varnish plugin: VSM_n_Arg (\"%s\") failed "
1003 conf->instance, status);
1009 ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1010 #else /* if HAVE_VARNISH_V4 */
1011 ok = (VSM_Open(vd) == 0);
1015 ERROR("varnish plugin: Unable to open connection.");
1021 stats = VSC_Main(vd);
1022 #else /* if HAVE_VARNISH_V4 */
1023 stats = VSC_Main(vd, NULL);
1027 ERROR("varnish plugin: Unable to get statistics.");
1033 VSC_Iter(vd, varnish_monitor, conf);
1034 #else /* if HAVE_VARNISH_V4 */
1035 VSC_Iter(vd, NULL, varnish_monitor, conf);
1041 #else /* if HAVE_VARNISH_V2 */
1042 static int varnish_read(user_data_t *ud) /* {{{ */
1044 const c_varnish_stats_t *stats;
1046 user_config_t *conf;
1048 if ((ud == NULL) || (ud->data == NULL))
1053 stats = VSL_OpenStats(conf->instance);
1054 if (stats == NULL) {
1055 ERROR("Varnish plugin : unable to load statistics");
1060 varnish_monitor(conf, stats);
1066 static void varnish_config_free(void *ptr) /* {{{ */
1068 user_config_t *conf = ptr;
1073 sfree(conf->instance);
1077 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1082 conf->collect_backend = 1;
1083 conf->collect_cache = 1;
1084 conf->collect_connections = 1;
1085 #ifdef HAVE_VARNISH_V3
1086 conf->collect_dirdns = 0;
1088 conf->collect_esi = 0;
1089 conf->collect_fetch = 0;
1090 conf->collect_hcb = 0;
1091 conf->collect_objects = 0;
1093 conf->collect_purge = 0;
1095 conf->collect_ban = 0;
1097 conf->collect_session = 0;
1098 conf->collect_shm = 1;
1100 conf->collect_sm = 0;
1101 conf->collect_sma = 0;
1103 conf->collect_sms = 0;
1104 conf->collect_struct = 0;
1105 conf->collect_totals = 0;
1106 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1107 conf->collect_uptime = 0;
1109 conf->collect_vcl = 0;
1110 conf->collect_workers = 0;
1112 conf->collect_vsm = 0;
1116 } /* }}} int varnish_config_apply_default */
1118 static int varnish_init(void) /* {{{ */
1120 user_config_t *conf;
1125 conf = calloc(1, sizeof(*conf));
1129 /* Default settings: */
1130 conf->instance = NULL;
1132 varnish_config_apply_default(conf);
1134 plugin_register_complex_read(
1135 /* group = */ "varnish",
1136 /* name = */ "varnish/localhost",
1137 /* callback = */ varnish_read,
1138 /* interval = */ 0, &(user_data_t){
1139 .data = conf, .free_func = varnish_config_free,
1143 } /* }}} int varnish_init */
1145 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1147 user_config_t *conf;
1148 char callback_name[DATA_MAX_NAME_LEN];
1150 conf = calloc(1, sizeof(*conf));
1153 conf->instance = NULL;
1155 varnish_config_apply_default(conf);
1157 if (ci->values_num == 1) {
1160 status = cf_util_get_string(ci, &conf->instance);
1165 assert(conf->instance != NULL);
1167 if (strcmp("localhost", conf->instance) == 0) {
1168 sfree(conf->instance);
1169 conf->instance = NULL;
1171 } else if (ci->values_num > 1) {
1172 WARNING("Varnish plugin: \"Instance\" blocks accept only "
1178 for (int i = 0; i < ci->children_num; i++) {
1179 oconfig_item_t *child = ci->children + i;
1181 if (strcasecmp("CollectCache", child->key) == 0)
1182 cf_util_get_boolean(child, &conf->collect_cache);
1183 else if (strcasecmp("CollectConnections", child->key) == 0)
1184 cf_util_get_boolean(child, &conf->collect_connections);
1185 else if (strcasecmp("CollectESI", child->key) == 0)
1186 cf_util_get_boolean(child, &conf->collect_esi);
1187 else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1188 #ifdef HAVE_VARNISH_V3
1189 cf_util_get_boolean(child, &conf->collect_dirdns);
1191 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1194 else if (strcasecmp("CollectBackend", child->key) == 0)
1195 cf_util_get_boolean(child, &conf->collect_backend);
1196 else if (strcasecmp("CollectFetch", child->key) == 0)
1197 cf_util_get_boolean(child, &conf->collect_fetch);
1198 else if (strcasecmp("CollectHCB", child->key) == 0)
1199 cf_util_get_boolean(child, &conf->collect_hcb);
1200 else if (strcasecmp("CollectObjects", child->key) == 0)
1201 cf_util_get_boolean(child, &conf->collect_objects);
1202 else if (strcasecmp("CollectPurge", child->key) == 0)
1204 cf_util_get_boolean(child, &conf->collect_purge);
1206 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1209 else if (strcasecmp("CollectBan", child->key) == 0)
1211 WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1214 cf_util_get_boolean(child, &conf->collect_ban);
1216 else if (strcasecmp("CollectSession", child->key) == 0)
1217 cf_util_get_boolean(child, &conf->collect_session);
1218 else if (strcasecmp("CollectSHM", child->key) == 0)
1219 cf_util_get_boolean(child, &conf->collect_shm);
1220 else if (strcasecmp("CollectSMS", child->key) == 0)
1221 cf_util_get_boolean(child, &conf->collect_sms);
1222 else if (strcasecmp("CollectSMA", child->key) == 0)
1224 cf_util_get_boolean(child, &conf->collect_sma);
1226 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1229 else if (strcasecmp("CollectSM", child->key) == 0)
1231 cf_util_get_boolean(child, &conf->collect_sm);
1233 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1236 else if (strcasecmp("CollectStruct", child->key) == 0)
1237 cf_util_get_boolean(child, &conf->collect_struct);
1238 else if (strcasecmp("CollectTotals", child->key) == 0)
1239 cf_util_get_boolean(child, &conf->collect_totals);
1240 else if (strcasecmp("CollectUptime", child->key) == 0)
1241 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1242 cf_util_get_boolean(child, &conf->collect_uptime);
1244 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1245 child->key, "v3 and v4");
1247 else if (strcasecmp("CollectVCL", child->key) == 0)
1248 cf_util_get_boolean(child, &conf->collect_vcl);
1249 else if (strcasecmp("CollectWorkers", child->key) == 0)
1250 cf_util_get_boolean(child, &conf->collect_workers);
1251 else if (strcasecmp("CollectVSM", child->key) == 0)
1253 cf_util_get_boolean(child, &conf->collect_vsm);
1255 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1259 WARNING("Varnish plugin: Ignoring unknown "
1260 "configuration option: \"%s\". Did "
1261 "you forget to add an <Instance /> "
1262 "block around the configuration?",
1267 if (!conf->collect_cache && !conf->collect_connections &&
1268 !conf->collect_esi && !conf->collect_backend
1269 #ifdef HAVE_VARNISH_V3
1270 && !conf->collect_dirdns
1272 && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1274 && !conf->collect_purge
1276 && !conf->collect_ban
1278 && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1280 && !conf->collect_sma && !conf->collect_sm
1282 && !conf->collect_struct && !conf->collect_totals
1283 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1284 && !conf->collect_uptime
1286 && !conf->collect_vcl && !conf->collect_workers
1288 && !conf->collect_vsm
1291 WARNING("Varnish plugin: No metric has been configured for "
1292 "instance \"%s\". Disabling this instance.",
1293 (conf->instance == NULL) ? "localhost" : conf->instance);
1298 ssnprintf(callback_name, sizeof(callback_name), "varnish/%s",
1299 (conf->instance == NULL) ? "localhost" : conf->instance);
1301 plugin_register_complex_read(
1302 /* group = */ "varnish",
1303 /* name = */ callback_name,
1304 /* callback = */ varnish_read,
1305 /* interval = */ 0, &(user_data_t){
1306 .data = conf, .free_func = varnish_config_free,
1312 } /* }}} int varnish_config_instance */
1314 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1316 for (int i = 0; i < ci->children_num; i++) {
1317 oconfig_item_t *child = ci->children + i;
1319 if (strcasecmp("Instance", child->key) == 0)
1320 varnish_config_instance(child);
1322 WARNING("Varnish plugin: Ignoring unknown "
1323 "configuration option: \"%s\"",
1329 } /* }}} int varnish_config */
1331 void module_register(void) /* {{{ */
1333 plugin_register_complex_config("varnish", varnish_config);
1334 plugin_register_init("varnish", varnish_init);