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