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