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