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 "plugin.h"
30 #include "utils/common/common.h"
31 
32 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
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 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
75   bool collect_sma;
76 #endif
77   bool collect_struct;
78   bool collect_totals;
79 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
80   bool collect_uptime;
81 #endif
82   bool collect_vcl;
83   bool collect_workers;
84 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
85   bool collect_vsm;
86 #endif
87 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
88   bool collect_lck;
89   bool collect_mempool;
90   bool collect_mgt;
91   bool collect_smf;
92   bool collect_vbe;
93   bool collect_mse;
94 #endif
95 #if HAVE_VARNISH_V6
96   bool collect_goto;
97 #endif
98 };
99 typedef struct user_config_s user_config_t; /* }}} */
100 
101 static bool have_instance;
102 
varnish_submit(const char * plugin_instance,const char * category,const char * target,const char * type,const char * type_instance,value_t value)103 static int varnish_submit(const char *plugin_instance, /* {{{ */
104                           const char *category, const char *target,
105                           const char *type, const char *type_instance,
106                           value_t value) {
107   value_list_t vl = VALUE_LIST_INIT;
108 
109   vl.values = &value;
110   vl.values_len = 1;
111 
112   sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
113 
114   if (plugin_instance == NULL)
115     plugin_instance = "default";
116 
117   if (target != NULL) {
118     ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s-%s",
119               plugin_instance, category, target);
120   } else {
121     ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
122               plugin_instance, category);
123   }
124 
125   sstrncpy(vl.type, type, sizeof(vl.type));
126 
127   if (type_instance != NULL)
128     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
129 
130   return plugin_dispatch_values(&vl);
131 } /* }}} int varnish_submit */
132 
varnish_submit_gauge(const char * plugin_instance,const char * category,const char * type,const char * type_instance,uint64_t gauge_value)133 static int varnish_submit_gauge(const char *plugin_instance, /* {{{ */
134                                 const char *category, const char *type,
135                                 const char *type_instance,
136                                 uint64_t gauge_value) {
137   return varnish_submit(plugin_instance, category, NULL, type, type_instance,
138                         (value_t){
139                             .gauge = (gauge_t)gauge_value,
140                         });
141 } /* }}} int varnish_submit_gauge */
142 
143 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
144 static int
varnish_submit_gauge_with_target(const char * plugin_instance,const char * category,const char * target,const char * type,const char * type_instance,uint64_t gauge_value)145 varnish_submit_gauge_with_target(const char *plugin_instance, /* {{{ */
146                                  const char *category, const char *target,
147                                  const char *type, const char *type_instance,
148                                  uint64_t gauge_value) {
149 
150   return varnish_submit(plugin_instance, category, target, type, type_instance,
151                         (value_t){
152                             .gauge = (gauge_t)gauge_value,
153                         });
154 } /* }}} int varnish_submit_gauge_with_target */
155 #endif
156 
varnish_submit_derive(const char * plugin_instance,const char * category,const char * type,const char * type_instance,uint64_t derive_value)157 static int varnish_submit_derive(const char *plugin_instance, /* {{{ */
158                                  const char *category, const char *type,
159                                  const char *type_instance,
160                                  uint64_t derive_value) {
161   return varnish_submit(plugin_instance, category, NULL, type, type_instance,
162                         (value_t){
163                             .derive = (derive_t)derive_value,
164                         });
165 } /* }}} int varnish_submit_derive */
166 
167 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
168 static int
varnish_submit_derive_with_target(const char * plugin_instance,const char * category,const char * target,const char * type,const char * type_instance,uint64_t derive_value)169 varnish_submit_derive_with_target(const char *plugin_instance, /* {{{ */
170                                   const char *category, const char *target,
171                                   const char *type, const char *type_instance,
172                                   uint64_t derive_value) {
173   return varnish_submit(plugin_instance, category, target, type, type_instance,
174                         (value_t){
175                             .derive = (derive_t)derive_value,
176                         });
177 } /* }}} int varnish_submit_derive_with_target */
178 #endif
179 
180 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
varnish_monitor(void * priv,const struct VSC_point * const pt)181 static int varnish_monitor(void *priv,
182                            const struct VSC_point *const pt) /* {{{ */
183 {
184   uint64_t val;
185   const user_config_t *conf;
186   const char *name;
187 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
188   const char *stat_target = NULL;
189 #endif
190 
191   if (pt == NULL)
192     return 0;
193 
194   conf = priv;
195 
196 #if HAVE_VARNISH_V6
197   /*
198    stats examples:
199    MAIN.threads => name=threads
200    SMA.s0.c_req => name=c_req, stat_target=s0
201    SMA.Transient.c_req => name=c_req, stat_target=Transient
202    VBE.vclName.backendName.req  => name=req, stat_target=backendName
203   */
204 
205   char namebuff[DATA_MAX_NAME_LEN];
206   char targetbuff[DATA_MAX_NAME_LEN];
207 
208   char *buffer = strdup(pt->name);
209   char *tokens[4] = {NULL};
210   size_t tokens_num = 0;
211   char *ptr = buffer;
212   char *saveptr = NULL;
213   char *token = NULL;
214 
215   while ((token = strtok_r(ptr, ".", &saveptr)) != NULL) {
216     ptr = NULL;
217     if (tokens_num < STATIC_ARRAY_SIZE(tokens)) {
218       tokens[tokens_num] = token;
219     }
220     tokens_num++;
221   }
222 
223   if ((tokens_num < 2) || (tokens_num > STATIC_ARRAY_SIZE(tokens))) {
224     free(buffer);
225     return EINVAL;
226   }
227 
228   sstrncpy(namebuff, tokens[tokens_num - 1], sizeof(namebuff));
229   name = namebuff;
230   if (tokens_num >= 3) {
231     sstrncpy(targetbuff, tokens[tokens_num - 2], sizeof(targetbuff));
232     stat_target = targetbuff;
233   }
234 
235   free(buffer);
236 
237 #elif HAVE_VARNISH_V5
238   char namebuff[DATA_MAX_NAME_LEN];
239 
240   char const *c = strrchr(pt->name, '.');
241   if (c == NULL) {
242     return EINVAL;
243   }
244   sstrncpy(namebuff, c + 1, sizeof(namebuff));
245   name = namebuff;
246 
247 #elif HAVE_VARNISH_V4
248   if (strcmp(pt->section->fantom->type, "MAIN") != 0)
249     return 0;
250 
251   name = pt->desc->name;
252 #elif HAVE_VARNISH_V3
253   if (strcmp(pt->class, "") != 0)
254     return 0;
255 
256   name = pt->name;
257 #endif
258 
259   val = *(const volatile uint64_t *)pt->ptr;
260 
261   if (conf->collect_cache) {
262     if (strcmp(name, "cache_hit") == 0)
263       return varnish_submit_derive(conf->instance, "cache", "cache_result",
264                                    "hit", val);
265     else if (strcmp(name, "cache_miss") == 0)
266       return varnish_submit_derive(conf->instance, "cache", "cache_result",
267                                    "miss", val);
268     else if (strcmp(name, "cache_hitpass") == 0)
269       return varnish_submit_derive(conf->instance, "cache", "cache_result",
270                                    "hitpass", val);
271 #if HAVE_VARNISH_V6
272     else if (strcmp(name, "cache_hit_grace") == 0)
273       return varnish_submit_derive(conf->instance, "cache", "cache_result",
274                                    "hit_grace", val);
275     else if (strcmp(name, "cache_hitmiss") == 0)
276       return varnish_submit_derive(conf->instance, "cache", "cache_result",
277                                    "hitmiss", val);
278 #endif
279   }
280 
281   if (conf->collect_connections) {
282     if (strcmp(name, "client_conn") == 0)
283       return varnish_submit_derive(conf->instance, "connections", "connections",
284                                    "accepted", val);
285     else if (strcmp(name, "client_drop") == 0)
286       return varnish_submit_derive(conf->instance, "connections", "connections",
287                                    "dropped", val);
288     else if (strcmp(name, "client_req") == 0)
289       return varnish_submit_derive(conf->instance, "connections", "connections",
290                                    "received", val);
291 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
292     else if (strcmp(name, "client_req_400") == 0)
293       return varnish_submit_derive(conf->instance, "connections", "connections",
294                                    "error_400", val);
295     else if (strcmp(name, "client_req_417") == 0)
296       return varnish_submit_derive(conf->instance, "connections", "connections",
297                                    "error_417", val);
298 #endif
299   }
300 
301 #ifdef HAVE_VARNISH_V3
302   if (conf->collect_dirdns) {
303     if (strcmp(name, "dir_dns_lookups") == 0)
304       return varnish_submit_derive(conf->instance, "dirdns", "cache_operation",
305                                    "lookups", val);
306     else if (strcmp(name, "dir_dns_failed") == 0)
307       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
308                                    "failed", val);
309     else if (strcmp(name, "dir_dns_hit") == 0)
310       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
311                                    "hits", val);
312     else if (strcmp(name, "dir_dns_cache_full") == 0)
313       return varnish_submit_derive(conf->instance, "dirdns", "cache_result",
314                                    "cache_full", val);
315   }
316 #endif
317 
318   if (conf->collect_esi) {
319     if (strcmp(name, "esi_errors") == 0)
320       return varnish_submit_derive(conf->instance, "esi", "total_operations",
321                                    "error", val);
322     else if (strcmp(name, "esi_parse") == 0)
323       return varnish_submit_derive(conf->instance, "esi", "total_operations",
324                                    "parsed", val);
325     else if (strcmp(name, "esi_warnings") == 0)
326       return varnish_submit_derive(conf->instance, "esi", "total_operations",
327                                    "warning", val);
328     else if (strcmp(name, "esi_maxdepth") == 0)
329       return varnish_submit_derive(conf->instance, "esi", "total_operations",
330                                    "max_depth", val);
331   }
332 
333   if (conf->collect_backend) {
334     if (strcmp(name, "backend_conn") == 0)
335       return varnish_submit_derive(conf->instance, "backend", "connections",
336                                    "success", val);
337     else if (strcmp(name, "backend_unhealthy") == 0)
338       return varnish_submit_derive(conf->instance, "backend", "connections",
339                                    "not-attempted", val);
340     else if (strcmp(name, "backend_busy") == 0)
341       return varnish_submit_derive(conf->instance, "backend", "connections",
342                                    "too-many", val);
343     else if (strcmp(name, "backend_fail") == 0)
344       return varnish_submit_derive(conf->instance, "backend", "connections",
345                                    "failures", val);
346     else if (strcmp(name, "backend_reuse") == 0)
347       return varnish_submit_derive(conf->instance, "backend", "connections",
348                                    "reuses", val);
349     else if (strcmp(name, "backend_toolate") == 0)
350       return varnish_submit_derive(conf->instance, "backend", "connections",
351                                    "was-closed", val);
352     else if (strcmp(name, "backend_recycle") == 0)
353       return varnish_submit_derive(conf->instance, "backend", "connections",
354                                    "recycled", val);
355     else if (strcmp(name, "backend_unused") == 0)
356       return varnish_submit_derive(conf->instance, "backend", "connections",
357                                    "unused", val);
358     else if (strcmp(name, "backend_retry") == 0)
359       return varnish_submit_derive(conf->instance, "backend", "connections",
360                                    "retries", val);
361     else if (strcmp(name, "backend_req") == 0)
362       return varnish_submit_derive(conf->instance, "backend", "http_requests",
363                                    "requests", val);
364     else if (strcmp(name, "n_backend") == 0)
365       return varnish_submit_gauge(conf->instance, "backend", "backends",
366                                   "n_backends", val);
367   }
368 
369   if (conf->collect_fetch) {
370     if (strcmp(name, "fetch_head") == 0)
371       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
372                                    "head", val);
373     else if (strcmp(name, "fetch_length") == 0)
374       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
375                                    "length", val);
376     else if (strcmp(name, "fetch_chunked") == 0)
377       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
378                                    "chunked", val);
379     else if (strcmp(name, "fetch_eof") == 0)
380       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
381                                    "eof", val);
382     else if (strcmp(name, "fetch_bad") == 0)
383       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
384                                    "bad_headers", val);
385     else if (strcmp(name, "fetch_close") == 0)
386       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
387                                    "close", val);
388     else if (strcmp(name, "fetch_oldhttp") == 0)
389       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
390                                    "oldhttp", val);
391     else if (strcmp(name, "fetch_zero") == 0)
392       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
393                                    "zero", val);
394     else if (strcmp(name, "fetch_failed") == 0)
395       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
396                                    "failed", val);
397     else if (strcmp(name, "fetch_1xx") == 0)
398       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
399                                    "no_body_1xx", val);
400     else if (strcmp(name, "fetch_204") == 0)
401       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
402                                    "no_body_204", val);
403     else if (strcmp(name, "fetch_304") == 0)
404       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
405                                    "no_body_304", val);
406 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
407     else if (strcmp(name, "fetch_no_thread") == 0)
408       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
409                                    "no_thread", val);
410     else if (strcmp(name, "fetch_none") == 0)
411       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
412                                    "none", val);
413     else if (strcmp(name, "busy_sleep") == 0)
414       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
415                                    "busy_sleep", val);
416     else if (strcmp(name, "busy_wakeup") == 0)
417       return varnish_submit_derive(conf->instance, "fetch", "http_requests",
418                                    "busy_wakeup", val);
419 #endif
420   }
421 
422   if (conf->collect_hcb) {
423     if (strcmp(name, "hcb_nolock") == 0)
424       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
425                                    "lookup_nolock", val);
426     else if (strcmp(name, "hcb_lock") == 0)
427       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
428                                    "lookup_lock", val);
429     else if (strcmp(name, "hcb_insert") == 0)
430       return varnish_submit_derive(conf->instance, "hcb", "cache_operation",
431                                    "insert", val);
432   }
433 
434   if (conf->collect_objects) {
435     if (strcmp(name, "n_expired") == 0)
436       return varnish_submit_derive(conf->instance, "objects", "total_objects",
437                                    "expired", val);
438     else if (strcmp(name, "n_lru_nuked") == 0)
439       return varnish_submit_derive(conf->instance, "objects", "total_objects",
440                                    "lru_nuked", val);
441     else if (strcmp(name, "n_lru_saved") == 0)
442       return varnish_submit_derive(conf->instance, "objects", "total_objects",
443                                    "lru_saved", val);
444     else if (strcmp(name, "n_lru_moved") == 0)
445       return varnish_submit_derive(conf->instance, "objects", "total_objects",
446                                    "lru_moved", val);
447 #if HAVE_VARNISH_V6
448     else if (strcmp(name, "n_lru_limited") == 0)
449       return varnish_submit_derive(conf->instance, "objects", "total_objects",
450                                    "lru_limited", val);
451 #endif
452     else if (strcmp(name, "n_deathrow") == 0)
453       return varnish_submit_derive(conf->instance, "objects", "total_objects",
454                                    "deathrow", val);
455     else if (strcmp(name, "losthdr") == 0)
456       return varnish_submit_derive(conf->instance, "objects", "total_objects",
457                                    "header_overflow", val);
458     else if (strcmp(name, "n_obj_purged") == 0)
459       return varnish_submit_derive(conf->instance, "objects", "total_objects",
460                                    "purged", val);
461     else if (strcmp(name, "n_objsendfile") == 0)
462       return varnish_submit_derive(conf->instance, "objects", "total_objects",
463                                    "sent_sendfile", val);
464     else if (strcmp(name, "n_objwrite") == 0)
465       return varnish_submit_derive(conf->instance, "objects", "total_objects",
466                                    "sent_write", val);
467     else if (strcmp(name, "n_objoverflow") == 0)
468       return varnish_submit_derive(conf->instance, "objects", "total_objects",
469                                    "workspace_overflow", val);
470 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
471     else if (strcmp(name, "exp_mailed") == 0)
472       return varnish_submit_gauge(conf->instance, "struct", "objects",
473                                   "exp_mailed", val);
474     else if (strcmp(name, "exp_received") == 0)
475       return varnish_submit_gauge(conf->instance, "struct", "objects",
476                                   "exp_received", val);
477 #endif
478   }
479 
480 #if HAVE_VARNISH_V3
481   if (conf->collect_ban) {
482     if (strcmp(name, "n_ban") == 0)
483       return varnish_submit_derive(conf->instance, "ban", "total_operations",
484                                    "total", val);
485     else if (strcmp(name, "n_ban_add") == 0)
486       return varnish_submit_derive(conf->instance, "ban", "total_operations",
487                                    "added", val);
488     else if (strcmp(name, "n_ban_retire") == 0)
489       return varnish_submit_derive(conf->instance, "ban", "total_operations",
490                                    "deleted", val);
491     else if (strcmp(name, "n_ban_obj_test") == 0)
492       return varnish_submit_derive(conf->instance, "ban", "total_operations",
493                                    "objects_tested", val);
494     else if (strcmp(name, "n_ban_re_test") == 0)
495       return varnish_submit_derive(conf->instance, "ban", "total_operations",
496                                    "regexps_tested", val);
497     else if (strcmp(name, "n_ban_dups") == 0)
498       return varnish_submit_derive(conf->instance, "ban", "total_operations",
499                                    "duplicate", val);
500   }
501 #endif
502 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
503   if (conf->collect_ban) {
504     if (strcmp(name, "bans") == 0)
505       return varnish_submit_derive(conf->instance, "ban", "total_operations",
506                                    "total", val);
507     else if (strcmp(name, "bans_added") == 0)
508       return varnish_submit_derive(conf->instance, "ban", "total_operations",
509                                    "added", val);
510     else if (strcmp(name, "bans_obj") == 0)
511       return varnish_submit_derive(conf->instance, "ban", "total_operations",
512                                    "obj", val);
513     else if (strcmp(name, "bans_req") == 0)
514       return varnish_submit_derive(conf->instance, "ban", "total_operations",
515                                    "req", val);
516     else if (strcmp(name, "bans_completed") == 0)
517       return varnish_submit_derive(conf->instance, "ban", "total_operations",
518                                    "completed", val);
519     else if (strcmp(name, "bans_deleted") == 0)
520       return varnish_submit_derive(conf->instance, "ban", "total_operations",
521                                    "deleted", val);
522     else if (strcmp(name, "bans_tested") == 0)
523       return varnish_submit_derive(conf->instance, "ban", "total_operations",
524                                    "tested", val);
525     else if (strcmp(name, "bans_dups") == 0)
526       return varnish_submit_derive(conf->instance, "ban", "total_operations",
527                                    "duplicate", val);
528     else if (strcmp(name, "bans_tested") == 0)
529       return varnish_submit_derive(conf->instance, "ban", "total_operations",
530                                    "tested", val);
531     else if (strcmp(name, "bans_lurker_contention") == 0)
532       return varnish_submit_derive(conf->instance, "ban", "total_operations",
533                                    "lurker_contention", val);
534     else if (strcmp(name, "bans_lurker_obj_killed") == 0)
535       return varnish_submit_derive(conf->instance, "ban", "total_operations",
536                                    "lurker_obj_killed", val);
537     else if (strcmp(name, "bans_lurker_tested") == 0)
538       return varnish_submit_derive(conf->instance, "ban", "total_operations",
539                                    "lurker_tested", val);
540     else if (strcmp(name, "bans_lurker_tests_tested") == 0)
541       return varnish_submit_derive(conf->instance, "ban", "total_operations",
542                                    "lurker_tests_tested", val);
543     else if (strcmp(name, "bans_obj_killed") == 0)
544       return varnish_submit_derive(conf->instance, "ban", "total_operations",
545                                    "obj_killed", val);
546     else if (strcmp(name, "bans_persisted_bytes") == 0)
547       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
548                                    "persisted_bytes", val);
549     else if (strcmp(name, "bans_persisted_fragmentation") == 0)
550       return varnish_submit_derive(conf->instance, "ban", "total_bytes",
551                                    "persisted_fragmentation", val);
552     else if (strcmp(name, "bans_tests_tested") == 0)
553       return varnish_submit_derive(conf->instance, "ban", "total_operations",
554                                    "tests_tested", val);
555   }
556 #endif
557 
558   if (conf->collect_session) {
559     if (strcmp(name, "sess_closed") == 0)
560       return varnish_submit_derive(conf->instance, "session",
561                                    "total_operations", "closed", val);
562     else if (strcmp(name, "sess_pipeline") == 0)
563       return varnish_submit_derive(conf->instance, "session",
564                                    "total_operations", "pipeline", val);
565     else if (strcmp(name, "sess_readahead") == 0)
566       return varnish_submit_derive(conf->instance, "session",
567                                    "total_operations", "readahead", val);
568     else if (strcmp(name, "sess_conn") == 0)
569       return varnish_submit_derive(conf->instance, "session",
570                                    "total_operations", "accepted", val);
571     else if (strcmp(name, "sess_drop") == 0)
572       return varnish_submit_derive(conf->instance, "session",
573                                    "total_operations", "dropped", val);
574     else if (strcmp(name, "sess_fail") == 0)
575       return varnish_submit_derive(conf->instance, "session",
576                                    "total_operations", "failed", val);
577 #if HAVE_VARNISH_V6
578     else if (strcmp(name, "sess_fail_econnaborted") == 0)
579       return varnish_submit_derive(conf->instance, "session",
580                                    "total_operations", "failed_econnaborted",
581                                    val);
582     else if (strcmp(name, "sess_fail_eintr") == 0)
583       return varnish_submit_derive(conf->instance, "session",
584                                    "total_operations", "failed_eintr", val);
585     else if (strcmp(name, "sess_fail_emfile") == 0)
586       return varnish_submit_derive(conf->instance, "session",
587                                    "total_operations", "failed_emfile", val);
588     else if (strcmp(name, "sess_fail_ebadf") == 0)
589       return varnish_submit_derive(conf->instance, "session",
590                                    "total_operations", "failed_ebadf", val);
591     else if (strcmp(name, "sess_fail_enomem") == 0)
592       return varnish_submit_derive(conf->instance, "session",
593                                    "total_operations", "failed_enomem", val);
594     else if (strcmp(name, "sess_fail_other") == 0)
595       return varnish_submit_derive(conf->instance, "session",
596                                    "total_operations", "failed_other", val);
597 #endif
598     else if (strcmp(name, "sess_pipe_overflow") == 0)
599       return varnish_submit_derive(conf->instance, "session",
600                                    "total_operations", "overflow", val);
601     else if (strcmp(name, "sess_queued") == 0)
602       return varnish_submit_derive(conf->instance, "session",
603                                    "total_operations", "queued", val);
604     else if (strcmp(name, "sess_linger") == 0)
605       return varnish_submit_derive(conf->instance, "session",
606                                    "total_operations", "linger", val);
607     else if (strcmp(name, "sess_herd") == 0)
608       return varnish_submit_derive(conf->instance, "session",
609                                    "total_operations", "herd", val);
610 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
611     else if (strcmp(name, "sess_closed_err") == 0)
612       return varnish_submit_derive(conf->instance, "session",
613                                    "total_operations", "closed_err", val);
614     else if (strcmp(name, "sess_dropped") == 0)
615       return varnish_submit_derive(conf->instance, "session",
616                                    "total_operations", "dropped_for_thread",
617                                    val);
618 #endif
619   }
620 
621   if (conf->collect_shm) {
622     if (strcmp(name, "shm_records") == 0)
623       return varnish_submit_derive(conf->instance, "shm", "total_operations",
624                                    "records", val);
625     else if (strcmp(name, "shm_writes") == 0)
626       return varnish_submit_derive(conf->instance, "shm", "total_operations",
627                                    "writes", val);
628     else if (strcmp(name, "shm_flushes") == 0)
629       return varnish_submit_derive(conf->instance, "shm", "total_operations",
630                                    "flushes", val);
631     else if (strcmp(name, "shm_cont") == 0)
632       return varnish_submit_derive(conf->instance, "shm", "total_operations",
633                                    "contention", val);
634     else if (strcmp(name, "shm_cycles") == 0)
635       return varnish_submit_derive(conf->instance, "shm", "total_operations",
636                                    "cycles", val);
637   }
638 
639   if (conf->collect_sms) {
640     if (strcmp(name, "sms_nreq") == 0)
641       return varnish_submit_derive(conf->instance, "sms", "total_requests",
642                                    "allocator", val);
643     else if (strcmp(name, "sms_nobj") == 0)
644       return varnish_submit_gauge(conf->instance, "sms", "requests",
645                                   "outstanding", val);
646     else if (strcmp(name, "sms_nbytes") == 0)
647       return varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
648                                   val);
649     else if (strcmp(name, "sms_balloc") == 0)
650       return varnish_submit_derive(conf->instance, "sms", "total_bytes",
651                                    "allocated", val);
652     else if (strcmp(name, "sms_bfree") == 0)
653       return varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
654                                    val);
655   }
656 
657   if (conf->collect_struct) {
658     if (strcmp(name, "n_sess_mem") == 0)
659       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
660                                   "sess_mem", val);
661     else if (strcmp(name, "n_sess") == 0)
662       return varnish_submit_gauge(conf->instance, "struct", "current_sessions",
663                                   "sess", val);
664     else if (strcmp(name, "n_object") == 0)
665       return varnish_submit_gauge(conf->instance, "struct", "objects", "object",
666                                   val);
667     else if (strcmp(name, "n_vampireobject") == 0)
668       return varnish_submit_gauge(conf->instance, "struct", "objects",
669                                   "vampireobject", val);
670     else if (strcmp(name, "n_objectcore") == 0)
671       return varnish_submit_gauge(conf->instance, "struct", "objects",
672                                   "objectcore", val);
673     else if (strcmp(name, "n_waitinglist") == 0)
674       return varnish_submit_gauge(conf->instance, "struct", "objects",
675                                   "waitinglist", val);
676     else if (strcmp(name, "n_objecthead") == 0)
677       return varnish_submit_gauge(conf->instance, "struct", "objects",
678                                   "objecthead", val);
679     else if (strcmp(name, "n_smf") == 0)
680       return varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
681                                   val);
682     else if (strcmp(name, "n_smf_frag") == 0)
683       return varnish_submit_gauge(conf->instance, "struct", "objects",
684                                   "smf_frag", val);
685     else if (strcmp(name, "n_smf_large") == 0)
686       return varnish_submit_gauge(conf->instance, "struct", "objects",
687                                   "smf_large", val);
688     else if (strcmp(name, "n_vbe_conn") == 0)
689       return varnish_submit_gauge(conf->instance, "struct", "objects",
690                                   "vbe_conn", val);
691   }
692 
693   if (conf->collect_totals) {
694     if (strcmp(name, "s_sess") == 0)
695       return varnish_submit_derive(conf->instance, "totals", "total_sessions",
696                                    "sessions", val);
697     else if (strcmp(name, "s_req") == 0)
698       return varnish_submit_derive(conf->instance, "totals", "total_requests",
699                                    "requests", val);
700     else if (strcmp(name, "s_pipe") == 0)
701       return varnish_submit_derive(conf->instance, "totals", "total_operations",
702                                    "pipe", val);
703     else if (strcmp(name, "s_pass") == 0)
704       return varnish_submit_derive(conf->instance, "totals", "total_operations",
705                                    "pass", val);
706     else if (strcmp(name, "s_fetch") == 0)
707       return varnish_submit_derive(conf->instance, "totals", "total_operations",
708                                    "fetches", val);
709     else if (strcmp(name, "s_synth") == 0)
710       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
711                                    "synth", val);
712     else if (strcmp(name, "s_req_hdrbytes") == 0)
713       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
714                                    "req_header", val);
715     else if (strcmp(name, "s_req_bodybytes") == 0)
716       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
717                                    "req_body", val);
718     else if (strcmp(name, "s_req_protobytes") == 0)
719       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
720                                    "req_proto", val);
721     else if (strcmp(name, "s_resp_hdrbytes") == 0)
722       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
723                                    "resp_header", val);
724     else if (strcmp(name, "s_resp_bodybytes") == 0)
725       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
726                                    "resp_body", val);
727     else if (strcmp(name, "s_resp_protobytes") == 0)
728       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
729                                    "resp_proto", val);
730     else if (strcmp(name, "s_pipe_hdrbytes") == 0)
731       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
732                                    "pipe_header", val);
733     else if (strcmp(name, "s_pipe_in") == 0)
734       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
735                                    "pipe_in", val);
736     else if (strcmp(name, "s_pipe_out") == 0)
737       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
738                                    "pipe_out", val);
739     else if (strcmp(name, "n_purges") == 0)
740       return varnish_submit_derive(conf->instance, "totals", "total_operations",
741                                    "purges", val);
742     else if (strcmp(name, "s_hdrbytes") == 0)
743       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
744                                    "header-bytes", val);
745     else if (strcmp(name, "s_bodybytes") == 0)
746       return varnish_submit_derive(conf->instance, "totals", "total_bytes",
747                                    "body-bytes", val);
748     else if (strcmp(name, "n_gzip") == 0)
749       return varnish_submit_derive(conf->instance, "totals", "total_operations",
750                                    "gzip", val);
751     else if (strcmp(name, "n_gunzip") == 0)
752       return varnish_submit_derive(conf->instance, "totals", "total_operations",
753                                    "gunzip", val);
754   }
755 
756   if (conf->collect_uptime) {
757     if (strcmp(name, "uptime") == 0)
758       return varnish_submit_gauge(conf->instance, "uptime", "uptime",
759                                   "client_uptime", val);
760   }
761 
762   if (conf->collect_vcl) {
763     if (strcmp(name, "n_vcl") == 0)
764       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
765                                   val);
766     else if (strcmp(name, "n_vcl_avail") == 0)
767       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
768                                   val);
769     else if (strcmp(name, "n_vcl_discard") == 0)
770       return varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
771                                   val);
772     else if (strcmp(name, "vmods") == 0)
773       return varnish_submit_gauge(conf->instance, "vcl", "objects", "vmod",
774                                   val);
775   }
776 
777   if (conf->collect_workers) {
778     if (strcmp(name, "threads") == 0)
779       return varnish_submit_gauge(conf->instance, "workers", "threads",
780                                   "worker", val);
781     else if (strcmp(name, "threads_created") == 0)
782       return varnish_submit_derive(conf->instance, "workers", "total_threads",
783                                    "created", val);
784     else if (strcmp(name, "threads_failed") == 0)
785       return varnish_submit_derive(conf->instance, "workers", "total_threads",
786                                    "failed", val);
787     else if (strcmp(name, "threads_limited") == 0)
788       return varnish_submit_derive(conf->instance, "workers", "total_threads",
789                                    "limited", val);
790     else if (strcmp(name, "threads_destroyed") == 0)
791       return varnish_submit_derive(conf->instance, "workers", "total_threads",
792                                    "dropped", val);
793     else if (strcmp(name, "thread_queue_len") == 0)
794       return varnish_submit_gauge(conf->instance, "workers", "queue_length",
795                                   "threads", val);
796 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5
797     else if (strcmp(name, "n_wrk") == 0)
798       return varnish_submit_gauge(conf->instance, "workers", "threads",
799                                   "worker", val);
800     else if (strcmp(name, "n_wrk_create") == 0)
801       return varnish_submit_derive(conf->instance, "workers", "total_threads",
802                                    "created", val);
803     else if (strcmp(name, "n_wrk_failed") == 0)
804       return varnish_submit_derive(conf->instance, "workers", "total_threads",
805                                    "failed", val);
806     else if (strcmp(name, "n_wrk_max") == 0)
807       return varnish_submit_derive(conf->instance, "workers", "total_threads",
808                                    "limited", val);
809     else if (strcmp(name, "n_wrk_drop") == 0)
810       return varnish_submit_derive(conf->instance, "workers", "total_threads",
811                                    "dropped", val);
812     else if (strcmp(name, "n_wrk_queue") == 0)
813       return varnish_submit_derive(conf->instance, "workers", "total_requests",
814                                    "queued", val);
815     else if (strcmp(name, "n_wrk_overflow") == 0)
816       return varnish_submit_derive(conf->instance, "workers", "total_requests",
817                                    "overflowed", val);
818     else if (strcmp(name, "n_wrk_queued") == 0)
819       return varnish_submit_derive(conf->instance, "workers", "total_requests",
820                                    "queued", val);
821     else if (strcmp(name, "n_wrk_lqueue") == 0)
822       return varnish_submit_derive(conf->instance, "workers", "total_requests",
823                                    "queue_length", val);
824 #endif
825 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
826     else if (strcmp(name, "pools") == 0)
827       return varnish_submit_gauge(conf->instance, "workers", "pools", "pools",
828                                   val);
829     else if (strcmp(name, "busy_killed") == 0)
830       return varnish_submit_derive(conf->instance, "workers", "http_requests",
831                                    "busy_killed", val);
832 #endif
833   }
834 
835 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
836   if (conf->collect_vsm) {
837     if (strcmp(name, "vsm_free") == 0)
838       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "free", val);
839     else if (strcmp(name, "vsm_used") == 0)
840       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "used", val);
841     else if (strcmp(name, "vsm_cooling") == 0)
842       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "cooling",
843                                   val);
844     else if (strcmp(name, "vsm_overflow") == 0)
845       return varnish_submit_gauge(conf->instance, "vsm", "bytes", "overflow",
846                                   val);
847     else if (strcmp(name, "vsm_overflowed") == 0)
848       return varnish_submit_derive(conf->instance, "vsm", "total_bytes",
849                                    "overflowed", val);
850   }
851 #endif
852 
853 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
854   if (conf->collect_vbe) {
855     /* @TODO figure out the collectd type for bitmap
856     if (strcmp(name, "happy") == 0)
857       return varnish_submit_derive(conf->instance, "vbe",
858                                    "bitmap", "happy_hprobes", val);
859     */
860     if (strcmp(name, "bereq_hdrbytes") == 0)
861       return varnish_submit_derive_with_target(conf->instance, "vbe",
862                                                stat_target, "total_bytes",
863                                                "bereq_hdrbytes", val);
864     else if (strcmp(name, "bereq_bodybytes") == 0)
865       return varnish_submit_derive_with_target(conf->instance, "vbe",
866                                                stat_target, "total_bytes",
867                                                "bereq_bodybytes", val);
868     else if (strcmp(name, "bereq_protobytes") == 0)
869       return varnish_submit_derive_with_target(conf->instance, "vbe",
870                                                stat_target, "total_bytes",
871                                                "bereq_protobytes", val);
872     else if (strcmp(name, "beresp_hdrbytes") == 0)
873       return varnish_submit_derive_with_target(conf->instance, "vbe",
874                                                stat_target, "total_bytes",
875                                                "beresp_hdrbytes", val);
876     else if (strcmp(name, "beresp_bodybytes") == 0)
877       return varnish_submit_derive_with_target(conf->instance, "vbe",
878                                                stat_target, "total_bytes",
879                                                "beresp_bodybytes", val);
880     else if (strcmp(name, "beresp_protobytes") == 0)
881       return varnish_submit_derive_with_target(conf->instance, "vbe",
882                                                stat_target, "total_bytes",
883                                                "beresp_protobytes", val);
884     else if (strcmp(name, "pipe_hdrbytes") == 0)
885       return varnish_submit_derive_with_target(conf->instance, "vbe",
886                                                stat_target, "total_bytes",
887                                                "pipe_hdrbytes", val);
888     else if (strcmp(name, "pipe_out") == 0)
889       return varnish_submit_derive_with_target(
890           conf->instance, "vbe", stat_target, "total_bytes", "pipe_out", val);
891     else if (strcmp(name, "pipe_in") == 0)
892       return varnish_submit_derive_with_target(
893           conf->instance, "vbe", stat_target, "total_bytes", "pipe_in", val);
894     else if (strcmp(name, "conn") == 0)
895       return varnish_submit_derive_with_target(
896           conf->instance, "vbe", stat_target, "connections", "c_conns", val);
897     else if (strcmp(name, "req") == 0)
898       return varnish_submit_derive_with_target(
899           conf->instance, "vbe", stat_target, "http_requests", "b_reqs", val);
900   }
901 
902   /* All Stevedores support these counters */
903   if (conf->collect_sma || conf->collect_smf || conf->collect_mse) {
904 
905     char category[4];
906     if (conf->collect_sma)
907       strncpy(category, "sma", 4);
908     else if (conf->collect_smf)
909       strncpy(category, "smf", 4);
910     else
911       strncpy(category, "mse", 4);
912 
913     if (strcmp(name, "c_req") == 0)
914       return varnish_submit_derive_with_target(conf->instance, category,
915                                                stat_target, "total_operations",
916                                                "alloc_req", val);
917     else if (strcmp(name, "c_fail") == 0)
918       return varnish_submit_derive_with_target(conf->instance, category,
919                                                stat_target, "total_operations",
920                                                "alloc_fail", val);
921     else if (strcmp(name, "c_bytes") == 0)
922       return varnish_submit_derive_with_target(conf->instance, category,
923                                                stat_target, "total_bytes",
924                                                "bytes_allocated", val);
925     else if (strcmp(name, "c_freed") == 0)
926       return varnish_submit_derive_with_target(conf->instance, category,
927                                                stat_target, "total_bytes",
928                                                "bytes_freed", val);
929     else if (strcmp(name, "g_alloc") == 0)
930       return varnish_submit_derive_with_target(conf->instance, category,
931                                                stat_target, "total_operations",
932                                                "alloc_outstanding", val);
933     else if (strcmp(name, "g_bytes") == 0)
934       return varnish_submit_gauge_with_target(conf->instance, category,
935                                               stat_target, "bytes",
936                                               "bytes_outstanding", val);
937     else if (strcmp(name, "g_space") == 0)
938       return varnish_submit_gauge_with_target(conf->instance, category,
939                                               stat_target, "bytes",
940                                               "bytes_available", val);
941   }
942 
943 #if HAVE_VARNISH_V6
944   /* No SMA specific counters */
945   if (conf->collect_mse) {
946     if (strcmp(name, "c_fail_malloc") == 0)
947       return varnish_submit_derive(conf->instance, "mse", "total_operations",
948                                    "alloc_fail_malloc", val);
949     else if (strcmp(name, "n_lru_nuked") == 0)
950       return varnish_submit_derive(conf->instance, "mse", "total_objects",
951                                    "lru_nuked", val);
952     else if (strcmp(name, "n_lru_moved") == 0)
953       return varnish_submit_derive(conf->instance, "mse", "total_objects",
954                                    "lru_moved", val);
955     else if (strcmp(name, "n_vary") == 0)
956       return varnish_submit_derive(conf->instance, "mse", "total_objects",
957                                    "vary_headers", val);
958     else if (strcmp(name, "c_memcache_hit") == 0)
959       return varnish_submit_derive(conf->instance, "mse", "total_operations",
960                                    "memcache_hit", val);
961     else if (strcmp(name, "c_memcache_miss") == 0)
962       return varnish_submit_derive(conf->instance, "mse", "total_operations",
963                                    "memcache_miss", val);
964     else if (strcmp(name, "g_ykey_keys") == 0)
965       return varnish_submit_gauge(conf->instance, "mse", "objects", "ykey",
966                                   val);
967   }
968 #endif
969 
970   /* No SMA specific counters */
971   if (conf->collect_smf) {
972     if (strcmp(name, "g_smf") == 0)
973       return varnish_submit_gauge(conf->instance, "smf", "objects",
974                                   "n_struct_smf", val);
975     else if (strcmp(name, "g_smf_frag") == 0)
976       return varnish_submit_gauge(conf->instance, "smf", "objects",
977                                   "n_small_free_smf", val);
978     else if (strcmp(name, "g_smf_large") == 0)
979       return varnish_submit_gauge(conf->instance, "smf", "objects",
980                                   "n_large_free_smf", val);
981   }
982 
983   if (conf->collect_mgt) {
984     if (strcmp(name, "uptime") == 0)
985       return varnish_submit_gauge(conf->instance, "mgt", "uptime",
986                                   "mgt_proc_uptime", val);
987     else if (strcmp(name, "child_start") == 0)
988       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
989                                    "child_start", val);
990     else if (strcmp(name, "child_exit") == 0)
991       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
992                                    "child_exit", val);
993     else if (strcmp(name, "child_stop") == 0)
994       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
995                                    "child_stop", val);
996     else if (strcmp(name, "child_died") == 0)
997       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
998                                    "child_died", val);
999     else if (strcmp(name, "child_dump") == 0)
1000       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
1001                                    "child_dump", val);
1002     else if (strcmp(name, "child_panic") == 0)
1003       return varnish_submit_derive(conf->instance, "mgt", "total_operations",
1004                                    "child_panic", val);
1005   }
1006 
1007   if (conf->collect_lck) {
1008     if (strcmp(name, "creat") == 0)
1009       return varnish_submit_gauge(conf->instance, "lck", "objects", "created",
1010                                   val);
1011     else if (strcmp(name, "destroy") == 0)
1012       return varnish_submit_gauge(conf->instance, "lck", "objects", "destroyed",
1013                                   val);
1014     else if (strcmp(name, "locks") == 0)
1015       return varnish_submit_derive(conf->instance, "lck", "total_operations",
1016                                    "lock_ops", val);
1017   }
1018 
1019   if (conf->collect_mempool) {
1020     if (strcmp(name, "live") == 0)
1021       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1022                                   "in_use", val);
1023     else if (strcmp(name, "pool") == 0)
1024       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1025                                   "in_pool", val);
1026     else if (strcmp(name, "sz_wanted") == 0)
1027       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
1028                                   "size_requested", val);
1029     else if (strcmp(name, "sz_actual") == 0)
1030       return varnish_submit_gauge(conf->instance, "mempool", "bytes",
1031                                   "size_allocated", val);
1032     else if (strcmp(name, "allocs") == 0)
1033       return varnish_submit_derive(conf->instance, "mempool",
1034                                    "total_operations", "allocations", val);
1035     else if (strcmp(name, "frees") == 0)
1036       return varnish_submit_derive(conf->instance, "mempool",
1037                                    "total_operations", "frees", val);
1038     else if (strcmp(name, "recycle") == 0)
1039       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1040                                   "recycled", val);
1041     else if (strcmp(name, "timeout") == 0)
1042       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1043                                   "timed_out", val);
1044     else if (strcmp(name, "toosmall") == 0)
1045       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1046                                   "too_small", val);
1047     else if (strcmp(name, "surplus") == 0)
1048       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1049                                   "surplus", val);
1050     else if (strcmp(name, "randry") == 0)
1051       return varnish_submit_gauge(conf->instance, "mempool", "objects",
1052                                   "ran_dry", val);
1053   }
1054 
1055   if (conf->collect_mse) {
1056     if (strcmp(name, "c_full") == 0)
1057       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1058                                    "full_allocs", val);
1059     else if (strcmp(name, "c_truncated") == 0)
1060       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1061                                    "truncated_allocs", val);
1062     else if (strcmp(name, "c_expanded") == 0)
1063       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1064                                    "expanded_allocs", val);
1065     else if (strcmp(name, "c_failed") == 0)
1066       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1067                                    "failed_allocs", val);
1068     else if (strcmp(name, "c_bytes") == 0)
1069       return varnish_submit_derive(conf->instance, "mse", "total_bytes",
1070                                    "bytes_allocated", val);
1071     else if (strcmp(name, "c_freed") == 0)
1072       return varnish_submit_derive(conf->instance, "mse", "total_bytes",
1073                                    "bytes_freed", val);
1074     else if (strcmp(name, "g_fo_alloc") == 0)
1075       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1076                                    "fo_allocs_outstanding", val);
1077     else if (strcmp(name, "g_fo_bytes") == 0)
1078       return varnish_submit_gauge(conf->instance, "mse", "bytes",
1079                                   "fo_bytes_outstanding", val);
1080     else if (strcmp(name, "g_membuf_alloc") == 0)
1081       return varnish_submit_gauge(conf->instance, "mse", "objects",
1082                                   "membufs_allocated", val);
1083     else if (strcmp(name, "g_membuf_inuse") == 0)
1084       return varnish_submit_gauge(conf->instance, "mse", "objects",
1085                                   "membufs_inuse", val);
1086     else if (strcmp(name, "g_bans_bytes") == 0)
1087       return varnish_submit_gauge(conf->instance, "mse", "bytes",
1088                                   "persisted_banspace_used", val);
1089     else if (strcmp(name, "g_bans_space") == 0)
1090       return varnish_submit_gauge(conf->instance, "mse", "bytes",
1091                                   "persisted_banspace_available", val);
1092     else if (strcmp(name, "g_bans_persisted") == 0)
1093       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1094                                    "bans_persisted", val);
1095     else if (strcmp(name, "g_bans_lost") == 0)
1096       return varnish_submit_derive(conf->instance, "mse", "total_operations",
1097                                    "bans_lost", val);
1098 
1099     /* mse seg */
1100     else if (strcmp(name, "g_journal_bytes") == 0)
1101       return varnish_submit_gauge(conf->instance, "mse_reg", "bytes",
1102                                   "journal_bytes_used", val);
1103     else if (strcmp(name, "g_journal_space") == 0)
1104       return varnish_submit_gauge(conf->instance, "mse_reg", "bytes",
1105                                   "journal_bytes_free", val);
1106 
1107     /* mse segagg */
1108     else if (strcmp(name, "g_bigspace") == 0)
1109       return varnish_submit_gauge(conf->instance, "mse_segagg", "bytes",
1110                                   "big_extents_bytes_available", val);
1111     else if (strcmp(name, "g_extfree") == 0)
1112       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
1113                                   "free_extents", val);
1114     else if (strcmp(name, "g_sparenode") == 0)
1115       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
1116                                   "spare_nodes_available", val);
1117     else if (strcmp(name, "g_objnode") == 0)
1118       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
1119                                   "object_nodes_in_use", val);
1120     else if (strcmp(name, "g_extnode") == 0)
1121       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
1122                                   "extent_nodes_in_use", val);
1123     else if (strcmp(name, "g_bigextfree") == 0)
1124       return varnish_submit_gauge(conf->instance, "mse_segagg", "objects",
1125                                   "free_big_extents", val);
1126     else if (strcmp(name, "c_pruneloop") == 0)
1127       return varnish_submit_derive(conf->instance, "mse_segagg",
1128                                    "total_operations", "prune_loops", val);
1129     else if (strcmp(name, "c_pruned") == 0)
1130       return varnish_submit_derive(conf->instance, "mse_segagg",
1131                                    "total_objects", "pruned_objects", val);
1132     else if (strcmp(name, "c_spared") == 0)
1133       return varnish_submit_derive(conf->instance, "mse_segagg",
1134                                    "total_operations", "spared_objects", val);
1135     else if (strcmp(name, "c_skipped") == 0)
1136       return varnish_submit_derive(conf->instance, "mse_segagg",
1137                                    "total_operations", "missed_objects", val);
1138     else if (strcmp(name, "c_nuked") == 0)
1139       return varnish_submit_derive(conf->instance, "mse_segagg",
1140                                    "total_operations", "nuked_objects", val);
1141     else if (strcmp(name, "c_sniped") == 0)
1142       return varnish_submit_derive(conf->instance, "mse_segagg",
1143                                    "total_operations", "sniped_objects", val);
1144   }
1145 
1146 #endif
1147 
1148 #if HAVE_VARNISH_V6
1149   if (conf->collect_goto) {
1150     if (strcmp(name, "goto_dns_cache_hits") == 0)
1151       return varnish_submit_derive(conf->instance, "goto", "total_operations",
1152                                    "dns_cache_hits", val);
1153     else if (strcmp(name, "goto_dns_lookups") == 0)
1154       return varnish_submit_derive(conf->instance, "goto", "total_operations",
1155                                    "dns_lookups", val);
1156     else if (strcmp(name, "goto_dns_lookup_fails") == 0)
1157       return varnish_submit_derive(conf->instance, "goto", "total_operations",
1158                                    "dns_lookup_fails", val);
1159   }
1160 #endif
1161 
1162   return 0;
1163 
1164 } /* }}} static int varnish_monitor */
1165 #else /* if HAVE_VARNISH_V2 */
varnish_monitor(const user_config_t * conf,const c_varnish_stats_t * stats)1166 static void varnish_monitor(const user_config_t *conf, /* {{{ */
1167                             const c_varnish_stats_t *stats) {
1168   if (conf->collect_cache) {
1169     /* Cache hits */
1170     varnish_submit_derive(conf->instance, "cache", "cache_result", "hit",
1171                           stats->cache_hit);
1172     /* Cache misses */
1173     varnish_submit_derive(conf->instance, "cache", "cache_result", "miss",
1174                           stats->cache_miss);
1175     /* Cache hits for pass */
1176     varnish_submit_derive(conf->instance, "cache", "cache_result", "hitpass",
1177                           stats->cache_hitpass);
1178   }
1179 
1180   if (conf->collect_connections) {
1181     /* Client connections accepted */
1182     varnish_submit_derive(conf->instance, "connections", "connections",
1183                           "accepted", stats->client_conn);
1184     /* Connection dropped, no sess */
1185     varnish_submit_derive(conf->instance, "connections", "connections",
1186                           "dropped", stats->client_drop);
1187     /* Client requests received    */
1188     varnish_submit_derive(conf->instance, "connections", "connections",
1189                           "received", stats->client_req);
1190   }
1191 
1192   if (conf->collect_esi) {
1193     /* ESI parse errors (unlock)   */
1194     varnish_submit_derive(conf->instance, "esi", "total_operations", "error",
1195                           stats->esi_errors);
1196     /* Objects ESI parsed (unlock) */
1197     varnish_submit_derive(conf->instance, "esi", "total_operations", "parsed",
1198                           stats->esi_parse);
1199   }
1200 
1201   if (conf->collect_backend) {
1202     /* Backend conn. success       */
1203     varnish_submit_derive(conf->instance, "backend", "connections", "success",
1204                           stats->backend_conn);
1205     /* Backend conn. not attempted */
1206     varnish_submit_derive(conf->instance, "backend", "connections",
1207                           "not-attempted", stats->backend_unhealthy);
1208     /* Backend conn. too many      */
1209     varnish_submit_derive(conf->instance, "backend", "connections", "too-many",
1210                           stats->backend_busy);
1211     /* Backend conn. failures      */
1212     varnish_submit_derive(conf->instance, "backend", "connections", "failures",
1213                           stats->backend_fail);
1214     /* Backend conn. reuses        */
1215     varnish_submit_derive(conf->instance, "backend", "connections", "reuses",
1216                           stats->backend_reuse);
1217     /* Backend conn. was closed    */
1218     varnish_submit_derive(conf->instance, "backend", "connections",
1219                           "was-closed", stats->backend_toolate);
1220     /* Backend conn. recycles      */
1221     varnish_submit_derive(conf->instance, "backend", "connections", "recycled",
1222                           stats->backend_recycle);
1223     /* Backend conn. unused        */
1224     varnish_submit_derive(conf->instance, "backend", "connections", "unused",
1225                           stats->backend_unused);
1226     /* Backend requests mades      */
1227     varnish_submit_derive(conf->instance, "backend", "http_requests",
1228                           "requests", stats->backend_req);
1229     /* N backends                  */
1230     varnish_submit_gauge(conf->instance, "backend", "backends", "n_backends",
1231                          stats->n_backend);
1232   }
1233 
1234   if (conf->collect_fetch) {
1235     /* Fetch head                */
1236     varnish_submit_derive(conf->instance, "fetch", "http_requests", "head",
1237                           stats->fetch_head);
1238     /* Fetch with length         */
1239     varnish_submit_derive(conf->instance, "fetch", "http_requests", "length",
1240                           stats->fetch_length);
1241     /* Fetch chunked             */
1242     varnish_submit_derive(conf->instance, "fetch", "http_requests", "chunked",
1243                           stats->fetch_chunked);
1244     /* Fetch EOF                 */
1245     varnish_submit_derive(conf->instance, "fetch", "http_requests", "eof",
1246                           stats->fetch_eof);
1247     /* Fetch bad headers         */
1248     varnish_submit_derive(conf->instance, "fetch", "http_requests",
1249                           "bad_headers", stats->fetch_bad);
1250     /* Fetch wanted close        */
1251     varnish_submit_derive(conf->instance, "fetch", "http_requests", "close",
1252                           stats->fetch_close);
1253     /* Fetch pre HTTP/1.1 closed */
1254     varnish_submit_derive(conf->instance, "fetch", "http_requests", "oldhttp",
1255                           stats->fetch_oldhttp);
1256     /* Fetch zero len            */
1257     varnish_submit_derive(conf->instance, "fetch", "http_requests", "zero",
1258                           stats->fetch_zero);
1259     /* Fetch failed              */
1260     varnish_submit_derive(conf->instance, "fetch", "http_requests", "failed",
1261                           stats->fetch_failed);
1262   }
1263 
1264   if (conf->collect_hcb) {
1265     /* HCB Lookups without lock */
1266     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
1267                           "lookup_nolock", stats->hcb_nolock);
1268     /* HCB Lookups with lock    */
1269     varnish_submit_derive(conf->instance, "hcb", "cache_operation",
1270                           "lookup_lock", stats->hcb_lock);
1271     /* HCB Inserts              */
1272     varnish_submit_derive(conf->instance, "hcb", "cache_operation", "insert",
1273                           stats->hcb_insert);
1274   }
1275 
1276   if (conf->collect_objects) {
1277     /* N expired objects             */
1278     varnish_submit_derive(conf->instance, "objects", "total_objects", "expired",
1279                           stats->n_expired);
1280     /* N LRU nuked objects           */
1281     varnish_submit_derive(conf->instance, "objects", "total_objects",
1282                           "lru_nuked", stats->n_lru_nuked);
1283     /* N LRU saved objects           */
1284     varnish_submit_derive(conf->instance, "objects", "total_objects",
1285                           "lru_saved", stats->n_lru_saved);
1286     /* N LRU moved objects           */
1287     varnish_submit_derive(conf->instance, "objects", "total_objects",
1288                           "lru_moved", stats->n_lru_moved);
1289     /* N objects on deathrow         */
1290     varnish_submit_derive(conf->instance, "objects", "total_objects",
1291                           "deathrow", stats->n_deathrow);
1292     /* HTTP header overflows         */
1293     varnish_submit_derive(conf->instance, "objects", "total_objects",
1294                           "header_overflow", stats->losthdr);
1295     /* Objects sent with sendfile    */
1296     varnish_submit_derive(conf->instance, "objects", "total_objects",
1297                           "sent_sendfile", stats->n_objsendfile);
1298     /* Objects sent with write       */
1299     varnish_submit_derive(conf->instance, "objects", "total_objects",
1300                           "sent_write", stats->n_objwrite);
1301     /* Objects overflowing workspace */
1302     varnish_submit_derive(conf->instance, "objects", "total_objects",
1303                           "workspace_overflow", stats->n_objoverflow);
1304   }
1305 
1306   if (conf->collect_purge) {
1307     /* N total active purges      */
1308     varnish_submit_derive(conf->instance, "purge", "total_operations", "total",
1309                           stats->n_purge);
1310     /* N new purges added         */
1311     varnish_submit_derive(conf->instance, "purge", "total_operations", "added",
1312                           stats->n_purge_add);
1313     /* N old purges deleted       */
1314     varnish_submit_derive(conf->instance, "purge", "total_operations",
1315                           "deleted", stats->n_purge_retire);
1316     /* N objects tested           */
1317     varnish_submit_derive(conf->instance, "purge", "total_operations",
1318                           "objects_tested", stats->n_purge_obj_test);
1319     /* N regexps tested against   */
1320     varnish_submit_derive(conf->instance, "purge", "total_operations",
1321                           "regexps_tested", stats->n_purge_re_test);
1322     /* N duplicate purges removed */
1323     varnish_submit_derive(conf->instance, "purge", "total_operations",
1324                           "duplicate", stats->n_purge_dups);
1325   }
1326 
1327   if (conf->collect_session) {
1328     /* Session Closed     */
1329     varnish_submit_derive(conf->instance, "session", "total_operations",
1330                           "closed", stats->sess_closed);
1331     /* Session Pipeline   */
1332     varnish_submit_derive(conf->instance, "session", "total_operations",
1333                           "pipeline", stats->sess_pipeline);
1334     /* Session Read Ahead */
1335     varnish_submit_derive(conf->instance, "session", "total_operations",
1336                           "readahead", stats->sess_readahead);
1337     /* Session Linger     */
1338     varnish_submit_derive(conf->instance, "session", "total_operations",
1339                           "linger", stats->sess_linger);
1340     /* Session herd       */
1341     varnish_submit_derive(conf->instance, "session", "total_operations", "herd",
1342                           stats->sess_herd);
1343   }
1344 
1345   if (conf->collect_shm) {
1346     /* SHM records                 */
1347     varnish_submit_derive(conf->instance, "shm", "total_operations", "records",
1348                           stats->shm_records);
1349     /* SHM writes                  */
1350     varnish_submit_derive(conf->instance, "shm", "total_operations", "writes",
1351                           stats->shm_writes);
1352     /* SHM flushes due to overflow */
1353     varnish_submit_derive(conf->instance, "shm", "total_operations", "flushes",
1354                           stats->shm_flushes);
1355     /* SHM MTX contention          */
1356     varnish_submit_derive(conf->instance, "shm", "total_operations",
1357                           "contention", stats->shm_cont);
1358     /* SHM cycles through buffer   */
1359     varnish_submit_derive(conf->instance, "shm", "total_operations", "cycles",
1360                           stats->shm_cycles);
1361   }
1362 
1363   if (conf->collect_sm) {
1364     /* allocator requests */
1365     varnish_submit_derive(conf->instance, "sm", "total_requests", "nreq",
1366                           stats->sm_nreq);
1367     /* outstanding allocations */
1368     varnish_submit_gauge(conf->instance, "sm", "requests", "outstanding",
1369                          stats->sm_nobj);
1370     /* bytes allocated */
1371     varnish_submit_derive(conf->instance, "sm", "total_bytes", "allocated",
1372                           stats->sm_balloc);
1373     /* bytes free */
1374     varnish_submit_derive(conf->instance, "sm", "total_bytes", "free",
1375                           stats->sm_bfree);
1376   }
1377 
1378   if (conf->collect_sma) {
1379     /* SMA allocator requests */
1380     varnish_submit_derive(conf->instance, "sma", "total_requests", "nreq",
1381                           stats->sma_nreq);
1382     /* SMA outstanding allocations */
1383     varnish_submit_gauge(conf->instance, "sma", "requests", "outstanding",
1384                          stats->sma_nobj);
1385     /* SMA outstanding bytes */
1386     varnish_submit_gauge(conf->instance, "sma", "bytes", "outstanding",
1387                          stats->sma_nbytes);
1388     /* SMA bytes allocated */
1389     varnish_submit_derive(conf->instance, "sma", "total_bytes", "allocated",
1390                           stats->sma_balloc);
1391     /* SMA bytes free */
1392     varnish_submit_derive(conf->instance, "sma", "total_bytes", "free",
1393                           stats->sma_bfree);
1394   }
1395 
1396   if (conf->collect_sms) {
1397     /* SMS allocator requests */
1398     varnish_submit_derive(conf->instance, "sms", "total_requests", "allocator",
1399                           stats->sms_nreq);
1400     /* SMS outstanding allocations */
1401     varnish_submit_gauge(conf->instance, "sms", "requests", "outstanding",
1402                          stats->sms_nobj);
1403     /* SMS outstanding bytes */
1404     varnish_submit_gauge(conf->instance, "sms", "bytes", "outstanding",
1405                          stats->sms_nbytes);
1406     /* SMS bytes allocated */
1407     varnish_submit_derive(conf->instance, "sms", "total_bytes", "allocated",
1408                           stats->sms_balloc);
1409     /* SMS bytes freed */
1410     varnish_submit_derive(conf->instance, "sms", "total_bytes", "free",
1411                           stats->sms_bfree);
1412   }
1413 
1414   if (conf->collect_struct) {
1415     /* N struct sess_mem       */
1416     varnish_submit_gauge(conf->instance, "struct", "current_sessions",
1417                          "sess_mem", stats->n_sess_mem);
1418     /* N struct sess           */
1419     varnish_submit_gauge(conf->instance, "struct", "current_sessions", "sess",
1420                          stats->n_sess);
1421     /* N struct object         */
1422     varnish_submit_gauge(conf->instance, "struct", "objects", "object",
1423                          stats->n_object);
1424     /* N struct objecthead     */
1425     varnish_submit_gauge(conf->instance, "struct", "objects", "objecthead",
1426                          stats->n_objecthead);
1427     /* N struct smf            */
1428     varnish_submit_gauge(conf->instance, "struct", "objects", "smf",
1429                          stats->n_smf);
1430     /* N small free smf         */
1431     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_frag",
1432                          stats->n_smf_frag);
1433     /* N large free smf         */
1434     varnish_submit_gauge(conf->instance, "struct", "objects", "smf_large",
1435                          stats->n_smf_large);
1436     /* N struct vbe_conn        */
1437     varnish_submit_gauge(conf->instance, "struct", "objects", "vbe_conn",
1438                          stats->n_vbe_conn);
1439   }
1440 
1441   if (conf->collect_totals) {
1442     /* Total Sessions */
1443     varnish_submit_derive(conf->instance, "totals", "total_sessions",
1444                           "sessions", stats->s_sess);
1445     /* Total Requests */
1446     varnish_submit_derive(conf->instance, "totals", "total_requests",
1447                           "requests", stats->s_req);
1448     /* Total pipe */
1449     varnish_submit_derive(conf->instance, "totals", "total_operations", "pipe",
1450                           stats->s_pipe);
1451     /* Total pass */
1452     varnish_submit_derive(conf->instance, "totals", "total_operations", "pass",
1453                           stats->s_pass);
1454     /* Total fetch */
1455     varnish_submit_derive(conf->instance, "totals", "total_operations",
1456                           "fetches", stats->s_fetch);
1457     /* Total header bytes */
1458     varnish_submit_derive(conf->instance, "totals", "total_bytes",
1459                           "header-bytes", stats->s_hdrbytes);
1460     /* Total body byte */
1461     varnish_submit_derive(conf->instance, "totals", "total_bytes", "body-bytes",
1462                           stats->s_bodybytes);
1463   }
1464 
1465   if (conf->collect_vcl) {
1466     /* N vcl total     */
1467     varnish_submit_gauge(conf->instance, "vcl", "vcl", "total_vcl",
1468                          stats->n_vcl);
1469     /* N vcl available */
1470     varnish_submit_gauge(conf->instance, "vcl", "vcl", "avail_vcl",
1471                          stats->n_vcl_avail);
1472     /* N vcl discarded */
1473     varnish_submit_gauge(conf->instance, "vcl", "vcl", "discarded_vcl",
1474                          stats->n_vcl_discard);
1475   }
1476 
1477   if (conf->collect_workers) {
1478     /* worker threads */
1479     varnish_submit_gauge(conf->instance, "workers", "threads", "worker",
1480                          stats->n_wrk);
1481     /* worker threads created */
1482     varnish_submit_derive(conf->instance, "workers", "total_threads", "created",
1483                           stats->n_wrk_create);
1484     /* worker threads not created */
1485     varnish_submit_derive(conf->instance, "workers", "total_threads", "failed",
1486                           stats->n_wrk_failed);
1487     /* worker threads limited */
1488     varnish_submit_derive(conf->instance, "workers", "total_threads", "limited",
1489                           stats->n_wrk_max);
1490     /* dropped work requests */
1491     varnish_submit_derive(conf->instance, "workers", "total_threads", "dropped",
1492                           stats->n_wrk_drop);
1493     /* queued work requests */
1494     varnish_submit_derive(conf->instance, "workers", "total_requests", "queued",
1495                           stats->n_wrk_queue);
1496     /* overflowed work requests */
1497     varnish_submit_derive(conf->instance, "workers", "total_requests",
1498                           "overflowed", stats->n_wrk_overflow);
1499   }
1500 
1501 } /* }}} void varnish_monitor */
1502 #endif
1503 
1504 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
varnish_read(user_data_t * ud)1505 static int varnish_read(user_data_t *ud) /* {{{ */
1506 {
1507 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1508   struct VSM_data *vd;
1509   bool ok;
1510   const c_varnish_stats_t *stats;
1511 #elif HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1512   struct vsm *vd;
1513   struct vsc *vsc;
1514   int vsm_status;
1515 #endif
1516 
1517   user_config_t *conf;
1518 
1519   if ((ud == NULL) || (ud->data == NULL))
1520     return EINVAL;
1521 
1522   conf = ud->data;
1523 
1524   vd = VSM_New();
1525 
1526 #if HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1527   vsc = VSC_New();
1528 #endif
1529 
1530 #if HAVE_VARNISH_V3
1531   VSC_Setup(vd);
1532 #endif
1533 
1534   if (conf->instance != NULL) {
1535     int status;
1536 
1537 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1538     status = VSM_n_Arg(vd, conf->instance);
1539 #elif HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1540     status = VSM_Arg(vd, 'n', conf->instance);
1541 #endif
1542 
1543     if (status < 0) {
1544 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1545       VSM_Delete(vd);
1546 #elif HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1547       VSC_Destroy(&vsc, vd);
1548       VSM_Destroy(&vd);
1549 #endif
1550       ERROR("varnish plugin: VSM_Arg (\"%s\") failed "
1551             "with status %i.",
1552             conf->instance, status);
1553       return -1;
1554     }
1555   }
1556 
1557 #if HAVE_VARNISH_V3
1558   ok = (VSC_Open(vd, /* diag = */ 1) == 0);
1559 #elif HAVE_VARNISH_V4
1560   ok = (VSM_Open(vd) == 0);
1561 #endif
1562 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1563   if (!ok) {
1564     VSM_Delete(vd);
1565     ERROR("varnish plugin: Unable to open connection.");
1566     return -1;
1567   }
1568 #endif
1569 
1570 #if HAVE_VARNISH_V3
1571   stats = VSC_Main(vd);
1572 #elif HAVE_VARNISH_V4
1573   stats = VSC_Main(vd, NULL);
1574 #endif
1575 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1576   if (!stats) {
1577     VSM_Delete(vd);
1578     ERROR("varnish plugin: Unable to get statistics.");
1579     return -1;
1580   }
1581 #endif
1582 
1583 #if HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1584   if (VSM_Attach(vd, STDERR_FILENO)) {
1585     ERROR("varnish plugin: Cannot attach to varnish. %s", VSM_Error(vd));
1586     VSC_Destroy(&vsc, vd);
1587     VSM_Destroy(&vd);
1588     return -1;
1589   }
1590 
1591   vsm_status = VSM_Status(vd);
1592   if (vsm_status & ~(VSM_MGT_RUNNING | VSM_WRK_RUNNING)) {
1593     ERROR("varnish plugin: Unable to get statistics.");
1594     VSC_Destroy(&vsc, vd);
1595     VSM_Destroy(&vd);
1596     return -1;
1597   }
1598 #endif
1599 
1600 #if HAVE_VARNISH_V3
1601   VSC_Iter(vd, varnish_monitor, conf);
1602 #elif HAVE_VARNISH_V4
1603   VSC_Iter(vd, NULL, varnish_monitor, conf);
1604 #elif HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1605   VSC_Iter(vsc, vd, varnish_monitor, conf);
1606 #endif
1607 
1608 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
1609   VSM_Delete(vd);
1610 #elif HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1611   VSC_Destroy(&vsc, vd);
1612   VSM_Destroy(&vd);
1613 #endif
1614 
1615   return 0;
1616 } /* }}} */
1617 #else /* if HAVE_VARNISH_V2 */
varnish_read(user_data_t * ud)1618 static int varnish_read(user_data_t *ud) /* {{{ */
1619 {
1620   const c_varnish_stats_t *stats;
1621 
1622   user_config_t *conf;
1623 
1624   if ((ud == NULL) || (ud->data == NULL))
1625     return EINVAL;
1626 
1627   conf = ud->data;
1628 
1629   stats = VSL_OpenStats(conf->instance);
1630   if (stats == NULL) {
1631     ERROR("Varnish plugin : unable to load statistics");
1632 
1633     return -1;
1634   }
1635 
1636   varnish_monitor(conf, stats);
1637 
1638   return 0;
1639 } /* }}} */
1640 #endif
1641 
varnish_config_free(void * ptr)1642 static void varnish_config_free(void *ptr) /* {{{ */
1643 {
1644   user_config_t *conf = ptr;
1645 
1646   if (conf == NULL)
1647     return;
1648 
1649   sfree(conf->instance);
1650   sfree(conf);
1651 } /* }}} */
1652 
varnish_config_apply_default(user_config_t * conf)1653 static int varnish_config_apply_default(user_config_t *conf) /* {{{ */
1654 {
1655   if (conf == NULL)
1656     return EINVAL;
1657 
1658   conf->collect_backend = true;
1659   conf->collect_cache = true;
1660   conf->collect_connections = true;
1661 #ifdef HAVE_VARNISH_V3
1662   conf->collect_dirdns = false;
1663 #endif
1664   conf->collect_esi = false;
1665   conf->collect_fetch = false;
1666   conf->collect_hcb = false;
1667   conf->collect_objects = false;
1668 #if HAVE_VARNISH_V2
1669   conf->collect_purge = false;
1670 #else
1671   conf->collect_ban = false;
1672 #endif
1673   conf->collect_session = false;
1674   conf->collect_shm = true;
1675 #if HAVE_VARNISH_V2
1676   conf->collect_sm = false;
1677 #endif
1678 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1679   conf->collect_sma = false;
1680 #endif
1681   conf->collect_sms = false;
1682   conf->collect_struct = false;
1683   conf->collect_totals = false;
1684 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1685   conf->collect_uptime = false;
1686 #endif
1687   conf->collect_vcl = false;
1688   conf->collect_workers = false;
1689 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1690   conf->collect_vsm = false;
1691 #endif
1692 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1693   conf->collect_lck = false;
1694   conf->collect_mempool = false;
1695   conf->collect_mgt = false;
1696   conf->collect_smf = false;
1697   conf->collect_vbe = false;
1698   conf->collect_mse = false;
1699 #endif
1700 #if HAVE_VARNISH_V6
1701   conf->collect_goto = false;
1702 #endif
1703 
1704   return 0;
1705 } /* }}} int varnish_config_apply_default */
1706 
varnish_init(void)1707 static int varnish_init(void) /* {{{ */
1708 {
1709   user_config_t *conf;
1710 
1711   if (have_instance)
1712     return 0;
1713 
1714   conf = calloc(1, sizeof(*conf));
1715   if (conf == NULL)
1716     return ENOMEM;
1717 
1718   /* Default settings: */
1719   conf->instance = NULL;
1720 
1721   varnish_config_apply_default(conf);
1722 
1723   plugin_register_complex_read(
1724       /* group = */ "varnish",
1725       /* name      = */ "varnish/localhost",
1726       /* callback  = */ varnish_read,
1727       /* interval  = */ 0,
1728       &(user_data_t){
1729           .data = conf,
1730           .free_func = varnish_config_free,
1731       });
1732 
1733   return 0;
1734 } /* }}} int varnish_init */
1735 
varnish_config_instance(const oconfig_item_t * ci)1736 static int varnish_config_instance(const oconfig_item_t *ci) /* {{{ */
1737 {
1738   user_config_t *conf;
1739   char callback_name[DATA_MAX_NAME_LEN];
1740 
1741   conf = calloc(1, sizeof(*conf));
1742   if (conf == NULL)
1743     return ENOMEM;
1744   conf->instance = NULL;
1745 
1746   varnish_config_apply_default(conf);
1747 
1748   if (ci->values_num == 1) {
1749     int status;
1750 
1751     status = cf_util_get_string(ci, &conf->instance);
1752     if (status != 0) {
1753       sfree(conf);
1754       return status;
1755     }
1756     assert(conf->instance != NULL);
1757 
1758     if (strcmp("localhost", conf->instance) == 0) {
1759       sfree(conf->instance);
1760       conf->instance = NULL;
1761     }
1762   } else if (ci->values_num > 1) {
1763     WARNING("Varnish plugin: \"Instance\" blocks accept only "
1764             "one argument.");
1765     sfree(conf);
1766     return EINVAL;
1767   }
1768 
1769   for (int i = 0; i < ci->children_num; i++) {
1770     oconfig_item_t *child = ci->children + i;
1771 
1772     if (strcasecmp("CollectCache", child->key) == 0)
1773       cf_util_get_boolean(child, &conf->collect_cache);
1774     else if (strcasecmp("CollectConnections", child->key) == 0)
1775       cf_util_get_boolean(child, &conf->collect_connections);
1776     else if (strcasecmp("CollectESI", child->key) == 0)
1777       cf_util_get_boolean(child, &conf->collect_esi);
1778     else if (strcasecmp("CollectDirectorDNS", child->key) == 0)
1779 #ifdef HAVE_VARNISH_V3
1780       cf_util_get_boolean(child, &conf->collect_dirdns);
1781 #else
1782       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1783               child->key, "v3");
1784 #endif
1785     else if (strcasecmp("CollectBackend", child->key) == 0)
1786       cf_util_get_boolean(child, &conf->collect_backend);
1787     else if (strcasecmp("CollectFetch", child->key) == 0)
1788       cf_util_get_boolean(child, &conf->collect_fetch);
1789     else if (strcasecmp("CollectHCB", child->key) == 0)
1790       cf_util_get_boolean(child, &conf->collect_hcb);
1791     else if (strcasecmp("CollectObjects", child->key) == 0)
1792       cf_util_get_boolean(child, &conf->collect_objects);
1793     else if (strcasecmp("CollectPurge", child->key) == 0)
1794 #if HAVE_VARNISH_V2
1795       cf_util_get_boolean(child, &conf->collect_purge);
1796 #else
1797       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1798               child->key, "v2");
1799 #endif
1800     else if (strcasecmp("CollectBan", child->key) == 0)
1801 #if HAVE_VARNISH_V2
1802       WARNING("Varnish plugin: \"%s\" is not available for Varnish %s.",
1803               child->key, "v2");
1804 #else
1805       cf_util_get_boolean(child, &conf->collect_ban);
1806 #endif
1807     else if (strcasecmp("CollectSession", child->key) == 0)
1808       cf_util_get_boolean(child, &conf->collect_session);
1809     else if (strcasecmp("CollectSHM", child->key) == 0)
1810       cf_util_get_boolean(child, &conf->collect_shm);
1811     else if (strcasecmp("CollectSMS", child->key) == 0)
1812       cf_util_get_boolean(child, &conf->collect_sms);
1813     else if (strcasecmp("CollectSMA", child->key) == 0)
1814 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1815       cf_util_get_boolean(child, &conf->collect_sma);
1816 #else
1817       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1818               child->key, "v2 and v4");
1819 #endif
1820     else if (strcasecmp("CollectSM", child->key) == 0)
1821 #if HAVE_VARNISH_V2
1822       cf_util_get_boolean(child, &conf->collect_sm);
1823 #else
1824       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1825               child->key, "v2");
1826 #endif
1827     else if (strcasecmp("CollectStruct", child->key) == 0)
1828       cf_util_get_boolean(child, &conf->collect_struct);
1829     else if (strcasecmp("CollectTotals", child->key) == 0)
1830       cf_util_get_boolean(child, &conf->collect_totals);
1831     else if (strcasecmp("CollectUptime", child->key) == 0)
1832 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1833       cf_util_get_boolean(child, &conf->collect_uptime);
1834 #else
1835       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1836               child->key, "v3 and v4");
1837 #endif
1838     else if (strcasecmp("CollectVCL", child->key) == 0)
1839       cf_util_get_boolean(child, &conf->collect_vcl);
1840     else if (strcasecmp("CollectWorkers", child->key) == 0)
1841       cf_util_get_boolean(child, &conf->collect_workers);
1842     else if (strcasecmp("CollectVSM", child->key) == 0)
1843 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1844       cf_util_get_boolean(child, &conf->collect_vsm);
1845 #else
1846       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1847               child->key, "v4 or v5");
1848 #endif
1849     else if (strcasecmp("CollectLock", child->key) == 0)
1850 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1851       cf_util_get_boolean(child, &conf->collect_lck);
1852 #else
1853       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1854               child->key, "v4");
1855 #endif
1856     else if (strcasecmp("CollectMempool", child->key) == 0)
1857 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1858       cf_util_get_boolean(child, &conf->collect_mempool);
1859 #else
1860       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1861               child->key, "v4");
1862 #endif
1863     else if (strcasecmp("CollectManagement", child->key) == 0)
1864 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1865       cf_util_get_boolean(child, &conf->collect_mgt);
1866 #else
1867       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1868               child->key, "v4");
1869 #endif
1870     else if (strcasecmp("CollectSMF", child->key) == 0)
1871 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1872       cf_util_get_boolean(child, &conf->collect_smf);
1873 #else
1874       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1875               child->key, "v4");
1876 #endif
1877     else if (strcasecmp("CollectSMF", child->key) == 0)
1878 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1879       cf_util_get_boolean(child, &conf->collect_smf);
1880 #else
1881       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1882               child->key, "v4");
1883 #endif
1884     else if (strcasecmp("CollectVBE", child->key) == 0)
1885 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1886       cf_util_get_boolean(child, &conf->collect_vbe);
1887 #else
1888       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1889               child->key, "v4");
1890 #endif
1891     else if (strcasecmp("CollectMSE", child->key) == 0)
1892 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1893       cf_util_get_boolean(child, &conf->collect_mse);
1894 #else
1895       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1896               child->key, "Plus v4");
1897 #endif
1898     else if (strcasecmp("CollectGOTO", child->key) == 0)
1899 #if HAVE_VARNISH_V6
1900       cf_util_get_boolean(child, &conf->collect_goto);
1901 #else
1902       WARNING("Varnish plugin: \"%s\" is available for Varnish %s only.",
1903               child->key, "v6");
1904 #endif
1905     else {
1906       WARNING("Varnish plugin: Ignoring unknown "
1907               "configuration option: \"%s\". Did "
1908               "you forget to add an <Instance /> "
1909               "block around the configuration?",
1910               child->key);
1911     }
1912   }
1913 
1914   if (!conf->collect_cache && !conf->collect_connections &&
1915       !conf->collect_esi && !conf->collect_backend
1916 #ifdef HAVE_VARNISH_V3
1917       && !conf->collect_dirdns
1918 #endif
1919       && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects
1920 #if HAVE_VARNISH_V2
1921       && !conf->collect_purge
1922 #else
1923       && !conf->collect_ban
1924 #endif
1925       && !conf->collect_session && !conf->collect_shm && !conf->collect_sms
1926 #if HAVE_VARNISH_V2
1927       && !conf->collect_sm
1928 #endif
1929 #if HAVE_VARNISH_V2 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1930       && !conf->collect_sma
1931 #endif
1932       && !conf->collect_struct && !conf->collect_totals
1933 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1934       && !conf->collect_uptime
1935 #endif
1936       && !conf->collect_vcl && !conf->collect_workers
1937 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5
1938       && !conf->collect_vsm
1939 #endif
1940 #if HAVE_VARNISH_V4 || HAVE_VARNISH_V5 || HAVE_VARNISH_V6
1941       && !conf->collect_vbe && !conf->collect_smf && !conf->collect_mgt &&
1942       !conf->collect_lck && !conf->collect_mempool && !conf->collect_mse
1943 #endif
1944 #if HAVE_VARNISH_V6
1945       && !conf->collect_goto
1946 #endif
1947   ) {
1948     WARNING("Varnish plugin: No metric has been configured for "
1949             "instance \"%s\". Disabling this instance.",
1950             (conf->instance == NULL) ? "localhost" : conf->instance);
1951     sfree(conf);
1952     return EINVAL;
1953   }
1954 
1955   ssnprintf(callback_name, sizeof(callback_name), "varnish/%s",
1956             (conf->instance == NULL) ? "localhost" : conf->instance);
1957 
1958   plugin_register_complex_read(
1959       /* group = */ "varnish",
1960       /* name      = */ callback_name,
1961       /* callback  = */ varnish_read,
1962       /* interval  = */ 0,
1963       &(user_data_t){
1964           .data = conf,
1965           .free_func = varnish_config_free,
1966       });
1967 
1968   have_instance = true;
1969 
1970   return 0;
1971 } /* }}} int varnish_config_instance */
1972 
varnish_config(oconfig_item_t * ci)1973 static int varnish_config(oconfig_item_t *ci) /* {{{ */
1974 {
1975   for (int i = 0; i < ci->children_num; i++) {
1976     oconfig_item_t *child = ci->children + i;
1977 
1978     if (strcasecmp("Instance", child->key) == 0)
1979       varnish_config_instance(child);
1980     else {
1981       WARNING("Varnish plugin: Ignoring unknown "
1982               "configuration option: \"%s\"",
1983               child->key);
1984     }
1985   }
1986 
1987   return 0;
1988 } /* }}} int varnish_config */
1989 
module_register(void)1990 void module_register(void) /* {{{ */
1991 {
1992   plugin_register_complex_config("varnish", varnish_config);
1993   plugin_register_init("varnish", varnish_init);
1994 } /* }}} */
1995