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