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.host, hostname_g, sizeof(vl.host));
99 sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
101 if (plugin_instance == NULL)
102 plugin_instance = "default";
104 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
105 plugin_instance, category);
107 sstrncpy(vl.type, type, sizeof(vl.type));
109 if (type_instance != NULL)
110 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
112 return (plugin_dispatch_values(&vl));
113 } /* }}} int varnish_submit */
115 static int varnish_submit_gauge(const char *plugin_instance, /* {{{ */
116 const char *category, const char *type,
117 const char *type_instance,
118 uint64_t gauge_value) {
121 value.gauge = (gauge_t)gauge_value;
124 varnish_submit(plugin_instance, category, type, type_instance, value));
125 } /* }}} int varnish_submit_gauge */
127 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
128 const char *category, const char *type,
129 const char *type_instance,
130 uint64_t derive_value) {
133 value.derive = (derive_t)derive_value;
136 varnish_submit(plugin_instance, category, type, type_instance, value));
137 } /* }}} int varnish_submit_derive */
139 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
140 static int varnish_monitor(void *priv,
141 const struct VSC_point *const pt) /* {{{ */
144 const user_config_t *conf;
154 class = pt->section->fantom->type;
155 name = pt->desc->name;
157 if (strcmp(class, "MAIN") != 0)
160 #elif HAVE_VARNISH_V3
164 if (strcmp(class, "") != 0)
168 val = *(const volatile uint64_t *)pt->ptr;
170 if (conf->collect_cache) {
171 if (strcmp(name, "cache_hit") == 0)
172 return varnish_submit_derive(conf->instance, "cache", "cache_result",
174 else if (strcmp(name, "cache_miss") == 0)
175 return varnish_submit_derive(conf->instance, "cache", "cache_result",
177 else if (strcmp(name, "cache_hitpass") == 0)
178 return varnish_submit_derive(conf->instance, "cache", "cache_result",
182 if (conf->collect_connections) {
183 if (strcmp(name, "client_conn") == 0)
184 return varnish_submit_derive(conf->instance, "connections", "connections",
186 else if (strcmp(name, "client_drop") == 0)
187 return varnish_submit_derive(conf->instance, "connections", "connections",
189 else if (strcmp(name, "client_req") == 0)
190 return varnish_submit_derive(conf->instance, "connections", "connections",
194 #ifdef HAVE_VARNISH_V3
195 if (conf->collect_dirdns) {
196 if (strcmp(name, "dir_dns_lookups") == 0)
197 return varnish_submit_derive(conf->instance, "dirdns", "cache_operation",
199 else if (strcmp(name, "dir_dns_failed") == 0)
200 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
202 else if (strcmp(name, "dir_dns_hit") == 0)
203 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
205 else if (strcmp(name, "dir_dns_cache_full") == 0)
206 return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
211 if (conf->collect_esi) {
212 if (strcmp(name, "esi_errors") == 0)
213 return varnish_submit_derive(conf->instance, "esi", "total_operations",
215 else if (strcmp(name, "esi_parse") == 0)
216 return varnish_submit_derive(conf->instance, "esi", "total_operations",
218 else if (strcmp(name, "esi_warnings") == 0)
219 return varnish_submit_derive(conf->instance, "esi", "total_operations",
223 if (conf->collect_backend) {
224 if (strcmp(name, "backend_conn") == 0)
225 return varnish_submit_derive(conf->instance, "backend", "connections",
227 else if (strcmp(name, "backend_unhealthy") == 0)
228 return varnish_submit_derive(conf->instance, "backend", "connections",
229 "not-attempted", val);
230 else if (strcmp(name, "backend_busy") == 0)
231 return varnish_submit_derive(conf->instance, "backend", "connections",
233 else if (strcmp(name, "backend_fail") == 0)
234 return varnish_submit_derive(conf->instance, "backend", "connections",
236 else if (strcmp(name, "backend_reuse") == 0)
237 return varnish_submit_derive(conf->instance, "backend", "connections",
239 else if (strcmp(name, "backend_toolate") == 0)
240 return varnish_submit_derive(conf->instance, "backend", "connections",
242 else if (strcmp(name, "backend_recycle") == 0)
243 return varnish_submit_derive(conf->instance, "backend", "connections",
245 else if (strcmp(name, "backend_unused") == 0)
246 return varnish_submit_derive(conf->instance, "backend", "connections",
248 else if (strcmp(name, "backend_retry") == 0)
249 return varnish_submit_derive(conf->instance, "backend", "connections",
251 else if (strcmp(name, "backend_req") == 0)
252 return varnish_submit_derive(conf->instance, "backend", "http_requests",
254 else if (strcmp(name, "n_backend") == 0)
255 return varnish_submit_gauge(conf->instance, "backend", "backends",
259 if (conf->collect_fetch) {
260 if (strcmp(name, "fetch_head") == 0)
261 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
263 else if (strcmp(name, "fetch_length") == 0)
264 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
266 else if (strcmp(name, "fetch_chunked") == 0)
267 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
269 else if (strcmp(name, "fetch_eof") == 0)
270 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
272 else if (strcmp(name, "fetch_bad") == 0)
273 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
275 else if (strcmp(name, "fetch_close") == 0)
276 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
278 else if (strcmp(name, "fetch_oldhttp") == 0)
279 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
281 else if (strcmp(name, "fetch_zero") == 0)
282 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
284 else if (strcmp(name, "fetch_failed") == 0)
285 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
287 else if (strcmp(name, "fetch_1xx") == 0)
288 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
290 else if (strcmp(name, "fetch_204") == 0)
291 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
293 else if (strcmp(name, "fetch_304") == 0)
294 return varnish_submit_derive(conf->instance, "fetch", "http_requests",
298 if (conf->collect_hcb) {
299 if (strcmp(name, "hcb_nolock") == 0)
300 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
301 "lookup_nolock", val);
302 else if (strcmp(name, "hcb_lock") == 0)
303 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
305 else if (strcmp(name, "hcb_insert") == 0)
306 return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
310 if (conf->collect_objects) {
311 if (strcmp(name, "n_expired") == 0)
312 return varnish_submit_derive(conf->instance, "objects", "total_objects",
314 else if (strcmp(name, "n_lru_nuked") == 0)
315 return varnish_submit_derive(conf->instance, "objects", "total_objects",
317 else if (strcmp(name, "n_lru_saved") == 0)
318 return varnish_submit_derive(conf->instance, "objects", "total_objects",
320 else if (strcmp(name, "n_lru_moved") == 0)
321 return varnish_submit_derive(conf->instance, "objects", "total_objects",
323 else if (strcmp(name, "n_deathrow") == 0)
324 return varnish_submit_derive(conf->instance, "objects", "total_objects",
326 else if (strcmp(name, "losthdr") == 0)
327 return varnish_submit_derive(conf->instance, "objects", "total_objects",
328 "header_overflow", val);
329 else if (strcmp(name, "n_obj_purged") == 0)
330 return varnish_submit_derive(conf->instance, "objects", "total_objects",
332 else if (strcmp(name, "n_objsendfile") == 0)
333 return varnish_submit_derive(conf->instance, "objects", "total_objects",
334 "sent_sendfile", val);
335 else if (strcmp(name, "n_objwrite") == 0)
336 return varnish_submit_derive(conf->instance, "objects", "total_objects",
338 else if (strcmp(name, "n_objoverflow") == 0)
339 return varnish_submit_derive(conf->instance, "objects", "total_objects",
340 "workspace_overflow", val);
344 if (conf->collect_ban) {
345 if (strcmp(name, "n_ban") == 0)
346 return varnish_submit_derive(conf->instance, "ban", "total_operations",
348 else if (strcmp(name, "n_ban_add") == 0)
349 return varnish_submit_derive(conf->instance, "ban", "total_operations",
351 else if (strcmp(name, "n_ban_retire") == 0)
352 return varnish_submit_derive(conf->instance, "ban", "total_operations",
354 else if (strcmp(name, "n_ban_obj_test") == 0)
355 return varnish_submit_derive(conf->instance, "ban", "total_operations",
356 "objects_tested", val);
357 else if (strcmp(name, "n_ban_re_test") == 0)
358 return varnish_submit_derive(conf->instance, "ban", "total_operations",
359 "regexps_tested", val);
360 else if (strcmp(name, "n_ban_dups") == 0)
361 return varnish_submit_derive(conf->instance, "ban", "total_operations",
366 if (conf->collect_ban) {
367 if (strcmp(name, "bans") == 0)
368 return varnish_submit_derive(conf->instance, "ban", "total_operations",
370 else if (strcmp(name, "bans_added") == 0)
371 return varnish_submit_derive(conf->instance, "ban", "total_operations",
373 else if (strcmp(name, "bans_obj") == 0)
374 return varnish_submit_derive(conf->instance, "ban", "total_operations",
376 else if (strcmp(name, "bans_req") == 0)
377 return varnish_submit_derive(conf->instance, "ban", "total_operations",
379 else if (strcmp(name, "bans_completed") == 0)
380 return varnish_submit_derive(conf->instance, "ban", "total_operations",
382 else if (strcmp(name, "bans_deleted") == 0)
383 return varnish_submit_derive(conf->instance, "ban", "total_operations",
385 else if (strcmp(name, "bans_tested") == 0)
386 return varnish_submit_derive(conf->instance, "ban", "total_operations",
388 else if (strcmp(name, "bans_dups") == 0)
389 return varnish_submit_derive(conf->instance, "ban", "total_operations",
394 if (conf->collect_session) {
395 if (strcmp(name, "sess_closed") == 0)
396 return varnish_submit_derive(conf->instance, "session",
397 "total_operations", "closed", val);
398 else if (strcmp(name, "sess_pipeline") == 0)
399 return varnish_submit_derive(conf->instance, "session",
400 "total_operations", "pipeline", val);
401 else if (strcmp(name, "sess_readahead") == 0)
402 return varnish_submit_derive(conf->instance, "session",
403 "total_operations", "readahead", val);
404 else if (strcmp(name, "sess_conn") == 0)
405 return varnish_submit_derive(conf->instance, "session",
406 "total_operations", "accepted", val);
407 else if (strcmp(name, "sess_drop") == 0)
408 return varnish_submit_derive(conf->instance, "session",
409 "total_operations", "dropped", val);
410 else if (strcmp(name, "sess_fail") == 0)
411 return varnish_submit_derive(conf->instance, "session",
412 "total_operations", "failed", val);
413 else if (strcmp(name, "sess_pipe_overflow") == 0)
414 return varnish_submit_derive(conf->instance, "session",
415 "total_operations", "overflow", val);
416 else if (strcmp(name, "sess_queued") == 0)
417 return varnish_submit_derive(conf->instance, "session",
418 "total_operations", "queued", val);
419 else if (strcmp(name, "sess_linger") == 0)
420 return varnish_submit_derive(conf->instance, "session",
421 "total_operations", "linger", val);
422 else if (strcmp(name, "sess_herd") == 0)
423 return varnish_submit_derive(conf->instance, "session",
424 "total_operations", "herd", val);
427 if (conf->collect_shm) {
428 if (strcmp(name, "shm_records") == 0)
429 return varnish_submit_derive(conf->instance, "shm", "total_operations",
431 else if (strcmp(name, "shm_writes") == 0)
432 return varnish_submit_derive(conf->instance, "shm", "total_operations",
434 else if (strcmp(name, "shm_flushes") == 0)
435 return varnish_submit_derive(conf->instance, "shm", "total_operations",
437 else if (strcmp(name, "shm_cont") == 0)
438 return varnish_submit_derive(conf->instance, "shm", "total_operations",
440 else if (strcmp(name, "shm_cycles") == 0)
441 return varnish_submit_derive(conf->instance, "shm", "total_operations",
445 if (conf->collect_sms) {
446 if (strcmp(name, "sms_nreq") == 0)
447 return varnish_submit_derive(conf->instance, "sms", "total_requests",
449 else if (strcmp(name, "sms_nobj") == 0)
450 return varnish_submit_gauge(conf->instance, "sms", "requests",
452 else if (strcmp(name, "sms_nbytes") == 0)
453 return varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
455 else if (strcmp(name, "sms_balloc") == 0)
456 return varnish_submit_derive(conf->instance, "sms", "total_bytes",
458 else if (strcmp(name, "sms_bfree") == 0)
459 return varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
463 if (conf->collect_struct) {
464 if (strcmp(name, "n_sess_mem") == 0)
465 return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
467 else if (strcmp(name, "n_sess") == 0)
468 return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
470 else if (strcmp(name, "n_object") == 0)
471 return varnish_submit_gauge(conf->instance, "struct", "objects", "object",
473 else if (strcmp(name, "n_vampireobject") == 0)
474 return varnish_submit_gauge(conf->instance, "struct", "objects",
475 "vampireobject", val);
476 else if (strcmp(name, "n_objectcore") == 0)
477 return varnish_submit_gauge(conf->instance, "struct", "objects",
479 else if (strcmp(name, "n_waitinglist") == 0)
480 return varnish_submit_gauge(conf->instance, "struct", "objects",
482 else if (strcmp(name, "n_objecthead") == 0)
483 return varnish_submit_gauge(conf->instance, "struct", "objects",
485 else if (strcmp(name, "n_smf") == 0)
486 return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
488 else if (strcmp(name, "n_smf_frag") == 0)
489 return varnish_submit_gauge(conf->instance, "struct", "objects",
491 else if (strcmp(name, "n_smf_large") == 0)
492 return varnish_submit_gauge(conf->instance, "struct", "objects",
494 else if (strcmp(name, "n_vbe_conn") == 0)
495 return varnish_submit_gauge(conf->instance, "struct", "objects",
499 if (conf->collect_totals) {
500 if (strcmp(name, "s_sess") == 0)
501 return varnish_submit_derive(conf->instance, "totals", "total_sessions",
503 else if (strcmp(name, "s_req") == 0)
504 return varnish_submit_derive(conf->instance, "totals", "total_requests",
506 else if (strcmp(name, "s_pipe") == 0)
507 return varnish_submit_derive(conf->instance, "totals", "total_operations",
509 else if (strcmp(name, "s_pass") == 0)
510 return varnish_submit_derive(conf->instance, "totals", "total_operations",
512 else if (strcmp(name, "s_fetch") == 0)
513 return varnish_submit_derive(conf->instance, "totals", "total_operations",
515 else if (strcmp(name, "s_synth") == 0)
516 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
518 else if (strcmp(name, "s_req_hdrbytes") == 0)
519 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
521 else if (strcmp(name, "s_req_bodybytes") == 0)
522 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
524 else if (strcmp(name, "s_resp_hdrbytes") == 0)
525 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
527 else if (strcmp(name, "s_resp_bodybytes") == 0)
528 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
530 else if (strcmp(name, "s_pipe_hdrbytes") == 0)
531 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
533 else if (strcmp(name, "s_pipe_in") == 0)
534 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
536 else if (strcmp(name, "s_pipe_out") == 0)
537 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
539 else if (strcmp(name, "n_purges") == 0)
540 return varnish_submit_derive(conf->instance, "totals", "total_operations",
542 else if (strcmp(name, "s_hdrbytes") == 0)
543 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
544 "header-bytes", val);
545 else if (strcmp(name, "s_bodybytes") == 0)
546 return varnish_submit_derive(conf->instance, "totals", "total_bytes",
548 else if (strcmp(name, "n_gzip") == 0)
549 return varnish_submit_derive(conf->instance, "totals", "total_operations",
551 else if (strcmp(name, "n_gunzip") == 0)
552 return varnish_submit_derive(conf->instance, "totals", "total_operations",
556 if (conf->collect_uptime) {
557 if (strcmp(name, "uptime") == 0)
558 return varnish_submit_gauge(conf->instance, "uptime", "uptime",
559 "client_uptime", val);
562 if (conf->collect_vcl) {
563 if (strcmp(name, "n_vcl") == 0)
564 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
566 else if (strcmp(name, "n_vcl_avail") == 0)
567 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
569 else if (strcmp(name, "n_vcl_discard") == 0)
570 return varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
572 else if (strcmp(name, "vmods") == 0)
573 return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
577 if (conf->collect_workers) {
578 if (strcmp(name, "threads") == 0)
579 return varnish_submit_gauge(conf->instance, "workers", "threads",
581 else if (strcmp(name, "threads_created") == 0)
582 return varnish_submit_derive(conf->instance, "workers", "total_threads",
584 else if (strcmp(name, "threads_failed") == 0)
585 return varnish_submit_derive(conf->instance, "workers", "total_threads",
587 else if (strcmp(name, "threads_limited") == 0)
588 return varnish_submit_derive(conf->instance, "workers", "total_threads",
590 else if (strcmp(name, "threads_destroyed") == 0)
591 return varnish_submit_derive(conf->instance, "workers", "total_threads",
593 else if (strcmp(name, "thread_queue_len") == 0)
594 return varnish_submit_derive(conf->instance, "workers", "queue_length",
596 else if (strcmp(name, "n_wrk") == 0)
597 return varnish_submit_gauge(conf->instance, "workers", "threads",
599 else if (strcmp(name, "n_wrk_create") == 0)
600 return varnish_submit_derive(conf->instance, "workers", "total_threads",
602 else if (strcmp(name, "n_wrk_failed") == 0)
603 return varnish_submit_derive(conf->instance, "workers", "total_threads",
605 else if (strcmp(name, "n_wrk_max") == 0)
606 return varnish_submit_derive(conf->instance, "workers", "total_threads",
608 else if (strcmp(name, "n_wrk_drop") == 0)
609 return varnish_submit_derive(conf->instance, "workers", "total_threads",
611 else if (strcmp(name, "n_wrk_queue") == 0)
612 return varnish_submit_derive(conf->instance, "workers", "total_requests",
614 else if (strcmp(name, "n_wrk_overflow") == 0)
615 return varnish_submit_derive(conf->instance, "workers", "total_requests",
617 else if (strcmp(name, "n_wrk_queued") == 0)
618 return varnish_submit_derive(conf->instance, "workers", "total_requests",
620 else if (strcmp(name, "n_wrk_lqueue") == 0)
621 return varnish_submit_derive(conf->instance, "workers", "total_requests",
622 "queue_length", val);
626 if (conf->collect_vsm) {
627 if (strcmp(name, "vsm_free") == 0)
628 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
629 else if (strcmp(name, "vsm_used") == 0)
630 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
631 else if (strcmp(name, "vsm_cooling") == 0)
632 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
634 else if (strcmp(name, "vsm_overflow") == 0)
635 return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
637 else if (strcmp(name, "vsm_overflowed") == 0)
638 return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
645 } /* }}} static int varnish_monitor */
646 #else /* if HAVE_VARNISH_V2 */
647 static void varnish_monitor(const user_config_t *conf, /* {{{ */
648 const c_varnish_stats_t *stats) {
649 if (conf->collect_cache) {
651 varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
654 varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
656 /* Cache hits for pass */
657 varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
658 stats->cache_hitpass);
661 if (conf->collect_connections) {
662 /* Client connections accepted */
663 varnish_submit_derive(conf->instance, "connections", "connections",
664 "accepted", stats->client_conn);
665 /* Connection dropped, no sess */
666 varnish_submit_derive(conf->instance, "connections", "connections",
667 "dropped", stats->client_drop);
668 /* Client requests received */
669 varnish_submit_derive(conf->instance, "connections", "connections",
670 "received", stats->client_req);
673 if (conf->collect_esi) {
674 /* ESI parse errors (unlock) */
675 varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
677 /* Objects ESI parsed (unlock) */
678 varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
682 if (conf->collect_backend) {
683 /* Backend conn. success */
684 varnish_submit_derive(conf->instance, "backend", "connections", "success",
685 stats->backend_conn);
686 /* Backend conn. not attempted */
687 varnish_submit_derive(conf->instance, "backend", "connections",
688 "not-attempted", stats->backend_unhealthy);
689 /* Backend conn. too many */
690 varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
691 stats->backend_busy);
692 /* Backend conn. failures */
693 varnish_submit_derive(conf->instance, "backend", "connections", "failures",
694 stats->backend_fail);
695 /* Backend conn. reuses */
696 varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
697 stats->backend_reuse);
698 /* Backend conn. was closed */
699 varnish_submit_derive(conf->instance, "backend", "connections",
700 "was-closed", stats->backend_toolate);
701 /* Backend conn. recycles */
702 varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
703 stats->backend_recycle);
704 /* Backend conn. unused */
705 varnish_submit_derive(conf->instance, "backend", "connections", "unused",
706 stats->backend_unused);
707 /* Backend requests mades */
708 varnish_submit_derive(conf->instance, "backend", "http_requests",
709 "requests", stats->backend_req);
711 varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
715 if (conf->collect_fetch) {
717 varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
719 /* Fetch with length */
720 varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
721 stats->fetch_length);
723 varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
724 stats->fetch_chunked);
726 varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
728 /* Fetch bad headers */
729 varnish_submit_derive(conf->instance, "fetch", "http_requests",
730 "bad_headers", stats->fetch_bad);
731 /* Fetch wanted close */
732 varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
734 /* Fetch pre HTTP/1.1 closed */
735 varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
736 stats->fetch_oldhttp);
738 varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
741 varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
742 stats->fetch_failed);
745 if (conf->collect_hcb) {
746 /* HCB Lookups without lock */
747 varnish_submit_derive(conf->instance, "hcb", "cache_operation",
748 "lookup_nolock", stats->hcb_nolock);
749 /* HCB Lookups with lock */
750 varnish_submit_derive(conf->instance, "hcb", "cache_operation",
751 "lookup_lock", stats->hcb_lock);
753 varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
757 if (conf->collect_objects) {
758 /* N expired objects */
759 varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
761 /* N LRU nuked objects */
762 varnish_submit_derive(conf->instance, "objects", "total_objects",
763 "lru_nuked", stats->n_lru_nuked);
764 /* N LRU saved objects */
765 varnish_submit_derive(conf->instance, "objects", "total_objects",
766 "lru_saved", stats->n_lru_saved);
767 /* N LRU moved objects */
768 varnish_submit_derive(conf->instance, "objects", "total_objects",
769 "lru_moved", stats->n_lru_moved);
770 /* N objects on deathrow */
771 varnish_submit_derive(conf->instance, "objects", "total_objects",
772 "deathrow", stats->n_deathrow);
773 /* HTTP header overflows */
774 varnish_submit_derive(conf->instance, "objects", "total_objects",
775 "header_overflow", stats->losthdr);
776 /* Objects sent with sendfile */
777 varnish_submit_derive(conf->instance, "objects", "total_objects",
778 "sent_sendfile", stats->n_objsendfile);
779 /* Objects sent with write */
780 varnish_submit_derive(conf->instance, "objects", "total_objects",
781 "sent_write", stats->n_objwrite);
782 /* Objects overflowing workspace */
783 varnish_submit_derive(conf->instance, "objects", "total_objects",
784 "workspace_overflow", stats->n_objoverflow);
787 if (conf->collect_purge) {
788 /* N total active purges */
789 varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
791 /* N new purges added */
792 varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
794 /* N old purges deleted */
795 varnish_submit_derive(conf->instance, "purge", "total_operations",
796 "deleted", stats->n_purge_retire);
797 /* N objects tested */
798 varnish_submit_derive(conf->instance, "purge", "total_operations",
799 "objects_tested", stats->n_purge_obj_test);
800 /* N regexps tested against */
801 varnish_submit_derive(conf->instance, "purge", "total_operations",
802 "regexps_tested", stats->n_purge_re_test);
803 /* N duplicate purges removed */
804 varnish_submit_derive(conf->instance, "purge", "total_operations",
805 "duplicate", stats->n_purge_dups);
808 if (conf->collect_session) {
810 varnish_submit_derive(conf->instance, "session", "total_operations",
811 "closed", stats->sess_closed);
812 /* Session Pipeline */
813 varnish_submit_derive(conf->instance, "session", "total_operations",
814 "pipeline", stats->sess_pipeline);
815 /* Session Read Ahead */
816 varnish_submit_derive(conf->instance, "session", "total_operations",
817 "readahead", stats->sess_readahead);
819 varnish_submit_derive(conf->instance, "session", "total_operations",
820 "linger", stats->sess_linger);
822 varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
826 if (conf->collect_shm) {
828 varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
831 varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
833 /* SHM flushes due to overflow */
834 varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
836 /* SHM MTX contention */
837 varnish_submit_derive(conf->instance, "shm", "total_operations",
838 "contention", stats->shm_cont);
839 /* SHM cycles through buffer */
840 varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
844 if (conf->collect_sm) {
845 /* allocator requests */
846 varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
848 /* outstanding allocations */
849 varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
851 /* bytes allocated */
852 varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
855 varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
859 if (conf->collect_sma) {
860 /* SMA allocator requests */
861 varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
863 /* SMA outstanding allocations */
864 varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
866 /* SMA outstanding bytes */
867 varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
869 /* SMA bytes allocated */
870 varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
873 varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
877 if (conf->collect_sms) {
878 /* SMS allocator requests */
879 varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
881 /* SMS outstanding allocations */
882 varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
884 /* SMS outstanding bytes */
885 varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
887 /* SMS bytes allocated */
888 varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
890 /* SMS bytes freed */
891 varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
895 if (conf->collect_struct) {
896 /* N struct sess_mem */
897 varnish_submit_gauge(conf->instance, "struct", "current_sessions",
898 "sess_mem", stats->n_sess_mem);
900 varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
902 /* N struct object */
903 varnish_submit_gauge(conf->instance, "struct", "objects", "object",
905 /* N struct objecthead */
906 varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
907 stats->n_objecthead);
909 varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
911 /* N small free smf */
912 varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
914 /* N large free smf */
915 varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
917 /* N struct vbe_conn */
918 varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
922 if (conf->collect_totals) {
924 varnish_submit_derive(conf->instance, "totals", "total_sessions",
925 "sessions", stats->s_sess);
927 varnish_submit_derive(conf->instance, "totals", "total_requests",
928 "requests", stats->s_req);
930 varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
933 varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
936 varnish_submit_derive(conf->instance, "totals", "total_operations",
937 "fetches", stats->s_fetch);
938 /* Total header bytes */
939 varnish_submit_derive(conf->instance, "totals", "total_bytes",
940 "header-bytes", stats->s_hdrbytes);
941 /* Total body byte */
942 varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
946 if (conf->collect_vcl) {
948 varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
950 /* N vcl available */
951 varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
953 /* N vcl discarded */
954 varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
955 stats->n_vcl_discard);
958 if (conf->collect_workers) {
960 varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
962 /* worker threads created */
963 varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
964 stats->n_wrk_create);
965 /* worker threads not created */
966 varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
967 stats->n_wrk_failed);
968 /* worker threads limited */
969 varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
971 /* dropped work requests */
972 varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
974 /* queued work requests */
975 varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
977 /* overflowed work requests */
978 varnish_submit_derive(conf->instance, "workers", "total_requests",
979 "overflowed", stats->n_wrk_overflow);
982 } /* }}} void varnish_monitor */
985 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
986 static int varnish_read(user_data_t *ud) /* {{{ */
989 const c_varnish_stats_t *stats;
994 if ((ud == NULL) || (ud->data == NULL))
1004 if (conf->instance != NULL) {
1007 status = VSM_n_Arg(vd, conf->instance);
1010 ERROR("varnish plugin: VSM_n_Arg (\"%s\") failed "
1012 conf->instance, status);
1018 ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1019 #else /* if HAVE_VARNISH_V4 */
1020 ok = (VSM_Open(vd) == 0);
1024 ERROR("varnish plugin: Unable to open connection.");
1030 stats = VSC_Main(vd);
1031 #else /* if HAVE_VARNISH_V4 */
1032 stats = VSC_Main(vd, NULL);
1036 ERROR("varnish plugin: Unable to get statistics.");
1042 VSC_Iter(vd, varnish_monitor, conf);
1043 #else /* if HAVE_VARNISH_V4 */
1044 VSC_Iter(vd, NULL, varnish_monitor, conf);
1050 #else /* if HAVE_VARNISH_V2 */
1051 static int varnish_read(user_data_t *ud) /* {{{ */
1053 const c_varnish_stats_t *stats;
1055 user_config_t *conf;
1057 if ((ud == NULL) || (ud->data == NULL))
1062 stats = VSL_OpenStats(conf->instance);
1063 if (stats == NULL) {
1064 ERROR("Varnish plugin : unable to load statistics");
1069 varnish_monitor(conf, stats);
1075 static void varnish_config_free(void *ptr) /* {{{ */
1077 user_config_t *conf = ptr;
1082 sfree(conf->instance);
1086 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1091 conf->collect_backend = 1;
1092 conf->collect_cache = 1;
1093 conf->collect_connections = 1;
1094 #ifdef HAVE_VARNISH_V3
1095 conf->collect_dirdns = 0;
1097 conf->collect_esi = 0;
1098 conf->collect_fetch = 0;
1099 conf->collect_hcb = 0;
1100 conf->collect_objects = 0;
1102 conf->collect_purge = 0;
1104 conf->collect_ban = 0;
1106 conf->collect_session = 0;
1107 conf->collect_shm = 1;
1109 conf->collect_sm = 0;
1110 conf->collect_sma = 0;
1112 conf->collect_sms = 0;
1113 conf->collect_struct = 0;
1114 conf->collect_totals = 0;
1115 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1116 conf->collect_uptime = 0;
1118 conf->collect_vcl = 0;
1119 conf->collect_workers = 0;
1121 conf->collect_vsm = 0;
1125 } /* }}} int varnish_config_apply_default */
1127 static int varnish_init(void) /* {{{ */
1129 user_config_t *conf;
1134 conf = calloc(1, sizeof(*conf));
1138 /* Default settings: */
1139 conf->instance = NULL;
1141 varnish_config_apply_default(conf);
1143 user_data_t ud = {.data = conf, .free_func = varnish_config_free};
1145 plugin_register_complex_read(/* group = */ "varnish",
1146 /* name = */ "varnish/localhost",
1147 /* callback = */ varnish_read,
1149 /* user data = */ &ud);
1152 } /* }}} int varnish_init */
1154 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1156 user_config_t *conf;
1158 char callback_name[DATA_MAX_NAME_LEN];
1160 conf = calloc(1, sizeof(*conf));
1163 conf->instance = NULL;
1165 varnish_config_apply_default(conf);
1167 if (ci->values_num == 1) {
1170 status = cf_util_get_string(ci, &conf->instance);
1175 assert(conf->instance != NULL);
1177 if (strcmp("localhost", conf->instance) == 0) {
1178 sfree(conf->instance);
1179 conf->instance = NULL;
1181 } else if (ci->values_num > 1) {
1182 WARNING("Varnish plugin: \"Instance\" blocks accept only "
1188 for (int i = 0; i < ci->children_num; i++) {
1189 oconfig_item_t *child = ci->children + i;
1191 if (strcasecmp("CollectCache", child->key) == 0)
1192 cf_util_get_boolean(child, &conf->collect_cache);
1193 else if (strcasecmp("CollectConnections", child->key) == 0)
1194 cf_util_get_boolean(child, &conf->collect_connections);
1195 else if (strcasecmp("CollectESI", child->key) == 0)
1196 cf_util_get_boolean(child, &conf->collect_esi);
1197 else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1198 #ifdef HAVE_VARNISH_V3
1199 cf_util_get_boolean(child, &conf->collect_dirdns);
1201 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1204 else if (strcasecmp("CollectBackend", child->key) == 0)
1205 cf_util_get_boolean(child, &conf->collect_backend);
1206 else if (strcasecmp("CollectFetch", child->key) == 0)
1207 cf_util_get_boolean(child, &conf->collect_fetch);
1208 else if (strcasecmp("CollectHCB", child->key) == 0)
1209 cf_util_get_boolean(child, &conf->collect_hcb);
1210 else if (strcasecmp("CollectObjects", child->key) == 0)
1211 cf_util_get_boolean(child, &conf->collect_objects);
1212 else if (strcasecmp("CollectPurge", child->key) == 0)
1214 cf_util_get_boolean(child, &conf->collect_purge);
1216 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1219 else if (strcasecmp("CollectBan", child->key) == 0)
1221 WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1224 cf_util_get_boolean(child, &conf->collect_ban);
1226 else if (strcasecmp("CollectSession", child->key) == 0)
1227 cf_util_get_boolean(child, &conf->collect_session);
1228 else if (strcasecmp("CollectSHM", child->key) == 0)
1229 cf_util_get_boolean(child, &conf->collect_shm);
1230 else if (strcasecmp("CollectSMS", child->key) == 0)
1231 cf_util_get_boolean(child, &conf->collect_sms);
1232 else if (strcasecmp("CollectSMA", child->key) == 0)
1234 cf_util_get_boolean(child, &conf->collect_sma);
1236 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1239 else if (strcasecmp("CollectSM", child->key) == 0)
1241 cf_util_get_boolean(child, &conf->collect_sm);
1243 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1246 else if (strcasecmp("CollectStruct", child->key) == 0)
1247 cf_util_get_boolean(child, &conf->collect_struct);
1248 else if (strcasecmp("CollectTotals", child->key) == 0)
1249 cf_util_get_boolean(child, &conf->collect_totals);
1250 else if (strcasecmp("CollectUptime", child->key) == 0)
1251 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1252 cf_util_get_boolean(child, &conf->collect_uptime);
1254 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1255 child->key, "v3 and v4");
1257 else if (strcasecmp("CollectVCL", child->key) == 0)
1258 cf_util_get_boolean(child, &conf->collect_vcl);
1259 else if (strcasecmp("CollectWorkers", child->key) == 0)
1260 cf_util_get_boolean(child, &conf->collect_workers);
1261 else if (strcasecmp("CollectVSM", child->key) == 0)
1263 cf_util_get_boolean(child, &conf->collect_vsm);
1265 WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1269 WARNING("Varnish plugin: Ignoring unknown "
1270 "configuration option: \"%s\". Did "
1271 "you forget to add an <Instance /> "
1272 "block around the configuration?",
1277 if (!conf->collect_cache && !conf->collect_connections &&
1278 !conf->collect_esi && !conf->collect_backend
1279 #ifdef HAVE_VARNISH_V3
1280 && !conf->collect_dirdns
1282 && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1284 && !conf->collect_purge
1286 && !conf->collect_ban
1288 && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1290 && !conf->collect_sma && !conf->collect_sm
1292 && !conf->collect_struct && !conf->collect_totals
1293 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1294 && !conf->collect_uptime
1296 && !conf->collect_vcl && !conf->collect_workers
1298 && !conf->collect_vsm
1301 WARNING("Varnish plugin: No metric has been configured for "
1302 "instance \"%s\". Disabling this instance.",
1303 (conf->instance == NULL) ? "localhost" : conf->instance);
1308 ssnprintf(callback_name, sizeof(callback_name), "varnish/%s",
1309 (conf->instance == NULL) ? "localhost" : conf->instance);
1312 ud.free_func = varnish_config_free;
1314 plugin_register_complex_read(/* group = */ "varnish",
1315 /* name = */ callback_name,
1316 /* callback = */ varnish_read,
1318 /* user data = */ &ud);
1323 } /* }}} int varnish_config_instance */
1325 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1327 for (int i = 0; i < ci->children_num; i++) {
1328 oconfig_item_t *child = ci->children + i;
1330 if (strcasecmp("Instance", child->key) == 0)
1331 varnish_config_instance(child);
1333 WARNING("Varnish plugin: Ignoring unknown "
1334 "configuration option: \"%s\"",
1340 } /* }}} int varnish_config */
1342 void module_register(void) /* {{{ */
1344 plugin_register_complex_config("varnish", varnish_config);
1345 plugin_register_init("varnish", varnish_init);
1348 /* vim: set sw=8 noet fdm=marker : */