Code

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