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