Treewide: replace ssnprintf with snprintf
[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  **/
25
26 #include "collectd.h"
27
28 #include "common.h"
29 #include "plugin.h"
30
31 #if HAVE_VARNISH_V4
32 #include <vapi/vsc.h>
33 #include <vapi/vsm.h>
34 typedef struct VSC_C_main c_varnish_stats_t;
35 #endif
36
37 #if HAVE_VARNISH_V3
38 #include <varnishapi.h>
39 #include <vsc.h>
40 typedef struct VSC_C_main c_varnish_stats_t;
41 #endif
42
43 #if HAVE_VARNISH_V2
44 #include <varnishapi.h>
45 typedef struct varnish_stats c_varnish_stats_t;
46 #endif
47
48 /* {{{ user_config_s */
49 struct user_config_s {
50   char *instance;
51
52   _Bool collect_cache;
53   _Bool collect_connections;
54   _Bool collect_esi;
55   _Bool collect_backend;
56 #ifdef HAVE_VARNISH_V3
57   _Bool collect_dirdns;
58 #endif
59   _Bool collect_fetch;
60   _Bool collect_hcb;
61   _Bool collect_objects;
62 #if HAVE_VARNISH_V2
63   _Bool collect_purge;
64 #else
65   _Bool collect_ban;
66 #endif
67   _Bool collect_session;
68   _Bool collect_shm;
69   _Bool collect_sms;
70 #if HAVE_VARNISH_V2
71   _Bool collect_sm;
72   _Bool collect_sma;
73 #endif
74   _Bool collect_struct;
75   _Bool collect_totals;
76 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
77   _Bool collect_uptime;
78 #endif
79   _Bool collect_vcl;
80   _Bool collect_workers;
81 #if HAVE_VARNISH_V4
82   _Bool collect_vsm;
83 #endif
84 };
85 typedef struct user_config_s user_config_t; /* }}} */
86
87 static _Bool have_instance = 0;
88
89 static int varnish_submit(const char *plugin_instance, /* {{{ */
90                           const char *category, const char *type,
91                           const char *type_instance, value_t value) {
92   value_list_t vl = VALUE_LIST_INIT;
93
94   vl.values = &value;
95   vl.values_len = 1;
96
97   sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
98
99   if (plugin_instance == NULL)
100     plugin_instance = "default";
101   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
102             plugin_instance, category);
103
104   sstrncpy(vl.type, type, sizeof(vl.type));
105
106   if (type_instance != NULL)
107     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
108
109   return plugin_dispatch_values(&vl);
110 } /* }}} int varnish_submit */
111
112 static int varnish_submit_gauge(const char *plugin_instance, /* {{{ */
113                                 const char *category, const char *type,
114                                 const char *type_instance,
115                                 uint64_t gauge_value) {
116   return varnish_submit(plugin_instance, category, type, type_instance, (value_t){
117       .gauge=(gauge_t)gauge_value,
118     });
119 } /* }}} int varnish_submit_gauge */
120
121 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
122                                  const char *category, const char *type,
123                                  const char *type_instance,
124                                  uint64_t derive_value) {
125   return varnish_submit(plugin_instance, category, type, type_instance, (value_t){
126       .derive=(derive_t)derive_value,
127     });
128 } /* }}} int varnish_submit_derive */
129
130 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
131 static int varnish_monitor(void *priv,
132                            const struct VSC_point *const pt) /* {{{ */
133 {
134   uint64_t val;
135   const user_config_t *conf;
136   const char *class;
137   const char *name;
138
139   if (pt == NULL)
140     return 0;
141
142   conf = priv;
143
144 #if HAVE_VARNISH_V4
145   class = pt->section->fantom->type;
146   name = pt->desc->name;
147
148   if (strcmp(class, "MAIN") != 0)
149     return 0;
150
151 #elif HAVE_VARNISH_V3
152   class = pt->class;
153   name = pt->name;
154
155   if (strcmp(class, "") != 0)
156     return 0;
157 #endif
158
159   val = *(const volatile uint64_t *)pt->ptr;
160
161   if (conf->collect_cache) {
162     if (strcmp(name, "cache_hit") == 0)
163       return varnish_submit_derive(conf->instance, "cache", "cache_result",
164                                    "hit", val);
165     else if (strcmp(name, "cache_miss") == 0)
166       return varnish_submit_derive(conf->instance, "cache", "cache_result",
167                                    "miss", val);
168     else if (strcmp(name, "cache_hitpass") == 0)
169       return varnish_submit_derive(conf->instance, "cache", "cache_result",
170                                    "hitpass", val);
171   }
172
173   if (conf->collect_connections) {
174     if (strcmp(name, "client_conn") == 0)
175       return varnish_submit_derive(conf->instance, "connections",
176                                    "connections", "accepted", val);
177     else if (strcmp(name, "client_drop") == 0)
178       return varnish_submit_derive(conf->instance, "connections",
179                                    "connections", "dropped", val);
180     else if (strcmp(name, "client_req") == 0)
181       return varnish_submit_derive(conf->instance, "connections",
182                                    "connections", "received", val);
183   }
184
185 #ifdef HAVE_VARNISH_V3
186   if (conf->collect_dirdns) {
187     if (strcmp(name, "dir_dns_lookups") == 0)
188       return varnish_submit_derive(conf->instance, "dirdns",
189                                    "cache_operation", "lookups", val);
190     else if (strcmp(name, "dir_dns_failed") == 0)
191       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
192                                    "failed", val);
193     else if (strcmp(name, "dir_dns_hit") == 0)
194       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
195                                    "hits", val);
196     else if (strcmp(name, "dir_dns_cache_full") == 0)
197       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
198                                    "cache_full", val);
199   }
200 #endif
201
202   if (conf->collect_esi) {
203     if (strcmp(name, "esi_errors") == 0)
204       return varnish_submit_derive(conf->instance, "esi", "total_operations",
205                                    "error", val);
206     else if (strcmp(name, "esi_parse") == 0)
207       return varnish_submit_derive(conf->instance, "esi", "total_operations",
208                                    "parsed", val);
209     else if (strcmp(name, "esi_warnings") == 0)
210       return varnish_submit_derive(conf->instance, "esi", "total_operations",
211                                    "warning", val);
212   }
213
214   if (conf->collect_backend) {
215     if (strcmp(name, "backend_conn") == 0)
216       return varnish_submit_derive(conf->instance, "backend", "connections",
217                                    "success", val);
218     else if (strcmp(name, "backend_unhealthy") == 0)
219       return varnish_submit_derive(conf->instance, "backend", "connections",
220                                    "not-attempted", val);
221     else if (strcmp(name, "backend_busy") == 0)
222       return varnish_submit_derive(conf->instance, "backend", "connections",
223                                    "too-many", val);
224     else if (strcmp(name, "backend_fail") == 0)
225       return varnish_submit_derive(conf->instance, "backend", "connections",
226                                    "failures", val);
227     else if (strcmp(name, "backend_reuse") == 0)
228       return varnish_submit_derive(conf->instance, "backend", "connections",
229                                    "reuses", val);
230     else if (strcmp(name, "backend_toolate") == 0)
231       return varnish_submit_derive(conf->instance, "backend", "connections",
232                                    "was-closed", val);
233     else if (strcmp(name, "backend_recycle") == 0)
234       return varnish_submit_derive(conf->instance, "backend", "connections",
235                                    "recycled", val);
236     else if (strcmp(name, "backend_unused") == 0)
237       return varnish_submit_derive(conf->instance, "backend", "connections",
238                                    "unused", val);
239     else if (strcmp(name, "backend_retry") == 0)
240       return varnish_submit_derive(conf->instance, "backend", "connections",
241                                    "retries", val);
242     else if (strcmp(name, "backend_req") == 0)
243       return varnish_submit_derive(conf->instance, "backend", "http_requests",
244                                    "requests", val);
245     else if (strcmp(name, "n_backend") == 0)
246       return varnish_submit_gauge(conf->instance, "backend", "backends",
247                                   "n_backends", val);
248   }
249
250   if (conf->collect_fetch) {
251     if (strcmp(name, "fetch_head") == 0)
252       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
253                                    "head", val);
254     else if (strcmp(name, "fetch_length") == 0)
255       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
256                                    "length", val);
257     else if (strcmp(name, "fetch_chunked") == 0)
258       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
259                                    "chunked", val);
260     else if (strcmp(name, "fetch_eof") == 0)
261       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
262                                    "eof", val);
263     else if (strcmp(name, "fetch_bad") == 0)
264       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
265                                    "bad_headers", val);
266     else if (strcmp(name, "fetch_close") == 0)
267       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
268                                    "close", val);
269     else if (strcmp(name, "fetch_oldhttp") == 0)
270       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
271                                    "oldhttp", val);
272     else if (strcmp(name, "fetch_zero") == 0)
273       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
274                                    "zero", val);
275     else if (strcmp(name, "fetch_failed") == 0)
276       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
277                                    "failed", val);
278     else if (strcmp(name, "fetch_1xx") == 0)
279       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
280                                    "no_body_1xx", val);
281     else if (strcmp(name, "fetch_204") == 0)
282       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
283                                    "no_body_204", val);
284     else if (strcmp(name, "fetch_304") == 0)
285       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
286                                    "no_body_304", val);
287   }
288
289   if (conf->collect_hcb) {
290     if (strcmp(name, "hcb_nolock") == 0)
291       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
292                                    "lookup_nolock", val);
293     else if (strcmp(name, "hcb_lock") == 0)
294       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
295                                    "lookup_lock", val);
296     else if (strcmp(name, "hcb_insert") == 0)
297       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
298                                    "insert", val);
299   }
300
301   if (conf->collect_objects) {
302     if (strcmp(name, "n_expired") == 0)
303       return varnish_submit_derive(conf->instance, "objects", "total_objects",
304                                    "expired", val);
305     else if (strcmp(name, "n_lru_nuked") == 0)
306       return varnish_submit_derive(conf->instance, "objects", "total_objects",
307                                    "lru_nuked", val);
308     else if (strcmp(name, "n_lru_saved") == 0)
309       return varnish_submit_derive(conf->instance, "objects", "total_objects",
310                                    "lru_saved", val);
311     else if (strcmp(name, "n_lru_moved") == 0)
312       return varnish_submit_derive(conf->instance, "objects", "total_objects",
313                                    "lru_moved", val);
314     else if (strcmp(name, "n_deathrow") == 0)
315       return varnish_submit_derive(conf->instance, "objects", "total_objects",
316                                    "deathrow", val);
317     else if (strcmp(name, "losthdr") == 0)
318       return varnish_submit_derive(conf->instance, "objects", "total_objects",
319                                    "header_overflow", val);
320     else if (strcmp(name, "n_obj_purged") == 0)
321       return varnish_submit_derive(conf->instance, "objects", "total_objects",
322                                    "purged", val);
323     else if (strcmp(name, "n_objsendfile") == 0)
324       return varnish_submit_derive(conf->instance, "objects", "total_objects",
325                                    "sent_sendfile", val);
326     else if (strcmp(name, "n_objwrite") == 0)
327       return varnish_submit_derive(conf->instance, "objects", "total_objects",
328                                    "sent_write", val);
329     else if (strcmp(name, "n_objoverflow") == 0)
330       return varnish_submit_derive(conf->instance, "objects", "total_objects",
331                                    "workspace_overflow", val);
332   }
333
334 #if HAVE_VARNISH_V3
335   if (conf->collect_ban) {
336     if (strcmp(name, "n_ban") == 0)
337       return varnish_submit_derive(conf->instance, "ban", "total_operations",
338                                    "total", val);
339     else if (strcmp(name, "n_ban_add") == 0)
340       return varnish_submit_derive(conf->instance, "ban", "total_operations",
341                                    "added", val);
342     else if (strcmp(name, "n_ban_retire") == 0)
343       return varnish_submit_derive(conf->instance, "ban", "total_operations",
344                                    "deleted", val);
345     else if (strcmp(name, "n_ban_obj_test") == 0)
346       return varnish_submit_derive(conf->instance, "ban", "total_operations",
347                                    "objects_tested", val);
348     else if (strcmp(name, "n_ban_re_test") == 0)
349       return varnish_submit_derive(conf->instance, "ban", "total_operations",
350                                    "regexps_tested", val);
351     else if (strcmp(name, "n_ban_dups") == 0)
352       return varnish_submit_derive(conf->instance, "ban", "total_operations",
353                                    "duplicate", val);
354   }
355 #endif
356 #if HAVE_VARNISH_V4
357   if (conf->collect_ban) {
358     if (strcmp(name, "bans") == 0)
359       return varnish_submit_derive(conf->instance, "ban", "total_operations",
360                                    "total", val);
361     else if (strcmp(name, "bans_added") == 0)
362       return varnish_submit_derive(conf->instance, "ban", "total_operations",
363                                    "added", val);
364     else if (strcmp(name, "bans_obj") == 0)
365       return varnish_submit_derive(conf->instance, "ban", "total_operations",
366                                    "obj", val);
367     else if (strcmp(name, "bans_req") == 0)
368       return varnish_submit_derive(conf->instance, "ban", "total_operations",
369                                    "req", val);
370     else if (strcmp(name, "bans_completed") == 0)
371       return varnish_submit_derive(conf->instance, "ban", "total_operations",
372                                    "completed", val);
373     else if (strcmp(name, "bans_deleted") == 0)
374       return varnish_submit_derive(conf->instance, "ban", "total_operations",
375                                    "deleted", val);
376     else if (strcmp(name, "bans_tested") == 0)
377       return varnish_submit_derive(conf->instance, "ban", "total_operations",
378                                    "tested", val);
379     else if (strcmp(name, "bans_dups") == 0)
380       return varnish_submit_derive(conf->instance, "ban", "total_operations",
381                                    "duplicate", val);
382   }
383 #endif
384
385   if (conf->collect_session) {
386     if (strcmp(name, "sess_closed") == 0)
387       return varnish_submit_derive(conf->instance, "session",
388                                    "total_operations", "closed", val);
389     else if (strcmp(name, "sess_pipeline") == 0)
390       return varnish_submit_derive(conf->instance, "session",
391                                    "total_operations", "pipeline", val);
392     else if (strcmp(name, "sess_readahead") == 0)
393       return varnish_submit_derive(conf->instance, "session",
394                                    "total_operations", "readahead", val);
395     else if (strcmp(name, "sess_conn") == 0)
396       return varnish_submit_derive(conf->instance, "session",
397                                    "total_operations", "accepted", val);
398     else if (strcmp(name, "sess_drop") == 0)
399       return varnish_submit_derive(conf->instance, "session",
400                                    "total_operations", "dropped", val);
401     else if (strcmp(name, "sess_fail") == 0)
402       return varnish_submit_derive(conf->instance, "session",
403                                    "total_operations", "failed", val);
404     else if (strcmp(name, "sess_pipe_overflow") == 0)
405       return varnish_submit_derive(conf->instance, "session",
406                                    "total_operations", "overflow", val);
407     else if (strcmp(name, "sess_queued") == 0)
408       return varnish_submit_derive(conf->instance, "session",
409                                    "total_operations", "queued", val);
410     else if (strcmp(name, "sess_linger") == 0)
411       return varnish_submit_derive(conf->instance, "session",
412                                    "total_operations", "linger", val);
413     else if (strcmp(name, "sess_herd") == 0)
414       return varnish_submit_derive(conf->instance, "session",
415                                    "total_operations", "herd", val);
416   }
417
418   if (conf->collect_shm) {
419     if (strcmp(name, "shm_records") == 0)
420       return varnish_submit_derive(conf->instance, "shm", "total_operations",
421                                    "records", val);
422     else if (strcmp(name, "shm_writes") == 0)
423       return varnish_submit_derive(conf->instance, "shm", "total_operations",
424                                    "writes", val);
425     else if (strcmp(name, "shm_flushes") == 0)
426       return varnish_submit_derive(conf->instance, "shm", "total_operations",
427                                    "flushes", val);
428     else if (strcmp(name, "shm_cont") == 0)
429       return varnish_submit_derive(conf->instance, "shm", "total_operations",
430                                    "contention", val);
431     else if (strcmp(name, "shm_cycles") == 0)
432       return varnish_submit_derive(conf->instance, "shm", "total_operations",
433                                    "cycles", val);
434   }
435
436   if (conf->collect_sms) {
437     if (strcmp(name, "sms_nreq") == 0)
438       return varnish_submit_derive(conf->instance, "sms", "total_requests",
439                                    "allocator", val);
440     else if (strcmp(name, "sms_nobj") == 0)
441       return varnish_submit_gauge(conf->instance, "sms", "requests",
442                                   "outstanding", val);
443     else if (strcmp(name, "sms_nbytes") == 0)
444       return varnish_submit_gauge(conf->instance, "sms", "bytes",
445                                   "outstanding", val);
446     else if (strcmp(name, "sms_balloc") == 0)
447       return varnish_submit_derive(conf->instance, "sms", "total_bytes",
448                                    "allocated", val);
449     else if (strcmp(name, "sms_bfree") == 0)
450       return varnish_submit_derive(conf->instance, "sms", "total_bytes",
451                                    "free", val);
452   }
453
454   if (conf->collect_struct) {
455     if (strcmp(name, "n_sess_mem") == 0)
456       return varnish_submit_gauge(conf->instance, "struct",
457                                   "current_sessions", "sess_mem", val);
458     else if (strcmp(name, "n_sess") == 0)
459       return varnish_submit_gauge(conf->instance, "struct",
460                                   "current_sessions", "sess", val);
461     else if (strcmp(name, "n_object") == 0)
462       return varnish_submit_gauge(conf->instance, "struct", "objects",
463                                   "object", val);
464     else if (strcmp(name, "n_vampireobject") == 0)
465       return varnish_submit_gauge(conf->instance, "struct", "objects",
466                                   "vampireobject", val);
467     else if (strcmp(name, "n_objectcore") == 0)
468       return varnish_submit_gauge(conf->instance, "struct", "objects",
469                                   "objectcore", val);
470     else if (strcmp(name, "n_waitinglist") == 0)
471       return varnish_submit_gauge(conf->instance, "struct", "objects",
472                                   "waitinglist", val);
473     else if (strcmp(name, "n_objecthead") == 0)
474       return varnish_submit_gauge(conf->instance, "struct", "objects",
475                                   "objecthead", val);
476     else if (strcmp(name, "n_smf") == 0)
477       return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
478                                   val);
479     else if (strcmp(name, "n_smf_frag") == 0)
480       return varnish_submit_gauge(conf->instance, "struct", "objects",
481                                   "smf_frag", val);
482     else if (strcmp(name, "n_smf_large") == 0)
483       return varnish_submit_gauge(conf->instance, "struct", "objects",
484                                   "smf_large", val);
485     else if (strcmp(name, "n_vbe_conn") == 0)
486       return varnish_submit_gauge(conf->instance, "struct", "objects",
487                                   "vbe_conn", val);
488   }
489
490   if (conf->collect_totals) {
491     if (strcmp(name, "s_sess") == 0)
492       return varnish_submit_derive(conf->instance, "totals", "total_sessions",
493                                    "sessions", val);
494     else if (strcmp(name, "s_req") == 0)
495       return varnish_submit_derive(conf->instance, "totals", "total_requests",
496                                    "requests", val);
497     else if (strcmp(name, "s_pipe") == 0)
498       return varnish_submit_derive(conf->instance, "totals",
499                                    "total_operations", "pipe", val);
500     else if (strcmp(name, "s_pass") == 0)
501       return varnish_submit_derive(conf->instance, "totals",
502                                    "total_operations", "pass", val);
503     else if (strcmp(name, "s_fetch") == 0)
504       return varnish_submit_derive(conf->instance, "totals",
505                                    "total_operations", "fetches", val);
506     else if (strcmp(name, "s_synth") == 0)
507       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
508                                    "synth", val);
509     else if (strcmp(name, "s_req_hdrbytes") == 0)
510       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
511                                    "req_header", val);
512     else if (strcmp(name, "s_req_bodybytes") == 0)
513       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
514                                    "req_body", val);
515     else if (strcmp(name, "s_resp_hdrbytes") == 0)
516       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
517                                    "resp_header", val);
518     else if (strcmp(name, "s_resp_bodybytes") == 0)
519       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
520                                    "resp_body", val);
521     else if (strcmp(name, "s_pipe_hdrbytes") == 0)
522       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
523                                    "pipe_header", val);
524     else if (strcmp(name, "s_pipe_in") == 0)
525       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
526                                    "pipe_in", val);
527     else if (strcmp(name, "s_pipe_out") == 0)
528       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
529                                    "pipe_out", val);
530     else if (strcmp(name, "n_purges") == 0)
531       return varnish_submit_derive(conf->instance, "totals",
532                                    "total_operations", "purges", val);
533     else if (strcmp(name, "s_hdrbytes") == 0)
534       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
535                                    "header-bytes", val);
536     else if (strcmp(name, "s_bodybytes") == 0)
537       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
538                                    "body-bytes", val);
539     else if (strcmp(name, "n_gzip") == 0)
540       return varnish_submit_derive(conf->instance, "totals",
541                                    "total_operations", "gzip", val);
542     else if (strcmp(name, "n_gunzip") == 0)
543       return varnish_submit_derive(conf->instance, "totals",
544                                    "total_operations", "gunzip", val);
545   }
546
547   if (conf->collect_uptime) {
548     if (strcmp(name, "uptime") == 0)
549       return varnish_submit_gauge(conf->instance, "uptime", "uptime",
550                                   "client_uptime", val);
551   }
552
553   if (conf->collect_vcl) {
554     if (strcmp(name, "n_vcl") == 0)
555       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
556                                   val);
557     else if (strcmp(name, "n_vcl_avail") == 0)
558       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
559                                   val);
560     else if (strcmp(name, "n_vcl_discard") == 0)
561       return varnish_submit_gauge(conf->instance, "vcl", "vcl",
562                                   "discarded_vcl", val);
563     else if (strcmp(name, "vmods") == 0)
564       return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
565                                   val);
566   }
567
568   if (conf->collect_workers) {
569     if (strcmp(name, "threads") == 0)
570       return varnish_submit_gauge(conf->instance, "workers", "threads",
571                                   "worker", val);
572     else if (strcmp(name, "threads_created") == 0)
573       return varnish_submit_derive(conf->instance, "workers", "total_threads",
574                                    "created", val);
575     else if (strcmp(name, "threads_failed") == 0)
576       return varnish_submit_derive(conf->instance, "workers", "total_threads",
577                                    "failed", val);
578     else if (strcmp(name, "threads_limited") == 0)
579       return varnish_submit_derive(conf->instance, "workers", "total_threads",
580                                    "limited", val);
581     else if (strcmp(name, "threads_destroyed") == 0)
582       return varnish_submit_derive(conf->instance, "workers", "total_threads",
583                                    "dropped", val);
584     else if (strcmp(name, "thread_queue_len") == 0)
585       return varnish_submit_derive(conf->instance, "workers", "queue_length",
586                                    "threads", val);
587     else if (strcmp(name, "n_wrk") == 0)
588       return varnish_submit_gauge(conf->instance, "workers", "threads",
589                                   "worker", val);
590     else if (strcmp(name, "n_wrk_create") == 0)
591       return varnish_submit_derive(conf->instance, "workers", "total_threads",
592                                    "created", val);
593     else if (strcmp(name, "n_wrk_failed") == 0)
594       return varnish_submit_derive(conf->instance, "workers", "total_threads",
595                                    "failed", val);
596     else if (strcmp(name, "n_wrk_max") == 0)
597       return varnish_submit_derive(conf->instance, "workers", "total_threads",
598                                    "limited", val);
599     else if (strcmp(name, "n_wrk_drop") == 0)
600       return varnish_submit_derive(conf->instance, "workers", "total_threads",
601                                    "dropped", val);
602     else if (strcmp(name, "n_wrk_queue") == 0)
603       return varnish_submit_derive(conf->instance, "workers",
604                                    "total_requests", "queued", val);
605     else if (strcmp(name, "n_wrk_overflow") == 0)
606       return varnish_submit_derive(conf->instance, "workers",
607                                    "total_requests", "overflowed", val);
608     else if (strcmp(name, "n_wrk_queued") == 0)
609       return varnish_submit_derive(conf->instance, "workers",
610                                    "total_requests", "queued", val);
611     else if (strcmp(name, "n_wrk_lqueue") == 0)
612       return varnish_submit_derive(conf->instance, "workers",
613                                    "total_requests", "queue_length", val);
614   }
615
616 #if HAVE_VARNISH_V4
617   if (conf->collect_vsm) {
618     if (strcmp(name, "vsm_free") == 0)
619       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
620     else if (strcmp(name, "vsm_used") == 0)
621       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
622     else if (strcmp(name, "vsm_cooling") == 0)
623       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
624                                   val);
625     else if (strcmp(name, "vsm_overflow") == 0)
626       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
627                                   val);
628     else if (strcmp(name, "vsm_overflowed") == 0)
629       return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
630                                    "overflowed", val);
631   }
632 #endif
633
634   return 0;
635
636 } /* }}} static int varnish_monitor */
637 #else /* if HAVE_VARNISH_V2 */
638 static void varnish_monitor(const user_config_t *conf, /* {{{ */
639                             const c_varnish_stats_t *stats) {
640   if (conf->collect_cache) {
641     /* Cache hits */
642     varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
643                           stats->cache_hit);
644     /* Cache misses */
645     varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
646                           stats->cache_miss);
647     /* Cache hits for pass */
648     varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
649                           stats->cache_hitpass);
650   }
651
652   if (conf->collect_connections) {
653     /* Client connections accepted */
654     varnish_submit_derive(conf->instance, "connections", "connections",
655                           "accepted", stats->client_conn);
656     /* Connection dropped, no sess */
657     varnish_submit_derive(conf->instance, "connections", "connections",
658                           "dropped", stats->client_drop);
659     /* Client requests received    */
660     varnish_submit_derive(conf->instance, "connections", "connections",
661                           "received", stats->client_req);
662   }
663
664   if (conf->collect_esi) {
665     /* ESI parse errors (unlock)   */
666     varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
667                           stats->esi_errors);
668     /* Objects ESI parsed (unlock) */
669     varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
670                           stats->esi_parse);
671   }
672
673   if (conf->collect_backend) {
674     /* Backend conn. success       */
675     varnish_submit_derive(conf->instance, "backend", "connections", "success",
676                           stats->backend_conn);
677     /* Backend conn. not attempted */
678     varnish_submit_derive(conf->instance, "backend", "connections",
679                           "not-attempted", stats->backend_unhealthy);
680     /* Backend conn. too many      */
681     varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
682                           stats->backend_busy);
683     /* Backend conn. failures      */
684     varnish_submit_derive(conf->instance, "backend", "connections", "failures",
685                           stats->backend_fail);
686     /* Backend conn. reuses        */
687     varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
688                           stats->backend_reuse);
689     /* Backend conn. was closed    */
690     varnish_submit_derive(conf->instance, "backend", "connections",
691                           "was-closed", stats->backend_toolate);
692     /* Backend conn. recycles      */
693     varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
694                           stats->backend_recycle);
695     /* Backend conn. unused        */
696     varnish_submit_derive(conf->instance, "backend", "connections", "unused",
697                           stats->backend_unused);
698     /* Backend requests mades      */
699     varnish_submit_derive(conf->instance, "backend", "http_requests",
700                           "requests", stats->backend_req);
701     /* N backends                  */
702     varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
703                          stats->n_backend);
704   }
705
706   if (conf->collect_fetch) {
707     /* Fetch head                */
708     varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
709                           stats->fetch_head);
710     /* Fetch with length         */
711     varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
712                           stats->fetch_length);
713     /* Fetch chunked             */
714     varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
715                           stats->fetch_chunked);
716     /* Fetch EOF                 */
717     varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
718                           stats->fetch_eof);
719     /* Fetch bad headers         */
720     varnish_submit_derive(conf->instance, "fetch", "http_requests",
721                           "bad_headers", stats->fetch_bad);
722     /* Fetch wanted close        */
723     varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
724                           stats->fetch_close);
725     /* Fetch pre HTTP/1.1 closed */
726     varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
727                           stats->fetch_oldhttp);
728     /* Fetch zero len            */
729     varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
730                           stats->fetch_zero);
731     /* Fetch failed              */
732     varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
733                           stats->fetch_failed);
734   }
735
736   if (conf->collect_hcb) {
737     /* HCB Lookups without lock */
738     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
739                           "lookup_nolock", stats->hcb_nolock);
740     /* HCB Lookups with lock    */
741     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
742                           "lookup_lock", stats->hcb_lock);
743     /* HCB Inserts              */
744     varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
745                           stats->hcb_insert);
746   }
747
748   if (conf->collect_objects) {
749     /* N expired objects             */
750     varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
751                           stats->n_expired);
752     /* N LRU nuked objects           */
753     varnish_submit_derive(conf->instance, "objects", "total_objects",
754                           "lru_nuked", stats->n_lru_nuked);
755     /* N LRU saved objects           */
756     varnish_submit_derive(conf->instance, "objects", "total_objects",
757                           "lru_saved", stats->n_lru_saved);
758     /* N LRU moved objects           */
759     varnish_submit_derive(conf->instance, "objects", "total_objects",
760                           "lru_moved", stats->n_lru_moved);
761     /* N objects on deathrow         */
762     varnish_submit_derive(conf->instance, "objects", "total_objects",
763                           "deathrow", stats->n_deathrow);
764     /* HTTP header overflows         */
765     varnish_submit_derive(conf->instance, "objects", "total_objects",
766                           "header_overflow", stats->losthdr);
767     /* Objects sent with sendfile    */
768     varnish_submit_derive(conf->instance, "objects", "total_objects",
769                           "sent_sendfile", stats->n_objsendfile);
770     /* Objects sent with write       */
771     varnish_submit_derive(conf->instance, "objects", "total_objects",
772                           "sent_write", stats->n_objwrite);
773     /* Objects overflowing workspace */
774     varnish_submit_derive(conf->instance, "objects", "total_objects",
775                           "workspace_overflow", stats->n_objoverflow);
776   }
777
778   if (conf->collect_purge) {
779     /* N total active purges      */
780     varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
781                           stats->n_purge);
782     /* N new purges added         */
783     varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
784                           stats->n_purge_add);
785     /* N old purges deleted       */
786     varnish_submit_derive(conf->instance, "purge", "total_operations",
787                           "deleted", stats->n_purge_retire);
788     /* N objects tested           */
789     varnish_submit_derive(conf->instance, "purge", "total_operations",
790                           "objects_tested", stats->n_purge_obj_test);
791     /* N regexps tested against   */
792     varnish_submit_derive(conf->instance, "purge", "total_operations",
793                           "regexps_tested", stats->n_purge_re_test);
794     /* N duplicate purges removed */
795     varnish_submit_derive(conf->instance, "purge", "total_operations",
796                           "duplicate", stats->n_purge_dups);
797   }
798
799   if (conf->collect_session) {
800     /* Session Closed     */
801     varnish_submit_derive(conf->instance, "session", "total_operations",
802                           "closed", stats->sess_closed);
803     /* Session Pipeline   */
804     varnish_submit_derive(conf->instance, "session", "total_operations",
805                           "pipeline", stats->sess_pipeline);
806     /* Session Read Ahead */
807     varnish_submit_derive(conf->instance, "session", "total_operations",
808                           "readahead", stats->sess_readahead);
809     /* Session Linger     */
810     varnish_submit_derive(conf->instance, "session", "total_operations",
811                           "linger", stats->sess_linger);
812     /* Session herd       */
813     varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
814                           stats->sess_herd);
815   }
816
817   if (conf->collect_shm) {
818     /* SHM records                 */
819     varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
820                           stats->shm_records);
821     /* SHM writes                  */
822     varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
823                           stats->shm_writes);
824     /* SHM flushes due to overflow */
825     varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
826                           stats->shm_flushes);
827     /* SHM MTX contention          */
828     varnish_submit_derive(conf->instance, "shm", "total_operations",
829                           "contention", stats->shm_cont);
830     /* SHM cycles through buffer   */
831     varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
832                           stats->shm_cycles);
833   }
834
835   if (conf->collect_sm) {
836     /* allocator requests */
837     varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
838                           stats->sm_nreq);
839     /* outstanding allocations */
840     varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
841                          stats->sm_nobj);
842     /* bytes allocated */
843     varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
844                           stats->sm_balloc);
845     /* bytes free */
846     varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
847                           stats->sm_bfree);
848   }
849
850   if (conf->collect_sma) {
851     /* SMA allocator requests */
852     varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
853                           stats->sma_nreq);
854     /* SMA outstanding allocations */
855     varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
856                          stats->sma_nobj);
857     /* SMA outstanding bytes */
858     varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
859                          stats->sma_nbytes);
860     /* SMA bytes allocated */
861     varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
862                           stats->sma_balloc);
863     /* SMA bytes free */
864     varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
865                           stats->sma_bfree);
866   }
867
868   if (conf->collect_sms) {
869     /* SMS allocator requests */
870     varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
871                           stats->sms_nreq);
872     /* SMS outstanding allocations */
873     varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
874                          stats->sms_nobj);
875     /* SMS outstanding bytes */
876     varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
877                          stats->sms_nbytes);
878     /* SMS bytes allocated */
879     varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
880                           stats->sms_balloc);
881     /* SMS bytes freed */
882     varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
883                           stats->sms_bfree);
884   }
885
886   if (conf->collect_struct) {
887     /* N struct sess_mem       */
888     varnish_submit_gauge(conf->instance, "struct", "current_sessions",
889                          "sess_mem", stats->n_sess_mem);
890     /* N struct sess           */
891     varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
892                          stats->n_sess);
893     /* N struct object         */
894     varnish_submit_gauge(conf->instance, "struct", "objects", "object",
895                          stats->n_object);
896     /* N struct objecthead     */
897     varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
898                          stats->n_objecthead);
899     /* N struct smf            */
900     varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
901                          stats->n_smf);
902     /* N small free smf         */
903     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
904                          stats->n_smf_frag);
905     /* N large free smf         */
906     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
907                          stats->n_smf_large);
908     /* N struct vbe_conn        */
909     varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
910                          stats->n_vbe_conn);
911   }
912
913   if (conf->collect_totals) {
914     /* Total Sessions */
915     varnish_submit_derive(conf->instance, "totals", "total_sessions",
916                           "sessions", stats->s_sess);
917     /* Total Requests */
918     varnish_submit_derive(conf->instance, "totals", "total_requests",
919                           "requests", stats->s_req);
920     /* Total pipe */
921     varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
922                           stats->s_pipe);
923     /* Total pass */
924     varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
925                           stats->s_pass);
926     /* Total fetch */
927     varnish_submit_derive(conf->instance, "totals", "total_operations",
928                           "fetches", stats->s_fetch);
929     /* Total header bytes */
930     varnish_submit_derive(conf->instance, "totals", "total_bytes",
931                           "header-bytes", stats->s_hdrbytes);
932     /* Total body byte */
933     varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
934                           stats->s_bodybytes);
935   }
936
937   if (conf->collect_vcl) {
938     /* N vcl total     */
939     varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
940                          stats->n_vcl);
941     /* N vcl available */
942     varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
943                          stats->n_vcl_avail);
944     /* N vcl discarded */
945     varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
946                          stats->n_vcl_discard);
947   }
948
949   if (conf->collect_workers) {
950     /* worker threads */
951     varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
952                          stats->n_wrk);
953     /* worker threads created */
954     varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
955                           stats->n_wrk_create);
956     /* worker threads not created */
957     varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
958                           stats->n_wrk_failed);
959     /* worker threads limited */
960     varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
961                           stats->n_wrk_max);
962     /* dropped work requests */
963     varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
964                           stats->n_wrk_drop);
965     /* queued work requests */
966     varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
967                           stats->n_wrk_queue);
968     /* overflowed work requests */
969     varnish_submit_derive(conf->instance, "workers", "total_requests",
970                           "overflowed", stats->n_wrk_overflow);
971   }
972
973 } /* }}} void varnish_monitor */
974 #endif
975
976 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
977 static int varnish_read(user_data_t *ud) /* {{{ */
978 {
979   struct VSM_data *vd;
980   const c_varnish_stats_t *stats;
981   _Bool ok;
982
983   user_config_t *conf;
984
985   if ((ud == NULL) || (ud->data == NULL))
986     return EINVAL;
987
988   conf = ud->data;
989
990   vd = VSM_New();
991 #if HAVE_VARNISH_V3
992   VSC_Setup(vd);
993 #endif
994
995   if (conf->instance != NULL) {
996     int status;
997
998     status = VSM_n_Arg(vd, conf->instance);
999     if (status < 0) {
1000       VSM_Delete(vd);
1001       ERROR("varnish plugin: VSM_n_Arg (\"%s\") failed "
1002             "with status %i.",
1003             conf->instance, status);
1004       return -1;
1005     }
1006   }
1007
1008 #if HAVE_VARNISH_V3
1009   ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1010 #else /* if HAVE_VARNISH_V4 */
1011   ok = (VSM_Open(vd) == 0);
1012 #endif
1013   if (!ok) {
1014     VSM_Delete(vd);
1015     ERROR("varnish plugin: Unable to open connection.");
1016
1017     return -1;
1018   }
1019
1020 #if HAVE_VARNISH_V3
1021   stats = VSC_Main(vd);
1022 #else /* if HAVE_VARNISH_V4 */
1023   stats = VSC_Main(vd, NULL);
1024 #endif
1025   if (!stats) {
1026     VSM_Delete(vd);
1027     ERROR("varnish plugin: Unable to get statistics.");
1028
1029     return -1;
1030   }
1031
1032 #if HAVE_VARNISH_V3
1033   VSC_Iter(vd, varnish_monitor, conf);
1034 #else /* if HAVE_VARNISH_V4 */
1035   VSC_Iter(vd, NULL, varnish_monitor, conf);
1036 #endif
1037   VSM_Delete(vd);
1038
1039   return 0;
1040 } /* }}} */
1041 #else /* if HAVE_VARNISH_V2 */
1042 static int varnish_read(user_data_t *ud) /* {{{ */
1043 {
1044   const c_varnish_stats_t *stats;
1045
1046   user_config_t *conf;
1047
1048   if ((ud == NULL) || (ud->data == NULL))
1049     return EINVAL;
1050
1051   conf = ud->data;
1052
1053   stats = VSL_OpenStats(conf->instance);
1054   if (stats == NULL) {
1055     ERROR("Varnish plugin : unable to load statistics");
1056
1057     return -1;
1058   }
1059
1060   varnish_monitor(conf, stats);
1061
1062   return 0;
1063 } /* }}} */
1064 #endif
1065
1066 static void varnish_config_free(void *ptr) /* {{{ */
1067 {
1068   user_config_t *conf = ptr;
1069
1070   if (conf == NULL)
1071     return;
1072
1073   sfree(conf->instance);
1074   sfree(conf);
1075 } /* }}} */
1076
1077 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1078 {
1079   if (conf == NULL)
1080     return EINVAL;
1081
1082   conf->collect_backend = 1;
1083   conf->collect_cache = 1;
1084   conf->collect_connections = 1;
1085 #ifdef HAVE_VARNISH_V3
1086   conf->collect_dirdns = 0;
1087 #endif
1088   conf->collect_esi = 0;
1089   conf->collect_fetch = 0;
1090   conf->collect_hcb = 0;
1091   conf->collect_objects = 0;
1092 #if HAVE_VARNISH_V2
1093   conf->collect_purge = 0;
1094 #else
1095   conf->collect_ban = 0;
1096 #endif
1097   conf->collect_session = 0;
1098   conf->collect_shm = 1;
1099 #if HAVE_VARNISH_V2
1100   conf->collect_sm = 0;
1101   conf->collect_sma = 0;
1102 #endif
1103   conf->collect_sms = 0;
1104   conf->collect_struct = 0;
1105   conf->collect_totals = 0;
1106 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1107   conf->collect_uptime = 0;
1108 #endif
1109   conf->collect_vcl = 0;
1110   conf->collect_workers = 0;
1111 #if HAVE_VARNISH_V4
1112   conf->collect_vsm = 0;
1113 #endif
1114
1115   return 0;
1116 } /* }}} int varnish_config_apply_default */
1117
1118 static int varnish_init(void) /* {{{ */
1119 {
1120   user_config_t *conf;
1121
1122   if (have_instance)
1123     return 0;
1124
1125   conf = calloc(1, sizeof(*conf));
1126   if (conf == NULL)
1127     return ENOMEM;
1128
1129   /* Default settings: */
1130   conf->instance = NULL;
1131
1132   varnish_config_apply_default(conf);
1133
1134   plugin_register_complex_read(
1135       /* group = */ "varnish",
1136       /* name      = */ "varnish/localhost",
1137       /* callback  = */ varnish_read,
1138       /* interval  = */ 0, &(user_data_t){
1139                                .data = conf, .free_func = varnish_config_free,
1140                            });
1141
1142   return 0;
1143 } /* }}} int varnish_init */
1144
1145 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1146 {
1147   user_config_t *conf;
1148   char callback_name[DATA_MAX_NAME_LEN];
1149
1150   conf = calloc(1, sizeof(*conf));
1151   if (conf == NULL)
1152     return ENOMEM;
1153   conf->instance = NULL;
1154
1155   varnish_config_apply_default(conf);
1156
1157   if (ci->values_num == 1) {
1158     int status;
1159
1160     status = cf_util_get_string(ci, &conf->instance);
1161     if (status != 0) {
1162       sfree(conf);
1163       return status;
1164     }
1165     assert(conf->instance != NULL);
1166
1167     if (strcmp("localhost", conf->instance) == 0) {
1168       sfree(conf->instance);
1169       conf->instance = NULL;
1170     }
1171   } else if (ci->values_num > 1) {
1172     WARNING("Varnish plugin: \"Instance\" blocks accept only "
1173             "one argument.");
1174     sfree(conf);
1175     return EINVAL;
1176   }
1177
1178   for (int i = 0; i < ci->children_num; i++) {
1179     oconfig_item_t *child = ci->children + i;
1180
1181     if (strcasecmp("CollectCache", child->key) == 0)
1182       cf_util_get_boolean(child, &conf->collect_cache);
1183     else if (strcasecmp("CollectConnections", child->key) == 0)
1184       cf_util_get_boolean(child, &conf->collect_connections);
1185     else if (strcasecmp("CollectESI", child->key) == 0)
1186       cf_util_get_boolean(child, &conf->collect_esi);
1187     else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1188 #ifdef HAVE_VARNISH_V3
1189       cf_util_get_boolean(child, &conf->collect_dirdns);
1190 #else
1191       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1192               child->key, "v3");
1193 #endif
1194     else if (strcasecmp("CollectBackend", child->key) == 0)
1195       cf_util_get_boolean(child, &conf->collect_backend);
1196     else if (strcasecmp("CollectFetch", child->key) == 0)
1197       cf_util_get_boolean(child, &conf->collect_fetch);
1198     else if (strcasecmp("CollectHCB", child->key) == 0)
1199       cf_util_get_boolean(child, &conf->collect_hcb);
1200     else if (strcasecmp("CollectObjects", child->key) == 0)
1201       cf_util_get_boolean(child, &conf->collect_objects);
1202     else if (strcasecmp("CollectPurge", child->key) == 0)
1203 #if HAVE_VARNISH_V2
1204       cf_util_get_boolean(child, &conf->collect_purge);
1205 #else
1206       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1207               child->key, "v2");
1208 #endif
1209     else if (strcasecmp("CollectBan", child->key) == 0)
1210 #if HAVE_VARNISH_V2
1211       WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1212               child->key, "v2");
1213 #else
1214       cf_util_get_boolean(child, &conf->collect_ban);
1215 #endif
1216     else if (strcasecmp("CollectSession", child->key) == 0)
1217       cf_util_get_boolean(child, &conf->collect_session);
1218     else if (strcasecmp("CollectSHM", child->key) == 0)
1219       cf_util_get_boolean(child, &conf->collect_shm);
1220     else if (strcasecmp("CollectSMS", child->key) == 0)
1221       cf_util_get_boolean(child, &conf->collect_sms);
1222     else if (strcasecmp("CollectSMA", child->key) == 0)
1223 #if HAVE_VARNISH_V2
1224       cf_util_get_boolean(child, &conf->collect_sma);
1225 #else
1226       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1227               child->key, "v2");
1228 #endif
1229     else if (strcasecmp("CollectSM", child->key) == 0)
1230 #if HAVE_VARNISH_V2
1231       cf_util_get_boolean(child, &conf->collect_sm);
1232 #else
1233       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1234               child->key, "v2");
1235 #endif
1236     else if (strcasecmp("CollectStruct", child->key) == 0)
1237       cf_util_get_boolean(child, &conf->collect_struct);
1238     else if (strcasecmp("CollectTotals", child->key) == 0)
1239       cf_util_get_boolean(child, &conf->collect_totals);
1240     else if (strcasecmp("CollectUptime", child->key) == 0)
1241 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1242       cf_util_get_boolean(child, &conf->collect_uptime);
1243 #else
1244       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1245               child->key, "v3 and v4");
1246 #endif
1247     else if (strcasecmp("CollectVCL", child->key) == 0)
1248       cf_util_get_boolean(child, &conf->collect_vcl);
1249     else if (strcasecmp("CollectWorkers", child->key) == 0)
1250       cf_util_get_boolean(child, &conf->collect_workers);
1251     else if (strcasecmp("CollectVSM", child->key) == 0)
1252 #if HAVE_VARNISH_V4
1253       cf_util_get_boolean(child, &conf->collect_vsm);
1254 #else
1255       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1256               child->key, "v4");
1257 #endif
1258     else {
1259       WARNING("Varnish plugin: Ignoring unknown "
1260               "configuration option: \"%s\". Did "
1261               "you forget to add an <Instance /> "
1262               "block around the configuration?",
1263               child->key);
1264     }
1265   }
1266
1267   if (!conf->collect_cache && !conf->collect_connections &&
1268       !conf->collect_esi && !conf->collect_backend
1269 #ifdef HAVE_VARNISH_V3
1270       && !conf->collect_dirdns
1271 #endif
1272       && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1273 #if HAVE_VARNISH_V2
1274       && !conf->collect_purge
1275 #else
1276       && !conf->collect_ban
1277 #endif
1278       && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1279 #if HAVE_VARNISH_V2
1280       && !conf->collect_sma && !conf->collect_sm
1281 #endif
1282       && !conf->collect_struct && !conf->collect_totals
1283 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1284       && !conf->collect_uptime
1285 #endif
1286       && !conf->collect_vcl && !conf->collect_workers
1287 #if HAVE_VARNISH_V4
1288       && !conf->collect_vsm
1289 #endif
1290       ) {
1291     WARNING("Varnish plugin: No metric has been configured for "
1292             "instance \"%s\". Disabling this instance.",
1293             (conf->instance == NULL) ? "localhost" : conf->instance);
1294     sfree(conf);
1295     return EINVAL;
1296   }
1297
1298   snprintf(callback_name, sizeof(callback_name), "varnish/%s",
1299             (conf->instance == NULL) ? "localhost" : conf->instance);
1300
1301   plugin_register_complex_read(
1302       /* group = */ "varnish",
1303       /* name      = */ callback_name,
1304       /* callback  = */ varnish_read,
1305       /* interval  = */ 0, &(user_data_t){
1306                                .data = conf, .free_func = varnish_config_free,
1307                            });
1308
1309   have_instance = 1;
1310
1311   return 0;
1312 } /* }}} int varnish_config_instance */
1313
1314 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1315 {
1316   for (int i = 0; i < ci->children_num; i++) {
1317     oconfig_item_t *child = ci->children + i;
1318
1319     if (strcasecmp("Instance", child->key) == 0)
1320       varnish_config_instance(child);
1321     else {
1322       WARNING("Varnish plugin: Ignoring unknown "
1323               "configuration option: \"%s\"",
1324               child->key);
1325     }
1326   }
1327
1328   return 0;
1329 } /* }}} int varnish_config */
1330
1331 void module_register(void) /* {{{ */
1332 {
1333   plugin_register_complex_config("varnish", varnish_config);
1334   plugin_register_init("varnish", varnish_init);
1335 } /* }}} */