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