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