running contrib/format.sh on src/varnish.c
[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 #endif
465
466   if (conf->collect_session) {
467     if (strcmp(name, "sess_closed") == 0)
468       return varnish_submit_derive(conf->instance, "session",
469                                    "total_operations", "closed", val);
470     else if (strcmp(name, "sess_pipeline") == 0)
471       return varnish_submit_derive(conf->instance, "session",
472                                    "total_operations", "pipeline", val);
473     else if (strcmp(name, "sess_readahead") == 0)
474       return varnish_submit_derive(conf->instance, "session",
475                                    "total_operations", "readahead", val);
476     else if (strcmp(name, "sess_conn") == 0)
477       return varnish_submit_derive(conf->instance, "session",
478                                    "total_operations", "accepted", val);
479     else if (strcmp(name, "sess_drop") == 0)
480       return varnish_submit_derive(conf->instance, "session",
481                                    "total_operations", "dropped", val);
482     else if (strcmp(name, "sess_fail") == 0)
483       return varnish_submit_derive(conf->instance, "session",
484                                    "total_operations", "failed", val);
485     else if (strcmp(name, "sess_pipe_overflow") == 0)
486       return varnish_submit_derive(conf->instance, "session",
487                                    "total_operations", "overflow", val);
488     else if (strcmp(name, "sess_queued") == 0)
489       return varnish_submit_derive(conf->instance, "session",
490                                    "total_operations", "queued", val);
491     else if (strcmp(name, "sess_linger") == 0)
492       return varnish_submit_derive(conf->instance, "session",
493                                    "total_operations", "linger", val);
494     else if (strcmp(name, "sess_herd") == 0)
495       return varnish_submit_derive(conf->instance, "session",
496                                    "total_operations", "herd", val);
497 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
498     else if (strcmp(name, "sess_closed_err") == 0)
499       return varnish_submit_derive(conf->instance, "session",
500                                    "total_operations", "closed_err", val);
501     else if (strcmp(name, "sess_dropped") == 0)
502       return varnish_submit_derive(conf->instance, "session",
503                                    "total_operations", "dropped_for_thread",
504                                    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", "pools",
713                                   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", "total_bytes",
745                                    "bereq_hdrbytes", val);
746     else if (strcmp(name, "bereq_bodybytes") == 0)
747       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
748                                    "bereq_bodybytes", val);
749     else if (strcmp(name, "bereq_protobytes") == 0)
750       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
751                                    "bereq_protobytes", val);
752     else if (strcmp(name, "beresp_hdrbytes") == 0)
753       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
754                                    "beresp_hdrbytes", val);
755     else if (strcmp(name, "beresp_bodybytes") == 0)
756       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
757                                    "beresp_bodybytes", val);
758     else if (strcmp(name, "beresp_protobytes") == 0)
759       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
760                                    "beresp_protobytes", val);
761     else if (strcmp(name, "pipe_hdrbytes") == 0)
762       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
763                                    "pipe_hdrbytes", val);
764     else if (strcmp(name, "pipe_out") == 0)
765       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
766                                    "pipe_out", val);
767     else if (strcmp(name, "pipe_in") == 0)
768       return varnish_submit_derive(conf->instance, "vbe", "total_bytes",
769                                    "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, "total_operations",
791                                    "alloc_req", val);
792     else if (strcmp(name, "c_fail") == 0)
793       return varnish_submit_derive(conf->instance, category, "total_operations",
794                                    "alloc_fail", val);
795     else if (strcmp(name, "c_bytes") == 0)
796       return varnish_submit_derive(conf->instance, category, "total_bytes",
797                                    "bytes_allocated", val);
798     else if (strcmp(name, "c_freed") == 0)
799       return varnish_submit_derive(conf->instance, category, "total_bytes",
800                                    "bytes_freed", val);
801     else if (strcmp(name, "g_alloc") == 0)
802       return varnish_submit_derive(conf->instance, category, "total_operations",
803                                    "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", "total_operations",
832                                    "child_start", val);
833     else if (strcmp(name, "child_exit") == 0)
834       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
835                                    "child_exit", val);
836     else if (strcmp(name, "child_stop") == 0)
837       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
838                                    "child_stop", val);
839     else if (strcmp(name, "child_died") == 0)
840       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
841                                    "child_died", val);
842     else if (strcmp(name, "child_dump") == 0)
843       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
844                                    "child_dump", val);
845     else if (strcmp(name, "child_panic") == 0)
846       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
847                                    "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", "created",
853                                   val);
854     else if (strcmp(name, "destroy") == 0)
855       return varnish_submit_gauge(conf->instance, "lck", "objects", "destroyed",
856                                   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", "objects",
883                                   "recycled", val);
884     else if (strcmp(name, "timeout") == 0)
885       return varnish_submit_gauge(conf->instance, "mempool", "objects",
886                                   "timed_out", val);
887     else if (strcmp(name, "toosmall") == 0)
888       return varnish_submit_gauge(conf->instance, "mempool", "objects",
889                                   "too_small", val);
890     else if (strcmp(name, "surplus") == 0)
891       return varnish_submit_gauge(conf->instance, "mempool", "objects",
892                                   "surplus", val);
893     else if (strcmp(name, "randry") == 0)
894       return varnish_submit_gauge(conf->instance, "mempool", "objects",
895                                   "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", "total_operations",
901                                    "full_allocs", val);
902     else if (strcmp(name, "c_truncated") == 0)
903       return varnish_submit_derive(conf->instance, "mse", "total_operations",
904                                    "truncated_allocs", val);
905     else if (strcmp(name, "c_expanded") == 0)
906       return varnish_submit_derive(conf->instance, "mse", "total_operations",
907                                    "expanded_allocs", val);
908     else if (strcmp(name, "c_failed") == 0)
909       return varnish_submit_derive(conf->instance, "mse", "total_operations",
910                                    "failed_allocs", val);
911     else if (strcmp(name, "c_bytes") == 0)
912       return varnish_submit_derive(conf->instance, "mse", "total_bytes",
913                                    "bytes_allocated", val);
914     else if (strcmp(name, "c_freed") == 0)
915       return varnish_submit_derive(conf->instance, "mse", "total_bytes",
916                                    "bytes_freed", val);
917     else if (strcmp(name, "g_fo_alloc") == 0)
918       return varnish_submit_derive(conf->instance, "mse", "total_operations",
919                                    "fo_allocs_outstanding", val);
920     else if (strcmp(name, "g_fo_bytes") == 0)
921       return varnish_submit_gauge(conf->instance, "mse", "bytes",
922                                   "fo_bytes_outstanding", val);
923     else if (strcmp(name, "g_membuf_alloc") == 0)
924       return varnish_submit_gauge(conf->instance, "mse", "objects",
925                                   "membufs_allocated", val);
926     else if (strcmp(name, "g_membuf_inuse") == 0)
927       return varnish_submit_gauge(conf->instance, "mse", "objects",
928                                   "membufs_inuse", val);
929     else if (strcmp(name, "g_bans_bytes") == 0)
930       return varnish_submit_gauge(conf->instance, "mse", "bytes",
931                                   "persisted_banspace_used", val);
932     else if (strcmp(name, "g_bans_space") == 0)
933       return varnish_submit_gauge(conf->instance, "mse", "bytes",
934                                   "persisted_banspace_available", val);
935     else if (strcmp(name, "g_bans_persisted") == 0)
936       return varnish_submit_derive(conf->instance, "mse", "total_operations",
937                                    "bans_persisted", val);
938     else if (strcmp(name, "g_bans_lost") == 0)
939       return varnish_submit_derive(conf->instance, "mse", "total_operations",
940                                    "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", "bytes",
945                                   "journal_bytes_used", val);
946     else if (strcmp(name, "g_journal_space") == 0)
947       return varnish_submit_gauge(conf->instance, "mse_reg", "bytes",
948                                   "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", "bytes",
953                                   "big_extents_bytes_available", val);
954     else if (strcmp(name, "g_extfree") == 0)
955       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
956                                   "free_extents", val);
957     else if (strcmp(name, "g_sparenode") == 0)
958       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
959                                   "spare_nodes_available", val);
960     else if (strcmp(name, "g_objnode") == 0)
961       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
962                                   "object_nodes_in_use", val);
963     else if (strcmp(name, "g_extnode") == 0)
964       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
965                                   "extent_nodes_in_use", val);
966     else if (strcmp(name, "g_bigextfree") == 0)
967       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
968                                   "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 #endif
990
991   return 0;
992
993 } /* }}} static int varnish_monitor */
994 #else /* if HAVE_VARNISH_V2 */
995 static void varnish_monitor(const user_config_t *conf, /* {{{ */
996                             const c_varnish_stats_t *stats) {
997   if (conf->collect_cache) {
998     /* Cache hits */
999     varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
1000                           stats->cache_hit);
1001     /* Cache misses */
1002     varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
1003                           stats->cache_miss);
1004     /* Cache hits for pass */
1005     varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
1006                           stats->cache_hitpass);
1007   }
1008
1009   if (conf->collect_connections) {
1010     /* Client connections accepted */
1011     varnish_submit_derive(conf->instance, "connections", "connections",
1012                           "accepted", stats->client_conn);
1013     /* Connection dropped, no sess */
1014     varnish_submit_derive(conf->instance, "connections", "connections",
1015                           "dropped", stats->client_drop);
1016     /* Client requests received    */
1017     varnish_submit_derive(conf->instance, "connections", "connections",
1018                           "received", stats->client_req);
1019   }
1020
1021   if (conf->collect_esi) {
1022     /* ESI parse errors (unlock)   */
1023     varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
1024                           stats->esi_errors);
1025     /* Objects ESI parsed (unlock) */
1026     varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
1027                           stats->esi_parse);
1028   }
1029
1030   if (conf->collect_backend) {
1031     /* Backend conn. success       */
1032     varnish_submit_derive(conf->instance, "backend", "connections", "success",
1033                           stats->backend_conn);
1034     /* Backend conn. not attempted */
1035     varnish_submit_derive(conf->instance, "backend", "connections",
1036                           "not-attempted", stats->backend_unhealthy);
1037     /* Backend conn. too many      */
1038     varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
1039                           stats->backend_busy);
1040     /* Backend conn. failures      */
1041     varnish_submit_derive(conf->instance, "backend", "connections", "failures",
1042                           stats->backend_fail);
1043     /* Backend conn. reuses        */
1044     varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
1045                           stats->backend_reuse);
1046     /* Backend conn. was closed    */
1047     varnish_submit_derive(conf->instance, "backend", "connections",
1048                           "was-closed", stats->backend_toolate);
1049     /* Backend conn. recycles      */
1050     varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
1051                           stats->backend_recycle);
1052     /* Backend conn. unused        */
1053     varnish_submit_derive(conf->instance, "backend", "connections", "unused",
1054                           stats->backend_unused);
1055     /* Backend requests mades      */
1056     varnish_submit_derive(conf->instance, "backend", "http_requests",
1057                           "requests", stats->backend_req);
1058     /* N backends                  */
1059     varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
1060                          stats->n_backend);
1061   }
1062
1063   if (conf->collect_fetch) {
1064     /* Fetch head                */
1065     varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
1066                           stats->fetch_head);
1067     /* Fetch with length         */
1068     varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
1069                           stats->fetch_length);
1070     /* Fetch chunked             */
1071     varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
1072                           stats->fetch_chunked);
1073     /* Fetch EOF                 */
1074     varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
1075                           stats->fetch_eof);
1076     /* Fetch bad headers         */
1077     varnish_submit_derive(conf->instance, "fetch", "http_requests",
1078                           "bad_headers", stats->fetch_bad);
1079     /* Fetch wanted close        */
1080     varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
1081                           stats->fetch_close);
1082     /* Fetch pre HTTP/1.1 closed */
1083     varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
1084                           stats->fetch_oldhttp);
1085     /* Fetch zero len            */
1086     varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
1087                           stats->fetch_zero);
1088     /* Fetch failed              */
1089     varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
1090                           stats->fetch_failed);
1091   }
1092
1093   if (conf->collect_hcb) {
1094     /* HCB Lookups without lock */
1095     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
1096                           "lookup_nolock", stats->hcb_nolock);
1097     /* HCB Lookups with lock    */
1098     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
1099                           "lookup_lock", stats->hcb_lock);
1100     /* HCB Inserts              */
1101     varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
1102                           stats->hcb_insert);
1103   }
1104
1105   if (conf->collect_objects) {
1106     /* N expired objects             */
1107     varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
1108                           stats->n_expired);
1109     /* N LRU nuked objects           */
1110     varnish_submit_derive(conf->instance, "objects", "total_objects",
1111                           "lru_nuked", stats->n_lru_nuked);
1112     /* N LRU saved objects           */
1113     varnish_submit_derive(conf->instance, "objects", "total_objects",
1114                           "lru_saved", stats->n_lru_saved);
1115     /* N LRU moved objects           */
1116     varnish_submit_derive(conf->instance, "objects", "total_objects",
1117                           "lru_moved", stats->n_lru_moved);
1118     /* N objects on deathrow         */
1119     varnish_submit_derive(conf->instance, "objects", "total_objects",
1120                           "deathrow", stats->n_deathrow);
1121     /* HTTP header overflows         */
1122     varnish_submit_derive(conf->instance, "objects", "total_objects",
1123                           "header_overflow", stats->losthdr);
1124     /* Objects sent with sendfile    */
1125     varnish_submit_derive(conf->instance, "objects", "total_objects",
1126                           "sent_sendfile", stats->n_objsendfile);
1127     /* Objects sent with write       */
1128     varnish_submit_derive(conf->instance, "objects", "total_objects",
1129                           "sent_write", stats->n_objwrite);
1130     /* Objects overflowing workspace */
1131     varnish_submit_derive(conf->instance, "objects", "total_objects",
1132                           "workspace_overflow", stats->n_objoverflow);
1133   }
1134
1135   if (conf->collect_purge) {
1136     /* N total active purges      */
1137     varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
1138                           stats->n_purge);
1139     /* N new purges added         */
1140     varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
1141                           stats->n_purge_add);
1142     /* N old purges deleted       */
1143     varnish_submit_derive(conf->instance, "purge", "total_operations",
1144                           "deleted", stats->n_purge_retire);
1145     /* N objects tested           */
1146     varnish_submit_derive(conf->instance, "purge", "total_operations",
1147                           "objects_tested", stats->n_purge_obj_test);
1148     /* N regexps tested against   */
1149     varnish_submit_derive(conf->instance, "purge", "total_operations",
1150                           "regexps_tested", stats->n_purge_re_test);
1151     /* N duplicate purges removed */
1152     varnish_submit_derive(conf->instance, "purge", "total_operations",
1153                           "duplicate", stats->n_purge_dups);
1154   }
1155
1156   if (conf->collect_session) {
1157     /* Session Closed     */
1158     varnish_submit_derive(conf->instance, "session", "total_operations",
1159                           "closed", stats->sess_closed);
1160     /* Session Pipeline   */
1161     varnish_submit_derive(conf->instance, "session", "total_operations",
1162                           "pipeline", stats->sess_pipeline);
1163     /* Session Read Ahead */
1164     varnish_submit_derive(conf->instance, "session", "total_operations",
1165                           "readahead", stats->sess_readahead);
1166     /* Session Linger     */
1167     varnish_submit_derive(conf->instance, "session", "total_operations",
1168                           "linger", stats->sess_linger);
1169     /* Session herd       */
1170     varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
1171                           stats->sess_herd);
1172   }
1173
1174   if (conf->collect_shm) {
1175     /* SHM records                 */
1176     varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
1177                           stats->shm_records);
1178     /* SHM writes                  */
1179     varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
1180                           stats->shm_writes);
1181     /* SHM flushes due to overflow */
1182     varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
1183                           stats->shm_flushes);
1184     /* SHM MTX contention          */
1185     varnish_submit_derive(conf->instance, "shm", "total_operations",
1186                           "contention", stats->shm_cont);
1187     /* SHM cycles through buffer   */
1188     varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
1189                           stats->shm_cycles);
1190   }
1191
1192   if (conf->collect_sm) {
1193     /* allocator requests */
1194     varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
1195                           stats->sm_nreq);
1196     /* outstanding allocations */
1197     varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
1198                          stats->sm_nobj);
1199     /* bytes allocated */
1200     varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
1201                           stats->sm_balloc);
1202     /* bytes free */
1203     varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
1204                           stats->sm_bfree);
1205   }
1206
1207   if (conf->collect_sma) {
1208     /* SMA allocator requests */
1209     varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
1210                           stats->sma_nreq);
1211     /* SMA outstanding allocations */
1212     varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
1213                          stats->sma_nobj);
1214     /* SMA outstanding bytes */
1215     varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
1216                          stats->sma_nbytes);
1217     /* SMA bytes allocated */
1218     varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
1219                           stats->sma_balloc);
1220     /* SMA bytes free */
1221     varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
1222                           stats->sma_bfree);
1223   }
1224
1225   if (conf->collect_sms) {
1226     /* SMS allocator requests */
1227     varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
1228                           stats->sms_nreq);
1229     /* SMS outstanding allocations */
1230     varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
1231                          stats->sms_nobj);
1232     /* SMS outstanding bytes */
1233     varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
1234                          stats->sms_nbytes);
1235     /* SMS bytes allocated */
1236     varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
1237                           stats->sms_balloc);
1238     /* SMS bytes freed */
1239     varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
1240                           stats->sms_bfree);
1241   }
1242
1243   if (conf->collect_struct) {
1244     /* N struct sess_mem       */
1245     varnish_submit_gauge(conf->instance, "struct", "current_sessions",
1246                          "sess_mem", stats->n_sess_mem);
1247     /* N struct sess           */
1248     varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
1249                          stats->n_sess);
1250     /* N struct object         */
1251     varnish_submit_gauge(conf->instance, "struct", "objects", "object",
1252                          stats->n_object);
1253     /* N struct objecthead     */
1254     varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
1255                          stats->n_objecthead);
1256     /* N struct smf            */
1257     varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
1258                          stats->n_smf);
1259     /* N small free smf         */
1260     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
1261                          stats->n_smf_frag);
1262     /* N large free smf         */
1263     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
1264                          stats->n_smf_large);
1265     /* N struct vbe_conn        */
1266     varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
1267                          stats->n_vbe_conn);
1268   }
1269
1270   if (conf->collect_totals) {
1271     /* Total Sessions */
1272     varnish_submit_derive(conf->instance, "totals", "total_sessions",
1273                           "sessions", stats->s_sess);
1274     /* Total Requests */
1275     varnish_submit_derive(conf->instance, "totals", "total_requests",
1276                           "requests", stats->s_req);
1277     /* Total pipe */
1278     varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
1279                           stats->s_pipe);
1280     /* Total pass */
1281     varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
1282                           stats->s_pass);
1283     /* Total fetch */
1284     varnish_submit_derive(conf->instance, "totals", "total_operations",
1285                           "fetches", stats->s_fetch);
1286     /* Total header bytes */
1287     varnish_submit_derive(conf->instance, "totals", "total_bytes",
1288                           "header-bytes", stats->s_hdrbytes);
1289     /* Total body byte */
1290     varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
1291                           stats->s_bodybytes);
1292   }
1293
1294   if (conf->collect_vcl) {
1295     /* N vcl total     */
1296     varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
1297                          stats->n_vcl);
1298     /* N vcl available */
1299     varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
1300                          stats->n_vcl_avail);
1301     /* N vcl discarded */
1302     varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
1303                          stats->n_vcl_discard);
1304   }
1305
1306   if (conf->collect_workers) {
1307     /* worker threads */
1308     varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
1309                          stats->n_wrk);
1310     /* worker threads created */
1311     varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
1312                           stats->n_wrk_create);
1313     /* worker threads not created */
1314     varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
1315                           stats->n_wrk_failed);
1316     /* worker threads limited */
1317     varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
1318                           stats->n_wrk_max);
1319     /* dropped work requests */
1320     varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
1321                           stats->n_wrk_drop);
1322     /* queued work requests */
1323     varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
1324                           stats->n_wrk_queue);
1325     /* overflowed work requests */
1326     varnish_submit_derive(conf->instance, "workers", "total_requests",
1327                           "overflowed", stats->n_wrk_overflow);
1328   }
1329
1330 } /* }}} void varnish_monitor */
1331 #endif
1332
1333 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1334 static int varnish_read(user_data_t *ud) /* {{{ */
1335 {
1336 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1337   struct VSM_data *vd;
1338   _Bool ok;
1339   const c_varnish_stats_t *stats;
1340 #elif HAVE_VARNISH_V5
1341   struct vsm *vd;
1342   struct vsc *vsc;
1343   int vsm_status;
1344 #endif
1345
1346   user_config_t *conf;
1347
1348   if ((ud == NULL) || (ud->data == NULL))
1349     return EINVAL;
1350
1351   conf = ud->data;
1352
1353   vd = VSM_New();
1354
1355 #if HAVE_VARNISH_V5
1356   vsc = VSC_New();
1357 #endif
1358
1359 #if HAVE_VARNISH_V3
1360   VSC_Setup(vd);
1361 #endif
1362
1363   if (conf->instance != NULL) {
1364     int status;
1365
1366 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1367     status = VSM_n_Arg(vd, conf->instance);
1368 #elif HAVE_VARNISH_V5
1369     status = VSM_Arg(vd, 'n', conf->instance);
1370 #endif
1371
1372     if (status < 0) {
1373 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1374       VSM_Delete(vd);
1375 #elif HAVE_VARNISH_V5
1376       VSC_Destroy(&vsc, vd);
1377       VSM_Destroy(&vd);
1378 #endif
1379       ERROR("varnish plugin: VSM_Arg (\"%s\") failed "
1380             "with status %i.",
1381             conf->instance, status);
1382       return -1;
1383     }
1384   }
1385
1386 #if HAVE_VARNISH_V3
1387   ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1388 #elif HAVE_VARNISH_V4
1389   ok = (VSM_Open(vd) == 0);
1390 #endif
1391 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1392   if (!ok) {
1393     VSM_Destroy(&vd);
1394     ERROR("varnish plugin: Unable to open connection.");
1395     return -1;
1396   }
1397 #endif
1398
1399 #if HAVE_VARNISH_V3
1400   stats = VSC_Main(vd);
1401 #elif HAVE_VARNISH_V4
1402   stats = VSC_Main(vd, NULL);
1403 #endif
1404 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1405   if (!stats) {
1406     VSM_Destroy(&vd);
1407     ERROR("varnish plugin: Unable to get statistics.");
1408     return -1;
1409   }
1410 #endif
1411
1412 #if HAVE_VARNISH_V5
1413   if (VSM_Attach(vd, STDERR_FILENO)) {
1414     ERROR("varnish plugin: Cannot attach to varnish. %s", VSM_Error(vd));
1415     VSC_Destroy(&vsc, vd);
1416     VSM_Destroy(&vd);
1417     return -1;
1418   }
1419
1420   vsm_status = VSM_Status(vd);
1421   if (vsm_status & ~(VSM_MGT_RUNNING | VSM_WRK_RUNNING)) {
1422     ERROR("varnish plugin: Unable to get statistics.");
1423     VSC_Destroy(&vsc, vd);
1424     VSM_Destroy(&vd);
1425     return -1;
1426   }
1427 #endif
1428
1429 #if HAVE_VARNISH_V3
1430   VSC_Iter(vd, varnish_monitor, conf);
1431 #elif HAVE_VARNISH_V4
1432   VSC_Iter(vd, NULL, varnish_monitor, conf);
1433 #elif HAVE_VARNISH_V5
1434   VSC_Iter(vsc, vd, varnish_monitor, conf);
1435 #endif
1436
1437 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1438   VSM_Delete(vd);
1439 #elif HAVE_VARNISH_V5
1440   VSC_Destroy(&vsc, vd);
1441   VSM_Destroy(&vd);
1442 #endif
1443
1444   return 0;
1445 } /* }}} */
1446 #else /* if HAVE_VARNISH_V2 */
1447 static int varnish_read(user_data_t *ud) /* {{{ */
1448 {
1449   const c_varnish_stats_t *stats;
1450
1451   user_config_t *conf;
1452
1453   if ((ud == NULL) || (ud->data == NULL))
1454     return EINVAL;
1455
1456   conf = ud->data;
1457
1458   stats = VSL_OpenStats(conf->instance);
1459   if (stats == NULL) {
1460     ERROR("Varnish plugin : unable to load statistics");
1461
1462     return -1;
1463   }
1464
1465   varnish_monitor(conf, stats);
1466
1467   return 0;
1468 } /* }}} */
1469 #endif
1470
1471 static void varnish_config_free(void *ptr) /* {{{ */
1472 {
1473   user_config_t *conf = ptr;
1474
1475   if (conf == NULL)
1476     return;
1477
1478   sfree(conf->instance);
1479   sfree(conf);
1480 } /* }}} */
1481
1482 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1483 {
1484   if (conf == NULL)
1485     return EINVAL;
1486
1487   conf->collect_backend = 1;
1488   conf->collect_cache = 1;
1489   conf->collect_connections = 1;
1490 #ifdef HAVE_VARNISH_V3
1491   conf->collect_dirdns = 0;
1492 #endif
1493   conf->collect_esi = 0;
1494   conf->collect_fetch = 0;
1495   conf->collect_hcb = 0;
1496   conf->collect_objects = 0;
1497 #if HAVE_VARNISH_V2
1498   conf->collect_purge = 0;
1499 #else
1500   conf->collect_ban = 0;
1501 #endif
1502   conf->collect_session = 0;
1503   conf->collect_shm = 1;
1504 #if HAVE_VARNISH_V2
1505   conf->collect_sm = 0;
1506 #endif
1507 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1508   conf->collect_sma = 0;
1509 #endif
1510   conf->collect_sms = 0;
1511   conf->collect_struct = 0;
1512   conf->collect_totals = 0;
1513 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1514   conf->collect_uptime = 0;
1515 #endif
1516   conf->collect_vcl = 0;
1517   conf->collect_workers = 0;
1518 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1519   conf->collect_vsm = 0;
1520   conf->collect_lck = 0;
1521   conf->collect_mempool = 0;
1522   conf->collect_mgt = 0;
1523   conf->collect_smf = 0;
1524   conf->collect_vbe = 0;
1525   conf->collect_mse = 0;
1526 #endif
1527
1528   return 0;
1529 } /* }}} int varnish_config_apply_default */
1530
1531 static int varnish_init(void) /* {{{ */
1532 {
1533   user_config_t *conf;
1534
1535   if (have_instance)
1536     return 0;
1537
1538   conf = calloc(1, sizeof(*conf));
1539   if (conf == NULL)
1540     return ENOMEM;
1541
1542   /* Default settings: */
1543   conf->instance = NULL;
1544
1545   varnish_config_apply_default(conf);
1546
1547   plugin_register_complex_read(
1548       /* group = */ "varnish",
1549       /* name      = */ "varnish/localhost",
1550       /* callback  = */ varnish_read,
1551       /* interval  = */ 0,
1552       &(user_data_t){
1553           .data = conf, .free_func = varnish_config_free,
1554       });
1555
1556   return 0;
1557 } /* }}} int varnish_init */
1558
1559 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1560 {
1561   user_config_t *conf;
1562   char callback_name[DATA_MAX_NAME_LEN];
1563
1564   conf = calloc(1, sizeof(*conf));
1565   if (conf == NULL)
1566     return ENOMEM;
1567   conf->instance = NULL;
1568
1569   varnish_config_apply_default(conf);
1570
1571   if (ci->values_num == 1) {
1572     int status;
1573
1574     status = cf_util_get_string(ci, &conf->instance);
1575     if (status != 0) {
1576       sfree(conf);
1577       return status;
1578     }
1579     assert(conf->instance != NULL);
1580
1581     if (strcmp("localhost", conf->instance) == 0) {
1582       sfree(conf->instance);
1583       conf->instance = NULL;
1584     }
1585   } else if (ci->values_num > 1) {
1586     WARNING("Varnish plugin: \"Instance\" blocks accept only "
1587             "one argument.");
1588     sfree(conf);
1589     return EINVAL;
1590   }
1591
1592   for (int i = 0; i < ci->children_num; i++) {
1593     oconfig_item_t *child = ci->children + i;
1594
1595     if (strcasecmp("CollectCache", child->key) == 0)
1596       cf_util_get_boolean(child, &conf->collect_cache);
1597     else if (strcasecmp("CollectConnections", child->key) == 0)
1598       cf_util_get_boolean(child, &conf->collect_connections);
1599     else if (strcasecmp("CollectESI", child->key) == 0)
1600       cf_util_get_boolean(child, &conf->collect_esi);
1601     else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1602 #ifdef HAVE_VARNISH_V3
1603       cf_util_get_boolean(child, &conf->collect_dirdns);
1604 #else
1605       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1606               child->key, "v3");
1607 #endif
1608     else if (strcasecmp("CollectBackend", child->key) == 0)
1609       cf_util_get_boolean(child, &conf->collect_backend);
1610     else if (strcasecmp("CollectFetch", child->key) == 0)
1611       cf_util_get_boolean(child, &conf->collect_fetch);
1612     else if (strcasecmp("CollectHCB", child->key) == 0)
1613       cf_util_get_boolean(child, &conf->collect_hcb);
1614     else if (strcasecmp("CollectObjects", child->key) == 0)
1615       cf_util_get_boolean(child, &conf->collect_objects);
1616     else if (strcasecmp("CollectPurge", child->key) == 0)
1617 #if HAVE_VARNISH_V2
1618       cf_util_get_boolean(child, &conf->collect_purge);
1619 #else
1620       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1621               child->key, "v2");
1622 #endif
1623     else if (strcasecmp("CollectBan", child->key) == 0)
1624 #if HAVE_VARNISH_V2
1625       WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1626               child->key, "v2");
1627 #else
1628       cf_util_get_boolean(child, &conf->collect_ban);
1629 #endif
1630     else if (strcasecmp("CollectSession", child->key) == 0)
1631       cf_util_get_boolean(child, &conf->collect_session);
1632     else if (strcasecmp("CollectSHM", child->key) == 0)
1633       cf_util_get_boolean(child, &conf->collect_shm);
1634     else if (strcasecmp("CollectSMS", child->key) == 0)
1635       cf_util_get_boolean(child, &conf->collect_sms);
1636     else if (strcasecmp("CollectSMA", child->key) == 0)
1637 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1638       cf_util_get_boolean(child, &conf->collect_sma);
1639 #else
1640       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1641               child->key, "v2 and v4");
1642 #endif
1643     else if (strcasecmp("CollectSM", child->key) == 0)
1644 #if HAVE_VARNISH_V2
1645       cf_util_get_boolean(child, &conf->collect_sm);
1646 #else
1647       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1648               child->key, "v2");
1649 #endif
1650     else if (strcasecmp("CollectStruct", child->key) == 0)
1651       cf_util_get_boolean(child, &conf->collect_struct);
1652     else if (strcasecmp("CollectTotals", child->key) == 0)
1653       cf_util_get_boolean(child, &conf->collect_totals);
1654     else if (strcasecmp("CollectUptime", child->key) == 0)
1655 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1656       cf_util_get_boolean(child, &conf->collect_uptime);
1657 #else
1658       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1659               child->key, "v3 and v4");
1660 #endif
1661     else if (strcasecmp("CollectVCL", child->key) == 0)
1662       cf_util_get_boolean(child, &conf->collect_vcl);
1663     else if (strcasecmp("CollectWorkers", child->key) == 0)
1664       cf_util_get_boolean(child, &conf->collect_workers);
1665     else if (strcasecmp("CollectVSM", child->key) == 0)
1666 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1667       cf_util_get_boolean(child, &conf->collect_vsm);
1668 #else
1669       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1670               child->key, "v4");
1671 #endif
1672     else if (strcasecmp("CollectLock", child->key) == 0)
1673 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1674       cf_util_get_boolean(child, &conf->collect_lck);
1675 #else
1676       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1677               child->key, "v4");
1678 #endif
1679     else if (strcasecmp("CollectMempool", child->key) == 0)
1680 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1681       cf_util_get_boolean(child, &conf->collect_mempool);
1682 #else
1683       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1684               child->key, "v4");
1685 #endif
1686     else if (strcasecmp("CollectManagement", child->key) == 0)
1687 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1688       cf_util_get_boolean(child, &conf->collect_mgt);
1689 #else
1690       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1691               child->key, "v4");
1692 #endif
1693     else if (strcasecmp("CollectSMF", child->key) == 0)
1694 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1695       cf_util_get_boolean(child, &conf->collect_smf);
1696 #else
1697       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1698               child->key, "v4");
1699 #endif
1700     else if (strcasecmp("CollectSMF", child->key) == 0)
1701 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1702       cf_util_get_boolean(child, &conf->collect_smf);
1703 #else
1704       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1705               child->key, "v4");
1706 #endif
1707     else if (strcasecmp("CollectVBE", child->key) == 0)
1708 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1709       cf_util_get_boolean(child, &conf->collect_vbe);
1710 #else
1711       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1712               child->key, "v4");
1713 #endif
1714     else if (strcasecmp("CollectMSE", child->key) == 0)
1715 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1716       cf_util_get_boolean(child, &conf->collect_mse);
1717 #else
1718       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1719               child->key, "Plus v4");
1720 #endif
1721     else {
1722       WARNING("Varnish plugin: Ignoring unknown "
1723               "configuration option: \"%s\". Did "
1724               "you forget to add an <Instance /> "
1725               "block around the configuration?",
1726               child->key);
1727     }
1728   }
1729
1730   if (!conf->collect_cache && !conf->collect_connections &&
1731       !conf->collect_esi && !conf->collect_backend
1732 #ifdef HAVE_VARNISH_V3
1733       && !conf->collect_dirdns
1734 #endif
1735       && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1736 #if HAVE_VARNISH_V2
1737       && !conf->collect_purge
1738 #else
1739       && !conf->collect_ban
1740 #endif
1741       && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1742 #if HAVE_VARNISH_V2
1743       && !conf->collect_sm
1744 #endif
1745 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1746       && !conf->collect_sma
1747 #endif
1748       && !conf->collect_struct && !conf->collect_totals
1749 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1750       && !conf->collect_uptime
1751 #endif
1752       && !conf->collect_vcl && !conf->collect_workers
1753 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1754       && !conf->collect_vsm && !conf->collect_vbe && !conf->collect_smf &&
1755       !conf->collect_mgt && !conf->collect_lck && !conf->collect_mempool &&
1756       !conf->collect_mse
1757 #endif
1758       ) {
1759     WARNING("Varnish plugin: No metric has been configured for "
1760             "instance \"%s\". Disabling this instance.",
1761             (conf->instance == NULL) ? "localhost" : conf->instance);
1762     sfree(conf);
1763     return EINVAL;
1764   }
1765
1766   snprintf(callback_name, sizeof(callback_name), "varnish/%s",
1767            (conf->instance == NULL) ? "localhost" : conf->instance);
1768
1769   plugin_register_complex_read(
1770       /* group = */ "varnish",
1771       /* name      = */ callback_name,
1772       /* callback  = */ varnish_read,
1773       /* interval  = */ 0,
1774       &(user_data_t){
1775           .data = conf, .free_func = varnish_config_free,
1776       });
1777
1778   have_instance = 1;
1779
1780   return 0;
1781 } /* }}} int varnish_config_instance */
1782
1783 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1784 {
1785   for (int i = 0; i < ci->children_num; i++) {
1786     oconfig_item_t *child = ci->children + i;
1787
1788     if (strcasecmp("Instance", child->key) == 0)
1789       varnish_config_instance(child);
1790     else {
1791       WARNING("Varnish plugin: Ignoring unknown "
1792               "configuration option: \"%s\"",
1793               child->key);
1794     }
1795   }
1796
1797   return 0;
1798 } /* }}} int varnish_config */
1799
1800 void module_register(void) /* {{{ */
1801 {
1802   plugin_register_complex_config("varnish", varnish_config);
1803   plugin_register_init("varnish", varnish_init);
1804 } /* }}} */