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,
117 (value_t){.gauge = (gauge_t)gauge_value}));
118 } /* }}} int varnish_submit_gauge */
120 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
121 const char *category, const char *type,
122 const char *type_instance,
123 uint64_t derive_value) {
124 return (varnish_submit(plugin_instance, category, type, type_instance,
125 (value_t){.derive = (derive_t)derive_value}));
126 } /* }}} int varnish_submit_derive */
128 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
129 static int varnish_monitor(void *priv,
130 const struct VSC_point *const pt) /* {{{ */
133 const user_config_t *conf;
143 class = pt->section->fantom->type;
144 name = pt->desc->name;
146 if (strcmp(class, "MAIN") != 0)
149 #elif HAVE_VARNISH_V3
153 if (strcmp(class, "") != 0)
157 val = *(const volatile uint64_t *)pt->ptr;
159 if (conf->collect_cache) {
160 if (strcmp(name, "cache_hit") == 0)
161 return varnish_submit_derive(conf->instance, "cache", "cache_result",
163 else if (strcmp(name, "cache_miss") == 0)
164 return varnish_submit_derive(conf->instance, "cache", "cache_result",
166 else if (strcmp(name, "cache_hitpass") == 0)
167 return varnish_submit_derive(conf->instance, "cache", "cache_result",
171 if (conf->collect_connections) {
172 if (strcmp(name, "client_conn") == 0)
173 return varnish_submit_derive(conf->instance, "connections", "connections",
175 else if (strcmp(name, "client_drop") == 0)
176 return varnish_submit_derive(conf->instance, "connections", "connections",
178 else if (strcmp(name, "client_req") == 0)
179 return varnish_submit_derive(conf->instance, "connections", "connections",
183 #ifdef HAVE_VARNISH_V3
184 if (conf->collect_dirdns) {
185 if (strcmp(name, "dir_dns_lookups") == 0)
186 return varnish_submit_derive(conf->instance, "dirdns", "cache_operation",
188 else if (strcmp(name, "dir_dns_failed") == 0)
189 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
191 else if (strcmp(name, "dir_dns_hit") == 0)
192 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
194 else if (strcmp(name, "dir_dns_cache_full") == 0)
195 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
200 if (conf->collect_esi) {
201 if (strcmp(name, "esi_errors") == 0)
202 return varnish_submit_derive(conf->instance, "esi", "total_operations",
204 else if (strcmp(name, "esi_parse") == 0)
205 return varnish_submit_derive(conf->instance, "esi", "total_operations",
207 else if (strcmp(name, "esi_warnings") == 0)
208 return varnish_submit_derive(conf->instance, "esi", "total_operations",
212 if (conf->collect_backend) {
213 if (strcmp(name, "backend_conn") == 0)
214 return varnish_submit_derive(conf->instance, "backend", "connections",
216 else if (strcmp(name, "backend_unhealthy") == 0)
217 return varnish_submit_derive(conf->instance, "backend", "connections",
218 "not-attempted", val);
219 else if (strcmp(name, "backend_busy") == 0)
220 return varnish_submit_derive(conf->instance, "backend", "connections",
222 else if (strcmp(name, "backend_fail") == 0)
223 return varnish_submit_derive(conf->instance, "backend", "connections",
225 else if (strcmp(name, "backend_reuse") == 0)
226 return varnish_submit_derive(conf->instance, "backend", "connections",
228 else if (strcmp(name, "backend_toolate") == 0)
229 return varnish_submit_derive(conf->instance, "backend", "connections",
231 else if (strcmp(name, "backend_recycle") == 0)
232 return varnish_submit_derive(conf->instance, "backend", "connections",
234 else if (strcmp(name, "backend_unused") == 0)
235 return varnish_submit_derive(conf->instance, "backend", "connections",
237 else if (strcmp(name, "backend_retry") == 0)
238 return varnish_submit_derive(conf->instance, "backend", "connections",
240 else if (strcmp(name, "backend_req") == 0)
241 return varnish_submit_derive(conf->instance, "backend", "http_requests",
243 else if (strcmp(name, "n_backend") == 0)
244 return varnish_submit_gauge(conf->instance, "backend", "backends",
248 if (conf->collect_fetch) {
249 if (strcmp(name, "fetch_head") == 0)
250 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
252 else if (strcmp(name, "fetch_length") == 0)
253 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
255 else if (strcmp(name, "fetch_chunked") == 0)
256 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
258 else if (strcmp(name, "fetch_eof") == 0)
259 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
261 else if (strcmp(name, "fetch_bad") == 0)
262 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
264 else if (strcmp(name, "fetch_close") == 0)
265 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
267 else if (strcmp(name, "fetch_oldhttp") == 0)
268 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
270 else if (strcmp(name, "fetch_zero") == 0)
271 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
273 else if (strcmp(name, "fetch_failed") == 0)
274 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
276 else if (strcmp(name, "fetch_1xx") == 0)
277 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
279 else if (strcmp(name, "fetch_204") == 0)
280 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
282 else if (strcmp(name, "fetch_304") == 0)
283 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
287 if (conf->collect_hcb) {
288 if (strcmp(name, "hcb_nolock") == 0)
289 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
290 "lookup_nolock", val);
291 else if (strcmp(name, "hcb_lock") == 0)
292 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
294 else if (strcmp(name, "hcb_insert") == 0)
295 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
299 if (conf->collect_objects) {
300 if (strcmp(name, "n_expired") == 0)
301 return varnish_submit_derive(conf->instance, "objects", "total_objects",
303 else if (strcmp(name, "n_lru_nuked") == 0)
304 return varnish_submit_derive(conf->instance, "objects", "total_objects",
306 else if (strcmp(name, "n_lru_saved") == 0)
307 return varnish_submit_derive(conf->instance, "objects", "total_objects",
309 else if (strcmp(name, "n_lru_moved") == 0)
310 return varnish_submit_derive(conf->instance, "objects", "total_objects",
312 else if (strcmp(name, "n_deathrow") == 0)
313 return varnish_submit_derive(conf->instance, "objects", "total_objects",
315 else if (strcmp(name, "losthdr") == 0)
316 return varnish_submit_derive(conf->instance, "objects", "total_objects",
317 "header_overflow", val);
318 else if (strcmp(name, "n_obj_purged") == 0)
319 return varnish_submit_derive(conf->instance, "objects", "total_objects",
321 else if (strcmp(name, "n_objsendfile") == 0)
322 return varnish_submit_derive(conf->instance, "objects", "total_objects",
323 "sent_sendfile", val);
324 else if (strcmp(name, "n_objwrite") == 0)
325 return varnish_submit_derive(conf->instance, "objects", "total_objects",
327 else if (strcmp(name, "n_objoverflow") == 0)
328 return varnish_submit_derive(conf->instance, "objects", "total_objects",
329 "workspace_overflow", val);
333 if (conf->collect_ban) {
334 if (strcmp(name, "n_ban") == 0)
335 return varnish_submit_derive(conf->instance, "ban", "total_operations",
337 else if (strcmp(name, "n_ban_add") == 0)
338 return varnish_submit_derive(conf->instance, "ban", "total_operations",
340 else if (strcmp(name, "n_ban_retire") == 0)
341 return varnish_submit_derive(conf->instance, "ban", "total_operations",
343 else if (strcmp(name, "n_ban_obj_test") == 0)
344 return varnish_submit_derive(conf->instance, "ban", "total_operations",
345 "objects_tested", val);
346 else if (strcmp(name, "n_ban_re_test") == 0)
347 return varnish_submit_derive(conf->instance, "ban", "total_operations",
348 "regexps_tested", val);
349 else if (strcmp(name, "n_ban_dups") == 0)
350 return varnish_submit_derive(conf->instance, "ban", "total_operations",
355 if (conf->collect_ban) {
356 if (strcmp(name, "bans") == 0)
357 return varnish_submit_derive(conf->instance, "ban", "total_operations",
359 else if (strcmp(name, "bans_added") == 0)
360 return varnish_submit_derive(conf->instance, "ban", "total_operations",
362 else if (strcmp(name, "bans_obj") == 0)
363 return varnish_submit_derive(conf->instance, "ban", "total_operations",
365 else if (strcmp(name, "bans_req") == 0)
366 return varnish_submit_derive(conf->instance, "ban", "total_operations",
368 else if (strcmp(name, "bans_completed") == 0)
369 return varnish_submit_derive(conf->instance, "ban", "total_operations",
371 else if (strcmp(name, "bans_deleted") == 0)
372 return varnish_submit_derive(conf->instance, "ban", "total_operations",
374 else if (strcmp(name, "bans_tested") == 0)
375 return varnish_submit_derive(conf->instance, "ban", "total_operations",
377 else if (strcmp(name, "bans_dups") == 0)
378 return varnish_submit_derive(conf->instance, "ban", "total_operations",
383 if (conf->collect_session) {
384 if (strcmp(name, "sess_closed") == 0)
385 return varnish_submit_derive(conf->instance, "session",
386 "total_operations", "closed", val);
387 else if (strcmp(name, "sess_pipeline") == 0)
388 return varnish_submit_derive(conf->instance, "session",
389 "total_operations", "pipeline", val);
390 else if (strcmp(name, "sess_readahead") == 0)
391 return varnish_submit_derive(conf->instance, "session",
392 "total_operations", "readahead", val);
393 else if (strcmp(name, "sess_conn") == 0)
394 return varnish_submit_derive(conf->instance, "session",
395 "total_operations", "accepted", val);
396 else if (strcmp(name, "sess_drop") == 0)
397 return varnish_submit_derive(conf->instance, "session",
398 "total_operations", "dropped", val);
399 else if (strcmp(name, "sess_fail") == 0)
400 return varnish_submit_derive(conf->instance, "session",
401 "total_operations", "failed", val);
402 else if (strcmp(name, "sess_pipe_overflow") == 0)
403 return varnish_submit_derive(conf->instance, "session",
404 "total_operations", "overflow", val);
405 else if (strcmp(name, "sess_queued") == 0)
406 return varnish_submit_derive(conf->instance, "session",
407 "total_operations", "queued", val);
408 else if (strcmp(name, "sess_linger") == 0)
409 return varnish_submit_derive(conf->instance, "session",
410 "total_operations", "linger", val);
411 else if (strcmp(name, "sess_herd") == 0)
412 return varnish_submit_derive(conf->instance, "session",
413 "total_operations", "herd", val);
416 if (conf->collect_shm) {
417 if (strcmp(name, "shm_records") == 0)
418 return varnish_submit_derive(conf->instance, "shm", "total_operations",
420 else if (strcmp(name, "shm_writes") == 0)
421 return varnish_submit_derive(conf->instance, "shm", "total_operations",
423 else if (strcmp(name, "shm_flushes") == 0)
424 return varnish_submit_derive(conf->instance, "shm", "total_operations",
426 else if (strcmp(name, "shm_cont") == 0)
427 return varnish_submit_derive(conf->instance, "shm", "total_operations",
429 else if (strcmp(name, "shm_cycles") == 0)
430 return varnish_submit_derive(conf->instance, "shm", "total_operations",
434 if (conf->collect_sms) {
435 if (strcmp(name, "sms_nreq") == 0)
436 return varnish_submit_derive(conf->instance, "sms", "total_requests",
438 else if (strcmp(name, "sms_nobj") == 0)
439 return varnish_submit_gauge(conf->instance, "sms", "requests",
441 else if (strcmp(name, "sms_nbytes") == 0)
442 return varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
444 else if (strcmp(name, "sms_balloc") == 0)
445 return varnish_submit_derive(conf->instance, "sms", "total_bytes",
447 else if (strcmp(name, "sms_bfree") == 0)
448 return varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
452 if (conf->collect_struct) {
453 if (strcmp(name, "n_sess_mem") == 0)
454 return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
456 else if (strcmp(name, "n_sess") == 0)
457 return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
459 else if (strcmp(name, "n_object") == 0)
460 return varnish_submit_gauge(conf->instance, "struct", "objects", "object",
462 else if (strcmp(name, "n_vampireobject") == 0)
463 return varnish_submit_gauge(conf->instance, "struct", "objects",
464 "vampireobject", val);
465 else if (strcmp(name, "n_objectcore") == 0)
466 return varnish_submit_gauge(conf->instance, "struct", "objects",
468 else if (strcmp(name, "n_waitinglist") == 0)
469 return varnish_submit_gauge(conf->instance, "struct", "objects",
471 else if (strcmp(name, "n_objecthead") == 0)
472 return varnish_submit_gauge(conf->instance, "struct", "objects",
474 else if (strcmp(name, "n_smf") == 0)
475 return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
477 else if (strcmp(name, "n_smf_frag") == 0)
478 return varnish_submit_gauge(conf->instance, "struct", "objects",
480 else if (strcmp(name, "n_smf_large") == 0)
481 return varnish_submit_gauge(conf->instance, "struct", "objects",
483 else if (strcmp(name, "n_vbe_conn") == 0)
484 return varnish_submit_gauge(conf->instance, "struct", "objects",
488 if (conf->collect_totals) {
489 if (strcmp(name, "s_sess") == 0)
490 return varnish_submit_derive(conf->instance, "totals", "total_sessions",
492 else if (strcmp(name, "s_req") == 0)
493 return varnish_submit_derive(conf->instance, "totals", "total_requests",
495 else if (strcmp(name, "s_pipe") == 0)
496 return varnish_submit_derive(conf->instance, "totals", "total_operations",
498 else if (strcmp(name, "s_pass") == 0)
499 return varnish_submit_derive(conf->instance, "totals", "total_operations",
501 else if (strcmp(name, "s_fetch") == 0)
502 return varnish_submit_derive(conf->instance, "totals", "total_operations",
504 else if (strcmp(name, "s_synth") == 0)
505 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
507 else if (strcmp(name, "s_req_hdrbytes") == 0)
508 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
510 else if (strcmp(name, "s_req_bodybytes") == 0)
511 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
513 else if (strcmp(name, "s_resp_hdrbytes") == 0)
514 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
516 else if (strcmp(name, "s_resp_bodybytes") == 0)
517 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
519 else if (strcmp(name, "s_pipe_hdrbytes") == 0)
520 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
522 else if (strcmp(name, "s_pipe_in") == 0)
523 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
525 else if (strcmp(name, "s_pipe_out") == 0)
526 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
528 else if (strcmp(name, "n_purges") == 0)
529 return varnish_submit_derive(conf->instance, "totals", "total_operations",
531 else if (strcmp(name, "s_hdrbytes") == 0)
532 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
533 "header-bytes", val);
534 else if (strcmp(name, "s_bodybytes") == 0)
535 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
537 else if (strcmp(name, "n_gzip") == 0)
538 return varnish_submit_derive(conf->instance, "totals", "total_operations",
540 else if (strcmp(name, "n_gunzip") == 0)
541 return varnish_submit_derive(conf->instance, "totals", "total_operations",
545 if (conf->collect_uptime) {
546 if (strcmp(name, "uptime") == 0)
547 return varnish_submit_gauge(conf->instance, "uptime", "uptime",
548 "client_uptime", val);
551 if (conf->collect_vcl) {
552 if (strcmp(name, "n_vcl") == 0)
553 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
555 else if (strcmp(name, "n_vcl_avail") == 0)
556 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
558 else if (strcmp(name, "n_vcl_discard") == 0)
559 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
561 else if (strcmp(name, "vmods") == 0)
562 return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
566 if (conf->collect_workers) {
567 if (strcmp(name, "threads") == 0)
568 return varnish_submit_gauge(conf->instance, "workers", "threads",
570 else if (strcmp(name, "threads_created") == 0)
571 return varnish_submit_derive(conf->instance, "workers", "total_threads",
573 else if (strcmp(name, "threads_failed") == 0)
574 return varnish_submit_derive(conf->instance, "workers", "total_threads",
576 else if (strcmp(name, "threads_limited") == 0)
577 return varnish_submit_derive(conf->instance, "workers", "total_threads",
579 else if (strcmp(name, "threads_destroyed") == 0)
580 return varnish_submit_derive(conf->instance, "workers", "total_threads",
582 else if (strcmp(name, "thread_queue_len") == 0)
583 return varnish_submit_derive(conf->instance, "workers", "queue_length",
585 else if (strcmp(name, "n_wrk") == 0)
586 return varnish_submit_gauge(conf->instance, "workers", "threads",
588 else if (strcmp(name, "n_wrk_create") == 0)
589 return varnish_submit_derive(conf->instance, "workers", "total_threads",
591 else if (strcmp(name, "n_wrk_failed") == 0)
592 return varnish_submit_derive(conf->instance, "workers", "total_threads",
594 else if (strcmp(name, "n_wrk_max") == 0)
595 return varnish_submit_derive(conf->instance, "workers", "total_threads",
597 else if (strcmp(name, "n_wrk_drop") == 0)
598 return varnish_submit_derive(conf->instance, "workers", "total_threads",
600 else if (strcmp(name, "n_wrk_queue") == 0)
601 return varnish_submit_derive(conf->instance, "workers", "total_requests",
603 else if (strcmp(name, "n_wrk_overflow") == 0)
604 return varnish_submit_derive(conf->instance, "workers", "total_requests",
606 else if (strcmp(name, "n_wrk_queued") == 0)
607 return varnish_submit_derive(conf->instance, "workers", "total_requests",
609 else if (strcmp(name, "n_wrk_lqueue") == 0)
610 return varnish_submit_derive(conf->instance, "workers", "total_requests",
611 "queue_length", val);
615 if (conf->collect_vsm) {
616 if (strcmp(name, "vsm_free") == 0)
617 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
618 else if (strcmp(name, "vsm_used") == 0)
619 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
620 else if (strcmp(name, "vsm_cooling") == 0)
621 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
623 else if (strcmp(name, "vsm_overflow") == 0)
624 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
626 else if (strcmp(name, "vsm_overflowed") == 0)
627 return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
634 } /* }}} static int varnish_monitor */
635 #else /* if HAVE_VARNISH_V2 */
636 static void varnish_monitor(const user_config_t *conf, /* {{{ */
637 const c_varnish_stats_t *stats) {
638 if (conf->collect_cache) {
640 varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
643 varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
645 /* Cache hits for pass */
646 varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
647 stats->cache_hitpass);
650 if (conf->collect_connections) {
651 /* Client connections accepted */
652 varnish_submit_derive(conf->instance, "connections", "connections",
653 "accepted", stats->client_conn);
654 /* Connection dropped, no sess */
655 varnish_submit_derive(conf->instance, "connections", "connections",
656 "dropped", stats->client_drop);
657 /* Client requests received */
658 varnish_submit_derive(conf->instance, "connections", "connections",
659 "received", stats->client_req);
662 if (conf->collect_esi) {
663 /* ESI parse errors (unlock) */
664 varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
666 /* Objects ESI parsed (unlock) */
667 varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
671 if (conf->collect_backend) {
672 /* Backend conn. success */
673 varnish_submit_derive(conf->instance, "backend", "connections", "success",
674 stats->backend_conn);
675 /* Backend conn. not attempted */
676 varnish_submit_derive(conf->instance, "backend", "connections",
677 "not-attempted", stats->backend_unhealthy);
678 /* Backend conn. too many */
679 varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
680 stats->backend_busy);
681 /* Backend conn. failures */
682 varnish_submit_derive(conf->instance, "backend", "connections", "failures",
683 stats->backend_fail);
684 /* Backend conn. reuses */
685 varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
686 stats->backend_reuse);
687 /* Backend conn. was closed */
688 varnish_submit_derive(conf->instance, "backend", "connections",
689 "was-closed", stats->backend_toolate);
690 /* Backend conn. recycles */
691 varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
692 stats->backend_recycle);
693 /* Backend conn. unused */
694 varnish_submit_derive(conf->instance, "backend", "connections", "unused",
695 stats->backend_unused);
696 /* Backend requests mades */
697 varnish_submit_derive(conf->instance, "backend", "http_requests",
698 "requests", stats->backend_req);
700 varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
704 if (conf->collect_fetch) {
706 varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
708 /* Fetch with length */
709 varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
710 stats->fetch_length);
712 varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
713 stats->fetch_chunked);
715 varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
717 /* Fetch bad headers */
718 varnish_submit_derive(conf->instance, "fetch", "http_requests",
719 "bad_headers", stats->fetch_bad);
720 /* Fetch wanted close */
721 varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
723 /* Fetch pre HTTP/1.1 closed */
724 varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
725 stats->fetch_oldhttp);
727 varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
730 varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
731 stats->fetch_failed);
734 if (conf->collect_hcb) {
735 /* HCB Lookups without lock */
736 varnish_submit_derive(conf->instance, "hcb", "cache_operation",
737 "lookup_nolock", stats->hcb_nolock);
738 /* HCB Lookups with lock */
739 varnish_submit_derive(conf->instance, "hcb", "cache_operation",
740 "lookup_lock", stats->hcb_lock);
742 varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
746 if (conf->collect_objects) {
747 /* N expired objects */
748 varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
750 /* N LRU nuked objects */
751 varnish_submit_derive(conf->instance, "objects", "total_objects",
752 "lru_nuked", stats->n_lru_nuked);
753 /* N LRU saved objects */
754 varnish_submit_derive(conf->instance, "objects", "total_objects",
755 "lru_saved", stats->n_lru_saved);
756 /* N LRU moved objects */
757 varnish_submit_derive(conf->instance, "objects", "total_objects",
758 "lru_moved", stats->n_lru_moved);
759 /* N objects on deathrow */
760 varnish_submit_derive(conf->instance, "objects", "total_objects",
761 "deathrow", stats->n_deathrow);
762 /* HTTP header overflows */
763 varnish_submit_derive(conf->instance, "objects", "total_objects",
764 "header_overflow", stats->losthdr);
765 /* Objects sent with sendfile */
766 varnish_submit_derive(conf->instance, "objects", "total_objects",
767 "sent_sendfile", stats->n_objsendfile);
768 /* Objects sent with write */
769 varnish_submit_derive(conf->instance, "objects", "total_objects",
770 "sent_write", stats->n_objwrite);
771 /* Objects overflowing workspace */
772 varnish_submit_derive(conf->instance, "objects", "total_objects",
773 "workspace_overflow", stats->n_objoverflow);
776 if (conf->collect_purge) {
777 /* N total active purges */
778 varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
780 /* N new purges added */
781 varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
783 /* N old purges deleted */
784 varnish_submit_derive(conf->instance, "purge", "total_operations",
785 "deleted", stats->n_purge_retire);
786 /* N objects tested */
787 varnish_submit_derive(conf->instance, "purge", "total_operations",
788 "objects_tested", stats->n_purge_obj_test);
789 /* N regexps tested against */
790 varnish_submit_derive(conf->instance, "purge", "total_operations",
791 "regexps_tested", stats->n_purge_re_test);
792 /* N duplicate purges removed */
793 varnish_submit_derive(conf->instance, "purge", "total_operations",
794 "duplicate", stats->n_purge_dups);
797 if (conf->collect_session) {
799 varnish_submit_derive(conf->instance, "session", "total_operations",
800 "closed", stats->sess_closed);
801 /* Session Pipeline */
802 varnish_submit_derive(conf->instance, "session", "total_operations",
803 "pipeline", stats->sess_pipeline);
804 /* Session Read Ahead */
805 varnish_submit_derive(conf->instance, "session", "total_operations",
806 "readahead", stats->sess_readahead);
808 varnish_submit_derive(conf->instance, "session", "total_operations",
809 "linger", stats->sess_linger);
811 varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
815 if (conf->collect_shm) {
817 varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
820 varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
822 /* SHM flushes due to overflow */
823 varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
825 /* SHM MTX contention */
826 varnish_submit_derive(conf->instance, "shm", "total_operations",
827 "contention", stats->shm_cont);
828 /* SHM cycles through buffer */
829 varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
833 if (conf->collect_sm) {
834 /* allocator requests */
835 varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
837 /* outstanding allocations */
838 varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
840 /* bytes allocated */
841 varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
844 varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
848 if (conf->collect_sma) {
849 /* SMA allocator requests */
850 varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
852 /* SMA outstanding allocations */
853 varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
855 /* SMA outstanding bytes */
856 varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
858 /* SMA bytes allocated */
859 varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
862 varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
866 if (conf->collect_sms) {
867 /* SMS allocator requests */
868 varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
870 /* SMS outstanding allocations */
871 varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
873 /* SMS outstanding bytes */
874 varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
876 /* SMS bytes allocated */
877 varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
879 /* SMS bytes freed */
880 varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
884 if (conf->collect_struct) {
885 /* N struct sess_mem */
886 varnish_submit_gauge(conf->instance, "struct", "current_sessions",
887 "sess_mem", stats->n_sess_mem);
889 varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
891 /* N struct object */
892 varnish_submit_gauge(conf->instance, "struct", "objects", "object",
894 /* N struct objecthead */
895 varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
896 stats->n_objecthead);
898 varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
900 /* N small free smf */
901 varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
903 /* N large free smf */
904 varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
906 /* N struct vbe_conn */
907 varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
911 if (conf->collect_totals) {
913 varnish_submit_derive(conf->instance, "totals", "total_sessions",
914 "sessions", stats->s_sess);
916 varnish_submit_derive(conf->instance, "totals", "total_requests",
917 "requests", stats->s_req);
919 varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
922 varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
925 varnish_submit_derive(conf->instance, "totals", "total_operations",
926 "fetches", stats->s_fetch);
927 /* Total header bytes */
928 varnish_submit_derive(conf->instance, "totals", "total_bytes",
929 "header-bytes", stats->s_hdrbytes);
930 /* Total body byte */
931 varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
935 if (conf->collect_vcl) {
937 varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
939 /* N vcl available */
940 varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
942 /* N vcl discarded */
943 varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
944 stats->n_vcl_discard);
947 if (conf->collect_workers) {
949 varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
951 /* worker threads created */
952 varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
953 stats->n_wrk_create);
954 /* worker threads not created */
955 varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
956 stats->n_wrk_failed);
957 /* worker threads limited */
958 varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
960 /* dropped work requests */
961 varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
963 /* queued work requests */
964 varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
966 /* overflowed work requests */
967 varnish_submit_derive(conf->instance, "workers", "total_requests",
968 "overflowed", stats->n_wrk_overflow);
971 } /* }}} void varnish_monitor */
974 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
975 static int varnish_read(user_data_t *ud) /* {{{ */
978 const c_varnish_stats_t *stats;
983 if ((ud == NULL) || (ud->data == NULL))
993 if (conf->instance != NULL) {
996 status = VSM_n_Arg(vd, conf->instance);
999 ERROR("varnish plugin: VSM_n_Arg (\"%s\") failed "
1001 conf->instance, status);
1007 ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1008 #else /* if HAVE_VARNISH_V4 */
1009 ok = (VSM_Open(vd) == 0);
1013 ERROR("varnish plugin: Unable to open connection.");
1019 stats = VSC_Main(vd);
1020 #else /* if HAVE_VARNISH_V4 */
1021 stats = VSC_Main(vd, NULL);
1025 ERROR("varnish plugin: Unable to get statistics.");
1031 VSC_Iter(vd, varnish_monitor, conf);
1032 #else /* if HAVE_VARNISH_V4 */
1033 VSC_Iter(vd, NULL, varnish_monitor, conf);
1039 #else /* if HAVE_VARNISH_V2 */
1040 static int varnish_read(user_data_t *ud) /* {{{ */
1042 const c_varnish_stats_t *stats;
1044 user_config_t *conf;
1046 if ((ud == NULL) || (ud->data == NULL))
1051 stats = VSL_OpenStats(conf->instance);
1052 if (stats == NULL) {
1053 ERROR("Varnish plugin : unable to load statistics");
1058 varnish_monitor(conf, stats);
1064 static void varnish_config_free(void *ptr) /* {{{ */
1066 user_config_t *conf = ptr;
1071 sfree(conf->instance);
1075 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1080 conf->collect_backend = 1;
1081 conf->collect_cache = 1;
1082 conf->collect_connections = 1;
1083 #ifdef HAVE_VARNISH_V3
1084 conf->collect_dirdns = 0;
1086 conf->collect_esi = 0;
1087 conf->collect_fetch = 0;
1088 conf->collect_hcb = 0;
1089 conf->collect_objects = 0;
1091 conf->collect_purge = 0;
1093 conf->collect_ban = 0;
1095 conf->collect_session = 0;
1096 conf->collect_shm = 1;
1098 conf->collect_sm = 0;
1099 conf->collect_sma = 0;
1101 conf->collect_sms = 0;
1102 conf->collect_struct = 0;
1103 conf->collect_totals = 0;
1104 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1105 conf->collect_uptime = 0;
1107 conf->collect_vcl = 0;
1108 conf->collect_workers = 0;
1110 conf->collect_vsm = 0;
1114 } /* }}} int varnish_config_apply_default */
1116 static int varnish_init(void) /* {{{ */
1118 user_config_t *conf;
1123 conf = calloc(1, sizeof(*conf));
1127 /* Default settings: */
1128 conf->instance = NULL;
1130 varnish_config_apply_default(conf);
1132 plugin_register_complex_read(
1133 /* group = */ "varnish",
1134 /* name = */ "varnish/localhost",
1135 /* callback = */ varnish_read,
1136 /* interval = */ 0, &(user_data_t){
1137 .data = conf, .free_func = varnish_config_free,
1141 } /* }}} int varnish_init */
1143 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1145 user_config_t *conf;
1146 char callback_name[DATA_MAX_NAME_LEN];
1148 conf = calloc(1, sizeof(*conf));
1151 conf->instance = NULL;
1153 varnish_config_apply_default(conf);
1155 if (ci->values_num == 1) {
1158 status = cf_util_get_string(ci, &conf->instance);
1163 assert(conf->instance != NULL);
1165 if (strcmp("localhost", conf->instance) == 0) {
1166 sfree(conf->instance);
1167 conf->instance = NULL;
1169 } else if (ci->values_num > 1) {
1170 WARNING("Varnish plugin: \"Instance\" blocks accept only "
1176 for (int i = 0; i < ci->children_num; i++) {
1177 oconfig_item_t *child = ci->children + i;
1179 if (strcasecmp("CollectCache", child->key) == 0)
1180 cf_util_get_boolean(child, &conf->collect_cache);
1181 else if (strcasecmp("CollectConnections", child->key) == 0)
1182 cf_util_get_boolean(child, &conf->collect_connections);
1183 else if (strcasecmp("CollectESI", child->key) == 0)
1184 cf_util_get_boolean(child, &conf->collect_esi);
1185 else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1186 #ifdef HAVE_VARNISH_V3
1187 cf_util_get_boolean(child, &conf->collect_dirdns);
1189 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1192 else if (strcasecmp("CollectBackend", child->key) == 0)
1193 cf_util_get_boolean(child, &conf->collect_backend);
1194 else if (strcasecmp("CollectFetch", child->key) == 0)
1195 cf_util_get_boolean(child, &conf->collect_fetch);
1196 else if (strcasecmp("CollectHCB", child->key) == 0)
1197 cf_util_get_boolean(child, &conf->collect_hcb);
1198 else if (strcasecmp("CollectObjects", child->key) == 0)
1199 cf_util_get_boolean(child, &conf->collect_objects);
1200 else if (strcasecmp("CollectPurge", child->key) == 0)
1202 cf_util_get_boolean(child, &conf->collect_purge);
1204 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1207 else if (strcasecmp("CollectBan", child->key) == 0)
1209 WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1212 cf_util_get_boolean(child, &conf->collect_ban);
1214 else if (strcasecmp("CollectSession", child->key) == 0)
1215 cf_util_get_boolean(child, &conf->collect_session);
1216 else if (strcasecmp("CollectSHM", child->key) == 0)
1217 cf_util_get_boolean(child, &conf->collect_shm);
1218 else if (strcasecmp("CollectSMS", child->key) == 0)
1219 cf_util_get_boolean(child, &conf->collect_sms);
1220 else if (strcasecmp("CollectSMA", child->key) == 0)
1222 cf_util_get_boolean(child, &conf->collect_sma);
1224 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1227 else if (strcasecmp("CollectSM", child->key) == 0)
1229 cf_util_get_boolean(child, &conf->collect_sm);
1231 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1234 else if (strcasecmp("CollectStruct", child->key) == 0)
1235 cf_util_get_boolean(child, &conf->collect_struct);
1236 else if (strcasecmp("CollectTotals", child->key) == 0)
1237 cf_util_get_boolean(child, &conf->collect_totals);
1238 else if (strcasecmp("CollectUptime", child->key) == 0)
1239 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1240 cf_util_get_boolean(child, &conf->collect_uptime);
1242 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1243 child->key, "v3 and v4");
1245 else if (strcasecmp("CollectVCL", child->key) == 0)
1246 cf_util_get_boolean(child, &conf->collect_vcl);
1247 else if (strcasecmp("CollectWorkers", child->key) == 0)
1248 cf_util_get_boolean(child, &conf->collect_workers);
1249 else if (strcasecmp("CollectVSM", child->key) == 0)
1251 cf_util_get_boolean(child, &conf->collect_vsm);
1253 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1257 WARNING("Varnish plugin: Ignoring unknown "
1258 "configuration option: \"%s\". Did "
1259 "you forget to add an <Instance /> "
1260 "block around the configuration?",
1265 if (!conf->collect_cache && !conf->collect_connections &&
1266 !conf->collect_esi && !conf->collect_backend
1267 #ifdef HAVE_VARNISH_V3
1268 && !conf->collect_dirdns
1270 && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1272 && !conf->collect_purge
1274 && !conf->collect_ban
1276 && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1278 && !conf->collect_sma && !conf->collect_sm
1280 && !conf->collect_struct && !conf->collect_totals
1281 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1282 && !conf->collect_uptime
1284 && !conf->collect_vcl && !conf->collect_workers
1286 && !conf->collect_vsm
1289 WARNING("Varnish plugin: No metric has been configured for "
1290 "instance \"%s\". Disabling this instance.",
1291 (conf->instance == NULL) ? "localhost" : conf->instance);
1296 ssnprintf(callback_name, sizeof(callback_name), "varnish/%s",
1297 (conf->instance == NULL) ? "localhost" : conf->instance);
1299 plugin_register_complex_read(
1300 /* group = */ "varnish",
1301 /* name = */ callback_name,
1302 /* callback = */ varnish_read,
1303 /* interval = */ 0, &(user_data_t){
1304 .data = conf, .free_func = varnish_config_free,
1310 } /* }}} int varnish_config_instance */
1312 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1314 for (int i = 0; i < ci->children_num; i++) {
1315 oconfig_item_t *child = ci->children + i;
1317 if (strcasecmp("Instance", child->key) == 0)
1318 varnish_config_instance(child);
1320 WARNING("Varnish plugin: Ignoring unknown "
1321 "configuration option: \"%s\"",
1327 } /* }}} int varnish_config */
1329 void module_register(void) /* {{{ */
1331 plugin_register_complex_config("varnish", varnish_config);
1332 plugin_register_init("varnish", varnish_init);