ebc8e7aa00fe5051729164f26ebbd4048db921d4
[collectd.git] / src / varnish.c
1 /**
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
6  *
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.
10  *
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.
15  *
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
19  *
20  * Authors:
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>
24  *   Denes Matetelki <dmatetelki at varnish-software.com>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
33 #include <vapi/vsc.h>
34 #include <vapi/vsm.h>
35 typedef struct VSC_C_main c_varnish_stats_t;
36 #endif
37
38 #if HAVE_VARNISH_V3
39 #include <varnishapi.h>
40 #include <vsc.h>
41 typedef struct VSC_C_main c_varnish_stats_t;
42 #endif
43
44 #if HAVE_VARNISH_V2
45 #include <varnishapi.h>
46 typedef struct varnish_stats c_varnish_stats_t;
47 #endif
48
49 /* {{{ user_config_s */
50 struct user_config_s {
51   char *instance;
52
53   _Bool collect_cache;
54   _Bool collect_connections;
55   _Bool collect_esi;
56   _Bool collect_backend;
57 #ifdef HAVE_VARNISH_V3
58   _Bool collect_dirdns;
59 #endif
60   _Bool collect_fetch;
61   _Bool collect_hcb;
62   _Bool collect_objects;
63 #if HAVE_VARNISH_V2
64   _Bool collect_purge;
65 #else
66   _Bool collect_ban;
67 #endif
68   _Bool collect_session;
69   _Bool collect_shm;
70   _Bool collect_sms;
71 #if HAVE_VARNISH_V2
72   _Bool collect_sm;
73 #endif
74 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
75   _Bool collect_sma;
76 #endif
77   _Bool collect_struct;
78   _Bool collect_totals;
79 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
80   _Bool collect_uptime;
81 #endif
82   _Bool collect_vcl;
83   _Bool collect_workers;
84 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
85   _Bool collect_vsm;
86   _Bool collect_lck;
87   _Bool collect_mempool;
88   _Bool collect_mgt;
89   _Bool collect_smf;
90   _Bool collect_vbe;
91   _Bool collect_mse;
92 #endif
93 };
94 typedef struct user_config_s user_config_t; /* }}} */
95
96 static _Bool have_instance = 0;
97
98 static int varnish_submit(const char *plugin_instance, /* {{{ */
99                           const char *category, const char *type,
100                           const char *type_instance, value_t value) {
101   value_list_t vl = VALUE_LIST_INIT;
102
103   vl.values = &value;
104   vl.values_len = 1;
105
106   sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
107
108   if (plugin_instance == NULL)
109     plugin_instance = "default";
110   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
111            plugin_instance, category);
112
113   sstrncpy(vl.type, type, sizeof(vl.type));
114
115   if (type_instance != NULL)
116     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
117
118   return plugin_dispatch_values(&vl);
119 } /* }}} int varnish_submit */
120
121 static int varnish_submit_gauge(const char *plugin_instance, /* {{{ */
122                                 const char *category, const char *type,
123                                 const char *type_instance,
124                                 uint64_t gauge_value) {
125   return varnish_submit(plugin_instance, category, type, type_instance,
126                         (value_t){
127                             .gauge = (gauge_t)gauge_value,
128                         });
129 } /* }}} int varnish_submit_gauge */
130
131 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
132                                  const char *category, const char *type,
133                                  const char *type_instance,
134                                  uint64_t derive_value) {
135   return varnish_submit(plugin_instance, category, type, type_instance,
136                         (value_t){
137                             .derive = (derive_t)derive_value,
138                         });
139 } /* }}} int varnish_submit_derive */
140
141 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
142 static int varnish_monitor(void *priv,
143                            const struct VSC_point *const pt) /* {{{ */
144 {
145   uint64_t val;
146   const user_config_t *conf;
147   const char *name;
148
149   if (pt == NULL)
150     return 0;
151
152   conf = priv;
153
154 #if HAVE_VARNISH_V5
155   char namebuff[100];
156   char *c;
157
158   c = rindex(pt->name, '.');
159   strcpy(namebuff, c + 1);
160   name = namebuff;
161
162 #elif HAVE_VARNISH_V4
163   const char *class;
164
165   class = pt->section->fantom->type;
166   name = pt->desc->name;
167
168   if (strcmp(class, "MAIN") != 0)
169     return 0;
170
171 #elif HAVE_VARNISH_V3
172   const char *class;
173
174   class = pt->class;
175   name = pt->name;
176
177   if (strcmp(class, "") != 0)
178     return 0;
179 #endif
180
181   val = *(const volatile uint64_t *)pt->ptr;
182
183   if (conf->collect_cache) {
184     if (strcmp(name, "cache_hit") == 0)
185       return varnish_submit_derive(conf->instance, "cache", "cache_result",
186                                    "hit", val);
187     else if (strcmp(name, "cache_miss") == 0)
188       return varnish_submit_derive(conf->instance, "cache", "cache_result",
189                                    "miss", val);
190     else if (strcmp(name, "cache_hitpass") == 0)
191       return varnish_submit_derive(conf->instance, "cache", "cache_result",
192                                    "hitpass", val);
193   }
194
195   if (conf->collect_connections) {
196     if (strcmp(name, "client_conn") == 0)
197       return varnish_submit_derive(conf->instance, "connections", "connections",
198                                    "accepted", val);
199     else if (strcmp(name, "client_drop") == 0)
200       return varnish_submit_derive(conf->instance, "connections", "connections",
201                                    "dropped", val);
202     else if (strcmp(name, "client_req") == 0)
203       return varnish_submit_derive(conf->instance, "connections", "connections",
204                                    "received", val);
205 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
206     else if (strcmp(name, "client_req_400") == 0)
207       return varnish_submit_derive(conf->instance, "connections", "connections",
208                                    "error_400", val);
209     else if (strcmp(name, "client_req_417") == 0)
210       return varnish_submit_derive(conf->instance, "connections", "connections",
211                                    "error_417", val);
212 #endif
213   }
214
215 #ifdef HAVE_VARNISH_V3
216   if (conf->collect_dirdns) {
217     if (strcmp(name, "dir_dns_lookups") == 0)
218       return varnish_submit_derive(conf->instance, "dirdns", "cache_operation",
219                                    "lookups", val);
220     else if (strcmp(name, "dir_dns_failed") == 0)
221       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
222                                    "failed", val);
223     else if (strcmp(name, "dir_dns_hit") == 0)
224       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
225                                    "hits", val);
226     else if (strcmp(name, "dir_dns_cache_full") == 0)
227       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
228                                    "cache_full", val);
229   }
230 #endif
231
232   if (conf->collect_esi) {
233     if (strcmp(name, "esi_errors") == 0)
234       return varnish_submit_derive(conf->instance, "esi", "total_operations",
235                                    "error", val);
236     else if (strcmp(name, "esi_parse") == 0)
237       return varnish_submit_derive(conf->instance, "esi", "total_operations",
238                                    "parsed", val);
239     else if (strcmp(name, "esi_warnings") == 0)
240       return varnish_submit_derive(conf->instance, "esi", "total_operations",
241                                    "warning", val);
242     else if (strcmp(name, "esi_maxdepth") == 0)
243       return varnish_submit_derive(conf->instance, "esi", "total_operations",
244                                    "max_depth", val);
245   }
246
247   if (conf->collect_backend) {
248     if (strcmp(name, "backend_conn") == 0)
249       return varnish_submit_derive(conf->instance, "backend", "connections",
250                                    "success", val);
251     else if (strcmp(name, "backend_unhealthy") == 0)
252       return varnish_submit_derive(conf->instance, "backend", "connections",
253                                    "not-attempted", val);
254     else if (strcmp(name, "backend_busy") == 0)
255       return varnish_submit_derive(conf->instance, "backend", "connections",
256                                    "too-many", val);
257     else if (strcmp(name, "backend_fail") == 0)
258       return varnish_submit_derive(conf->instance, "backend", "connections",
259                                    "failures", val);
260     else if (strcmp(name, "backend_reuse") == 0)
261       return varnish_submit_derive(conf->instance, "backend", "connections",
262                                    "reuses", val);
263     else if (strcmp(name, "backend_toolate") == 0)
264       return varnish_submit_derive(conf->instance, "backend", "connections",
265                                    "was-closed", val);
266     else if (strcmp(name, "backend_recycle") == 0)
267       return varnish_submit_derive(conf->instance, "backend", "connections",
268                                    "recycled", val);
269     else if (strcmp(name, "backend_unused") == 0)
270       return varnish_submit_derive(conf->instance, "backend", "connections",
271                                    "unused", val);
272     else if (strcmp(name, "backend_retry") == 0)
273       return varnish_submit_derive(conf->instance, "backend", "connections",
274                                    "retries", val);
275     else if (strcmp(name, "backend_req") == 0)
276       return varnish_submit_derive(conf->instance, "backend", "http_requests",
277                                    "requests", val);
278     else if (strcmp(name, "n_backend") == 0)
279       return varnish_submit_gauge(conf->instance, "backend", "backends",
280                                   "n_backends", val);
281   }
282
283   if (conf->collect_fetch) {
284     if (strcmp(name, "fetch_head") == 0)
285       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
286                                    "head", val);
287     else if (strcmp(name, "fetch_length") == 0)
288       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
289                                    "length", val);
290     else if (strcmp(name, "fetch_chunked") == 0)
291       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
292                                    "chunked", val);
293     else if (strcmp(name, "fetch_eof") == 0)
294       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
295                                    "eof", val);
296     else if (strcmp(name, "fetch_bad") == 0)
297       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
298                                    "bad_headers", val);
299     else if (strcmp(name, "fetch_close") == 0)
300       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
301                                    "close", val);
302     else if (strcmp(name, "fetch_oldhttp") == 0)
303       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
304                                    "oldhttp", val);
305     else if (strcmp(name, "fetch_zero") == 0)
306       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
307                                    "zero", val);
308     else if (strcmp(name, "fetch_failed") == 0)
309       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
310                                    "failed", val);
311     else if (strcmp(name, "fetch_1xx") == 0)
312       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
313                                    "no_body_1xx", val);
314     else if (strcmp(name, "fetch_204") == 0)
315       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
316                                    "no_body_204", val);
317     else if (strcmp(name, "fetch_304") == 0)
318       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
319                                    "no_body_304", val);
320 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
321     else if (strcmp(name, "fetch_no_thread") == 0)
322       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
323                                    "no_thread", val);
324     else if (strcmp(name, "fetch_none") == 0)
325       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
326                                    "none", val);
327     else if (strcmp(name, "busy_sleep") == 0)
328       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
329                                    "busy_sleep", val);
330     else if (strcmp(name, "busy_wakeup") == 0)
331       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
332                                    "busy_wakeup", val);
333 #endif
334   }
335
336   if (conf->collect_hcb) {
337     if (strcmp(name, "hcb_nolock") == 0)
338       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
339                                    "lookup_nolock", val);
340     else if (strcmp(name, "hcb_lock") == 0)
341       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
342                                    "lookup_lock", val);
343     else if (strcmp(name, "hcb_insert") == 0)
344       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
345                                    "insert", val);
346   }
347
348   if (conf->collect_objects) {
349     if (strcmp(name, "n_expired") == 0)
350       return varnish_submit_derive(conf->instance, "objects", "total_objects",
351                                    "expired", val);
352     else if (strcmp(name, "n_lru_nuked") == 0)
353       return varnish_submit_derive(conf->instance, "objects", "total_objects",
354                                    "lru_nuked", val);
355     else if (strcmp(name, "n_lru_saved") == 0)
356       return varnish_submit_derive(conf->instance, "objects", "total_objects",
357                                    "lru_saved", val);
358     else if (strcmp(name, "n_lru_moved") == 0)
359       return varnish_submit_derive(conf->instance, "objects", "total_objects",
360                                    "lru_moved", val);
361     else if (strcmp(name, "n_deathrow") == 0)
362       return varnish_submit_derive(conf->instance, "objects", "total_objects",
363                                    "deathrow", val);
364     else if (strcmp(name, "losthdr") == 0)
365       return varnish_submit_derive(conf->instance, "objects", "total_objects",
366                                    "header_overflow", val);
367     else if (strcmp(name, "n_obj_purged") == 0)
368       return varnish_submit_derive(conf->instance, "objects", "total_objects",
369                                    "purged", val);
370     else if (strcmp(name, "n_objsendfile") == 0)
371       return varnish_submit_derive(conf->instance, "objects", "total_objects",
372                                    "sent_sendfile", val);
373     else if (strcmp(name, "n_objwrite") == 0)
374       return varnish_submit_derive(conf->instance, "objects", "total_objects",
375                                    "sent_write", val);
376     else if (strcmp(name, "n_objoverflow") == 0)
377       return varnish_submit_derive(conf->instance, "objects", "total_objects",
378                                    "workspace_overflow", val);
379 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
380     else if (strcmp(name, "exp_mailed") == 0)
381       return varnish_submit_gauge(conf->instance, "struct", "objects",
382                                   "exp_mailed", val);
383     else if (strcmp(name, "exp_received") == 0)
384       return varnish_submit_gauge(conf->instance, "struct", "objects",
385                                   "exp_received", val);
386 #endif
387   }
388
389 #if HAVE_VARNISH_V3
390   if (conf->collect_ban) {
391     if (strcmp(name, "n_ban") == 0)
392       return varnish_submit_derive(conf->instance, "ban", "total_operations",
393                                    "total", val);
394     else if (strcmp(name, "n_ban_add") == 0)
395       return varnish_submit_derive(conf->instance, "ban", "total_operations",
396                                    "added", val);
397     else if (strcmp(name, "n_ban_retire") == 0)
398       return varnish_submit_derive(conf->instance, "ban", "total_operations",
399                                    "deleted", val);
400     else if (strcmp(name, "n_ban_obj_test") == 0)
401       return varnish_submit_derive(conf->instance, "ban", "total_operations",
402                                    "objects_tested", val);
403     else if (strcmp(name, "n_ban_re_test") == 0)
404       return varnish_submit_derive(conf->instance, "ban", "total_operations",
405                                    "regexps_tested", val);
406     else if (strcmp(name, "n_ban_dups") == 0)
407       return varnish_submit_derive(conf->instance, "ban", "total_operations",
408                                    "duplicate", val);
409   }
410 #endif
411 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
412   if (conf->collect_ban) {
413     if (strcmp(name, "bans") == 0)
414       return varnish_submit_derive(conf->instance, "ban", "total_operations",
415                                    "total", val);
416     else if (strcmp(name, "bans_added") == 0)
417       return varnish_submit_derive(conf->instance, "ban", "total_operations",
418                                    "added", val);
419     else if (strcmp(name, "bans_obj") == 0)
420       return varnish_submit_derive(conf->instance, "ban", "total_operations",
421                                    "obj", val);
422     else if (strcmp(name, "bans_req") == 0)
423       return varnish_submit_derive(conf->instance, "ban", "total_operations",
424                                    "req", val);
425     else if (strcmp(name, "bans_completed") == 0)
426       return varnish_submit_derive(conf->instance, "ban", "total_operations",
427                                    "completed", val);
428     else if (strcmp(name, "bans_deleted") == 0)
429       return varnish_submit_derive(conf->instance, "ban", "total_operations",
430                                    "deleted", val);
431     else if (strcmp(name, "bans_tested") == 0)
432       return varnish_submit_derive(conf->instance, "ban", "total_operations",
433                                    "tested", val);
434     else if (strcmp(name, "bans_dups") == 0)
435       return varnish_submit_derive(conf->instance, "ban", "total_operations",
436                                    "duplicate", val);
437     else if (strcmp(name, "bans_tested") == 0)
438       return varnish_submit_derive(conf->instance, "ban", "total_operations",
439                                    "tested", val);
440     else if (strcmp(name, "bans_lurker_contention") == 0)
441       return varnish_submit_derive(conf->instance, "ban", "total_operations",
442                                    "lurker_contention", val);
443     else if (strcmp(name, "bans_lurker_obj_killed") == 0)
444       return varnish_submit_derive(conf->instance, "ban", "total_operations",
445                                    "lurker_obj_killed", val);
446     else if (strcmp(name, "bans_lurker_tested") == 0)
447       return varnish_submit_derive(conf->instance, "ban", "total_operations",
448                                    "lurker_tested", val);
449     else if (strcmp(name, "bans_lurker_tests_tested") == 0)
450       return varnish_submit_derive(conf->instance, "ban", "total_operations",
451                                    "lurker_tests_tested", val);
452     else if (strcmp(name, "bans_obj_killed") == 0)
453       return varnish_submit_derive(conf->instance, "ban", "total_operations",
454                                    "obj_killed", val);
455     else if (strcmp(name, "bans_persisted_bytes") == 0)
456       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
457                                    "persisted_bytes", val);
458     else if (strcmp(name, "bans_persisted_fragmentation") == 0)
459       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
460                                    "persisted_fragmentation", val);
461     else if (strcmp(name, "bans_tests_tested") == 0)
462       return varnish_submit_derive(conf->instance, "ban", "total_operations",
463                                    "tests_tested", val);
464   }
465 #endif
466
467   if (conf->collect_session) {
468     if (strcmp(name, "sess_closed") == 0)
469       return varnish_submit_derive(conf->instance, "session",
470                                    "total_operations", "closed", val);
471     else if (strcmp(name, "sess_pipeline") == 0)
472       return varnish_submit_derive(conf->instance, "session",
473                                    "total_operations", "pipeline", val);
474     else if (strcmp(name, "sess_readahead") == 0)
475       return varnish_submit_derive(conf->instance, "session",
476                                    "total_operations", "readahead", val);
477     else if (strcmp(name, "sess_conn") == 0)
478       return varnish_submit_derive(conf->instance, "session",
479                                    "total_operations", "accepted", val);
480     else if (strcmp(name, "sess_drop") == 0)
481       return varnish_submit_derive(conf->instance, "session",
482                                    "total_operations", "dropped", val);
483     else if (strcmp(name, "sess_fail") == 0)
484       return varnish_submit_derive(conf->instance, "session",
485                                    "total_operations", "failed", val);
486     else if (strcmp(name, "sess_pipe_overflow") == 0)
487       return varnish_submit_derive(conf->instance, "session",
488                                    "total_operations", "overflow", val);
489     else if (strcmp(name, "sess_queued") == 0)
490       return varnish_submit_derive(conf->instance, "session",
491                                    "total_operations", "queued", val);
492     else if (strcmp(name, "sess_linger") == 0)
493       return varnish_submit_derive(conf->instance, "session",
494                                    "total_operations", "linger", val);
495     else if (strcmp(name, "sess_herd") == 0)
496       return varnish_submit_derive(conf->instance, "session",
497                                    "total_operations", "herd", val);
498 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
499     else if (strcmp(name, "sess_closed_err") == 0)
500       return varnish_submit_derive(conf->instance, "session",
501                                    "total_operations", "closed_err", val);
502     else if (strcmp(name, "sess_dropped") == 0)
503       return varnish_submit_derive(conf->instance, "session",
504                                    "total_operations", "dropped_for_thread",
505                                    val);
506 #endif
507   }
508
509   if (conf->collect_shm) {
510     if (strcmp(name, "shm_records") == 0)
511       return varnish_submit_derive(conf->instance, "shm", "total_operations",
512                                    "records", val);
513     else if (strcmp(name, "shm_writes") == 0)
514       return varnish_submit_derive(conf->instance, "shm", "total_operations",
515                                    "writes", val);
516     else if (strcmp(name, "shm_flushes") == 0)
517       return varnish_submit_derive(conf->instance, "shm", "total_operations",
518                                    "flushes", val);
519     else if (strcmp(name, "shm_cont") == 0)
520       return varnish_submit_derive(conf->instance, "shm", "total_operations",
521                                    "contention", val);
522     else if (strcmp(name, "shm_cycles") == 0)
523       return varnish_submit_derive(conf->instance, "shm", "total_operations",
524                                    "cycles", val);
525   }
526
527   if (conf->collect_sms) {
528     if (strcmp(name, "sms_nreq") == 0)
529       return varnish_submit_derive(conf->instance, "sms", "total_requests",
530                                    "allocator", val);
531     else if (strcmp(name, "sms_nobj") == 0)
532       return varnish_submit_gauge(conf->instance, "sms", "requests",
533                                   "outstanding", val);
534     else if (strcmp(name, "sms_nbytes") == 0)
535       return varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
536                                   val);
537     else if (strcmp(name, "sms_balloc") == 0)
538       return varnish_submit_derive(conf->instance, "sms", "total_bytes",
539                                    "allocated", val);
540     else if (strcmp(name, "sms_bfree") == 0)
541       return varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
542                                    val);
543   }
544
545   if (conf->collect_struct) {
546     if (strcmp(name, "n_sess_mem") == 0)
547       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
548                                   "sess_mem", val);
549     else if (strcmp(name, "n_sess") == 0)
550       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
551                                   "sess", val);
552     else if (strcmp(name, "n_object") == 0)
553       return varnish_submit_gauge(conf->instance, "struct", "objects", "object",
554                                   val);
555     else if (strcmp(name, "n_vampireobject") == 0)
556       return varnish_submit_gauge(conf->instance, "struct", "objects",
557                                   "vampireobject", val);
558     else if (strcmp(name, "n_objectcore") == 0)
559       return varnish_submit_gauge(conf->instance, "struct", "objects",
560                                   "objectcore", val);
561     else if (strcmp(name, "n_waitinglist") == 0)
562       return varnish_submit_gauge(conf->instance, "struct", "objects",
563                                   "waitinglist", val);
564     else if (strcmp(name, "n_objecthead") == 0)
565       return varnish_submit_gauge(conf->instance, "struct", "objects",
566                                   "objecthead", val);
567     else if (strcmp(name, "n_smf") == 0)
568       return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
569                                   val);
570     else if (strcmp(name, "n_smf_frag") == 0)
571       return varnish_submit_gauge(conf->instance, "struct", "objects",
572                                   "smf_frag", val);
573     else if (strcmp(name, "n_smf_large") == 0)
574       return varnish_submit_gauge(conf->instance, "struct", "objects",
575                                   "smf_large", val);
576     else if (strcmp(name, "n_vbe_conn") == 0)
577       return varnish_submit_gauge(conf->instance, "struct", "objects",
578                                   "vbe_conn", val);
579   }
580
581   if (conf->collect_totals) {
582     if (strcmp(name, "s_sess") == 0)
583       return varnish_submit_derive(conf->instance, "totals", "total_sessions",
584                                    "sessions", val);
585     else if (strcmp(name, "s_req") == 0)
586       return varnish_submit_derive(conf->instance, "totals", "total_requests",
587                                    "requests", val);
588     else if (strcmp(name, "s_pipe") == 0)
589       return varnish_submit_derive(conf->instance, "totals", "total_operations",
590                                    "pipe", val);
591     else if (strcmp(name, "s_pass") == 0)
592       return varnish_submit_derive(conf->instance, "totals", "total_operations",
593                                    "pass", val);
594     else if (strcmp(name, "s_fetch") == 0)
595       return varnish_submit_derive(conf->instance, "totals", "total_operations",
596                                    "fetches", val);
597     else if (strcmp(name, "s_synth") == 0)
598       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
599                                    "synth", val);
600     else if (strcmp(name, "s_req_hdrbytes") == 0)
601       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
602                                    "req_header", val);
603     else if (strcmp(name, "s_req_bodybytes") == 0)
604       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
605                                    "req_body", val);
606     else if (strcmp(name, "s_req_protobytes") == 0)
607       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
608                                    "req_proto", val);
609     else if (strcmp(name, "s_resp_hdrbytes") == 0)
610       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
611                                    "resp_header", val);
612     else if (strcmp(name, "s_resp_bodybytes") == 0)
613       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
614                                    "resp_body", val);
615     else if (strcmp(name, "s_resp_protobytes") == 0)
616       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
617                                    "resp_proto", val);
618     else if (strcmp(name, "s_pipe_hdrbytes") == 0)
619       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
620                                    "pipe_header", val);
621     else if (strcmp(name, "s_pipe_in") == 0)
622       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
623                                    "pipe_in", val);
624     else if (strcmp(name, "s_pipe_out") == 0)
625       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
626                                    "pipe_out", val);
627     else if (strcmp(name, "n_purges") == 0)
628       return varnish_submit_derive(conf->instance, "totals", "total_operations",
629                                    "purges", val);
630     else if (strcmp(name, "s_hdrbytes") == 0)
631       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
632                                    "header-bytes", val);
633     else if (strcmp(name, "s_bodybytes") == 0)
634       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
635                                    "body-bytes", val);
636     else if (strcmp(name, "n_gzip") == 0)
637       return varnish_submit_derive(conf->instance, "totals", "total_operations",
638                                    "gzip", val);
639     else if (strcmp(name, "n_gunzip") == 0)
640       return varnish_submit_derive(conf->instance, "totals", "total_operations",
641                                    "gunzip", val);
642   }
643
644   if (conf->collect_uptime) {
645     if (strcmp(name, "uptime") == 0)
646       return varnish_submit_gauge(conf->instance, "uptime", "uptime",
647                                   "client_uptime", val);
648   }
649
650   if (conf->collect_vcl) {
651     if (strcmp(name, "n_vcl") == 0)
652       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
653                                   val);
654     else if (strcmp(name, "n_vcl_avail") == 0)
655       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
656                                   val);
657     else if (strcmp(name, "n_vcl_discard") == 0)
658       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
659                                   val);
660     else if (strcmp(name, "vmods") == 0)
661       return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
662                                   val);
663   }
664
665   if (conf->collect_workers) {
666     if (strcmp(name, "threads") == 0)
667       return varnish_submit_gauge(conf->instance, "workers", "threads",
668                                   "worker", val);
669     else if (strcmp(name, "threads_created") == 0)
670       return varnish_submit_derive(conf->instance, "workers", "total_threads",
671                                    "created", val);
672     else if (strcmp(name, "threads_failed") == 0)
673       return varnish_submit_derive(conf->instance, "workers", "total_threads",
674                                    "failed", val);
675     else if (strcmp(name, "threads_limited") == 0)
676       return varnish_submit_derive(conf->instance, "workers", "total_threads",
677                                    "limited", val);
678     else if (strcmp(name, "threads_destroyed") == 0)
679       return varnish_submit_derive(conf->instance, "workers", "total_threads",
680                                    "dropped", val);
681     else if (strcmp(name, "thread_queue_len") == 0)
682       return varnish_submit_gauge(conf->instance, "workers", "queue_length",
683                                   "threads", val);
684     else if (strcmp(name, "n_wrk") == 0)
685       return varnish_submit_gauge(conf->instance, "workers", "threads",
686                                   "worker", val);
687     else if (strcmp(name, "n_wrk_create") == 0)
688       return varnish_submit_derive(conf->instance, "workers", "total_threads",
689                                    "created", val);
690     else if (strcmp(name, "n_wrk_failed") == 0)
691       return varnish_submit_derive(conf->instance, "workers", "total_threads",
692                                    "failed", val);
693     else if (strcmp(name, "n_wrk_max") == 0)
694       return varnish_submit_derive(conf->instance, "workers", "total_threads",
695                                    "limited", val);
696     else if (strcmp(name, "n_wrk_drop") == 0)
697       return varnish_submit_derive(conf->instance, "workers", "total_threads",
698                                    "dropped", val);
699     else if (strcmp(name, "n_wrk_queue") == 0)
700       return varnish_submit_derive(conf->instance, "workers", "total_requests",
701                                    "queued", val);
702     else if (strcmp(name, "n_wrk_overflow") == 0)
703       return varnish_submit_derive(conf->instance, "workers", "total_requests",
704                                    "overflowed", val);
705     else if (strcmp(name, "n_wrk_queued") == 0)
706       return varnish_submit_derive(conf->instance, "workers", "total_requests",
707                                    "queued", val);
708     else if (strcmp(name, "n_wrk_lqueue") == 0)
709       return varnish_submit_derive(conf->instance, "workers", "total_requests",
710                                    "queue_length", val);
711 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
712     else if (strcmp(name, "pools") == 0)
713       return varnish_submit_gauge(conf->instance, "workers", "pools", "pools",
714                                   val);
715     else if (strcmp(name, "busy_killed") == 0)
716       return varnish_submit_derive(conf->instance, "workers", "http_requests",
717                                    "busy_killed", val);
718 #endif
719   }
720
721 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
722   if (conf->collect_vsm) {
723     if (strcmp(name, "vsm_free") == 0)
724       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
725     else if (strcmp(name, "vsm_used") == 0)
726       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
727     else if (strcmp(name, "vsm_cooling") == 0)
728       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
729                                   val);
730     else if (strcmp(name, "vsm_overflow") == 0)
731       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
732                                   val);
733     else if (strcmp(name, "vsm_overflowed") == 0)
734       return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
735                                    "overflowed", val);
736   }
737
738   if (conf->collect_vbe) {
739     /* @TODO figure out the collectd type for bitmap
740     if (strcmp(name, "happy") == 0)
741       return varnish_submit_derive(conf->instance, "vbe",
742                                    "bitmap", "happy_hprobes", val);
743     */
744     if (strcmp(name, "bereq_hdrbytes") == 0)
745       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
746                                    "bereq_hdrbytes", val);
747     else if (strcmp(name, "bereq_bodybytes") == 0)
748       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
749                                    "bereq_bodybytes", val);
750     else if (strcmp(name, "bereq_protobytes") == 0)
751       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
752                                    "bereq_protobytes", val);
753     else if (strcmp(name, "beresp_hdrbytes") == 0)
754       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
755                                    "beresp_hdrbytes", val);
756     else if (strcmp(name, "beresp_bodybytes") == 0)
757       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
758                                    "beresp_bodybytes", val);
759     else if (strcmp(name, "beresp_protobytes") == 0)
760       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
761                                    "beresp_protobytes", val);
762     else if (strcmp(name, "pipe_hdrbytes") == 0)
763       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
764                                    "pipe_hdrbytes", val);
765     else if (strcmp(name, "pipe_out") == 0)
766       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
767                                    "pipe_out", val);
768     else if (strcmp(name, "pipe_in") == 0)
769       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
770                                    "pipe_in", val);
771     else if (strcmp(name, "conn") == 0)
772       return varnish_submit_derive(conf->instance, "vbe", "connections",
773                                    "c_conns", val);
774     else if (strcmp(name, "req") == 0)
775       return varnish_submit_derive(conf->instance, "vbe", "http_requests",
776                                    "b_reqs", val);
777   }
778
779   /* All Stevedores support these counters */
780   if (conf->collect_sma || conf->collect_smf || conf->collect_mse) {
781
782     char category[4];
783     if (conf->collect_sma)
784       strncpy(category, "sma", 4);
785     else if (conf->collect_smf)
786       strncpy(category, "smf", 4);
787     else
788       strncpy(category, "mse", 4);
789
790     if (strcmp(name, "c_req") == 0)
791       return varnish_submit_derive(conf->instance, category, "total_operations",
792                                    "alloc_req", val);
793     else if (strcmp(name, "c_fail") == 0)
794       return varnish_submit_derive(conf->instance, category, "total_operations",
795                                    "alloc_fail", val);
796     else if (strcmp(name, "c_bytes") == 0)
797       return varnish_submit_derive(conf->instance, category, "total_bytes",
798                                    "bytes_allocated", val);
799     else if (strcmp(name, "c_freed") == 0)
800       return varnish_submit_derive(conf->instance, category, "total_bytes",
801                                    "bytes_freed", val);
802     else if (strcmp(name, "g_alloc") == 0)
803       return varnish_submit_derive(conf->instance, category, "total_operations",
804                                    "alloc_outstanding", val);
805     else if (strcmp(name, "g_bytes") == 0)
806       return varnish_submit_gauge(conf->instance, category, "bytes",
807                                   "bytes_outstanding", val);
808     else if (strcmp(name, "g_space") == 0)
809       return varnish_submit_gauge(conf->instance, category, "bytes",
810                                   "bytes_available", val);
811   }
812
813   /* No SMA specific counters */
814
815   if (conf->collect_smf) {
816     if (strcmp(name, "g_smf") == 0)
817       return varnish_submit_gauge(conf->instance, "smf", "objects",
818                                   "n_struct_smf", val);
819     else if (strcmp(name, "g_smf_frag") == 0)
820       return varnish_submit_gauge(conf->instance, "smf", "objects",
821                                   "n_small_free_smf", val);
822     else if (strcmp(name, "g_smf_large") == 0)
823       return varnish_submit_gauge(conf->instance, "smf", "objects",
824                                   "n_large_free_smf", val);
825   }
826
827   if (conf->collect_mgt) {
828     if (strcmp(name, "uptime") == 0)
829       return varnish_submit_gauge(conf->instance, "mgt", "uptime",
830                                   "mgt_proc_uptime", val);
831     else if (strcmp(name, "child_start") == 0)
832       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
833                                    "child_start", val);
834     else if (strcmp(name, "child_exit") == 0)
835       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
836                                    "child_exit", val);
837     else if (strcmp(name, "child_stop") == 0)
838       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
839                                    "child_stop", val);
840     else if (strcmp(name, "child_died") == 0)
841       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
842                                    "child_died", val);
843     else if (strcmp(name, "child_dump") == 0)
844       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
845                                    "child_dump", val);
846     else if (strcmp(name, "child_panic") == 0)
847       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
848                                    "child_panic", val);
849   }
850
851   if (conf->collect_lck) {
852     if (strcmp(name, "creat") == 0)
853       return varnish_submit_gauge(conf->instance, "lck", "objects", "created",
854                                   val);
855     else if (strcmp(name, "destroy") == 0)
856       return varnish_submit_gauge(conf->instance, "lck", "objects", "destroyed",
857                                   val);
858     else if (strcmp(name, "locks") == 0)
859       return varnish_submit_derive(conf->instance, "lck", "total_operations",
860                                    "lock_ops", val);
861   }
862
863   if (conf->collect_mempool) {
864     if (strcmp(name, "live") == 0)
865       return varnish_submit_gauge(conf->instance, "mempool", "objects",
866                                   "in_use", val);
867     else if (strcmp(name, "pool") == 0)
868       return varnish_submit_gauge(conf->instance, "mempool", "objects",
869                                   "in_pool", val);
870     else if (strcmp(name, "sz_wanted") == 0)
871       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
872                                   "size_requested", val);
873     else if (strcmp(name, "sz_actual") == 0)
874       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
875                                   "size_allocated", val);
876     else if (strcmp(name, "allocs") == 0)
877       return varnish_submit_derive(conf->instance, "mempool",
878                                    "total_operations", "allocations", val);
879     else if (strcmp(name, "frees") == 0)
880       return varnish_submit_derive(conf->instance, "mempool",
881                                    "total_operations", "frees", val);
882     else if (strcmp(name, "recycle") == 0)
883       return varnish_submit_gauge(conf->instance, "mempool", "objects",
884                                   "recycled", val);
885     else if (strcmp(name, "timeout") == 0)
886       return varnish_submit_gauge(conf->instance, "mempool", "objects",
887                                   "timed_out", val);
888     else if (strcmp(name, "toosmall") == 0)
889       return varnish_submit_gauge(conf->instance, "mempool", "objects",
890                                   "too_small", val);
891     else if (strcmp(name, "surplus") == 0)
892       return varnish_submit_gauge(conf->instance, "mempool", "objects",
893                                   "surplus", val);
894     else if (strcmp(name, "randry") == 0)
895       return varnish_submit_gauge(conf->instance, "mempool", "objects",
896                                   "ran_dry", val);
897   }
898
899   if (conf->collect_mse) {
900     if (strcmp(name, "c_full") == 0)
901       return varnish_submit_derive(conf->instance, "mse", "total_operations",
902                                    "full_allocs", val);
903     else if (strcmp(name, "c_truncated") == 0)
904       return varnish_submit_derive(conf->instance, "mse", "total_operations",
905                                    "truncated_allocs", val);
906     else if (strcmp(name, "c_expanded") == 0)
907       return varnish_submit_derive(conf->instance, "mse", "total_operations",
908                                    "expanded_allocs", val);
909     else if (strcmp(name, "c_failed") == 0)
910       return varnish_submit_derive(conf->instance, "mse", "total_operations",
911                                    "failed_allocs", val);
912     else if (strcmp(name, "c_bytes") == 0)
913       return varnish_submit_derive(conf->instance, "mse", "total_bytes",
914                                    "bytes_allocated", val);
915     else if (strcmp(name, "c_freed") == 0)
916       return varnish_submit_derive(conf->instance, "mse", "total_bytes",
917                                    "bytes_freed", val);
918     else if (strcmp(name, "g_fo_alloc") == 0)
919       return varnish_submit_derive(conf->instance, "mse", "total_operations",
920                                    "fo_allocs_outstanding", val);
921     else if (strcmp(name, "g_fo_bytes") == 0)
922       return varnish_submit_gauge(conf->instance, "mse", "bytes",
923                                   "fo_bytes_outstanding", val);
924     else if (strcmp(name, "g_membuf_alloc") == 0)
925       return varnish_submit_gauge(conf->instance, "mse", "objects",
926                                   "membufs_allocated", val);
927     else if (strcmp(name, "g_membuf_inuse") == 0)
928       return varnish_submit_gauge(conf->instance, "mse", "objects",
929                                   "membufs_inuse", val);
930     else if (strcmp(name, "g_bans_bytes") == 0)
931       return varnish_submit_gauge(conf->instance, "mse", "bytes",
932                                   "persisted_banspace_used", val);
933     else if (strcmp(name, "g_bans_space") == 0)
934       return varnish_submit_gauge(conf->instance, "mse", "bytes",
935                                   "persisted_banspace_available", val);
936     else if (strcmp(name, "g_bans_persisted") == 0)
937       return varnish_submit_derive(conf->instance, "mse", "total_operations",
938                                    "bans_persisted", val);
939     else if (strcmp(name, "g_bans_lost") == 0)
940       return varnish_submit_derive(conf->instance, "mse", "total_operations",
941                                    "bans_lost", val);
942
943     /* mse seg */
944     else if (strcmp(name, "g_journal_bytes") == 0)
945       return varnish_submit_gauge(conf->instance, "mse_reg", "bytes",
946                                   "journal_bytes_used", val);
947     else if (strcmp(name, "g_journal_space") == 0)
948       return varnish_submit_gauge(conf->instance, "mse_reg", "bytes",
949                                   "journal_bytes_free", val);
950
951     /* mse segagg */
952     else if (strcmp(name, "g_bigspace") == 0)
953       return varnish_submit_gauge(conf->instance, "mse_segagg", "bytes",
954                                   "big_extents_bytes_available", val);
955     else if (strcmp(name, "g_extfree") == 0)
956       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
957                                   "free_extents", val);
958     else if (strcmp(name, "g_sparenode") == 0)
959       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
960                                   "spare_nodes_available", val);
961     else if (strcmp(name, "g_objnode") == 0)
962       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
963                                   "object_nodes_in_use", val);
964     else if (strcmp(name, "g_extnode") == 0)
965       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
966                                   "extent_nodes_in_use", val);
967     else if (strcmp(name, "g_bigextfree") == 0)
968       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
969                                   "free_big_extents", val);
970     else if (strcmp(name, "c_pruneloop") == 0)
971       return varnish_submit_derive(conf->instance, "mse_segagg",
972                                    "total_operations", "prune_loops", val);
973     else if (strcmp(name, "c_pruned") == 0)
974       return varnish_submit_derive(conf->instance, "mse_segagg",
975                                    "total_objects", "pruned_objects", val);
976     else if (strcmp(name, "c_spared") == 0)
977       return varnish_submit_derive(conf->instance, "mse_segagg",
978                                    "total_operations", "spared_objects", val);
979     else if (strcmp(name, "c_skipped") == 0)
980       return varnish_submit_derive(conf->instance, "mse_segagg",
981                                    "total_operations", "missed_objects", val);
982     else if (strcmp(name, "c_nuked") == 0)
983       return varnish_submit_derive(conf->instance, "mse_segagg",
984                                    "total_operations", "nuked_objects", val);
985     else if (strcmp(name, "c_sniped") == 0)
986       return varnish_submit_derive(conf->instance, "mse_segagg",
987                                    "total_operations", "sniped_objects", val);
988   }
989
990 #endif
991
992   return 0;
993
994 } /* }}} static int varnish_monitor */
995 #else /* if HAVE_VARNISH_V2 */
996 static void varnish_monitor(const user_config_t *conf, /* {{{ */
997                             const c_varnish_stats_t *stats) {
998   if (conf->collect_cache) {
999     /* Cache hits */
1000     varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
1001                           stats->cache_hit);
1002     /* Cache misses */
1003     varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
1004                           stats->cache_miss);
1005     /* Cache hits for pass */
1006     varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
1007                           stats->cache_hitpass);
1008   }
1009
1010   if (conf->collect_connections) {
1011     /* Client connections accepted */
1012     varnish_submit_derive(conf->instance, "connections", "connections",
1013                           "accepted", stats->client_conn);
1014     /* Connection dropped, no sess */
1015     varnish_submit_derive(conf->instance, "connections", "connections",
1016                           "dropped", stats->client_drop);
1017     /* Client requests received    */
1018     varnish_submit_derive(conf->instance, "connections", "connections",
1019                           "received", stats->client_req);
1020   }
1021
1022   if (conf->collect_esi) {
1023     /* ESI parse errors (unlock)   */
1024     varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
1025                           stats->esi_errors);
1026     /* Objects ESI parsed (unlock) */
1027     varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
1028                           stats->esi_parse);
1029   }
1030
1031   if (conf->collect_backend) {
1032     /* Backend conn. success       */
1033     varnish_submit_derive(conf->instance, "backend", "connections", "success",
1034                           stats->backend_conn);
1035     /* Backend conn. not attempted */
1036     varnish_submit_derive(conf->instance, "backend", "connections",
1037                           "not-attempted", stats->backend_unhealthy);
1038     /* Backend conn. too many      */
1039     varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
1040                           stats->backend_busy);
1041     /* Backend conn. failures      */
1042     varnish_submit_derive(conf->instance, "backend", "connections", "failures",
1043                           stats->backend_fail);
1044     /* Backend conn. reuses        */
1045     varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
1046                           stats->backend_reuse);
1047     /* Backend conn. was closed    */
1048     varnish_submit_derive(conf->instance, "backend", "connections",
1049                           "was-closed", stats->backend_toolate);
1050     /* Backend conn. recycles      */
1051     varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
1052                           stats->backend_recycle);
1053     /* Backend conn. unused        */
1054     varnish_submit_derive(conf->instance, "backend", "connections", "unused",
1055                           stats->backend_unused);
1056     /* Backend requests mades      */
1057     varnish_submit_derive(conf->instance, "backend", "http_requests",
1058                           "requests", stats->backend_req);
1059     /* N backends                  */
1060     varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
1061                          stats->n_backend);
1062   }
1063
1064   if (conf->collect_fetch) {
1065     /* Fetch head                */
1066     varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
1067                           stats->fetch_head);
1068     /* Fetch with length         */
1069     varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
1070                           stats->fetch_length);
1071     /* Fetch chunked             */
1072     varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
1073                           stats->fetch_chunked);
1074     /* Fetch EOF                 */
1075     varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
1076                           stats->fetch_eof);
1077     /* Fetch bad headers         */
1078     varnish_submit_derive(conf->instance, "fetch", "http_requests",
1079                           "bad_headers", stats->fetch_bad);
1080     /* Fetch wanted close        */
1081     varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
1082                           stats->fetch_close);
1083     /* Fetch pre HTTP/1.1 closed */
1084     varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
1085                           stats->fetch_oldhttp);
1086     /* Fetch zero len            */
1087     varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
1088                           stats->fetch_zero);
1089     /* Fetch failed              */
1090     varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
1091                           stats->fetch_failed);
1092   }
1093
1094   if (conf->collect_hcb) {
1095     /* HCB Lookups without lock */
1096     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
1097                           "lookup_nolock", stats->hcb_nolock);
1098     /* HCB Lookups with lock    */
1099     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
1100                           "lookup_lock", stats->hcb_lock);
1101     /* HCB Inserts              */
1102     varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
1103                           stats->hcb_insert);
1104   }
1105
1106   if (conf->collect_objects) {
1107     /* N expired objects             */
1108     varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
1109                           stats->n_expired);
1110     /* N LRU nuked objects           */
1111     varnish_submit_derive(conf->instance, "objects", "total_objects",
1112                           "lru_nuked", stats->n_lru_nuked);
1113     /* N LRU saved objects           */
1114     varnish_submit_derive(conf->instance, "objects", "total_objects",
1115                           "lru_saved", stats->n_lru_saved);
1116     /* N LRU moved objects           */
1117     varnish_submit_derive(conf->instance, "objects", "total_objects",
1118                           "lru_moved", stats->n_lru_moved);
1119     /* N objects on deathrow         */
1120     varnish_submit_derive(conf->instance, "objects", "total_objects",
1121                           "deathrow", stats->n_deathrow);
1122     /* HTTP header overflows         */
1123     varnish_submit_derive(conf->instance, "objects", "total_objects",
1124                           "header_overflow", stats->losthdr);
1125     /* Objects sent with sendfile    */
1126     varnish_submit_derive(conf->instance, "objects", "total_objects",
1127                           "sent_sendfile", stats->n_objsendfile);
1128     /* Objects sent with write       */
1129     varnish_submit_derive(conf->instance, "objects", "total_objects",
1130                           "sent_write", stats->n_objwrite);
1131     /* Objects overflowing workspace */
1132     varnish_submit_derive(conf->instance, "objects", "total_objects",
1133                           "workspace_overflow", stats->n_objoverflow);
1134   }
1135
1136   if (conf->collect_purge) {
1137     /* N total active purges      */
1138     varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
1139                           stats->n_purge);
1140     /* N new purges added         */
1141     varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
1142                           stats->n_purge_add);
1143     /* N old purges deleted       */
1144     varnish_submit_derive(conf->instance, "purge", "total_operations",
1145                           "deleted", stats->n_purge_retire);
1146     /* N objects tested           */
1147     varnish_submit_derive(conf->instance, "purge", "total_operations",
1148                           "objects_tested", stats->n_purge_obj_test);
1149     /* N regexps tested against   */
1150     varnish_submit_derive(conf->instance, "purge", "total_operations",
1151                           "regexps_tested", stats->n_purge_re_test);
1152     /* N duplicate purges removed */
1153     varnish_submit_derive(conf->instance, "purge", "total_operations",
1154                           "duplicate", stats->n_purge_dups);
1155   }
1156
1157   if (conf->collect_session) {
1158     /* Session Closed     */
1159     varnish_submit_derive(conf->instance, "session", "total_operations",
1160                           "closed", stats->sess_closed);
1161     /* Session Pipeline   */
1162     varnish_submit_derive(conf->instance, "session", "total_operations",
1163                           "pipeline", stats->sess_pipeline);
1164     /* Session Read Ahead */
1165     varnish_submit_derive(conf->instance, "session", "total_operations",
1166                           "readahead", stats->sess_readahead);
1167     /* Session Linger     */
1168     varnish_submit_derive(conf->instance, "session", "total_operations",
1169                           "linger", stats->sess_linger);
1170     /* Session herd       */
1171     varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
1172                           stats->sess_herd);
1173   }
1174
1175   if (conf->collect_shm) {
1176     /* SHM records                 */
1177     varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
1178                           stats->shm_records);
1179     /* SHM writes                  */
1180     varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
1181                           stats->shm_writes);
1182     /* SHM flushes due to overflow */
1183     varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
1184                           stats->shm_flushes);
1185     /* SHM MTX contention          */
1186     varnish_submit_derive(conf->instance, "shm", "total_operations",
1187                           "contention", stats->shm_cont);
1188     /* SHM cycles through buffer   */
1189     varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
1190                           stats->shm_cycles);
1191   }
1192
1193   if (conf->collect_sm) {
1194     /* allocator requests */
1195     varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
1196                           stats->sm_nreq);
1197     /* outstanding allocations */
1198     varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
1199                          stats->sm_nobj);
1200     /* bytes allocated */
1201     varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
1202                           stats->sm_balloc);
1203     /* bytes free */
1204     varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
1205                           stats->sm_bfree);
1206   }
1207
1208   if (conf->collect_sma) {
1209     /* SMA allocator requests */
1210     varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
1211                           stats->sma_nreq);
1212     /* SMA outstanding allocations */
1213     varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
1214                          stats->sma_nobj);
1215     /* SMA outstanding bytes */
1216     varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
1217                          stats->sma_nbytes);
1218     /* SMA bytes allocated */
1219     varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
1220                           stats->sma_balloc);
1221     /* SMA bytes free */
1222     varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
1223                           stats->sma_bfree);
1224   }
1225
1226   if (conf->collect_sms) {
1227     /* SMS allocator requests */
1228     varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
1229                           stats->sms_nreq);
1230     /* SMS outstanding allocations */
1231     varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
1232                          stats->sms_nobj);
1233     /* SMS outstanding bytes */
1234     varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
1235                          stats->sms_nbytes);
1236     /* SMS bytes allocated */
1237     varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
1238                           stats->sms_balloc);
1239     /* SMS bytes freed */
1240     varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
1241                           stats->sms_bfree);
1242   }
1243
1244   if (conf->collect_struct) {
1245     /* N struct sess_mem       */
1246     varnish_submit_gauge(conf->instance, "struct", "current_sessions",
1247                          "sess_mem", stats->n_sess_mem);
1248     /* N struct sess           */
1249     varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
1250                          stats->n_sess);
1251     /* N struct object         */
1252     varnish_submit_gauge(conf->instance, "struct", "objects", "object",
1253                          stats->n_object);
1254     /* N struct objecthead     */
1255     varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
1256                          stats->n_objecthead);
1257     /* N struct smf            */
1258     varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
1259                          stats->n_smf);
1260     /* N small free smf         */
1261     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
1262                          stats->n_smf_frag);
1263     /* N large free smf         */
1264     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
1265                          stats->n_smf_large);
1266     /* N struct vbe_conn        */
1267     varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
1268                          stats->n_vbe_conn);
1269   }
1270
1271   if (conf->collect_totals) {
1272     /* Total Sessions */
1273     varnish_submit_derive(conf->instance, "totals", "total_sessions",
1274                           "sessions", stats->s_sess);
1275     /* Total Requests */
1276     varnish_submit_derive(conf->instance, "totals", "total_requests",
1277                           "requests", stats->s_req);
1278     /* Total pipe */
1279     varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
1280                           stats->s_pipe);
1281     /* Total pass */
1282     varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
1283                           stats->s_pass);
1284     /* Total fetch */
1285     varnish_submit_derive(conf->instance, "totals", "total_operations",
1286                           "fetches", stats->s_fetch);
1287     /* Total header bytes */
1288     varnish_submit_derive(conf->instance, "totals", "total_bytes",
1289                           "header-bytes", stats->s_hdrbytes);
1290     /* Total body byte */
1291     varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
1292                           stats->s_bodybytes);
1293   }
1294
1295   if (conf->collect_vcl) {
1296     /* N vcl total     */
1297     varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
1298                          stats->n_vcl);
1299     /* N vcl available */
1300     varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
1301                          stats->n_vcl_avail);
1302     /* N vcl discarded */
1303     varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
1304                          stats->n_vcl_discard);
1305   }
1306
1307   if (conf->collect_workers) {
1308     /* worker threads */
1309     varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
1310                          stats->n_wrk);
1311     /* worker threads created */
1312     varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
1313                           stats->n_wrk_create);
1314     /* worker threads not created */
1315     varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
1316                           stats->n_wrk_failed);
1317     /* worker threads limited */
1318     varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
1319                           stats->n_wrk_max);
1320     /* dropped work requests */
1321     varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
1322                           stats->n_wrk_drop);
1323     /* queued work requests */
1324     varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
1325                           stats->n_wrk_queue);
1326     /* overflowed work requests */
1327     varnish_submit_derive(conf->instance, "workers", "total_requests",
1328                           "overflowed", stats->n_wrk_overflow);
1329   }
1330
1331 } /* }}} void varnish_monitor */
1332 #endif
1333
1334 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1335 static int varnish_read(user_data_t *ud) /* {{{ */
1336 {
1337 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1338   struct VSM_data *vd;
1339   _Bool ok;
1340   const c_varnish_stats_t *stats;
1341 #elif HAVE_VARNISH_V5
1342   struct vsm *vd;
1343   struct vsc *vsc;
1344   int vsm_status;
1345 #endif
1346
1347   user_config_t *conf;
1348
1349   if ((ud == NULL) || (ud->data == NULL))
1350     return EINVAL;
1351
1352   conf = ud->data;
1353
1354   vd = VSM_New();
1355
1356 #if HAVE_VARNISH_V5
1357   vsc = VSC_New();
1358 #endif
1359
1360 #if HAVE_VARNISH_V3
1361   VSC_Setup(vd);
1362 #endif
1363
1364   if (conf->instance != NULL) {
1365     int status;
1366
1367 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1368     status = VSM_n_Arg(vd, conf->instance);
1369 #elif HAVE_VARNISH_V5
1370     status = VSM_Arg(vd, 'n', conf->instance);
1371 #endif
1372
1373     if (status < 0) {
1374 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1375       VSM_Delete(vd);
1376 #elif HAVE_VARNISH_V5
1377       VSC_Destroy(&vsc, vd);
1378       VSM_Destroy(&vd);
1379 #endif
1380       ERROR("varnish plugin: VSM_Arg (\"%s\") failed "
1381             "with status %i.",
1382             conf->instance, status);
1383       return -1;
1384     }
1385   }
1386
1387 #if HAVE_VARNISH_V3
1388   ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1389 #elif HAVE_VARNISH_V4
1390   ok = (VSM_Open(vd) == 0);
1391 #endif
1392 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1393   if (!ok) {
1394     VSM_Delete(vd);
1395     ERROR("varnish plugin: Unable to open connection.");
1396     return -1;
1397   }
1398 #endif
1399
1400 #if HAVE_VARNISH_V3
1401   stats = VSC_Main(vd);
1402 #elif HAVE_VARNISH_V4
1403   stats = VSC_Main(vd, NULL);
1404 #endif
1405 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1406   if (!stats) {
1407     VSM_Delete(vd);
1408     ERROR("varnish plugin: Unable to get statistics.");
1409     return -1;
1410   }
1411 #endif
1412
1413 #if HAVE_VARNISH_V5
1414   if (VSM_Attach(vd, STDERR_FILENO)) {
1415     ERROR("varnish plugin: Cannot attach to varnish. %s", VSM_Error(vd));
1416     VSC_Destroy(&vsc, vd);
1417     VSM_Destroy(&vd);
1418     return -1;
1419   }
1420
1421   vsm_status = VSM_Status(vd);
1422   if (vsm_status & ~(VSM_MGT_RUNNING | VSM_WRK_RUNNING)) {
1423     ERROR("varnish plugin: Unable to get statistics.");
1424     VSC_Destroy(&vsc, vd);
1425     VSM_Destroy(&vd);
1426     return -1;
1427   }
1428 #endif
1429
1430 #if HAVE_VARNISH_V3
1431   VSC_Iter(vd, varnish_monitor, conf);
1432 #elif HAVE_VARNISH_V4
1433   VSC_Iter(vd, NULL, varnish_monitor, conf);
1434 #elif HAVE_VARNISH_V5
1435   VSC_Iter(vsc, vd, varnish_monitor, conf);
1436 #endif
1437
1438 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1439   VSM_Delete(vd);
1440 #elif HAVE_VARNISH_V5
1441   VSC_Destroy(&vsc, vd);
1442   VSM_Destroy(&vd);
1443 #endif
1444
1445   return 0;
1446 } /* }}} */
1447 #else /* if HAVE_VARNISH_V2 */
1448 static int varnish_read(user_data_t *ud) /* {{{ */
1449 {
1450   const c_varnish_stats_t *stats;
1451
1452   user_config_t *conf;
1453
1454   if ((ud == NULL) || (ud->data == NULL))
1455     return EINVAL;
1456
1457   conf = ud->data;
1458
1459   stats = VSL_OpenStats(conf->instance);
1460   if (stats == NULL) {
1461     ERROR("Varnish plugin : unable to load statistics");
1462
1463     return -1;
1464   }
1465
1466   varnish_monitor(conf, stats);
1467
1468   return 0;
1469 } /* }}} */
1470 #endif
1471
1472 static void varnish_config_free(void *ptr) /* {{{ */
1473 {
1474   user_config_t *conf = ptr;
1475
1476   if (conf == NULL)
1477     return;
1478
1479   sfree(conf->instance);
1480   sfree(conf);
1481 } /* }}} */
1482
1483 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1484 {
1485   if (conf == NULL)
1486     return EINVAL;
1487
1488   conf->collect_backend = 1;
1489   conf->collect_cache = 1;
1490   conf->collect_connections = 1;
1491 #ifdef HAVE_VARNISH_V3
1492   conf->collect_dirdns = 0;
1493 #endif
1494   conf->collect_esi = 0;
1495   conf->collect_fetch = 0;
1496   conf->collect_hcb = 0;
1497   conf->collect_objects = 0;
1498 #if HAVE_VARNISH_V2
1499   conf->collect_purge = 0;
1500 #else
1501   conf->collect_ban = 0;
1502 #endif
1503   conf->collect_session = 0;
1504   conf->collect_shm = 1;
1505 #if HAVE_VARNISH_V2
1506   conf->collect_sm = 0;
1507 #endif
1508 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1509   conf->collect_sma = 0;
1510 #endif
1511   conf->collect_sms = 0;
1512   conf->collect_struct = 0;
1513   conf->collect_totals = 0;
1514 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1515   conf->collect_uptime = 0;
1516 #endif
1517   conf->collect_vcl = 0;
1518   conf->collect_workers = 0;
1519 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1520   conf->collect_vsm = 0;
1521   conf->collect_lck = 0;
1522   conf->collect_mempool = 0;
1523   conf->collect_mgt = 0;
1524   conf->collect_smf = 0;
1525   conf->collect_vbe = 0;
1526   conf->collect_mse = 0;
1527 #endif
1528
1529   return 0;
1530 } /* }}} int varnish_config_apply_default */
1531
1532 static int varnish_init(void) /* {{{ */
1533 {
1534   user_config_t *conf;
1535
1536   if (have_instance)
1537     return 0;
1538
1539   conf = calloc(1, sizeof(*conf));
1540   if (conf == NULL)
1541     return ENOMEM;
1542
1543   /* Default settings: */
1544   conf->instance = NULL;
1545
1546   varnish_config_apply_default(conf);
1547
1548   plugin_register_complex_read(
1549       /* group = */ "varnish",
1550       /* name      = */ "varnish/localhost",
1551       /* callback  = */ varnish_read,
1552       /* interval  = */ 0,
1553       &(user_data_t){
1554           .data = conf, .free_func = varnish_config_free,
1555       });
1556
1557   return 0;
1558 } /* }}} int varnish_init */
1559
1560 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1561 {
1562   user_config_t *conf;
1563   char callback_name[DATA_MAX_NAME_LEN];
1564
1565   conf = calloc(1, sizeof(*conf));
1566   if (conf == NULL)
1567     return ENOMEM;
1568   conf->instance = NULL;
1569
1570   varnish_config_apply_default(conf);
1571
1572   if (ci->values_num == 1) {
1573     int status;
1574
1575     status = cf_util_get_string(ci, &conf->instance);
1576     if (status != 0) {
1577       sfree(conf);
1578       return status;
1579     }
1580     assert(conf->instance != NULL);
1581
1582     if (strcmp("localhost", conf->instance) == 0) {
1583       sfree(conf->instance);
1584       conf->instance = NULL;
1585     }
1586   } else if (ci->values_num > 1) {
1587     WARNING("Varnish plugin: \"Instance\" blocks accept only "
1588             "one argument.");
1589     sfree(conf);
1590     return EINVAL;
1591   }
1592
1593   for (int i = 0; i < ci->children_num; i++) {
1594     oconfig_item_t *child = ci->children + i;
1595
1596     if (strcasecmp("CollectCache", child->key) == 0)
1597       cf_util_get_boolean(child, &conf->collect_cache);
1598     else if (strcasecmp("CollectConnections", child->key) == 0)
1599       cf_util_get_boolean(child, &conf->collect_connections);
1600     else if (strcasecmp("CollectESI", child->key) == 0)
1601       cf_util_get_boolean(child, &conf->collect_esi);
1602     else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1603 #ifdef HAVE_VARNISH_V3
1604       cf_util_get_boolean(child, &conf->collect_dirdns);
1605 #else
1606       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1607               child->key, "v3");
1608 #endif
1609     else if (strcasecmp("CollectBackend", child->key) == 0)
1610       cf_util_get_boolean(child, &conf->collect_backend);
1611     else if (strcasecmp("CollectFetch", child->key) == 0)
1612       cf_util_get_boolean(child, &conf->collect_fetch);
1613     else if (strcasecmp("CollectHCB", child->key) == 0)
1614       cf_util_get_boolean(child, &conf->collect_hcb);
1615     else if (strcasecmp("CollectObjects", child->key) == 0)
1616       cf_util_get_boolean(child, &conf->collect_objects);
1617     else if (strcasecmp("CollectPurge", child->key) == 0)
1618 #if HAVE_VARNISH_V2
1619       cf_util_get_boolean(child, &conf->collect_purge);
1620 #else
1621       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1622               child->key, "v2");
1623 #endif
1624     else if (strcasecmp("CollectBan", child->key) == 0)
1625 #if HAVE_VARNISH_V2
1626       WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1627               child->key, "v2");
1628 #else
1629       cf_util_get_boolean(child, &conf->collect_ban);
1630 #endif
1631     else if (strcasecmp("CollectSession", child->key) == 0)
1632       cf_util_get_boolean(child, &conf->collect_session);
1633     else if (strcasecmp("CollectSHM", child->key) == 0)
1634       cf_util_get_boolean(child, &conf->collect_shm);
1635     else if (strcasecmp("CollectSMS", child->key) == 0)
1636       cf_util_get_boolean(child, &conf->collect_sms);
1637     else if (strcasecmp("CollectSMA", child->key) == 0)
1638 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1639       cf_util_get_boolean(child, &conf->collect_sma);
1640 #else
1641       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1642               child->key, "v2 and v4");
1643 #endif
1644     else if (strcasecmp("CollectSM", child->key) == 0)
1645 #if HAVE_VARNISH_V2
1646       cf_util_get_boolean(child, &conf->collect_sm);
1647 #else
1648       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1649               child->key, "v2");
1650 #endif
1651     else if (strcasecmp("CollectStruct", child->key) == 0)
1652       cf_util_get_boolean(child, &conf->collect_struct);
1653     else if (strcasecmp("CollectTotals", child->key) == 0)
1654       cf_util_get_boolean(child, &conf->collect_totals);
1655     else if (strcasecmp("CollectUptime", child->key) == 0)
1656 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1657       cf_util_get_boolean(child, &conf->collect_uptime);
1658 #else
1659       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1660               child->key, "v3 and v4");
1661 #endif
1662     else if (strcasecmp("CollectVCL", child->key) == 0)
1663       cf_util_get_boolean(child, &conf->collect_vcl);
1664     else if (strcasecmp("CollectWorkers", child->key) == 0)
1665       cf_util_get_boolean(child, &conf->collect_workers);
1666     else if (strcasecmp("CollectVSM", child->key) == 0)
1667 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1668       cf_util_get_boolean(child, &conf->collect_vsm);
1669 #else
1670       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1671               child->key, "v4");
1672 #endif
1673     else if (strcasecmp("CollectLock", child->key) == 0)
1674 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1675       cf_util_get_boolean(child, &conf->collect_lck);
1676 #else
1677       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1678               child->key, "v4");
1679 #endif
1680     else if (strcasecmp("CollectMempool", child->key) == 0)
1681 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1682       cf_util_get_boolean(child, &conf->collect_mempool);
1683 #else
1684       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1685               child->key, "v4");
1686 #endif
1687     else if (strcasecmp("CollectManagement", child->key) == 0)
1688 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1689       cf_util_get_boolean(child, &conf->collect_mgt);
1690 #else
1691       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1692               child->key, "v4");
1693 #endif
1694     else if (strcasecmp("CollectSMF", child->key) == 0)
1695 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1696       cf_util_get_boolean(child, &conf->collect_smf);
1697 #else
1698       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1699               child->key, "v4");
1700 #endif
1701     else if (strcasecmp("CollectSMF", child->key) == 0)
1702 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1703       cf_util_get_boolean(child, &conf->collect_smf);
1704 #else
1705       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1706               child->key, "v4");
1707 #endif
1708     else if (strcasecmp("CollectVBE", child->key) == 0)
1709 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1710       cf_util_get_boolean(child, &conf->collect_vbe);
1711 #else
1712       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1713               child->key, "v4");
1714 #endif
1715     else if (strcasecmp("CollectMSE", child->key) == 0)
1716 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1717       cf_util_get_boolean(child, &conf->collect_mse);
1718 #else
1719       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1720               child->key, "Plus v4");
1721 #endif
1722     else {
1723       WARNING("Varnish plugin: Ignoring unknown "
1724               "configuration option: \"%s\". Did "
1725               "you forget to add an <Instance /> "
1726               "block around the configuration?",
1727               child->key);
1728     }
1729   }
1730
1731   if (!conf->collect_cache && !conf->collect_connections &&
1732       !conf->collect_esi && !conf->collect_backend
1733 #ifdef HAVE_VARNISH_V3
1734       && !conf->collect_dirdns
1735 #endif
1736       && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1737 #if HAVE_VARNISH_V2
1738       && !conf->collect_purge
1739 #else
1740       && !conf->collect_ban
1741 #endif
1742       && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1743 #if HAVE_VARNISH_V2
1744       && !conf->collect_sm
1745 #endif
1746 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1747       && !conf->collect_sma
1748 #endif
1749       && !conf->collect_struct && !conf->collect_totals
1750 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1751       && !conf->collect_uptime
1752 #endif
1753       && !conf->collect_vcl && !conf->collect_workers
1754 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1755       && !conf->collect_vsm && !conf->collect_vbe && !conf->collect_smf &&
1756       !conf->collect_mgt && !conf->collect_lck && !conf->collect_mempool &&
1757       !conf->collect_mse
1758 #endif
1759       ) {
1760     WARNING("Varnish plugin: No metric has been configured for "
1761             "instance \"%s\". Disabling this instance.",
1762             (conf->instance == NULL) ? "localhost" : conf->instance);
1763     sfree(conf);
1764     return EINVAL;
1765   }
1766
1767   snprintf(callback_name, sizeof(callback_name), "varnish/%s",
1768            (conf->instance == NULL) ? "localhost" : conf->instance);
1769
1770   plugin_register_complex_read(
1771       /* group = */ "varnish",
1772       /* name      = */ callback_name,
1773       /* callback  = */ varnish_read,
1774       /* interval  = */ 0,
1775       &(user_data_t){
1776           .data = conf, .free_func = varnish_config_free,
1777       });
1778
1779   have_instance = 1;
1780
1781   return 0;
1782 } /* }}} int varnish_config_instance */
1783
1784 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1785 {
1786   for (int i = 0; i < ci->children_num; i++) {
1787     oconfig_item_t *child = ci->children + i;
1788
1789     if (strcasecmp("Instance", child->key) == 0)
1790       varnish_config_instance(child);
1791     else {
1792       WARNING("Varnish plugin: Ignoring unknown "
1793               "configuration option: \"%s\"",
1794               child->key);
1795     }
1796   }
1797
1798   return 0;
1799 } /* }}} int varnish_config */
1800
1801 void module_register(void) /* {{{ */
1802 {
1803   plugin_register_complex_config("varnish", varnish_config);
1804   plugin_register_init("varnish", varnish_init);
1805 } /* }}} */