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