varnish-cache-5.2 compatibility
[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 *class;
148   const char *name;
149
150   if (pt == NULL)
151     return 0;
152
153   conf = priv;
154
155 #if HAVE_VARNISH_V5
156   char namebuff[100];
157   char *c;
158
159   c = rindex(pt->name, '.');
160   strcpy(namebuff, c+1);
161   name = namebuff;
162
163   (void)class;
164
165 #elif HAVE_VARNISH_V4
166   class = pt->section->fantom->type;
167   name = pt->desc->name;
168
169   if (strcmp(class, "MAIN") != 0)
170     return 0;
171
172 #elif HAVE_VARNISH_V3
173   class = pt->class;
174   name = pt->name;
175
176   if (strcmp(class, "") != 0)
177     return 0;
178 #endif
179
180   val = *(const volatile uint64_t *)pt->ptr;
181
182   if (conf->collect_cache) {
183     if (strcmp(name, "cache_hit") == 0)
184       return varnish_submit_derive(conf->instance, "cache", "cache_result",
185                                    "hit", val);
186     else if (strcmp(name, "cache_miss") == 0)
187       return varnish_submit_derive(conf->instance, "cache", "cache_result",
188                                    "miss", val);
189     else if (strcmp(name, "cache_hitpass") == 0)
190       return varnish_submit_derive(conf->instance, "cache", "cache_result",
191                                    "hitpass", val);
192   }
193
194   if (conf->collect_connections) {
195     if (strcmp(name, "client_conn") == 0)
196       return varnish_submit_derive(conf->instance, "connections", "connections",
197                                    "accepted", val);
198     else if (strcmp(name, "client_drop") == 0)
199       return varnish_submit_derive(conf->instance, "connections", "connections",
200                                    "dropped", val);
201     else if (strcmp(name, "client_req") == 0)
202       return varnish_submit_derive(conf->instance, "connections", "connections",
203                                    "received", val);
204 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
205     else if (strcmp(name, "client_req_400") == 0)
206       return varnish_submit_derive(conf->instance, "connections", "connections",
207                                    "error_400", val);
208     else if (strcmp(name, "client_req_417") == 0)
209       return varnish_submit_derive(conf->instance, "connections", "connections",
210                                    "error_417", val);
211 #endif
212   }
213
214 #ifdef HAVE_VARNISH_V3
215   if (conf->collect_dirdns) {
216     if (strcmp(name, "dir_dns_lookups") == 0)
217       return varnish_submit_derive(conf->instance, "dirdns", "cache_operation",
218                                    "lookups", val);
219     else if (strcmp(name, "dir_dns_failed") == 0)
220       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
221                                    "failed", val);
222     else if (strcmp(name, "dir_dns_hit") == 0)
223       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
224                                    "hits", val);
225     else if (strcmp(name, "dir_dns_cache_full") == 0)
226       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
227                                    "cache_full", val);
228   }
229 #endif
230
231   if (conf->collect_esi) {
232     if (strcmp(name, "esi_errors") == 0)
233       return varnish_submit_derive(conf->instance, "esi", "total_operations",
234                                    "error", val);
235     else if (strcmp(name, "esi_parse") == 0)
236       return varnish_submit_derive(conf->instance, "esi", "total_operations",
237                                    "parsed", val);
238     else if (strcmp(name, "esi_warnings") == 0)
239       return varnish_submit_derive(conf->instance, "esi", "total_operations",
240                                    "warning", val);
241     else if (strcmp(name, "esi_maxdepth") == 0)
242       return varnish_submit_derive(conf->instance, "esi", "total_operations",
243                                    "max_depth", val);
244   }
245
246   if (conf->collect_backend) {
247     if (strcmp(name, "backend_conn") == 0)
248       return varnish_submit_derive(conf->instance, "backend", "connections",
249                                    "success", val);
250     else if (strcmp(name, "backend_unhealthy") == 0)
251       return varnish_submit_derive(conf->instance, "backend", "connections",
252                                    "not-attempted", val);
253     else if (strcmp(name, "backend_busy") == 0)
254       return varnish_submit_derive(conf->instance, "backend", "connections",
255                                    "too-many", val);
256     else if (strcmp(name, "backend_fail") == 0)
257       return varnish_submit_derive(conf->instance, "backend", "connections",
258                                    "failures", val);
259     else if (strcmp(name, "backend_reuse") == 0)
260       return varnish_submit_derive(conf->instance, "backend", "connections",
261                                    "reuses", val);
262     else if (strcmp(name, "backend_toolate") == 0)
263       return varnish_submit_derive(conf->instance, "backend", "connections",
264                                    "was-closed", val);
265     else if (strcmp(name, "backend_recycle") == 0)
266       return varnish_submit_derive(conf->instance, "backend", "connections",
267                                    "recycled", val);
268     else if (strcmp(name, "backend_unused") == 0)
269       return varnish_submit_derive(conf->instance, "backend", "connections",
270                                    "unused", val);
271     else if (strcmp(name, "backend_retry") == 0)
272       return varnish_submit_derive(conf->instance, "backend", "connections",
273                                    "retries", val);
274     else if (strcmp(name, "backend_req") == 0)
275       return varnish_submit_derive(conf->instance, "backend", "http_requests",
276                                    "requests", val);
277     else if (strcmp(name, "n_backend") == 0)
278       return varnish_submit_gauge(conf->instance, "backend", "backends",
279                                   "n_backends", val);
280   }
281
282   if (conf->collect_fetch) {
283     if (strcmp(name, "fetch_head") == 0)
284       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
285                                    "head", val);
286     else if (strcmp(name, "fetch_length") == 0)
287       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
288                                    "length", val);
289     else if (strcmp(name, "fetch_chunked") == 0)
290       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
291                                    "chunked", val);
292     else if (strcmp(name, "fetch_eof") == 0)
293       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
294                                    "eof", val);
295     else if (strcmp(name, "fetch_bad") == 0)
296       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
297                                    "bad_headers", val);
298     else if (strcmp(name, "fetch_close") == 0)
299       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
300                                    "close", val);
301     else if (strcmp(name, "fetch_oldhttp") == 0)
302       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
303                                    "oldhttp", val);
304     else if (strcmp(name, "fetch_zero") == 0)
305       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
306                                    "zero", val);
307     else if (strcmp(name, "fetch_failed") == 0)
308       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
309                                    "failed", val);
310     else if (strcmp(name, "fetch_1xx") == 0)
311       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
312                                    "no_body_1xx", val);
313     else if (strcmp(name, "fetch_204") == 0)
314       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
315                                    "no_body_204", val);
316     else if (strcmp(name, "fetch_304") == 0)
317       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
318                                    "no_body_304", val);
319 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
320     else if (strcmp(name, "fetch_no_thread") == 0)
321       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
322                                    "no_thread", val);
323     else if (strcmp(name, "fetch_none") == 0)
324       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
325                                    "none", val);
326     else if (strcmp(name, "busy_sleep") == 0)
327       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
328                                    "busy_sleep", val);
329     else if (strcmp(name, "busy_wakeup") == 0)
330       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
331                                    "busy_wakeup", val);
332 #endif
333   }
334
335   if (conf->collect_hcb) {
336     if (strcmp(name, "hcb_nolock") == 0)
337       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
338                                    "lookup_nolock", val);
339     else if (strcmp(name, "hcb_lock") == 0)
340       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
341                                    "lookup_lock", val);
342     else if (strcmp(name, "hcb_insert") == 0)
343       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
344                                    "insert", val);
345   }
346
347   if (conf->collect_objects) {
348     if (strcmp(name, "n_expired") == 0)
349       return varnish_submit_derive(conf->instance, "objects", "total_objects",
350                                    "expired", val);
351     else if (strcmp(name, "n_lru_nuked") == 0)
352       return varnish_submit_derive(conf->instance, "objects", "total_objects",
353                                    "lru_nuked", val);
354     else if (strcmp(name, "n_lru_saved") == 0)
355       return varnish_submit_derive(conf->instance, "objects", "total_objects",
356                                    "lru_saved", val);
357     else if (strcmp(name, "n_lru_moved") == 0)
358       return varnish_submit_derive(conf->instance, "objects", "total_objects",
359                                    "lru_moved", val);
360     else if (strcmp(name, "n_deathrow") == 0)
361       return varnish_submit_derive(conf->instance, "objects", "total_objects",
362                                    "deathrow", val);
363     else if (strcmp(name, "losthdr") == 0)
364       return varnish_submit_derive(conf->instance, "objects", "total_objects",
365                                    "header_overflow", val);
366     else if (strcmp(name, "n_obj_purged") == 0)
367       return varnish_submit_derive(conf->instance, "objects", "total_objects",
368                                    "purged", val);
369     else if (strcmp(name, "n_objsendfile") == 0)
370       return varnish_submit_derive(conf->instance, "objects", "total_objects",
371                                    "sent_sendfile", val);
372     else if (strcmp(name, "n_objwrite") == 0)
373       return varnish_submit_derive(conf->instance, "objects", "total_objects",
374                                    "sent_write", val);
375     else if (strcmp(name, "n_objoverflow") == 0)
376       return varnish_submit_derive(conf->instance, "objects", "total_objects",
377                                    "workspace_overflow", val);
378 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
379     else if (strcmp(name, "exp_mailed") == 0)
380       return varnish_submit_gauge(conf->instance, "struct", "objects",
381                                   "exp_mailed", val);
382     else if (strcmp(name, "exp_received") == 0)
383       return varnish_submit_gauge(conf->instance, "struct", "objects",
384                                   "exp_received", val);
385 #endif
386   }
387
388 #if HAVE_VARNISH_V3
389   if (conf->collect_ban) {
390     if (strcmp(name, "n_ban") == 0)
391       return varnish_submit_derive(conf->instance, "ban", "total_operations",
392                                    "total", val);
393     else if (strcmp(name, "n_ban_add") == 0)
394       return varnish_submit_derive(conf->instance, "ban", "total_operations",
395                                    "added", val);
396     else if (strcmp(name, "n_ban_retire") == 0)
397       return varnish_submit_derive(conf->instance, "ban", "total_operations",
398                                    "deleted", val);
399     else if (strcmp(name, "n_ban_obj_test") == 0)
400       return varnish_submit_derive(conf->instance, "ban", "total_operations",
401                                    "objects_tested", val);
402     else if (strcmp(name, "n_ban_re_test") == 0)
403       return varnish_submit_derive(conf->instance, "ban", "total_operations",
404                                    "regexps_tested", val);
405     else if (strcmp(name, "n_ban_dups") == 0)
406       return varnish_submit_derive(conf->instance, "ban", "total_operations",
407                                    "duplicate", val);
408   }
409 #endif
410 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
411   if (conf->collect_ban) {
412     if (strcmp(name, "bans") == 0)
413       return varnish_submit_derive(conf->instance, "ban", "total_operations",
414                                    "total", val);
415     else if (strcmp(name, "bans_added") == 0)
416       return varnish_submit_derive(conf->instance, "ban", "total_operations",
417                                    "added", val);
418     else if (strcmp(name, "bans_obj") == 0)
419       return varnish_submit_derive(conf->instance, "ban", "total_operations",
420                                    "obj", val);
421     else if (strcmp(name, "bans_req") == 0)
422       return varnish_submit_derive(conf->instance, "ban", "total_operations",
423                                    "req", val);
424     else if (strcmp(name, "bans_completed") == 0)
425       return varnish_submit_derive(conf->instance, "ban", "total_operations",
426                                    "completed", val);
427     else if (strcmp(name, "bans_deleted") == 0)
428       return varnish_submit_derive(conf->instance, "ban", "total_operations",
429                                    "deleted", val);
430     else if (strcmp(name, "bans_tested") == 0)
431       return varnish_submit_derive(conf->instance, "ban", "total_operations",
432                                    "tested", val);
433     else if (strcmp(name, "bans_dups") == 0)
434       return varnish_submit_derive(conf->instance, "ban", "total_operations",
435                                    "duplicate", val);
436     else if (strcmp(name, "bans_tested") == 0)
437       return varnish_submit_derive(conf->instance, "ban", "total_operations",
438                                    "tested", val);
439     else if (strcmp(name, "bans_lurker_contention") == 0)
440       return varnish_submit_derive(conf->instance, "ban", "total_operations",
441                                    "lurker_contention", val);
442     else if (strcmp(name, "bans_lurker_obj_killed") == 0)
443       return varnish_submit_derive(conf->instance, "ban", "total_operations",
444                                    "lurker_obj_killed", val);
445     else if (strcmp(name, "bans_lurker_tested") == 0)
446       return varnish_submit_derive(conf->instance, "ban", "total_operations",
447                                    "lurker_tested", val);
448     else if (strcmp(name, "bans_lurker_tests_tested") == 0)
449       return varnish_submit_derive(conf->instance, "ban", "total_operations",
450                                    "lurker_tests_tested", val);
451     else if (strcmp(name, "bans_obj_killed") == 0)
452       return varnish_submit_derive(conf->instance, "ban", "total_operations",
453                                    "obj_killed", val);
454     else if (strcmp(name, "bans_persisted_bytes") == 0)
455       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
456                                    "persisted_bytes", val);
457     else if (strcmp(name, "bans_persisted_fragmentation") == 0)
458       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
459                                    "persisted_fragmentation", val);
460     else if (strcmp(name, "bans_tests_tested") == 0)
461       return varnish_submit_derive(conf->instance, "ban", "total_operations",
462                                    "tests_tested", val);
463
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", val);
505 #endif
506   }
507
508   if (conf->collect_shm) {
509     if (strcmp(name, "shm_records") == 0)
510       return varnish_submit_derive(conf->instance, "shm", "total_operations",
511                                    "records", val);
512     else if (strcmp(name, "shm_writes") == 0)
513       return varnish_submit_derive(conf->instance, "shm", "total_operations",
514                                    "writes", val);
515     else if (strcmp(name, "shm_flushes") == 0)
516       return varnish_submit_derive(conf->instance, "shm", "total_operations",
517                                    "flushes", val);
518     else if (strcmp(name, "shm_cont") == 0)
519       return varnish_submit_derive(conf->instance, "shm", "total_operations",
520                                    "contention", val);
521     else if (strcmp(name, "shm_cycles") == 0)
522       return varnish_submit_derive(conf->instance, "shm", "total_operations",
523                                    "cycles", val);
524   }
525
526   if (conf->collect_sms) {
527     if (strcmp(name, "sms_nreq") == 0)
528       return varnish_submit_derive(conf->instance, "sms", "total_requests",
529                                    "allocator", val);
530     else if (strcmp(name, "sms_nobj") == 0)
531       return varnish_submit_gauge(conf->instance, "sms", "requests",
532                                   "outstanding", val);
533     else if (strcmp(name, "sms_nbytes") == 0)
534       return varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
535                                   val);
536     else if (strcmp(name, "sms_balloc") == 0)
537       return varnish_submit_derive(conf->instance, "sms", "total_bytes",
538                                    "allocated", val);
539     else if (strcmp(name, "sms_bfree") == 0)
540       return varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
541                                    val);
542   }
543
544   if (conf->collect_struct) {
545     if (strcmp(name, "n_sess_mem") == 0)
546       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
547                                   "sess_mem", val);
548     else if (strcmp(name, "n_sess") == 0)
549       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
550                                   "sess", val);
551     else if (strcmp(name, "n_object") == 0)
552       return varnish_submit_gauge(conf->instance, "struct", "objects", "object",
553                                   val);
554     else if (strcmp(name, "n_vampireobject") == 0)
555       return varnish_submit_gauge(conf->instance, "struct", "objects",
556                                   "vampireobject", val);
557     else if (strcmp(name, "n_objectcore") == 0)
558       return varnish_submit_gauge(conf->instance, "struct", "objects",
559                                   "objectcore", val);
560     else if (strcmp(name, "n_waitinglist") == 0)
561       return varnish_submit_gauge(conf->instance, "struct", "objects",
562                                   "waitinglist", val);
563     else if (strcmp(name, "n_objecthead") == 0)
564       return varnish_submit_gauge(conf->instance, "struct", "objects",
565                                   "objecthead", val);
566     else if (strcmp(name, "n_smf") == 0)
567       return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
568                                   val);
569     else if (strcmp(name, "n_smf_frag") == 0)
570       return varnish_submit_gauge(conf->instance, "struct", "objects",
571                                   "smf_frag", val);
572     else if (strcmp(name, "n_smf_large") == 0)
573       return varnish_submit_gauge(conf->instance, "struct", "objects",
574                                   "smf_large", val);
575     else if (strcmp(name, "n_vbe_conn") == 0)
576       return varnish_submit_gauge(conf->instance, "struct", "objects",
577                                   "vbe_conn", val);
578   }
579
580   if (conf->collect_totals) {
581     if (strcmp(name, "s_sess") == 0)
582       return varnish_submit_derive(conf->instance, "totals", "total_sessions",
583                                    "sessions", val);
584     else if (strcmp(name, "s_req") == 0)
585       return varnish_submit_derive(conf->instance, "totals", "total_requests",
586                                    "requests", val);
587     else if (strcmp(name, "s_pipe") == 0)
588       return varnish_submit_derive(conf->instance, "totals", "total_operations",
589                                    "pipe", val);
590     else if (strcmp(name, "s_pass") == 0)
591       return varnish_submit_derive(conf->instance, "totals", "total_operations",
592                                    "pass", val);
593     else if (strcmp(name, "s_fetch") == 0)
594       return varnish_submit_derive(conf->instance, "totals", "total_operations",
595                                    "fetches", val);
596     else if (strcmp(name, "s_synth") == 0)
597       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
598                                    "synth", val);
599     else if (strcmp(name, "s_req_hdrbytes") == 0)
600       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
601                                    "req_header", val);
602     else if (strcmp(name, "s_req_bodybytes") == 0)
603       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
604                                    "req_body", val);
605     else if (strcmp(name, "s_req_protobytes") == 0)
606       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
607                                    "req_proto", val);
608     else if (strcmp(name, "s_resp_hdrbytes") == 0)
609       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
610                                    "resp_header", val);
611     else if (strcmp(name, "s_resp_bodybytes") == 0)
612       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
613                                    "resp_body", val);
614     else if (strcmp(name, "s_resp_protobytes") == 0)
615       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
616                                    "resp_proto", val);
617     else if (strcmp(name, "s_pipe_hdrbytes") == 0)
618       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
619                                    "pipe_header", val);
620     else if (strcmp(name, "s_pipe_in") == 0)
621       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
622                                    "pipe_in", val);
623     else if (strcmp(name, "s_pipe_out") == 0)
624       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
625                                    "pipe_out", val);
626     else if (strcmp(name, "n_purges") == 0)
627       return varnish_submit_derive(conf->instance, "totals", "total_operations",
628                                    "purges", val);
629     else if (strcmp(name, "s_hdrbytes") == 0)
630       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
631                                    "header-bytes", val);
632     else if (strcmp(name, "s_bodybytes") == 0)
633       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
634                                    "body-bytes", val);
635     else if (strcmp(name, "n_gzip") == 0)
636       return varnish_submit_derive(conf->instance, "totals", "total_operations",
637                                    "gzip", val);
638     else if (strcmp(name, "n_gunzip") == 0)
639       return varnish_submit_derive(conf->instance, "totals", "total_operations",
640                                    "gunzip", val);
641   }
642
643   if (conf->collect_uptime) {
644     if (strcmp(name, "uptime") == 0)
645       return varnish_submit_gauge(conf->instance, "uptime", "uptime",
646                                   "client_uptime", val);
647   }
648
649   if (conf->collect_vcl) {
650     if (strcmp(name, "n_vcl") == 0)
651       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
652                                   val);
653     else if (strcmp(name, "n_vcl_avail") == 0)
654       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
655                                   val);
656     else if (strcmp(name, "n_vcl_discard") == 0)
657       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
658                                   val);
659     else if (strcmp(name, "vmods") == 0)
660       return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
661                                   val);
662   }
663
664   if (conf->collect_workers) {
665     if (strcmp(name, "threads") == 0)
666       return varnish_submit_gauge(conf->instance, "workers", "threads",
667                                   "worker", val);
668     else if (strcmp(name, "threads_created") == 0)
669       return varnish_submit_derive(conf->instance, "workers", "total_threads",
670                                    "created", val);
671     else if (strcmp(name, "threads_failed") == 0)
672       return varnish_submit_derive(conf->instance, "workers", "total_threads",
673                                    "failed", val);
674     else if (strcmp(name, "threads_limited") == 0)
675       return varnish_submit_derive(conf->instance, "workers", "total_threads",
676                                    "limited", val);
677     else if (strcmp(name, "threads_destroyed") == 0)
678       return varnish_submit_derive(conf->instance, "workers", "total_threads",
679                                    "dropped", val);
680     else if (strcmp(name, "thread_queue_len") == 0)
681       return varnish_submit_gauge(conf->instance, "workers", "queue_length",
682                                    "threads", val);
683     else if (strcmp(name, "n_wrk") == 0)
684       return varnish_submit_gauge(conf->instance, "workers", "threads",
685                                   "worker", val);
686     else if (strcmp(name, "n_wrk_create") == 0)
687       return varnish_submit_derive(conf->instance, "workers", "total_threads",
688                                    "created", val);
689     else if (strcmp(name, "n_wrk_failed") == 0)
690       return varnish_submit_derive(conf->instance, "workers", "total_threads",
691                                    "failed", val);
692     else if (strcmp(name, "n_wrk_max") == 0)
693       return varnish_submit_derive(conf->instance, "workers", "total_threads",
694                                    "limited", val);
695     else if (strcmp(name, "n_wrk_drop") == 0)
696       return varnish_submit_derive(conf->instance, "workers", "total_threads",
697                                    "dropped", val);
698     else if (strcmp(name, "n_wrk_queue") == 0)
699       return varnish_submit_derive(conf->instance, "workers", "total_requests",
700                                    "queued", val);
701     else if (strcmp(name, "n_wrk_overflow") == 0)
702       return varnish_submit_derive(conf->instance, "workers", "total_requests",
703                                    "overflowed", val);
704     else if (strcmp(name, "n_wrk_queued") == 0)
705       return varnish_submit_derive(conf->instance, "workers", "total_requests",
706                                    "queued", val);
707     else if (strcmp(name, "n_wrk_lqueue") == 0)
708       return varnish_submit_derive(conf->instance, "workers", "total_requests",
709                                    "queue_length", val);
710 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
711     else if (strcmp(name, "pools") == 0)
712       return varnish_submit_gauge(conf->instance, "workers", "pools",
713                                   "pools", val);
714     else if (strcmp(name, "busy_killed") == 0)
715       return varnish_submit_derive(conf->instance, "workers", "http_requests",
716                                    "busy_killed", val);
717 #endif
718   }
719
720 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
721   if (conf->collect_vsm) {
722     if (strcmp(name, "vsm_free") == 0)
723       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
724     else if (strcmp(name, "vsm_used") == 0)
725       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
726     else if (strcmp(name, "vsm_cooling") == 0)
727       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
728                                   val);
729     else if (strcmp(name, "vsm_overflow") == 0)
730       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
731                                   val);
732     else if (strcmp(name, "vsm_overflowed") == 0)
733       return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
734                                    "overflowed", val);
735   }
736
737   if (conf->collect_vbe) {
738     /* @TODO figure out the collectd type for bitmap
739     if (strcmp(name, "happy") == 0)
740       return varnish_submit_derive(conf->instance, "vbe",
741                                    "bitmap", "happy_hprobes", val);
742     */
743     if (strcmp(name, "bereq_hdrbytes") == 0)
744       return varnish_submit_derive(conf->instance, "vbe",
745                                    "total_bytes", "bereq_hdrbytes", val);
746     else if (strcmp(name, "bereq_bodybytes") == 0)
747       return varnish_submit_derive(conf->instance, "vbe",
748                                    "total_bytes", "bereq_bodybytes", val);
749     else if (strcmp(name, "bereq_protobytes") == 0)
750       return varnish_submit_derive(conf->instance, "vbe",
751                                    "total_bytes", "bereq_protobytes", val);
752     else if (strcmp(name, "beresp_hdrbytes") == 0)
753       return varnish_submit_derive(conf->instance, "vbe",
754                                    "total_bytes", "beresp_hdrbytes", val);
755     else if (strcmp(name, "beresp_bodybytes") == 0)
756       return varnish_submit_derive(conf->instance, "vbe",
757                                    "total_bytes", "beresp_bodybytes", val);
758     else if (strcmp(name, "beresp_protobytes") == 0)
759       return varnish_submit_derive(conf->instance, "vbe",
760                                    "total_bytes", "beresp_protobytes", val);
761     else if (strcmp(name, "pipe_hdrbytes") == 0)
762       return varnish_submit_derive(conf->instance, "vbe",
763                                    "total_bytes", "pipe_hdrbytes", val);
764     else if (strcmp(name, "pipe_out") == 0)
765       return varnish_submit_derive(conf->instance, "vbe",
766                                    "total_bytes", "pipe_out", val);
767     else if (strcmp(name, "pipe_in") == 0)
768       return varnish_submit_derive(conf->instance, "vbe",
769                                    "total_bytes", "pipe_in", val);
770     else if (strcmp(name, "conn") == 0)
771       return varnish_submit_derive(conf->instance, "vbe", "connections",
772                                   "c_conns", val);
773     else if (strcmp(name, "req") == 0)
774       return varnish_submit_derive(conf->instance, "vbe", "http_requests",
775                                   "b_reqs", val);
776   }
777
778   /* All Stevedores support these counters */
779   if (conf->collect_sma || conf->collect_smf || conf->collect_mse) {
780
781     char category[4];
782     if (conf->collect_sma)
783       strncpy(category, "sma", 4);
784     else if (conf->collect_smf)
785       strncpy(category, "smf", 4);
786     else
787       strncpy(category, "mse", 4);
788
789     if (strcmp(name, "c_req") == 0)
790       return varnish_submit_derive(conf->instance, category,
791                                    "total_operations", "alloc_req", val);
792     else if (strcmp(name, "c_fail") == 0)
793       return varnish_submit_derive(conf->instance, category,
794                                    "total_operations", "alloc_fail", val);
795     else if (strcmp(name, "c_bytes") == 0)
796       return varnish_submit_derive(conf->instance, category,
797                                    "total_bytes", "bytes_allocated", val);
798     else if (strcmp(name, "c_freed") == 0)
799       return varnish_submit_derive(conf->instance, category,
800                                    "total_bytes", "bytes_freed", val);
801     else if (strcmp(name, "g_alloc") == 0)
802       return varnish_submit_derive(conf->instance, category,
803                                   "total_operations", "alloc_outstanding", val);
804     else if (strcmp(name, "g_bytes") == 0)
805       return varnish_submit_gauge(conf->instance, category, "bytes",
806                                   "bytes_outstanding", val);
807     else if (strcmp(name, "g_space") == 0)
808       return varnish_submit_gauge(conf->instance, category, "bytes",
809                                   "bytes_available", val);
810   }
811
812   /* No SMA specific counters */
813
814   if (conf->collect_smf) {
815     if (strcmp(name, "g_smf") == 0)
816         return varnish_submit_gauge(conf->instance, "smf", "objects",
817                                     "n_struct_smf", val);
818       else if (strcmp(name, "g_smf_frag") == 0)
819         return varnish_submit_gauge(conf->instance, "smf", "objects",
820                                     "n_small_free_smf", val);
821       else if (strcmp(name, "g_smf_large") == 0)
822         return varnish_submit_gauge(conf->instance, "smf", "objects",
823                                     "n_large_free_smf", val);
824   }
825
826   if (conf->collect_mgt) {
827     if (strcmp(name, "uptime") == 0)
828       return varnish_submit_gauge(conf->instance, "mgt", "uptime",
829                                   "mgt_proc_uptime", val);
830     else if (strcmp(name, "child_start") == 0)
831       return varnish_submit_derive(conf->instance, "mgt",
832                                    "total_operations", "child_start", val);
833     else if (strcmp(name, "child_exit") == 0)
834       return varnish_submit_derive(conf->instance, "mgt",
835                                    "total_operations", "child_exit", val);
836     else if (strcmp(name, "child_stop") == 0)
837       return varnish_submit_derive(conf->instance, "mgt",
838                                    "total_operations", "child_stop", val);
839     else if (strcmp(name, "child_died") == 0)
840       return varnish_submit_derive(conf->instance, "mgt",
841                                    "total_operations", "child_died", val);
842     else if (strcmp(name, "child_dump") == 0)
843       return varnish_submit_derive(conf->instance, "mgt",
844                                    "total_operations", "child_dump", val);
845     else if (strcmp(name, "child_panic") == 0)
846       return varnish_submit_derive(conf->instance, "mgt",
847                                    "total_operations", "child_panic", val);
848   }
849
850   if (conf->collect_lck) {
851     if (strcmp(name, "creat") == 0)
852       return varnish_submit_gauge(conf->instance, "lck", "objects",
853                                   "created", val);
854     else if (strcmp(name, "destroy") == 0)
855       return varnish_submit_gauge(conf->instance, "lck", "objects",
856                                   "destroyed", val);
857     else if (strcmp(name, "locks") == 0)
858       return varnish_submit_derive(conf->instance, "lck", "total_operations",
859                                   "lock_ops", val);
860   }
861
862   if (conf->collect_mempool) {
863     if (strcmp(name, "live") == 0)
864       return varnish_submit_gauge(conf->instance, "mempool", "objects",
865                                   "in_use", val);
866     else if (strcmp(name, "pool") == 0)
867       return varnish_submit_gauge(conf->instance, "mempool", "objects",
868                                   "in_pool", val);
869     else if (strcmp(name, "sz_wanted") == 0)
870       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
871                                   "size_requested", val);
872     else if (strcmp(name, "sz_actual") == 0)
873       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
874                                   "size_allocated", val);
875     else if (strcmp(name, "allocs") == 0)
876       return varnish_submit_derive(conf->instance, "mempool",
877                                    "total_operations", "allocations", val);
878     else if (strcmp(name, "frees") == 0)
879       return varnish_submit_derive(conf->instance, "mempool",
880                                    "total_operations", "frees", val);
881     else if (strcmp(name, "recycle") == 0)
882       return varnish_submit_gauge(conf->instance, "mempool",
883                                    "objects", "recycled", val);
884     else if (strcmp(name, "timeout") == 0)
885       return varnish_submit_gauge(conf->instance, "mempool",
886                                    "objects", "timed_out", val);
887     else if (strcmp(name, "toosmall") == 0)
888       return varnish_submit_gauge(conf->instance, "mempool",
889                                    "objects", "too_small", val);
890     else if (strcmp(name, "surplus") == 0)
891       return varnish_submit_gauge(conf->instance, "mempool",
892                                    "objects", "surplus", val);
893     else if (strcmp(name, "randry") == 0)
894       return varnish_submit_gauge(conf->instance, "mempool",
895                                    "objects", "ran_dry", val);
896   }
897
898   if (conf->collect_mse) {
899     if (strcmp(name, "c_full") == 0)
900       return varnish_submit_derive(conf->instance, "mse",
901                                   "total_operations", "full_allocs", val);
902     else if (strcmp(name, "c_truncated") == 0)
903       return varnish_submit_derive(conf->instance, "mse",
904                                   "total_operations", "truncated_allocs", val);
905     else if (strcmp(name, "c_expanded") == 0)
906       return varnish_submit_derive(conf->instance, "mse",
907                                   "total_operations", "expanded_allocs", val);
908     else if (strcmp(name, "c_failed") == 0)
909       return varnish_submit_derive(conf->instance, "mse",
910                                   "total_operations", "failed_allocs", val);
911     else if (strcmp(name, "c_bytes") == 0)
912       return varnish_submit_derive(conf->instance, "mse",
913                                   "total_bytes", "bytes_allocated", val);
914     else if (strcmp(name, "c_freed") == 0)
915       return varnish_submit_derive(conf->instance, "mse",
916                                   "total_bytes", "bytes_freed", val);
917     else if (strcmp(name, "g_fo_alloc") == 0)
918       return varnish_submit_derive(conf->instance, "mse",
919                                   "total_operations", "fo_allocs_outstanding", val);
920     else if (strcmp(name, "g_fo_bytes") == 0)
921       return varnish_submit_gauge(conf->instance, "mse",
922                                   "bytes", "fo_bytes_outstanding", val);
923     else if (strcmp(name, "g_membuf_alloc") == 0)
924       return varnish_submit_gauge(conf->instance, "mse",
925                                   "objects", "membufs_allocated", val);
926     else if (strcmp(name, "g_membuf_inuse") == 0)
927       return varnish_submit_gauge(conf->instance, "mse",
928                                   "objects", "membufs_inuse", val);
929     else if (strcmp(name, "g_bans_bytes") == 0)
930       return varnish_submit_gauge(conf->instance, "mse",
931                                   "bytes", "persisted_banspace_used", val);
932     else if (strcmp(name, "g_bans_space") == 0)
933       return varnish_submit_gauge(conf->instance, "mse",
934                                   "bytes", "persisted_banspace_available", val);
935     else if (strcmp(name, "g_bans_persisted") == 0)
936       return varnish_submit_derive(conf->instance, "mse",
937                                   "total_operations", "bans_persisted", val);
938     else if (strcmp(name, "g_bans_lost") == 0)
939       return varnish_submit_derive(conf->instance, "mse",
940                                   "total_operations", "bans_lost", val);
941
942      /* mse seg */
943     else if (strcmp(name, "g_journal_bytes") == 0)
944       return varnish_submit_gauge(conf->instance, "mse_reg",
945                                   "bytes", "journal_bytes_used", val);
946     else if (strcmp(name, "g_journal_space") == 0)
947       return varnish_submit_gauge(conf->instance, "mse_reg",
948                                   "bytes", "journal_bytes_free", val);
949
950     /* mse segagg */
951     else if (strcmp(name, "g_bigspace") == 0)
952       return varnish_submit_gauge(conf->instance, "mse_segagg",
953                                   "bytes", "big_extents_bytes_available", val);
954     else if (strcmp(name, "g_extfree") == 0)
955       return varnish_submit_gauge(conf->instance, "mse_segagg",
956                                   "objects", "free_extents", val);
957     else if (strcmp(name, "g_sparenode") == 0)
958       return varnish_submit_gauge(conf->instance, "mse_segagg",
959                                   "objects", "spare_nodes_available", val);
960     else if (strcmp(name, "g_objnode") == 0)
961       return varnish_submit_gauge(conf->instance, "mse_segagg",
962                                   "objects", "object_nodes_in_use", val);
963     else if (strcmp(name, "g_extnode") == 0)
964       return varnish_submit_gauge(conf->instance, "mse_segagg",
965                                   "objects", "extent_nodes_in_use", val);
966     else if (strcmp(name, "g_bigextfree") == 0)
967       return varnish_submit_gauge(conf->instance, "mse_segagg",
968                                   "objects", "free_big_extents", val);
969     else if (strcmp(name, "c_pruneloop") == 0)
970       return varnish_submit_derive(conf->instance, "mse_segagg",
971                                   "total_operations", "prune_loops", val);
972     else if (strcmp(name, "c_pruned") == 0)
973       return varnish_submit_derive(conf->instance, "mse_segagg",
974                                   "total_objects", "pruned_objects", val);
975     else if (strcmp(name, "c_spared") == 0)
976       return varnish_submit_derive(conf->instance, "mse_segagg",
977                                   "total_operations", "spared_objects", val);
978     else if (strcmp(name, "c_skipped") == 0)
979       return varnish_submit_derive(conf->instance, "mse_segagg",
980                                   "total_operations", "missed_objects", val);
981     else if (strcmp(name, "c_nuked") == 0)
982       return varnish_submit_derive(conf->instance, "mse_segagg",
983                                   "total_operations", "nuked_objects", val);
984     else if (strcmp(name, "c_sniped") == 0)
985       return varnish_submit_derive(conf->instance, "mse_segagg",
986                                   "total_operations", "sniped_objects", val);
987
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_Destroy(&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_Destroy(&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 } /* }}} */