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