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