varnish: isolate varnish v2 code
[collectd.git] / src / varnish.c
1 /**
2  * collectd - src/varnish.c
3  * Copyright (C) 2010      Jérôme Renard
4  * Copyright (C) 2010      Marc Fournier
5  * Copyright (C) 2010-2012 Florian Forster
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Jérôme Renard <jerome.renard at gmail.com>
22  *   Marc Fournier <marc.fournier at camptocamp.com>
23  *   Florian octo Forster <octo at collectd.org>
24  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30
31 #if HAVE_VARNISH_V4
32 #include <vapi/vsm.h>
33 #include <vapi/vsc.h>
34 typedef struct VSC_C_main c_varnish_stats_t;
35 #endif
36
37 #if HAVE_VARNISH_V3
38 #include <varnishapi.h>
39 #include <vsc.h>
40 typedef struct VSC_C_main c_varnish_stats_t;
41 #endif
42
43 #if HAVE_VARNISH_V2
44 #include <varnishapi.h>
45 typedef struct varnish_stats c_varnish_stats_t;
46 #endif
47
48 /* {{{ user_config_s */
49 struct user_config_s {
50         char *instance;
51
52         _Bool collect_cache;
53         _Bool collect_connections;
54         _Bool collect_esi;
55         _Bool collect_backend;
56 #ifdef HAVE_VARNISH_V3
57         _Bool collect_dirdns;
58 #endif
59         _Bool collect_fetch;
60         _Bool collect_hcb;
61         _Bool collect_objects;
62 #if HAVE_VARNISH_V2
63         _Bool collect_purge;
64 #else
65         _Bool collect_ban;
66 #endif
67         _Bool collect_session;
68         _Bool collect_shm;
69         _Bool collect_sms;
70 #if HAVE_VARNISH_V2
71         _Bool collect_sm;
72         _Bool collect_sma;
73 #endif
74         _Bool collect_struct;
75         _Bool collect_totals;
76 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
77         _Bool collect_uptime;
78 #endif
79         _Bool collect_vcl;
80         _Bool collect_workers;
81 #if HAVE_VARNISH_V4
82         _Bool collect_vsm;
83 #endif
84 };
85 typedef struct user_config_s user_config_t; /* }}} */
86
87 static _Bool have_instance = 0;
88
89 static int varnish_submit (const char *plugin_instance, /* {{{ */
90                 const char *category, const char *type, const char *type_instance, value_t value)
91 {
92         value_list_t vl = VALUE_LIST_INIT;
93
94         vl.values = &value;
95         vl.values_len = 1;
96
97         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
98
99         sstrncpy (vl.plugin, "varnish", sizeof (vl.plugin));
100
101         if (plugin_instance == NULL)
102                 plugin_instance = "default";
103
104         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
105                 "%s-%s", plugin_instance, category);
106
107         sstrncpy (vl.type, type, sizeof (vl.type));
108
109         if (type_instance != NULL)
110                 sstrncpy (vl.type_instance, type_instance,
111                                 sizeof (vl.type_instance));
112
113         return (plugin_dispatch_values (&vl));
114 } /* }}} int varnish_submit */
115
116 static int varnish_submit_gauge (const char *plugin_instance, /* {{{ */
117                 const char *category, const char *type, const char *type_instance,
118                 uint64_t gauge_value)
119 {
120         value_t value;
121
122         value.gauge = (gauge_t) gauge_value;
123
124         return (varnish_submit (plugin_instance, category, type, type_instance, value));
125 } /* }}} int varnish_submit_gauge */
126
127 static int varnish_submit_derive (const char *plugin_instance, /* {{{ */
128                 const char *category, const char *type, const char *type_instance,
129                 uint64_t derive_value)
130 {
131         value_t value;
132
133         value.derive = (derive_t) derive_value;
134
135         return (varnish_submit (plugin_instance, category, type, type_instance, value));
136 } /* }}} int varnish_submit_derive */
137
138 #if HAVE_VARNISH_V2
139 static void varnish_monitor (const user_config_t *conf, /* {{{ */
140                 const c_varnish_stats_t *stats)
141 {
142         if (conf->collect_cache)
143         {
144                 /* Cache hits */
145                 varnish_submit_derive (conf->instance, "cache", "cache_result", "hit",     stats->cache_hit);
146                 /* Cache misses */
147                 varnish_submit_derive (conf->instance, "cache", "cache_result", "miss",    stats->cache_miss);
148                 /* Cache hits for pass */
149                 varnish_submit_derive (conf->instance, "cache", "cache_result", "hitpass", stats->cache_hitpass);
150         }
151
152         if (conf->collect_connections)
153         {
154                 /* Client connections accepted */
155                 varnish_submit_derive (conf->instance, "connections", "connections", "accepted", stats->client_conn);
156                 /* Connection dropped, no sess */
157                 varnish_submit_derive (conf->instance, "connections", "connections", "dropped" , stats->client_drop);
158                 /* Client requests received    */
159                 varnish_submit_derive (conf->instance, "connections", "connections", "received", stats->client_req);
160         }
161
162         if (conf->collect_esi)
163         {
164                 /* ESI parse errors (unlock)   */
165                 varnish_submit_derive (conf->instance, "esi", "total_operations", "error",   stats->esi_errors);
166                 /* Objects ESI parsed (unlock) */
167                 varnish_submit_derive (conf->instance, "esi", "total_operations", "parsed",  stats->esi_parse);
168         }
169
170         if (conf->collect_backend)
171         {
172                 /* Backend conn. success       */
173                 varnish_submit_derive (conf->instance, "backend", "connections", "success"      , stats->backend_conn);
174                 /* Backend conn. not attempted */
175                 varnish_submit_derive (conf->instance, "backend", "connections", "not-attempted", stats->backend_unhealthy);
176                 /* Backend conn. too many      */
177                 varnish_submit_derive (conf->instance, "backend", "connections", "too-many"     , stats->backend_busy);
178                 /* Backend conn. failures      */
179                 varnish_submit_derive (conf->instance, "backend", "connections", "failures"     , stats->backend_fail);
180                 /* Backend conn. reuses        */
181                 varnish_submit_derive (conf->instance, "backend", "connections", "reuses"       , stats->backend_reuse);
182                 /* Backend conn. was closed    */
183                 varnish_submit_derive (conf->instance, "backend", "connections", "was-closed"   , stats->backend_toolate);
184                 /* Backend conn. recycles      */
185                 varnish_submit_derive (conf->instance, "backend", "connections", "recycled"     , stats->backend_recycle);
186                 /* Backend conn. unused        */
187                 varnish_submit_derive (conf->instance, "backend", "connections", "unused"       , stats->backend_unused);
188                 /* Backend requests mades      */
189                 varnish_submit_derive (conf->instance, "backend", "http_requests", "requests"   , stats->backend_req);
190                 /* N backends                  */
191                 varnish_submit_gauge  (conf->instance, "backend", "backends", "n_backends"      , stats->n_backend);
192         }
193
194         if (conf->collect_fetch)
195         {
196                 /* Fetch head                */
197                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "head"       , stats->fetch_head);
198                 /* Fetch with length         */
199                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "length"     , stats->fetch_length);
200                 /* Fetch chunked             */
201                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "chunked"    , stats->fetch_chunked);
202                 /* Fetch EOF                 */
203                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "eof"        , stats->fetch_eof);
204                 /* Fetch bad headers         */
205                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "bad_headers", stats->fetch_bad);
206                 /* Fetch wanted close        */
207                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "close"      , stats->fetch_close);
208                 /* Fetch pre HTTP/1.1 closed */
209                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "oldhttp"    , stats->fetch_oldhttp);
210                 /* Fetch zero len            */
211                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "zero"       , stats->fetch_zero);
212                 /* Fetch failed              */
213                 varnish_submit_derive (conf->instance, "fetch", "http_requests", "failed"     , stats->fetch_failed);
214         }
215
216         if (conf->collect_hcb)
217         {
218                 /* HCB Lookups without lock */
219                 varnish_submit_derive (conf->instance, "hcb", "cache_operation", "lookup_nolock", stats->hcb_nolock);
220                 /* HCB Lookups with lock    */
221                 varnish_submit_derive (conf->instance, "hcb", "cache_operation", "lookup_lock",   stats->hcb_lock);
222                 /* HCB Inserts              */
223                 varnish_submit_derive (conf->instance, "hcb", "cache_operation", "insert",        stats->hcb_insert);
224         }
225
226         if (conf->collect_objects)
227         {
228                 /* N expired objects             */
229                 varnish_submit_derive (conf->instance, "objects", "total_objects", "expired",            stats->n_expired);
230                 /* N LRU nuked objects           */
231                 varnish_submit_derive (conf->instance, "objects", "total_objects", "lru_nuked",          stats->n_lru_nuked);
232                 /* N LRU saved objects           */
233                 varnish_submit_derive (conf->instance, "objects", "total_objects", "lru_saved",          stats->n_lru_saved);
234                 /* N LRU moved objects           */
235                 varnish_submit_derive (conf->instance, "objects", "total_objects", "lru_moved",          stats->n_lru_moved);
236                 /* N objects on deathrow         */
237                 varnish_submit_derive (conf->instance, "objects", "total_objects", "deathrow",           stats->n_deathrow);
238                 /* HTTP header overflows         */
239                 varnish_submit_derive (conf->instance, "objects", "total_objects", "header_overflow",    stats->losthdr);
240                 /* Objects sent with sendfile    */
241                 varnish_submit_derive (conf->instance, "objects", "total_objects", "sent_sendfile",      stats->n_objsendfile);
242                 /* Objects sent with write       */
243                 varnish_submit_derive (conf->instance, "objects", "total_objects", "sent_write",         stats->n_objwrite);
244                 /* Objects overflowing workspace */
245                 varnish_submit_derive (conf->instance, "objects", "total_objects", "workspace_overflow", stats->n_objoverflow);
246         }
247
248         if (conf->collect_purge)
249         {
250                 /* N total active purges      */
251                 varnish_submit_derive (conf->instance, "purge", "total_operations", "total",            stats->n_purge);
252                 /* N new purges added         */
253                 varnish_submit_derive (conf->instance, "purge", "total_operations", "added",            stats->n_purge_add);
254                 /* N old purges deleted       */
255                 varnish_submit_derive (conf->instance, "purge", "total_operations", "deleted",          stats->n_purge_retire);
256                 /* N objects tested           */
257                 varnish_submit_derive (conf->instance, "purge", "total_operations", "objects_tested",   stats->n_purge_obj_test);
258                 /* N regexps tested against   */
259                 varnish_submit_derive (conf->instance, "purge", "total_operations", "regexps_tested",   stats->n_purge_re_test);
260                 /* N duplicate purges removed */
261                 varnish_submit_derive (conf->instance, "purge", "total_operations", "duplicate",        stats->n_purge_dups);
262         }
263
264         if (conf->collect_session)
265         {
266                 /* Session Closed     */
267                 varnish_submit_derive (conf->instance, "session", "total_operations", "closed",    stats->sess_closed);
268                 /* Session Pipeline   */
269                 varnish_submit_derive (conf->instance, "session", "total_operations", "pipeline",  stats->sess_pipeline);
270                 /* Session Read Ahead */
271                 varnish_submit_derive (conf->instance, "session", "total_operations", "readahead", stats->sess_readahead);
272                 /* Session Linger     */
273                 varnish_submit_derive (conf->instance, "session", "total_operations", "linger",    stats->sess_linger);
274                 /* Session herd       */
275                 varnish_submit_derive (conf->instance, "session", "total_operations", "herd",      stats->sess_herd);
276         }
277
278         if (conf->collect_shm)
279         {
280                 /* SHM records                 */
281                 varnish_submit_derive (conf->instance, "shm", "total_operations", "records"   , stats->shm_records);
282                 /* SHM writes                  */
283                 varnish_submit_derive (conf->instance, "shm", "total_operations", "writes"    , stats->shm_writes);
284                 /* SHM flushes due to overflow */
285                 varnish_submit_derive (conf->instance, "shm", "total_operations", "flushes"   , stats->shm_flushes);
286                 /* SHM MTX contention          */
287                 varnish_submit_derive (conf->instance, "shm", "total_operations", "contention", stats->shm_cont);
288                 /* SHM cycles through buffer   */
289                 varnish_submit_derive (conf->instance, "shm", "total_operations", "cycles"    , stats->shm_cycles);
290         }
291
292         if (conf->collect_sm)
293         {
294                 /* allocator requests */
295                 varnish_submit_derive (conf->instance, "sm", "total_requests", "nreq",         stats->sm_nreq);
296                 /* outstanding allocations */
297                 varnish_submit_gauge (conf->instance,  "sm", "requests", "outstanding",        stats->sm_nobj);
298                 /* bytes allocated */
299                 varnish_submit_derive (conf->instance,  "sm", "total_bytes", "allocated",      stats->sm_balloc);
300                 /* bytes free */
301                 varnish_submit_derive (conf->instance,  "sm", "total_bytes", "free",           stats->sm_bfree);
302         }
303
304         if (conf->collect_sma)
305         {
306                 /* SMA allocator requests */
307                 varnish_submit_derive (conf->instance, "sma", "total_requests", "nreq",    stats->sma_nreq);
308                 /* SMA outstanding allocations */
309                 varnish_submit_gauge (conf->instance,  "sma", "requests", "outstanding",   stats->sma_nobj);
310                 /* SMA outstanding bytes */
311                 varnish_submit_gauge (conf->instance,  "sma", "bytes", "outstanding",      stats->sma_nbytes);
312                 /* SMA bytes allocated */
313                 varnish_submit_derive (conf->instance,  "sma", "total_bytes", "allocated", stats->sma_balloc);
314                 /* SMA bytes free */
315                 varnish_submit_derive (conf->instance,  "sma", "total_bytes", "free" ,     stats->sma_bfree);
316         }
317
318         if (conf->collect_sms)
319         {
320                 /* SMS allocator requests */
321                 varnish_submit_derive (conf->instance, "sms", "total_requests", "allocator", stats->sms_nreq);
322                 /* SMS outstanding allocations */
323                 varnish_submit_gauge (conf->instance,  "sms", "requests", "outstanding",     stats->sms_nobj);
324                 /* SMS outstanding bytes */
325                 varnish_submit_gauge (conf->instance,  "sms", "bytes", "outstanding",        stats->sms_nbytes);
326                 /* SMS bytes allocated */
327                 varnish_submit_derive (conf->instance,  "sms", "total_bytes", "allocated",   stats->sms_balloc);
328                 /* SMS bytes freed */
329                 varnish_submit_derive (conf->instance,  "sms", "total_bytes", "free",        stats->sms_bfree);
330         }
331
332         if (conf->collect_struct)
333         {
334                 /* N struct sess_mem       */
335                 varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess_mem",  stats->n_sess_mem);
336                 /* N struct sess           */
337                 varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess",      stats->n_sess);
338                 /* N struct object         */
339                 varnish_submit_gauge (conf->instance, "struct", "objects", "object",             stats->n_object);
340                 /* N struct objecthead     */
341                 varnish_submit_gauge (conf->instance, "struct", "objects", "objecthead",         stats->n_objecthead);
342                 /* N struct smf            */
343                 varnish_submit_gauge (conf->instance, "struct", "objects", "smf",                stats->n_smf);
344                 /* N small free smf         */
345                 varnish_submit_gauge (conf->instance, "struct", "objects", "smf_frag",           stats->n_smf_frag);
346                 /* N large free smf         */
347                 varnish_submit_gauge (conf->instance, "struct", "objects", "smf_large",          stats->n_smf_large);
348                 /* N struct vbe_conn        */
349                 varnish_submit_gauge (conf->instance, "struct", "objects", "vbe_conn",           stats->n_vbe_conn);
350         }
351
352         if (conf->collect_totals)
353         {
354                 /* Total Sessions */
355                 varnish_submit_derive (conf->instance, "totals", "total_sessions", "sessions",  stats->s_sess);
356                 /* Total Requests */
357                 varnish_submit_derive (conf->instance, "totals", "total_requests", "requests",  stats->s_req);
358                 /* Total pipe */
359                 varnish_submit_derive (conf->instance, "totals", "total_operations", "pipe",    stats->s_pipe);
360                 /* Total pass */
361                 varnish_submit_derive (conf->instance, "totals", "total_operations", "pass",    stats->s_pass);
362                 /* Total fetch */
363                 varnish_submit_derive (conf->instance, "totals", "total_operations", "fetches", stats->s_fetch);
364                 /* Total header bytes */
365                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "header-bytes", stats->s_hdrbytes);
366                 /* Total body byte */
367                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "body-bytes",   stats->s_bodybytes);
368         }
369
370         if (conf->collect_vcl)
371         {
372                 /* N vcl total     */
373                 varnish_submit_gauge (conf->instance, "vcl", "vcl", "total_vcl",     stats->n_vcl);
374                 /* N vcl available */
375                 varnish_submit_gauge (conf->instance, "vcl", "vcl", "avail_vcl",     stats->n_vcl_avail);
376                 /* N vcl discarded */
377                 varnish_submit_gauge (conf->instance, "vcl", "vcl", "discarded_vcl", stats->n_vcl_discard);
378         }
379
380         if (conf->collect_workers)
381         {
382                 /* worker threads */
383                 varnish_submit_gauge (conf->instance, "workers", "threads", "worker",             stats->n_wrk);
384                 /* worker threads created */
385                 varnish_submit_derive (conf->instance, "workers", "total_threads", "created",     stats->n_wrk_create);
386                 /* worker threads not created */
387                 varnish_submit_derive (conf->instance, "workers", "total_threads", "failed",      stats->n_wrk_failed);
388                 /* worker threads limited */
389                 varnish_submit_derive (conf->instance, "workers", "total_threads", "limited",     stats->n_wrk_max);
390                 /* dropped work requests */
391                 varnish_submit_derive (conf->instance, "workers", "total_threads", "dropped",     stats->n_wrk_drop);
392                 /* queued work requests */
393                 varnish_submit_derive (conf->instance, "workers", "total_requests", "queued",     stats->n_wrk_queue);
394                 /* overflowed work requests */
395                 varnish_submit_derive (conf->instance, "workers", "total_requests", "overflowed", stats->n_wrk_overflow);
396         }
397
398 } /* }}} void varnish_monitor */
399 #endif
400
401 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
402 static int varnish_read (user_data_t *ud) /* {{{ */
403 {
404         struct VSM_data *vd;
405         const c_varnish_stats_t *stats;
406
407         user_config_t *conf;
408
409         if ((ud == NULL) || (ud->data == NULL))
410                 return (EINVAL);
411
412         conf = ud->data;
413
414         vd = VSM_New();
415 #if HAVE_VARNISH_V3
416         VSC_Setup(vd);
417 #endif
418
419         if (conf->instance != NULL)
420         {
421                 int status;
422
423                 status = VSM_n_Arg (vd, conf->instance);
424                 if (status < 0)
425                 {
426                         VSM_Delete (vd);
427                         ERROR ("varnish plugin: VSM_n_Arg (\"%s\") failed "
428                                         "with status %i.",
429                                         conf->instance, status);
430                         return (-1);
431                 }
432         }
433
434 #if HAVE_VARNISH_V3
435         if (VSC_Open (vd, /* diag = */ 1))
436 #else /* if HAVE_VARNISH_V4 */
437         if (VSM_Open (vd))
438 #endif
439         {
440                 VSM_Delete (vd);
441                 ERROR ("varnish plugin: Unable to open connection.");
442
443                 return (-1);
444         }
445
446 #if HAVE_VARNISH_V3
447         stats = VSC_Main(vd);
448 #else /* if HAVE_VARNISH_V4 */
449         stats = VSC_Main(vd, NULL);
450 #endif
451         if (!stats)
452         {
453                 VSM_Delete (vd);
454                 ERROR ("varnish plugin: Unable to get statistics.");
455
456                 return (-1);
457         }
458
459
460         varnish_monitor (conf, stats);
461         VSM_Delete (vd);
462
463         return (0);
464 } /* }}} */
465 #else /* if HAVE_VARNISH_V2 */
466 static int varnish_read (user_data_t *ud) /* {{{ */
467 {
468         const c_varnish_stats_t *stats;
469
470         user_config_t *conf;
471
472         if ((ud == NULL) || (ud->data == NULL))
473                 return (EINVAL);
474
475         conf = ud->data;
476
477         stats = VSL_OpenStats (conf->instance);
478         if (stats == NULL)
479         {
480                 ERROR ("Varnish plugin : unable to load statistics");
481
482                 return (-1);
483         }
484
485         varnish_monitor (conf, stats);
486
487         return (0);
488 } /* }}} */
489 #endif
490
491 static void varnish_config_free (void *ptr) /* {{{ */
492 {
493         user_config_t *conf = ptr;
494
495         if (conf == NULL)
496                 return;
497
498         sfree (conf->instance);
499         sfree (conf);
500 } /* }}} */
501
502 static int varnish_config_apply_default (user_config_t *conf) /* {{{ */
503 {
504         if (conf == NULL)
505                 return (EINVAL);
506
507         conf->collect_backend     = 1;
508         conf->collect_cache       = 1;
509         conf->collect_connections = 1;
510 #ifdef HAVE_VARNISH_V3
511         conf->collect_dirdns      = 0;
512 #endif
513         conf->collect_esi         = 0;
514         conf->collect_fetch       = 0;
515         conf->collect_hcb         = 0;
516         conf->collect_objects     = 0;
517 #if HAVE_VARNISH_V2
518         conf->collect_purge       = 0;
519 #else
520         conf->collect_ban         = 0;
521 #endif
522         conf->collect_session     = 0;
523         conf->collect_shm         = 1;
524 #if HAVE_VARNISH_V2
525         conf->collect_sm          = 0;
526         conf->collect_sma         = 0;
527 #endif
528         conf->collect_sms         = 0;
529         conf->collect_struct      = 0;
530         conf->collect_totals      = 0;
531 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
532         conf->collect_uptime      = 0;
533 #endif
534         conf->collect_vcl         = 0;
535         conf->collect_workers     = 0;
536 #if HAVE_VARNISH_V4
537         conf->collect_vsm         = 0;
538 #endif
539
540         return (0);
541 } /* }}} int varnish_config_apply_default */
542
543 static int varnish_init (void) /* {{{ */
544 {
545         user_config_t *conf;
546         user_data_t ud;
547
548         if (have_instance)
549                 return (0);
550
551         conf = malloc (sizeof (*conf));
552         if (conf == NULL)
553                 return (ENOMEM);
554         memset (conf, 0, sizeof (*conf));
555
556         /* Default settings: */
557         conf->instance = NULL;
558
559         varnish_config_apply_default (conf);
560
561         ud.data = conf;
562         ud.free_func = varnish_config_free;
563
564         plugin_register_complex_read (/* group = */ "varnish",
565                         /* name      = */ "varnish/localhost",
566                         /* callback  = */ varnish_read,
567                         /* interval  = */ NULL,
568                         /* user data = */ &ud);
569
570         return (0);
571 } /* }}} int varnish_init */
572
573 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
574 {
575         user_config_t *conf;
576         user_data_t ud;
577         char callback_name[DATA_MAX_NAME_LEN];
578         int i;
579
580         conf = malloc (sizeof (*conf));
581         if (conf == NULL)
582                 return (ENOMEM);
583         memset (conf, 0, sizeof (*conf));
584         conf->instance = NULL;
585
586         varnish_config_apply_default (conf);
587
588         if (ci->values_num == 1)
589         {
590                 int status;
591
592                 status = cf_util_get_string (ci, &conf->instance);
593                 if (status != 0)
594                 {
595                         sfree (conf);
596                         return (status);
597                 }
598                 assert (conf->instance != NULL);
599
600                 if (strcmp ("localhost", conf->instance) == 0)
601                 {
602                         sfree (conf->instance);
603                         conf->instance = NULL;
604                 }
605         }
606         else if (ci->values_num > 1)
607         {
608                 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
609                                 "one argument.");
610                 return (EINVAL);
611         }
612
613         for (i = 0; i < ci->children_num; i++)
614         {
615                 oconfig_item_t *child = ci->children + i;
616
617                 if (strcasecmp ("CollectCache", child->key) == 0)
618                         cf_util_get_boolean (child, &conf->collect_cache);
619                 else if (strcasecmp ("CollectConnections", child->key) == 0)
620                         cf_util_get_boolean (child, &conf->collect_connections);
621                 else if (strcasecmp ("CollectESI", child->key) == 0)
622                         cf_util_get_boolean (child, &conf->collect_esi);
623 #ifdef HAVE_VARNISH_V3
624                 else if (strcasecmp ("CollectDirectorDNS", child->key) == 0)
625                         cf_util_get_boolean (child, &conf->collect_dirdns);
626 #endif
627                 else if (strcasecmp ("CollectBackend", child->key) == 0)
628                         cf_util_get_boolean (child, &conf->collect_backend);
629                 else if (strcasecmp ("CollectFetch", child->key) == 0)
630                         cf_util_get_boolean (child, &conf->collect_fetch);
631                 else if (strcasecmp ("CollectHCB", child->key) == 0)
632                         cf_util_get_boolean (child, &conf->collect_hcb);
633                 else if (strcasecmp ("CollectObjects", child->key) == 0)
634                         cf_util_get_boolean (child, &conf->collect_objects);
635 #if HAVE_VARNISH_V2
636                 else if (strcasecmp ("CollectPurge", child->key) == 0)
637                         cf_util_get_boolean (child, &conf->collect_purge);
638 #else
639                 else if (strcasecmp ("CollectBan", child->key) == 0)
640                         cf_util_get_boolean (child, &conf->collect_ban);
641 #endif
642                 else if (strcasecmp ("CollectSession", child->key) == 0)
643                         cf_util_get_boolean (child, &conf->collect_session);
644                 else if (strcasecmp ("CollectSHM", child->key) == 0)
645                         cf_util_get_boolean (child, &conf->collect_shm);
646                 else if (strcasecmp ("CollectSMS", child->key) == 0)
647                         cf_util_get_boolean (child, &conf->collect_sms);
648 #if HAVE_VARNISH_V2
649                 else if (strcasecmp ("CollectSMA", child->key) == 0)
650                         cf_util_get_boolean (child, &conf->collect_sma);
651                 else if (strcasecmp ("CollectSM", child->key) == 0)
652                         cf_util_get_boolean (child, &conf->collect_sm);
653 #endif
654                 else if (strcasecmp ("CollectStruct", child->key) == 0)
655                         cf_util_get_boolean (child, &conf->collect_struct);
656                 else if (strcasecmp ("CollectTotals", child->key) == 0)
657                         cf_util_get_boolean (child, &conf->collect_totals);
658 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
659                 else if (strcasecmp ("CollectUptime", child->key) == 0)
660                         cf_util_get_boolean (child, &conf->collect_uptime);
661 #endif
662                 else if (strcasecmp ("CollectVCL", child->key) == 0)
663                         cf_util_get_boolean (child, &conf->collect_vcl);
664                 else if (strcasecmp ("CollectWorkers", child->key) == 0)
665                         cf_util_get_boolean (child, &conf->collect_workers);
666 #if HAVE_VARNISH_V4
667                 else if (strcasecmp ("CollectVSM", child->key) == 0)
668                         cf_util_get_boolean (child, &conf->collect_vsm);
669 #endif
670                 else
671                 {
672                         WARNING ("Varnish plugin: Ignoring unknown "
673                                         "configuration option: \"%s\". Did "
674                                         "you forget to add an <Instance /> "
675                                         "block around the configuration?",
676                                         child->key);
677                 }
678         }
679
680         if (!conf->collect_cache
681                         && !conf->collect_connections
682                         && !conf->collect_esi
683                         && !conf->collect_backend
684 #ifdef HAVE_VARNISH_V3
685                         && !conf->collect_dirdns
686 #endif
687                         && !conf->collect_fetch
688                         && !conf->collect_hcb
689                         && !conf->collect_objects
690 #if HAVE_VARNISH_V2
691                         && !conf->collect_purge
692 #else
693                         && !conf->collect_ban
694 #endif
695                         && !conf->collect_session
696                         && !conf->collect_shm
697                         && !conf->collect_sms
698 #if HAVE_VARNISH_V2
699                         && !conf->collect_sma
700                         && !conf->collect_sm
701 #endif
702                         && !conf->collect_struct
703                         && !conf->collect_totals
704 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
705                         && !conf->collect_uptime
706 #endif
707                         && !conf->collect_vcl
708                         && !conf->collect_workers
709 #if HAVE_VARNISH_V4
710                         && !conf->collect_vsm
711 #endif
712         )
713         {
714                 WARNING ("Varnish plugin: No metric has been configured for "
715                                 "instance \"%s\". Disabling this instance.",
716                                 (conf->instance == NULL) ? "localhost" : conf->instance);
717                 return (EINVAL);
718         }
719
720         ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
721                         (conf->instance == NULL) ? "localhost" : conf->instance);
722
723         ud.data = conf;
724         ud.free_func = varnish_config_free;
725
726         plugin_register_complex_read (/* group = */ "varnish",
727                         /* name      = */ callback_name,
728                         /* callback  = */ varnish_read,
729                         /* interval  = */ NULL,
730                         /* user data = */ &ud);
731
732         have_instance = 1;
733
734         return (0);
735 } /* }}} int varnish_config_instance */
736
737 static int varnish_config (oconfig_item_t *ci) /* {{{ */
738 {
739         int i;
740
741         for (i = 0; i < ci->children_num; i++)
742         {
743                 oconfig_item_t *child = ci->children + i;
744
745                 if (strcasecmp ("Instance", child->key) == 0)
746                         varnish_config_instance (child);
747                 else
748                 {
749                         WARNING ("Varnish plugin: Ignoring unknown "
750                                         "configuration option: \"%s\"",
751                                         child->key);
752                 }
753         }
754
755         return (0);
756 } /* }}} int varnish_config */
757
758 void module_register (void) /* {{{ */
759 {
760         plugin_register_complex_config ("varnish", varnish_config);
761         plugin_register_init ("varnish", varnish_init);
762 } /* }}} */
763
764 /* vim: set sw=8 noet fdm=marker : */