Merge remote-tracking branch 'github/pr/2398'
[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  *   Denes Matetelki <dmatetelki at varnish-software.com>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if HAVE_VARNISH_V4
33 #include <vapi/vsc.h>
34 #include <vapi/vsm.h>
35 typedef struct VSC_C_main c_varnish_stats_t;
36 #endif
37
38 #if HAVE_VARNISH_V3
39 #include <varnishapi.h>
40 #include <vsc.h>
41 typedef struct VSC_C_main c_varnish_stats_t;
42 #endif
43
44 #if HAVE_VARNISH_V2
45 #include <varnishapi.h>
46 typedef struct varnish_stats c_varnish_stats_t;
47 #endif
48
49 /* {{{ user_config_s */
50 struct user_config_s {
51   char *instance;
52
53   _Bool collect_cache;
54   _Bool collect_connections;
55   _Bool collect_esi;
56   _Bool collect_backend;
57 #ifdef HAVE_VARNISH_V3
58   _Bool collect_dirdns;
59 #endif
60   _Bool collect_fetch;
61   _Bool collect_hcb;
62   _Bool collect_objects;
63 #if HAVE_VARNISH_V2
64   _Bool collect_purge;
65 #else
66   _Bool collect_ban;
67 #endif
68   _Bool collect_session;
69   _Bool collect_shm;
70   _Bool collect_sms;
71 #if HAVE_VARNISH_V2
72   _Bool collect_sm;
73 #endif
74 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4
75   _Bool collect_sma;
76 #endif
77   _Bool collect_struct;
78   _Bool collect_totals;
79 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
80   _Bool collect_uptime;
81 #endif
82   _Bool collect_vcl;
83   _Bool collect_workers;
84 #if HAVE_VARNISH_V4
85   _Bool collect_vsm;
86   _Bool collect_lck;
87   _Bool collect_mempool;
88   _Bool collect_mgt;
89   _Bool collect_smf;
90   _Bool collect_vbe;
91 #endif
92 };
93 typedef struct user_config_s user_config_t; /* }}} */
94
95 static _Bool have_instance = 0;
96
97 static int varnish_submit(const char *plugin_instance, /* {{{ */
98                           const char *category, const char *type,
99                           const char *type_instance, value_t value) {
100   value_list_t vl = VALUE_LIST_INIT;
101
102   vl.values = &value;
103   vl.values_len = 1;
104
105   sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
106
107   if (plugin_instance == NULL)
108     plugin_instance = "default";
109   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
110            plugin_instance, category);
111
112   sstrncpy(vl.type, type, sizeof(vl.type));
113
114   if (type_instance != NULL)
115     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
116
117   return plugin_dispatch_values(&vl);
118 } /* }}} int varnish_submit */
119
120 static int varnish_submit_gauge(const char *plugin_instance, /* {{{ */
121                                 const char *category, const char *type,
122                                 const char *type_instance,
123                                 uint64_t gauge_value) {
124   return varnish_submit(plugin_instance, category, type, type_instance,
125                         (value_t){
126                             .gauge = (gauge_t)gauge_value,
127                         });
128 } /* }}} int varnish_submit_gauge */
129
130 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
131                                  const char *category, const char *type,
132                                  const char *type_instance,
133                                  uint64_t derive_value) {
134   return varnish_submit(plugin_instance, category, type, type_instance,
135                         (value_t){
136                             .derive = (derive_t)derive_value,
137                         });
138 } /* }}} int varnish_submit_derive */
139
140 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
141 static int varnish_monitor(void *priv,
142                            const struct VSC_point *const pt) /* {{{ */
143 {
144   uint64_t val;
145   const user_config_t *conf;
146   const char *class;
147   const char *name;
148
149   if (pt == NULL)
150     return 0;
151
152   conf = priv;
153
154 #if HAVE_VARNISH_V4
155   class = pt->section->fantom->type;
156   name = pt->desc->name;
157
158   if (strcmp(class, "MAIN") != 0)
159     return 0;
160
161 #elif HAVE_VARNISH_V3
162   class = pt->class;
163   name = pt->name;
164
165   if (strcmp(class, "") != 0)
166     return 0;
167 #endif
168
169   val = *(const volatile uint64_t *)pt->ptr;
170
171   if (conf->collect_cache) {
172     if (strcmp(name, "cache_hit") == 0)
173       return varnish_submit_derive(conf->instance, "cache", "cache_result",
174                                    "hit", val);
175     else if (strcmp(name, "cache_miss") == 0)
176       return varnish_submit_derive(conf->instance, "cache", "cache_result",
177                                    "miss", val);
178     else if (strcmp(name, "cache_hitpass") == 0)
179       return varnish_submit_derive(conf->instance, "cache", "cache_result",
180                                    "hitpass", val);
181   }
182
183   if (conf->collect_connections) {
184     if (strcmp(name, "client_conn") == 0)
185       return varnish_submit_derive(conf->instance, "connections", "connections",
186                                    "accepted", val);
187     else if (strcmp(name, "client_drop") == 0)
188       return varnish_submit_derive(conf->instance, "connections", "connections",
189                                    "dropped", val);
190     else if (strcmp(name, "client_req") == 0)
191       return varnish_submit_derive(conf->instance, "connections", "connections",
192                                    "received", val);
193 #ifdef HAVE_VARNISH_V4
194     else if (strcmp(name, "client_req_400") == 0)
195       return varnish_submit_derive(conf->instance, "connections", "connections",
196                                    "error_400", val);
197     else if (strcmp(name, "client_req_417") == 0)
198       return varnish_submit_derive(conf->instance, "connections", "connections",
199                                    "error_417", val);
200 #endif
201   }
202
203 #ifdef HAVE_VARNISH_V3
204   if (conf->collect_dirdns) {
205     if (strcmp(name, "dir_dns_lookups") == 0)
206       return varnish_submit_derive(conf->instance, "dirdns", "cache_operation",
207                                    "lookups", val);
208     else if (strcmp(name, "dir_dns_failed") == 0)
209       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
210                                    "failed", val);
211     else if (strcmp(name, "dir_dns_hit") == 0)
212       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
213                                    "hits", val);
214     else if (strcmp(name, "dir_dns_cache_full") == 0)
215       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
216                                    "cache_full", val);
217   }
218 #endif
219
220   if (conf->collect_esi) {
221     if (strcmp(name, "esi_errors") == 0)
222       return varnish_submit_derive(conf->instance, "esi", "total_operations",
223                                    "error", val);
224     else if (strcmp(name, "esi_parse") == 0)
225       return varnish_submit_derive(conf->instance, "esi", "total_operations",
226                                    "parsed", val);
227     else if (strcmp(name, "esi_warnings") == 0)
228       return varnish_submit_derive(conf->instance, "esi", "total_operations",
229                                    "warning", val);
230   }
231
232   if (conf->collect_backend) {
233     if (strcmp(name, "backend_conn") == 0)
234       return varnish_submit_derive(conf->instance, "backend", "connections",
235                                    "success", val);
236     else if (strcmp(name, "backend_unhealthy") == 0)
237       return varnish_submit_derive(conf->instance, "backend", "connections",
238                                    "not-attempted", val);
239     else if (strcmp(name, "backend_busy") == 0)
240       return varnish_submit_derive(conf->instance, "backend", "connections",
241                                    "too-many", val);
242     else if (strcmp(name, "backend_fail") == 0)
243       return varnish_submit_derive(conf->instance, "backend", "connections",
244                                    "failures", val);
245     else if (strcmp(name, "backend_reuse") == 0)
246       return varnish_submit_derive(conf->instance, "backend", "connections",
247                                    "reuses", val);
248     else if (strcmp(name, "backend_toolate") == 0)
249       return varnish_submit_derive(conf->instance, "backend", "connections",
250                                    "was-closed", val);
251     else if (strcmp(name, "backend_recycle") == 0)
252       return varnish_submit_derive(conf->instance, "backend", "connections",
253                                    "recycled", val);
254     else if (strcmp(name, "backend_unused") == 0)
255       return varnish_submit_derive(conf->instance, "backend", "connections",
256                                    "unused", val);
257     else if (strcmp(name, "backend_retry") == 0)
258       return varnish_submit_derive(conf->instance, "backend", "connections",
259                                    "retries", val);
260     else if (strcmp(name, "backend_req") == 0)
261       return varnish_submit_derive(conf->instance, "backend", "http_requests",
262                                    "requests", val);
263     else if (strcmp(name, "n_backend") == 0)
264       return varnish_submit_gauge(conf->instance, "backend", "backends",
265                                   "n_backends", val);
266   }
267
268   if (conf->collect_fetch) {
269     if (strcmp(name, "fetch_head") == 0)
270       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
271                                    "head", val);
272     else if (strcmp(name, "fetch_length") == 0)
273       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
274                                    "length", val);
275     else if (strcmp(name, "fetch_chunked") == 0)
276       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
277                                    "chunked", val);
278     else if (strcmp(name, "fetch_eof") == 0)
279       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
280                                    "eof", val);
281     else if (strcmp(name, "fetch_bad") == 0)
282       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
283                                    "bad_headers", val);
284     else if (strcmp(name, "fetch_close") == 0)
285       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
286                                    "close", val);
287     else if (strcmp(name, "fetch_oldhttp") == 0)
288       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
289                                    "oldhttp", val);
290     else if (strcmp(name, "fetch_zero") == 0)
291       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
292                                    "zero", val);
293     else if (strcmp(name, "fetch_failed") == 0)
294       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
295                                    "failed", val);
296     else if (strcmp(name, "fetch_1xx") == 0)
297       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
298                                    "no_body_1xx", val);
299     else if (strcmp(name, "fetch_204") == 0)
300       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
301                                    "no_body_204", val);
302     else if (strcmp(name, "fetch_304") == 0)
303       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
304                                    "no_body_304", val);
305 #if HAVE_VARNISH_V4
306     else if (strcmp(name, "fetch_no_thread") == 0)
307       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
308                                    "no_thread", val);
309     else if (strcmp(name, "fetch_none") == 0)
310       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
311                                    "none", val);
312     else if (strcmp(name, "busy_sleep") == 0)
313       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
314                                    "busy_sleep", val);
315     else if (strcmp(name, "busy_wakeup") == 0)
316       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
317                                    "busy_wakeup", val);
318 #endif
319   }
320
321   if (conf->collect_hcb) {
322     if (strcmp(name, "hcb_nolock") == 0)
323       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
324                                    "lookup_nolock", val);
325     else if (strcmp(name, "hcb_lock") == 0)
326       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
327                                    "lookup_lock", val);
328     else if (strcmp(name, "hcb_insert") == 0)
329       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
330                                    "insert", val);
331   }
332
333   if (conf->collect_objects) {
334     if (strcmp(name, "n_expired") == 0)
335       return varnish_submit_derive(conf->instance, "objects", "total_objects",
336                                    "expired", val);
337     else if (strcmp(name, "n_lru_nuked") == 0)
338       return varnish_submit_derive(conf->instance, "objects", "total_objects",
339                                    "lru_nuked", val);
340     else if (strcmp(name, "n_lru_saved") == 0)
341       return varnish_submit_derive(conf->instance, "objects", "total_objects",
342                                    "lru_saved", val);
343     else if (strcmp(name, "n_lru_moved") == 0)
344       return varnish_submit_derive(conf->instance, "objects", "total_objects",
345                                    "lru_moved", val);
346     else if (strcmp(name, "n_deathrow") == 0)
347       return varnish_submit_derive(conf->instance, "objects", "total_objects",
348                                    "deathrow", val);
349     else if (strcmp(name, "losthdr") == 0)
350       return varnish_submit_derive(conf->instance, "objects", "total_objects",
351                                    "header_overflow", val);
352     else if (strcmp(name, "n_obj_purged") == 0)
353       return varnish_submit_derive(conf->instance, "objects", "total_objects",
354                                    "purged", val);
355     else if (strcmp(name, "n_objsendfile") == 0)
356       return varnish_submit_derive(conf->instance, "objects", "total_objects",
357                                    "sent_sendfile", val);
358     else if (strcmp(name, "n_objwrite") == 0)
359       return varnish_submit_derive(conf->instance, "objects", "total_objects",
360                                    "sent_write", val);
361     else if (strcmp(name, "n_objoverflow") == 0)
362       return varnish_submit_derive(conf->instance, "objects", "total_objects",
363                                    "workspace_overflow", val);
364 #if HAVE_VARNISH_V4
365     else if (strcmp(name, "exp_mailed") == 0)
366       return varnish_submit_gauge(conf->instance, "struct", "objects",
367                                   "exp_mailed", val);
368     else if (strcmp(name, "exp_received") == 0)
369       return varnish_submit_gauge(conf->instance, "struct", "objects",
370                                   "exp_received", val);
371 #endif
372   }
373
374 #if HAVE_VARNISH_V3
375   if (conf->collect_ban) {
376     if (strcmp(name, "n_ban") == 0)
377       return varnish_submit_derive(conf->instance, "ban", "total_operations",
378                                    "total", val);
379     else if (strcmp(name, "n_ban_add") == 0)
380       return varnish_submit_derive(conf->instance, "ban", "total_operations",
381                                    "added", val);
382     else if (strcmp(name, "n_ban_retire") == 0)
383       return varnish_submit_derive(conf->instance, "ban", "total_operations",
384                                    "deleted", val);
385     else if (strcmp(name, "n_ban_obj_test") == 0)
386       return varnish_submit_derive(conf->instance, "ban", "total_operations",
387                                    "objects_tested", val);
388     else if (strcmp(name, "n_ban_re_test") == 0)
389       return varnish_submit_derive(conf->instance, "ban", "total_operations",
390                                    "regexps_tested", val);
391     else if (strcmp(name, "n_ban_dups") == 0)
392       return varnish_submit_derive(conf->instance, "ban", "total_operations",
393                                    "duplicate", val);
394   }
395 #endif
396 #if HAVE_VARNISH_V4
397   if (conf->collect_ban) {
398     if (strcmp(name, "bans") == 0)
399       return varnish_submit_derive(conf->instance, "ban", "total_operations",
400                                    "total", val);
401     else if (strcmp(name, "bans_added") == 0)
402       return varnish_submit_derive(conf->instance, "ban", "total_operations",
403                                    "added", val);
404     else if (strcmp(name, "bans_obj") == 0)
405       return varnish_submit_derive(conf->instance, "ban", "total_operations",
406                                    "obj", val);
407     else if (strcmp(name, "bans_req") == 0)
408       return varnish_submit_derive(conf->instance, "ban", "total_operations",
409                                    "req", val);
410     else if (strcmp(name, "bans_completed") == 0)
411       return varnish_submit_derive(conf->instance, "ban", "total_operations",
412                                    "completed", val);
413     else if (strcmp(name, "bans_deleted") == 0)
414       return varnish_submit_derive(conf->instance, "ban", "total_operations",
415                                    "deleted", val);
416     else if (strcmp(name, "bans_tested") == 0)
417       return varnish_submit_derive(conf->instance, "ban", "total_operations",
418                                    "tested", val);
419     else if (strcmp(name, "bans_dups") == 0)
420       return varnish_submit_derive(conf->instance, "ban", "total_operations",
421                                    "duplicate", val);
422     else if (strcmp(name, "bans_tested") == 0)
423       return varnish_submit_derive(conf->instance, "ban", "total_operations",
424                                    "tested", val);
425     else if (strcmp(name, "bans_lurker_contention") == 0)
426       return varnish_submit_derive(conf->instance, "ban", "total_operations",
427                                    "lurker_contention", val);
428     else if (strcmp(name, "bans_lurker_obj_killed") == 0)
429       return varnish_submit_derive(conf->instance, "ban", "total_operations",
430                                    "lurker_obj_killed", val);
431     else if (strcmp(name, "bans_lurker_tested") == 0)
432       return varnish_submit_derive(conf->instance, "ban", "total_operations",
433                                    "lurker_tested", val);
434     else if (strcmp(name, "bans_lurker_tests_tested") == 0)
435       return varnish_submit_derive(conf->instance, "ban", "total_operations",
436                                    "lurker_tests_tested", val);
437     else if (strcmp(name, "bans_obj_killed") == 0)
438       return varnish_submit_derive(conf->instance, "ban", "total_operations",
439                                    "obj_killed", val);
440     else if (strcmp(name, "bans_persisted_bytes") == 0)
441       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
442                                    "persisted_bytes", val);
443     else if (strcmp(name, "bans_persisted_fragmentation") == 0)
444       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
445                                    "persisted_fragmentation", val);
446     else if (strcmp(name, "bans_tests_tested") == 0)
447       return varnish_submit_derive(conf->instance, "ban", "total_operations",
448                                    "tests_tested", val);
449
450   }
451 #endif
452
453   if (conf->collect_session) {
454     if (strcmp(name, "sess_closed") == 0)
455       return varnish_submit_derive(conf->instance, "session",
456                                    "total_operations", "closed", val);
457     else if (strcmp(name, "sess_pipeline") == 0)
458       return varnish_submit_derive(conf->instance, "session",
459                                    "total_operations", "pipeline", val);
460     else if (strcmp(name, "sess_readahead") == 0)
461       return varnish_submit_derive(conf->instance, "session",
462                                    "total_operations", "readahead", val);
463     else if (strcmp(name, "sess_conn") == 0)
464       return varnish_submit_derive(conf->instance, "session",
465                                    "total_operations", "accepted", val);
466     else if (strcmp(name, "sess_drop") == 0)
467       return varnish_submit_derive(conf->instance, "session",
468                                    "total_operations", "dropped", val);
469     else if (strcmp(name, "sess_fail") == 0)
470       return varnish_submit_derive(conf->instance, "session",
471                                    "total_operations", "failed", val);
472     else if (strcmp(name, "sess_pipe_overflow") == 0)
473       return varnish_submit_derive(conf->instance, "session",
474                                    "total_operations", "overflow", val);
475     else if (strcmp(name, "sess_queued") == 0)
476       return varnish_submit_derive(conf->instance, "session",
477                                    "total_operations", "queued", val);
478     else if (strcmp(name, "sess_linger") == 0)
479       return varnish_submit_derive(conf->instance, "session",
480                                    "total_operations", "linger", val);
481     else if (strcmp(name, "sess_herd") == 0)
482       return varnish_submit_derive(conf->instance, "session",
483                                    "total_operations", "herd", val);
484 #if HAVE_VARNISH_V4
485     else if (strcmp(name, "sess_closed_err") == 0)
486       return varnish_submit_derive(conf->instance, "session",
487                                    "total_operations", "closed_err", val);
488     else if (strcmp(name, "sess_dropped") == 0)
489       return varnish_submit_derive(conf->instance, "session",
490                                    "total_operations", "dropped_for_thread", val);
491 #endif
492   }
493
494   if (conf->collect_shm) {
495     if (strcmp(name, "shm_records") == 0)
496       return varnish_submit_derive(conf->instance, "shm", "total_operations",
497                                    "records", val);
498     else if (strcmp(name, "shm_writes") == 0)
499       return varnish_submit_derive(conf->instance, "shm", "total_operations",
500                                    "writes", val);
501     else if (strcmp(name, "shm_flushes") == 0)
502       return varnish_submit_derive(conf->instance, "shm", "total_operations",
503                                    "flushes", val);
504     else if (strcmp(name, "shm_cont") == 0)
505       return varnish_submit_derive(conf->instance, "shm", "total_operations",
506                                    "contention", val);
507     else if (strcmp(name, "shm_cycles") == 0)
508       return varnish_submit_derive(conf->instance, "shm", "total_operations",
509                                    "cycles", val);
510   }
511
512   if (conf->collect_sms) {
513     if (strcmp(name, "sms_nreq") == 0)
514       return varnish_submit_derive(conf->instance, "sms", "total_requests",
515                                    "allocator", val);
516     else if (strcmp(name, "sms_nobj") == 0)
517       return varnish_submit_gauge(conf->instance, "sms", "requests",
518                                   "outstanding", val);
519     else if (strcmp(name, "sms_nbytes") == 0)
520       return varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
521                                   val);
522     else if (strcmp(name, "sms_balloc") == 0)
523       return varnish_submit_derive(conf->instance, "sms", "total_bytes",
524                                    "allocated", val);
525     else if (strcmp(name, "sms_bfree") == 0)
526       return varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
527                                    val);
528   }
529
530   if (conf->collect_struct) {
531     if (strcmp(name, "n_sess_mem") == 0)
532       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
533                                   "sess_mem", val);
534     else if (strcmp(name, "n_sess") == 0)
535       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
536                                   "sess", val);
537     else if (strcmp(name, "n_object") == 0)
538       return varnish_submit_gauge(conf->instance, "struct", "objects", "object",
539                                   val);
540     else if (strcmp(name, "n_vampireobject") == 0)
541       return varnish_submit_gauge(conf->instance, "struct", "objects",
542                                   "vampireobject", val);
543     else if (strcmp(name, "n_objectcore") == 0)
544       return varnish_submit_gauge(conf->instance, "struct", "objects",
545                                   "objectcore", val);
546     else if (strcmp(name, "n_waitinglist") == 0)
547       return varnish_submit_gauge(conf->instance, "struct", "objects",
548                                   "waitinglist", val);
549     else if (strcmp(name, "n_objecthead") == 0)
550       return varnish_submit_gauge(conf->instance, "struct", "objects",
551                                   "objecthead", val);
552     else if (strcmp(name, "n_smf") == 0)
553       return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
554                                   val);
555     else if (strcmp(name, "n_smf_frag") == 0)
556       return varnish_submit_gauge(conf->instance, "struct", "objects",
557                                   "smf_frag", val);
558     else if (strcmp(name, "n_smf_large") == 0)
559       return varnish_submit_gauge(conf->instance, "struct", "objects",
560                                   "smf_large", val);
561     else if (strcmp(name, "n_vbe_conn") == 0)
562       return varnish_submit_gauge(conf->instance, "struct", "objects",
563                                   "vbe_conn", val);
564   }
565
566   if (conf->collect_totals) {
567     if (strcmp(name, "s_sess") == 0)
568       return varnish_submit_derive(conf->instance, "totals", "total_sessions",
569                                    "sessions", val);
570     else if (strcmp(name, "s_req") == 0)
571       return varnish_submit_derive(conf->instance, "totals", "total_requests",
572                                    "requests", val);
573     else if (strcmp(name, "s_pipe") == 0)
574       return varnish_submit_derive(conf->instance, "totals", "total_operations",
575                                    "pipe", val);
576     else if (strcmp(name, "s_pass") == 0)
577       return varnish_submit_derive(conf->instance, "totals", "total_operations",
578                                    "pass", val);
579     else if (strcmp(name, "s_fetch") == 0)
580       return varnish_submit_derive(conf->instance, "totals", "total_operations",
581                                    "fetches", val);
582     else if (strcmp(name, "s_synth") == 0)
583       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
584                                    "synth", val);
585     else if (strcmp(name, "s_req_hdrbytes") == 0)
586       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
587                                    "req_header", val);
588     else if (strcmp(name, "s_req_bodybytes") == 0)
589       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
590                                    "req_body", val);
591     else if (strcmp(name, "s_resp_hdrbytes") == 0)
592       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
593                                    "resp_header", val);
594     else if (strcmp(name, "s_resp_bodybytes") == 0)
595       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
596                                    "resp_body", val);
597     else if (strcmp(name, "s_pipe_hdrbytes") == 0)
598       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
599                                    "pipe_header", val);
600     else if (strcmp(name, "s_pipe_in") == 0)
601       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
602                                    "pipe_in", val);
603     else if (strcmp(name, "s_pipe_out") == 0)
604       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
605                                    "pipe_out", val);
606     else if (strcmp(name, "n_purges") == 0)
607       return varnish_submit_derive(conf->instance, "totals", "total_operations",
608                                    "purges", val);
609     else if (strcmp(name, "s_hdrbytes") == 0)
610       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
611                                    "header-bytes", val);
612     else if (strcmp(name, "s_bodybytes") == 0)
613       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
614                                    "body-bytes", val);
615     else if (strcmp(name, "n_gzip") == 0)
616       return varnish_submit_derive(conf->instance, "totals", "total_operations",
617                                    "gzip", val);
618     else if (strcmp(name, "n_gunzip") == 0)
619       return varnish_submit_derive(conf->instance, "totals", "total_operations",
620                                    "gunzip", val);
621   }
622
623   if (conf->collect_uptime) {
624     if (strcmp(name, "uptime") == 0)
625       return varnish_submit_gauge(conf->instance, "uptime", "uptime",
626                                   "client_uptime", val);
627   }
628
629   if (conf->collect_vcl) {
630     if (strcmp(name, "n_vcl") == 0)
631       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
632                                   val);
633     else if (strcmp(name, "n_vcl_avail") == 0)
634       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
635                                   val);
636     else if (strcmp(name, "n_vcl_discard") == 0)
637       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
638                                   val);
639     else if (strcmp(name, "vmods") == 0)
640       return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
641                                   val);
642   }
643
644   if (conf->collect_workers) {
645     if (strcmp(name, "threads") == 0)
646       return varnish_submit_gauge(conf->instance, "workers", "threads",
647                                   "worker", val);
648     else if (strcmp(name, "threads_created") == 0)
649       return varnish_submit_derive(conf->instance, "workers", "total_threads",
650                                    "created", val);
651     else if (strcmp(name, "threads_failed") == 0)
652       return varnish_submit_derive(conf->instance, "workers", "total_threads",
653                                    "failed", val);
654     else if (strcmp(name, "threads_limited") == 0)
655       return varnish_submit_derive(conf->instance, "workers", "total_threads",
656                                    "limited", val);
657     else if (strcmp(name, "threads_destroyed") == 0)
658       return varnish_submit_derive(conf->instance, "workers", "total_threads",
659                                    "dropped", val);
660     else if (strcmp(name, "thread_queue_len") == 0)
661       return varnish_submit_gauge(conf->instance, "workers", "queue_length",
662                                    "threads", val);
663     else if (strcmp(name, "n_wrk") == 0)
664       return varnish_submit_gauge(conf->instance, "workers", "threads",
665                                   "worker", val);
666     else if (strcmp(name, "n_wrk_create") == 0)
667       return varnish_submit_derive(conf->instance, "workers", "total_threads",
668                                    "created", val);
669     else if (strcmp(name, "n_wrk_failed") == 0)
670       return varnish_submit_derive(conf->instance, "workers", "total_threads",
671                                    "failed", val);
672     else if (strcmp(name, "n_wrk_max") == 0)
673       return varnish_submit_derive(conf->instance, "workers", "total_threads",
674                                    "limited", val);
675     else if (strcmp(name, "n_wrk_drop") == 0)
676       return varnish_submit_derive(conf->instance, "workers", "total_threads",
677                                    "dropped", val);
678     else if (strcmp(name, "n_wrk_queue") == 0)
679       return varnish_submit_derive(conf->instance, "workers", "total_requests",
680                                    "queued", val);
681     else if (strcmp(name, "n_wrk_overflow") == 0)
682       return varnish_submit_derive(conf->instance, "workers", "total_requests",
683                                    "overflowed", val);
684     else if (strcmp(name, "n_wrk_queued") == 0)
685       return varnish_submit_derive(conf->instance, "workers", "total_requests",
686                                    "queued", val);
687     else if (strcmp(name, "n_wrk_lqueue") == 0)
688       return varnish_submit_derive(conf->instance, "workers", "total_requests",
689                                    "queue_length", val);
690 #if HAVE_VARNISH_V4
691     else if (strcmp(name, "pools") == 0)
692       return varnish_submit_gauge(conf->instance, "workers", "pools",
693                                   "pools", val);
694     else if (strcmp(name, "busy_killed") == 0)
695       return varnish_submit_derive(conf->instance, "workers", "http_requests",
696                                    "busy_killed", val);
697
698 #endif
699   }
700
701 #if HAVE_VARNISH_V4
702   if (conf->collect_vsm) {
703     if (strcmp(name, "vsm_free") == 0)
704       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
705     else if (strcmp(name, "vsm_used") == 0)
706       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
707     else if (strcmp(name, "vsm_cooling") == 0)
708       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
709                                   val);
710     else if (strcmp(name, "vsm_overflow") == 0)
711       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
712                                   val);
713     else if (strcmp(name, "vsm_overflowed") == 0)
714       return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
715                                    "overflowed", val);
716   }
717
718   if (conf->collect_vbe) {
719     /* @TODO figure out the collectd type for bitmap
720     if (strcmp(name, "happy") == 0)
721       return varnish_submit_derive(conf->instance, "vbe",
722                                    "bitmap", "happy_hprobes", val);
723     */
724     if (strcmp(name, "bereq_hdrbytes") == 0)
725       return varnish_submit_derive(conf->instance, "vbe",
726                                    "total_bytes", "bereq_hdrbytes", val);
727     else if (strcmp(name, "bereq_bodybytes") == 0)
728       return varnish_submit_derive(conf->instance, "vbe",
729                                    "total_bytes", "bereq_bodybytes", val);
730     else if (strcmp(name, "beresp_hdrbytes") == 0)
731       return varnish_submit_derive(conf->instance, "vbe",
732                                    "total_bytes", "beresp_hdrbytes", val);
733     else if (strcmp(name, "beresp_bodybytes") == 0)
734       return varnish_submit_derive(conf->instance, "vbe",
735                                    "total_bytes", "beresp_bodybytes", val);
736     else if (strcmp(name, "pipe_hdrbytes") == 0)
737       return varnish_submit_derive(conf->instance, "vbe",
738                                    "total_bytes", "pipe_hdrbytes", val);
739     else if (strcmp(name, "pipe_out") == 0)
740       return varnish_submit_derive(conf->instance, "vbe",
741                                    "total_bytes", "pipe_out", val);
742     else if (strcmp(name, "pipe_in") == 0)
743       return varnish_submit_derive(conf->instance, "vbe",
744                                    "total_bytes", "pipe_in", val);
745     else if (strcmp(name, "conn") == 0)
746       return varnish_submit_derive(conf->instance, "vbe", "connections",
747                                   "c_conns", val);
748     else if (strcmp(name, "req") == 0)
749       return varnish_submit_derive(conf->instance, "vbe", "http_requests",
750                                   "b_reqs", val);
751   }
752
753   /* All Stevedores support these counters */
754   if (conf->collect_sma || conf->collect_smf) {
755
756     char category[4];
757     if (conf->collect_sma)
758       strncpy(category, "sma", 4);
759     else
760       strncpy(category, "smf", 4);
761
762     if (strcmp(name, "c_req") == 0)
763       return varnish_submit_derive(conf->instance, category,
764                                    "total_operations", "alloc_req", val);
765     else if (strcmp(name, "c_fail") == 0)
766       return varnish_submit_derive(conf->instance, category,
767                                    "total_operations", "alloc_fail", val);
768     else if (strcmp(name, "c_bytes") == 0)
769       return varnish_submit_derive(conf->instance, category,
770                                    "total_bytes", "bytes_allocated", val);
771     else if (strcmp(name, "c_freed") == 0)
772       return varnish_submit_derive(conf->instance, category,
773                                    "total_bytes", "bytes_freed", val);
774     else if (strcmp(name, "g_alloc") == 0)
775       return varnish_submit_derive(conf->instance, category,
776                                   "total_operations", "alloc_outstanding", val);
777     else if (strcmp(name, "g_bytes") == 0)
778       return varnish_submit_gauge(conf->instance, category, "bytes",
779                                   "bytes_outstanding", val);
780     else if (strcmp(name, "g_space") == 0)
781       return varnish_submit_gauge(conf->instance, category, "bytes",
782                                   "bytes_available", val);
783   }
784
785   /* No SMA specific counters */
786
787   if (conf->collect_smf) {
788     if (strcmp(name, "g_smf") == 0)
789         return varnish_submit_gauge(conf->instance, "smf", "objects",
790                                     "n_struct_smf", val);
791       else if (strcmp(name, "g_smf_frag") == 0)
792         return varnish_submit_gauge(conf->instance, "smf", "objects",
793                                     "n_small_free_smf", val);
794       else if (strcmp(name, "g_smf_large") == 0)
795         return varnish_submit_gauge(conf->instance, "smf", "objects",
796                                     "n_large_free_smf", val);
797   }
798
799   if (conf->collect_mgt) {
800     if (strcmp(name, "uptime") == 0)
801       return varnish_submit_gauge(conf->instance, "mgt", "uptime",
802                                   "mgt_proc_uptime", val);
803     else if (strcmp(name, "child_start") == 0)
804       return varnish_submit_derive(conf->instance, "mgt",
805                                    "total_operations", "child_start", val);
806     else if (strcmp(name, "child_exit") == 0)
807       return varnish_submit_derive(conf->instance, "mgt",
808                                    "total_operations", "child_exit", val);
809     else if (strcmp(name, "child_stop") == 0)
810       return varnish_submit_derive(conf->instance, "mgt",
811                                    "total_operations", "child_stop", val);
812     else if (strcmp(name, "child_died") == 0)
813       return varnish_submit_derive(conf->instance, "mgt",
814                                    "total_operations", "child_died", val);
815     else if (strcmp(name, "child_dump") == 0)
816       return varnish_submit_derive(conf->instance, "mgt",
817                                    "total_operations", "child_dump", val);
818     else if (strcmp(name, "child_panic") == 0)
819       return varnish_submit_derive(conf->instance, "mgt",
820                                    "total_operations", "child_panic", val);
821   }
822
823   if (conf->collect_lck) {
824     if (strcmp(name, "creat") == 0)
825       return varnish_submit_gauge(conf->instance, "lck", "objects",
826                                   "created", val);
827     else if (strcmp(name, "destroy") == 0)
828       return varnish_submit_gauge(conf->instance, "lck", "objects",
829                                   "destroyed", val);
830     else if (strcmp(name, "locks") == 0)
831       return varnish_submit_derive(conf->instance, "lck", "total_operations",
832                                   "lock_ops", val);
833   }
834
835   if (conf->collect_mempool) {
836     if (strcmp(name, "live") == 0)
837       return varnish_submit_gauge(conf->instance, "mempool", "objects",
838                                   "in_use", val);
839     else if (strcmp(name, "pool") == 0)
840       return varnish_submit_gauge(conf->instance, "mempool", "objects",
841                                   "in_pool", val);
842     else if (strcmp(name, "sz_wanted") == 0)
843       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
844                                   "size_requested", val);
845     else if (strcmp(name, "sz_actual") == 0)
846       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
847                                   "size_allocated", val);
848     else if (strcmp(name, "allocs") == 0)
849       return varnish_submit_derive(conf->instance, "mempool",
850                                    "total_operations", "allocations", val);
851     else if (strcmp(name, "frees") == 0)
852       return varnish_submit_derive(conf->instance, "mempool",
853                                    "total_operations", "frees", val);
854     else if (strcmp(name, "recycle") == 0)
855       return varnish_submit_gauge(conf->instance, "mempool",
856                                    "objects", "recycled", val);
857     else if (strcmp(name, "timeout") == 0)
858       return varnish_submit_gauge(conf->instance, "mempool",
859                                    "objects", "timed_out", val);
860     else if (strcmp(name, "toosmall") == 0)
861       return varnish_submit_gauge(conf->instance, "mempool",
862                                    "objects", "too_small", val);
863     else if (strcmp(name, "surplus") == 0)
864       return varnish_submit_gauge(conf->instance, "mempool",
865                                    "objects", "surplus", val);
866     else if (strcmp(name, "randry") == 0)
867       return varnish_submit_gauge(conf->instance, "mempool",
868                                    "objects", "ran_dry", val);
869   }
870 #endif
871
872   return 0;
873
874 } /* }}} static int varnish_monitor */
875 #else /* if HAVE_VARNISH_V2 */
876 static void varnish_monitor(const user_config_t *conf, /* {{{ */
877                             const c_varnish_stats_t *stats) {
878   if (conf->collect_cache) {
879     /* Cache hits */
880     varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
881                           stats->cache_hit);
882     /* Cache misses */
883     varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
884                           stats->cache_miss);
885     /* Cache hits for pass */
886     varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
887                           stats->cache_hitpass);
888   }
889
890   if (conf->collect_connections) {
891     /* Client connections accepted */
892     varnish_submit_derive(conf->instance, "connections", "connections",
893                           "accepted", stats->client_conn);
894     /* Connection dropped, no sess */
895     varnish_submit_derive(conf->instance, "connections", "connections",
896                           "dropped", stats->client_drop);
897     /* Client requests received    */
898     varnish_submit_derive(conf->instance, "connections", "connections",
899                           "received", stats->client_req);
900   }
901
902   if (conf->collect_esi) {
903     /* ESI parse errors (unlock)   */
904     varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
905                           stats->esi_errors);
906     /* Objects ESI parsed (unlock) */
907     varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
908                           stats->esi_parse);
909   }
910
911   if (conf->collect_backend) {
912     /* Backend conn. success       */
913     varnish_submit_derive(conf->instance, "backend", "connections", "success",
914                           stats->backend_conn);
915     /* Backend conn. not attempted */
916     varnish_submit_derive(conf->instance, "backend", "connections",
917                           "not-attempted", stats->backend_unhealthy);
918     /* Backend conn. too many      */
919     varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
920                           stats->backend_busy);
921     /* Backend conn. failures      */
922     varnish_submit_derive(conf->instance, "backend", "connections", "failures",
923                           stats->backend_fail);
924     /* Backend conn. reuses        */
925     varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
926                           stats->backend_reuse);
927     /* Backend conn. was closed    */
928     varnish_submit_derive(conf->instance, "backend", "connections",
929                           "was-closed", stats->backend_toolate);
930     /* Backend conn. recycles      */
931     varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
932                           stats->backend_recycle);
933     /* Backend conn. unused        */
934     varnish_submit_derive(conf->instance, "backend", "connections", "unused",
935                           stats->backend_unused);
936     /* Backend requests mades      */
937     varnish_submit_derive(conf->instance, "backend", "http_requests",
938                           "requests", stats->backend_req);
939     /* N backends                  */
940     varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
941                          stats->n_backend);
942   }
943
944   if (conf->collect_fetch) {
945     /* Fetch head                */
946     varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
947                           stats->fetch_head);
948     /* Fetch with length         */
949     varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
950                           stats->fetch_length);
951     /* Fetch chunked             */
952     varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
953                           stats->fetch_chunked);
954     /* Fetch EOF                 */
955     varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
956                           stats->fetch_eof);
957     /* Fetch bad headers         */
958     varnish_submit_derive(conf->instance, "fetch", "http_requests",
959                           "bad_headers", stats->fetch_bad);
960     /* Fetch wanted close        */
961     varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
962                           stats->fetch_close);
963     /* Fetch pre HTTP/1.1 closed */
964     varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
965                           stats->fetch_oldhttp);
966     /* Fetch zero len            */
967     varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
968                           stats->fetch_zero);
969     /* Fetch failed              */
970     varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
971                           stats->fetch_failed);
972   }
973
974   if (conf->collect_hcb) {
975     /* HCB Lookups without lock */
976     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
977                           "lookup_nolock", stats->hcb_nolock);
978     /* HCB Lookups with lock    */
979     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
980                           "lookup_lock", stats->hcb_lock);
981     /* HCB Inserts              */
982     varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
983                           stats->hcb_insert);
984   }
985
986   if (conf->collect_objects) {
987     /* N expired objects             */
988     varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
989                           stats->n_expired);
990     /* N LRU nuked objects           */
991     varnish_submit_derive(conf->instance, "objects", "total_objects",
992                           "lru_nuked", stats->n_lru_nuked);
993     /* N LRU saved objects           */
994     varnish_submit_derive(conf->instance, "objects", "total_objects",
995                           "lru_saved", stats->n_lru_saved);
996     /* N LRU moved objects           */
997     varnish_submit_derive(conf->instance, "objects", "total_objects",
998                           "lru_moved", stats->n_lru_moved);
999     /* N objects on deathrow         */
1000     varnish_submit_derive(conf->instance, "objects", "total_objects",
1001                           "deathrow", stats->n_deathrow);
1002     /* HTTP header overflows         */
1003     varnish_submit_derive(conf->instance, "objects", "total_objects",
1004                           "header_overflow", stats->losthdr);
1005     /* Objects sent with sendfile    */
1006     varnish_submit_derive(conf->instance, "objects", "total_objects",
1007                           "sent_sendfile", stats->n_objsendfile);
1008     /* Objects sent with write       */
1009     varnish_submit_derive(conf->instance, "objects", "total_objects",
1010                           "sent_write", stats->n_objwrite);
1011     /* Objects overflowing workspace */
1012     varnish_submit_derive(conf->instance, "objects", "total_objects",
1013                           "workspace_overflow", stats->n_objoverflow);
1014   }
1015
1016   if (conf->collect_purge) {
1017     /* N total active purges      */
1018     varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
1019                           stats->n_purge);
1020     /* N new purges added         */
1021     varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
1022                           stats->n_purge_add);
1023     /* N old purges deleted       */
1024     varnish_submit_derive(conf->instance, "purge", "total_operations",
1025                           "deleted", stats->n_purge_retire);
1026     /* N objects tested           */
1027     varnish_submit_derive(conf->instance, "purge", "total_operations",
1028                           "objects_tested", stats->n_purge_obj_test);
1029     /* N regexps tested against   */
1030     varnish_submit_derive(conf->instance, "purge", "total_operations",
1031                           "regexps_tested", stats->n_purge_re_test);
1032     /* N duplicate purges removed */
1033     varnish_submit_derive(conf->instance, "purge", "total_operations",
1034                           "duplicate", stats->n_purge_dups);
1035   }
1036
1037   if (conf->collect_session) {
1038     /* Session Closed     */
1039     varnish_submit_derive(conf->instance, "session", "total_operations",
1040                           "closed", stats->sess_closed);
1041     /* Session Pipeline   */
1042     varnish_submit_derive(conf->instance, "session", "total_operations",
1043                           "pipeline", stats->sess_pipeline);
1044     /* Session Read Ahead */
1045     varnish_submit_derive(conf->instance, "session", "total_operations",
1046                           "readahead", stats->sess_readahead);
1047     /* Session Linger     */
1048     varnish_submit_derive(conf->instance, "session", "total_operations",
1049                           "linger", stats->sess_linger);
1050     /* Session herd       */
1051     varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
1052                           stats->sess_herd);
1053   }
1054
1055   if (conf->collect_shm) {
1056     /* SHM records                 */
1057     varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
1058                           stats->shm_records);
1059     /* SHM writes                  */
1060     varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
1061                           stats->shm_writes);
1062     /* SHM flushes due to overflow */
1063     varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
1064                           stats->shm_flushes);
1065     /* SHM MTX contention          */
1066     varnish_submit_derive(conf->instance, "shm", "total_operations",
1067                           "contention", stats->shm_cont);
1068     /* SHM cycles through buffer   */
1069     varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
1070                           stats->shm_cycles);
1071   }
1072
1073   if (conf->collect_sm) {
1074     /* allocator requests */
1075     varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
1076                           stats->sm_nreq);
1077     /* outstanding allocations */
1078     varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
1079                          stats->sm_nobj);
1080     /* bytes allocated */
1081     varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
1082                           stats->sm_balloc);
1083     /* bytes free */
1084     varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
1085                           stats->sm_bfree);
1086   }
1087
1088   if (conf->collect_sma) {
1089     /* SMA allocator requests */
1090     varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
1091                           stats->sma_nreq);
1092     /* SMA outstanding allocations */
1093     varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
1094                          stats->sma_nobj);
1095     /* SMA outstanding bytes */
1096     varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
1097                          stats->sma_nbytes);
1098     /* SMA bytes allocated */
1099     varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
1100                           stats->sma_balloc);
1101     /* SMA bytes free */
1102     varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
1103                           stats->sma_bfree);
1104   }
1105
1106   if (conf->collect_sms) {
1107     /* SMS allocator requests */
1108     varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
1109                           stats->sms_nreq);
1110     /* SMS outstanding allocations */
1111     varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
1112                          stats->sms_nobj);
1113     /* SMS outstanding bytes */
1114     varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
1115                          stats->sms_nbytes);
1116     /* SMS bytes allocated */
1117     varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
1118                           stats->sms_balloc);
1119     /* SMS bytes freed */
1120     varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
1121                           stats->sms_bfree);
1122   }
1123
1124   if (conf->collect_struct) {
1125     /* N struct sess_mem       */
1126     varnish_submit_gauge(conf->instance, "struct", "current_sessions",
1127                          "sess_mem", stats->n_sess_mem);
1128     /* N struct sess           */
1129     varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
1130                          stats->n_sess);
1131     /* N struct object         */
1132     varnish_submit_gauge(conf->instance, "struct", "objects", "object",
1133                          stats->n_object);
1134     /* N struct objecthead     */
1135     varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
1136                          stats->n_objecthead);
1137     /* N struct smf            */
1138     varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
1139                          stats->n_smf);
1140     /* N small free smf         */
1141     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
1142                          stats->n_smf_frag);
1143     /* N large free smf         */
1144     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
1145                          stats->n_smf_large);
1146     /* N struct vbe_conn        */
1147     varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
1148                          stats->n_vbe_conn);
1149   }
1150
1151   if (conf->collect_totals) {
1152     /* Total Sessions */
1153     varnish_submit_derive(conf->instance, "totals", "total_sessions",
1154                           "sessions", stats->s_sess);
1155     /* Total Requests */
1156     varnish_submit_derive(conf->instance, "totals", "total_requests",
1157                           "requests", stats->s_req);
1158     /* Total pipe */
1159     varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
1160                           stats->s_pipe);
1161     /* Total pass */
1162     varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
1163                           stats->s_pass);
1164     /* Total fetch */
1165     varnish_submit_derive(conf->instance, "totals", "total_operations",
1166                           "fetches", stats->s_fetch);
1167     /* Total header bytes */
1168     varnish_submit_derive(conf->instance, "totals", "total_bytes",
1169                           "header-bytes", stats->s_hdrbytes);
1170     /* Total body byte */
1171     varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
1172                           stats->s_bodybytes);
1173   }
1174
1175   if (conf->collect_vcl) {
1176     /* N vcl total     */
1177     varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
1178                          stats->n_vcl);
1179     /* N vcl available */
1180     varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
1181                          stats->n_vcl_avail);
1182     /* N vcl discarded */
1183     varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
1184                          stats->n_vcl_discard);
1185   }
1186
1187   if (conf->collect_workers) {
1188     /* worker threads */
1189     varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
1190                          stats->n_wrk);
1191     /* worker threads created */
1192     varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
1193                           stats->n_wrk_create);
1194     /* worker threads not created */
1195     varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
1196                           stats->n_wrk_failed);
1197     /* worker threads limited */
1198     varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
1199                           stats->n_wrk_max);
1200     /* dropped work requests */
1201     varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
1202                           stats->n_wrk_drop);
1203     /* queued work requests */
1204     varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
1205                           stats->n_wrk_queue);
1206     /* overflowed work requests */
1207     varnish_submit_derive(conf->instance, "workers", "total_requests",
1208                           "overflowed", stats->n_wrk_overflow);
1209   }
1210
1211 } /* }}} void varnish_monitor */
1212 #endif
1213
1214 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1215 static int varnish_read(user_data_t *ud) /* {{{ */
1216 {
1217   struct VSM_data *vd;
1218   const c_varnish_stats_t *stats;
1219   _Bool ok;
1220
1221   user_config_t *conf;
1222
1223   if ((ud == NULL) || (ud->data == NULL))
1224     return EINVAL;
1225
1226   conf = ud->data;
1227
1228   vd = VSM_New();
1229 #if HAVE_VARNISH_V3
1230   VSC_Setup(vd);
1231 #endif
1232
1233   if (conf->instance != NULL) {
1234     int status;
1235
1236     status = VSM_n_Arg(vd, conf->instance);
1237     if (status < 0) {
1238       VSM_Delete(vd);
1239       ERROR("varnish plugin: VSM_n_Arg (\"%s\") failed "
1240             "with status %i.",
1241             conf->instance, status);
1242       return -1;
1243     }
1244   }
1245
1246 #if HAVE_VARNISH_V3
1247   ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1248 #else /* if HAVE_VARNISH_V4 */
1249   ok = (VSM_Open(vd) == 0);
1250 #endif
1251   if (!ok) {
1252     VSM_Delete(vd);
1253     ERROR("varnish plugin: Unable to open connection.");
1254
1255     return -1;
1256   }
1257
1258 #if HAVE_VARNISH_V3
1259   stats = VSC_Main(vd);
1260 #else /* if HAVE_VARNISH_V4 */
1261   stats = VSC_Main(vd, NULL);
1262 #endif
1263   if (!stats) {
1264     VSM_Delete(vd);
1265     ERROR("varnish plugin: Unable to get statistics.");
1266
1267     return -1;
1268   }
1269
1270 #if HAVE_VARNISH_V3
1271   VSC_Iter(vd, varnish_monitor, conf);
1272 #else /* if HAVE_VARNISH_V4 */
1273   VSC_Iter(vd, NULL, varnish_monitor, conf);
1274 #endif
1275   VSM_Delete(vd);
1276
1277   return 0;
1278 } /* }}} */
1279 #else /* if HAVE_VARNISH_V2 */
1280 static int varnish_read(user_data_t *ud) /* {{{ */
1281 {
1282   const c_varnish_stats_t *stats;
1283
1284   user_config_t *conf;
1285
1286   if ((ud == NULL) || (ud->data == NULL))
1287     return EINVAL;
1288
1289   conf = ud->data;
1290
1291   stats = VSL_OpenStats(conf->instance);
1292   if (stats == NULL) {
1293     ERROR("Varnish plugin : unable to load statistics");
1294
1295     return -1;
1296   }
1297
1298   varnish_monitor(conf, stats);
1299
1300   return 0;
1301 } /* }}} */
1302 #endif
1303
1304 static void varnish_config_free(void *ptr) /* {{{ */
1305 {
1306   user_config_t *conf = ptr;
1307
1308   if (conf == NULL)
1309     return;
1310
1311   sfree(conf->instance);
1312   sfree(conf);
1313 } /* }}} */
1314
1315 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1316 {
1317   if (conf == NULL)
1318     return EINVAL;
1319
1320   conf->collect_backend = 1;
1321   conf->collect_cache = 1;
1322   conf->collect_connections = 1;
1323 #ifdef HAVE_VARNISH_V3
1324   conf->collect_dirdns = 0;
1325 #endif
1326   conf->collect_esi = 0;
1327   conf->collect_fetch = 0;
1328   conf->collect_hcb = 0;
1329   conf->collect_objects = 0;
1330 #if HAVE_VARNISH_V2
1331   conf->collect_purge = 0;
1332 #else
1333   conf->collect_ban = 0;
1334 #endif
1335   conf->collect_session = 0;
1336   conf->collect_shm = 1;
1337 #if HAVE_VARNISH_V2
1338   conf->collect_sm = 0;
1339 #endif
1340 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4
1341   conf->collect_sma = 0;
1342 #endif
1343   conf->collect_sms = 0;
1344   conf->collect_struct = 0;
1345   conf->collect_totals = 0;
1346 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1347   conf->collect_uptime = 0;
1348 #endif
1349   conf->collect_vcl = 0;
1350   conf->collect_workers = 0;
1351 #if HAVE_VARNISH_V4
1352   conf->collect_vsm = 0;
1353   conf->collect_lck = 0;
1354   conf->collect_mempool = 0;
1355   conf->collect_mgt = 0;
1356   conf->collect_smf = 0;
1357   conf->collect_vbe = 0;
1358 #endif
1359
1360   return 0;
1361 } /* }}} int varnish_config_apply_default */
1362
1363 static int varnish_init(void) /* {{{ */
1364 {
1365   user_config_t *conf;
1366
1367   if (have_instance)
1368     return 0;
1369
1370   conf = calloc(1, sizeof(*conf));
1371   if (conf == NULL)
1372     return ENOMEM;
1373
1374   /* Default settings: */
1375   conf->instance = NULL;
1376
1377   varnish_config_apply_default(conf);
1378
1379   plugin_register_complex_read(
1380       /* group = */ "varnish",
1381       /* name      = */ "varnish/localhost",
1382       /* callback  = */ varnish_read,
1383       /* interval  = */ 0,
1384       &(user_data_t){
1385           .data = conf, .free_func = varnish_config_free,
1386       });
1387
1388   return 0;
1389 } /* }}} int varnish_init */
1390
1391 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1392 {
1393   user_config_t *conf;
1394   char callback_name[DATA_MAX_NAME_LEN];
1395
1396   conf = calloc(1, sizeof(*conf));
1397   if (conf == NULL)
1398     return ENOMEM;
1399   conf->instance = NULL;
1400
1401   varnish_config_apply_default(conf);
1402
1403   if (ci->values_num == 1) {
1404     int status;
1405
1406     status = cf_util_get_string(ci, &conf->instance);
1407     if (status != 0) {
1408       sfree(conf);
1409       return status;
1410     }
1411     assert(conf->instance != NULL);
1412
1413     if (strcmp("localhost", conf->instance) == 0) {
1414       sfree(conf->instance);
1415       conf->instance = NULL;
1416     }
1417   } else if (ci->values_num > 1) {
1418     WARNING("Varnish plugin: \"Instance\" blocks accept only "
1419             "one argument.");
1420     sfree(conf);
1421     return EINVAL;
1422   }
1423
1424   for (int i = 0; i < ci->children_num; i++) {
1425     oconfig_item_t *child = ci->children + i;
1426
1427     if (strcasecmp("CollectCache", child->key) == 0)
1428       cf_util_get_boolean(child, &conf->collect_cache);
1429     else if (strcasecmp("CollectConnections", child->key) == 0)
1430       cf_util_get_boolean(child, &conf->collect_connections);
1431     else if (strcasecmp("CollectESI", child->key) == 0)
1432       cf_util_get_boolean(child, &conf->collect_esi);
1433     else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1434 #ifdef HAVE_VARNISH_V3
1435       cf_util_get_boolean(child, &conf->collect_dirdns);
1436 #else
1437       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1438               child->key, "v3");
1439 #endif
1440     else if (strcasecmp("CollectBackend", child->key) == 0)
1441       cf_util_get_boolean(child, &conf->collect_backend);
1442     else if (strcasecmp("CollectFetch", child->key) == 0)
1443       cf_util_get_boolean(child, &conf->collect_fetch);
1444     else if (strcasecmp("CollectHCB", child->key) == 0)
1445       cf_util_get_boolean(child, &conf->collect_hcb);
1446     else if (strcasecmp("CollectObjects", child->key) == 0)
1447       cf_util_get_boolean(child, &conf->collect_objects);
1448     else if (strcasecmp("CollectPurge", child->key) == 0)
1449 #if HAVE_VARNISH_V2
1450       cf_util_get_boolean(child, &conf->collect_purge);
1451 #else
1452       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1453               child->key, "v2");
1454 #endif
1455     else if (strcasecmp("CollectBan", child->key) == 0)
1456 #if HAVE_VARNISH_V2
1457       WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1458               child->key, "v2");
1459 #else
1460       cf_util_get_boolean(child, &conf->collect_ban);
1461 #endif
1462     else if (strcasecmp("CollectSession", child->key) == 0)
1463       cf_util_get_boolean(child, &conf->collect_session);
1464     else if (strcasecmp("CollectSHM", child->key) == 0)
1465       cf_util_get_boolean(child, &conf->collect_shm);
1466     else if (strcasecmp("CollectSMS", child->key) == 0)
1467       cf_util_get_boolean(child, &conf->collect_sms);
1468     else if (strcasecmp("CollectSMA", child->key) == 0)
1469 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4
1470       cf_util_get_boolean(child, &conf->collect_sma);
1471 #else
1472       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1473               child->key, "v2 and v4");
1474 #endif
1475     else if (strcasecmp("CollectSM", child->key) == 0)
1476 #if HAVE_VARNISH_V2
1477       cf_util_get_boolean(child, &conf->collect_sm);
1478 #else
1479       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1480               child->key, "v2");
1481 #endif
1482     else if (strcasecmp("CollectStruct", child->key) == 0)
1483       cf_util_get_boolean(child, &conf->collect_struct);
1484     else if (strcasecmp("CollectTotals", child->key) == 0)
1485       cf_util_get_boolean(child, &conf->collect_totals);
1486     else if (strcasecmp("CollectUptime", child->key) == 0)
1487 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1488       cf_util_get_boolean(child, &conf->collect_uptime);
1489 #else
1490       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1491               child->key, "v3 and v4");
1492 #endif
1493     else if (strcasecmp("CollectVCL", child->key) == 0)
1494       cf_util_get_boolean(child, &conf->collect_vcl);
1495     else if (strcasecmp("CollectWorkers", child->key) == 0)
1496       cf_util_get_boolean(child, &conf->collect_workers);
1497     else if (strcasecmp("CollectVSM", child->key) == 0)
1498 #if HAVE_VARNISH_V4
1499       cf_util_get_boolean(child, &conf->collect_vsm);
1500 #else
1501       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1502               child->key, "v4");
1503 #endif
1504     else if (strcasecmp("CollectLock", child->key) == 0)
1505 #if HAVE_VARNISH_V4
1506       cf_util_get_boolean(child, &conf->collect_lck);
1507 #else
1508       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1509               child->key, "v4");
1510 #endif
1511     else if (strcasecmp("CollectMempool", child->key) == 0)
1512 #if HAVE_VARNISH_V4
1513       cf_util_get_boolean(child, &conf->collect_mempool);
1514 #else
1515       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1516               child->key, "v4");
1517 #endif
1518     else if (strcasecmp("CollectManagement", child->key) == 0)
1519 #if HAVE_VARNISH_V4
1520       cf_util_get_boolean(child, &conf->collect_mgt);
1521 #else
1522       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1523               child->key, "v4");
1524 #endif
1525     else if (strcasecmp("CollectSMF", child->key) == 0)
1526 #if HAVE_VARNISH_V4
1527       cf_util_get_boolean(child, &conf->collect_smf);
1528 #else
1529       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1530               child->key, "v4");
1531 #endif
1532     else if (strcasecmp("CollectSMF", child->key) == 0)
1533 #if HAVE_VARNISH_V4
1534       cf_util_get_boolean(child, &conf->collect_smf);
1535 #else
1536       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1537               child->key, "v4");
1538 #endif
1539     else if (strcasecmp("CollectVBE", child->key) == 0)
1540 #if HAVE_VARNISH_V4
1541       cf_util_get_boolean(child, &conf->collect_vbe);
1542 #else
1543       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1544               child->key, "v4");
1545 #endif
1546     else {
1547       WARNING("Varnish plugin: Ignoring unknown "
1548               "configuration option: \"%s\". Did "
1549               "you forget to add an <Instance /> "
1550               "block around the configuration?",
1551               child->key);
1552     }
1553   }
1554
1555   if (!conf->collect_cache && !conf->collect_connections &&
1556       !conf->collect_esi && !conf->collect_backend
1557 #ifdef HAVE_VARNISH_V3
1558       && !conf->collect_dirdns
1559 #endif
1560       && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1561 #if HAVE_VARNISH_V2
1562       && !conf->collect_purge
1563 #else
1564       && !conf->collect_ban
1565 #endif
1566       && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1567 #if HAVE_VARNISH_V2
1568       && !conf->collect_sm
1569 #endif
1570 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4
1571       && !conf->collect_sma
1572 #endif
1573       && !conf->collect_struct && !conf->collect_totals
1574 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1575       && !conf->collect_uptime
1576 #endif
1577       && !conf->collect_vcl && !conf->collect_workers
1578 #if HAVE_VARNISH_V4
1579       && !conf->collect_vsm && !conf->collect_vbe && !conf->collect_smf
1580       && !conf->collect_mgt && !conf->collect_lck && !conf->collect_mempool
1581 #endif
1582       ) {
1583     WARNING("Varnish plugin: No metric has been configured for "
1584             "instance \"%s\". Disabling this instance.",
1585             (conf->instance == NULL) ? "localhost" : conf->instance);
1586     sfree(conf);
1587     return EINVAL;
1588   }
1589
1590   snprintf(callback_name, sizeof(callback_name), "varnish/%s",
1591            (conf->instance == NULL) ? "localhost" : conf->instance);
1592
1593   plugin_register_complex_read(
1594       /* group = */ "varnish",
1595       /* name      = */ callback_name,
1596       /* callback  = */ varnish_read,
1597       /* interval  = */ 0,
1598       &(user_data_t){
1599           .data = conf, .free_func = varnish_config_free,
1600       });
1601
1602   have_instance = 1;
1603
1604   return 0;
1605 } /* }}} int varnish_config_instance */
1606
1607 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1608 {
1609   for (int i = 0; i < ci->children_num; i++) {
1610     oconfig_item_t *child = ci->children + i;
1611
1612     if (strcasecmp("Instance", child->key) == 0)
1613       varnish_config_instance(child);
1614     else {
1615       WARNING("Varnish plugin: Ignoring unknown "
1616               "configuration option: \"%s\"",
1617               child->key);
1618     }
1619   }
1620
1621   return 0;
1622 } /* }}} int varnish_config */
1623
1624 void module_register(void) /* {{{ */
1625 {
1626   plugin_register_complex_config("varnish", varnish_config);
1627   plugin_register_init("varnish", varnish_init);
1628 } /* }}} */