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
592         user_config_t *conf;
593
594         if ((ud == NULL) || (ud->data == NULL))
595                 return (EINVAL);
596
597         conf = ud->data;
598
599         vd = VSM_New();
600 #if HAVE_VARNISH_V3
601         VSC_Setup(vd);
602 #endif
603
604         if (conf->instance != NULL)
605         {
606                 int status;
607
608                 status = VSM_n_Arg (vd, conf->instance);
609                 if (status < 0)
610                 {
611                         VSM_Delete (vd);
612                         ERROR ("varnish plugin: VSM_n_Arg (\"%s\") failed "
613                                         "with status %i.",
614                                         conf->instance, status);
615                         return (-1);
616                 }
617         }
618
619 #if HAVE_VARNISH_V3
620         if (VSC_Open (vd, /* diag = */ 1))
621 #else /* if HAVE_VARNISH_V4 */
622         if (VSM_Open (vd))
623 #endif
624         {
625                 VSM_Delete (vd);
626                 ERROR ("varnish plugin: Unable to open connection.");
627
628                 return (-1);
629         }
630
631 #if HAVE_VARNISH_V3
632         stats = VSC_Main(vd);
633 #else /* if HAVE_VARNISH_V4 */
634         stats = VSC_Main(vd, NULL);
635 #endif
636         if (!stats)
637         {
638                 VSM_Delete (vd);
639                 ERROR ("varnish plugin: Unable to get statistics.");
640
641                 return (-1);
642         }
643
644
645         varnish_monitor (conf, stats);
646         VSM_Delete (vd);
647
648         return (0);
649 } /* }}} */
650 #else /* if HAVE_VARNISH_V2 */
651 static int varnish_read (user_data_t *ud) /* {{{ */
652 {
653         const c_varnish_stats_t *stats;
654
655         user_config_t *conf;
656
657         if ((ud == NULL) || (ud->data == NULL))
658                 return (EINVAL);
659
660         conf = ud->data;
661
662         stats = VSL_OpenStats (conf->instance);
663         if (stats == NULL)
664         {
665                 ERROR ("Varnish plugin : unable to load statistics");
666
667                 return (-1);
668         }
669
670         varnish_monitor (conf, stats);
671
672         return (0);
673 } /* }}} */
674 #endif
675
676 static void varnish_config_free (void *ptr) /* {{{ */
677 {
678         user_config_t *conf = ptr;
679
680         if (conf == NULL)
681                 return;
682
683         sfree (conf->instance);
684         sfree (conf);
685 } /* }}} */
686
687 static int varnish_config_apply_default (user_config_t *conf) /* {{{ */
688 {
689         if (conf == NULL)
690                 return (EINVAL);
691
692         conf->collect_backend     = 1;
693         conf->collect_cache       = 1;
694         conf->collect_connections = 1;
695 #ifdef HAVE_VARNISH_V3
696         conf->collect_dirdns      = 0;
697 #endif
698         conf->collect_esi         = 0;
699         conf->collect_fetch       = 0;
700         conf->collect_hcb         = 0;
701         conf->collect_objects     = 0;
702 #if HAVE_VARNISH_V2
703         conf->collect_purge       = 0;
704 #else
705         conf->collect_ban         = 0;
706 #endif
707         conf->collect_session     = 0;
708         conf->collect_shm         = 1;
709 #if HAVE_VARNISH_V2
710         conf->collect_sm          = 0;
711         conf->collect_sma         = 0;
712 #endif
713         conf->collect_sms         = 0;
714         conf->collect_struct      = 0;
715         conf->collect_totals      = 0;
716 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
717         conf->collect_uptime      = 0;
718 #endif
719         conf->collect_vcl         = 0;
720         conf->collect_workers     = 0;
721 #if HAVE_VARNISH_V4
722         conf->collect_vsm         = 0;
723 #endif
724
725         return (0);
726 } /* }}} int varnish_config_apply_default */
727
728 static int varnish_init (void) /* {{{ */
729 {
730         user_config_t *conf;
731         user_data_t ud;
732
733         if (have_instance)
734                 return (0);
735
736         conf = malloc (sizeof (*conf));
737         if (conf == NULL)
738                 return (ENOMEM);
739         memset (conf, 0, sizeof (*conf));
740
741         /* Default settings: */
742         conf->instance = NULL;
743
744         varnish_config_apply_default (conf);
745
746         ud.data = conf;
747         ud.free_func = varnish_config_free;
748
749         plugin_register_complex_read (/* group = */ "varnish",
750                         /* name      = */ "varnish/localhost",
751                         /* callback  = */ varnish_read,
752                         /* interval  = */ 0,
753                         /* user data = */ &ud);
754
755         return (0);
756 } /* }}} int varnish_init */
757
758 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
759 {
760         user_config_t *conf;
761         user_data_t ud;
762         char callback_name[DATA_MAX_NAME_LEN];
763         int i;
764
765         conf = malloc (sizeof (*conf));
766         if (conf == NULL)
767                 return (ENOMEM);
768         memset (conf, 0, sizeof (*conf));
769         conf->instance = NULL;
770
771         varnish_config_apply_default (conf);
772
773         if (ci->values_num == 1)
774         {
775                 int status;
776
777                 status = cf_util_get_string (ci, &conf->instance);
778                 if (status != 0)
779                 {
780                         sfree (conf);
781                         return (status);
782                 }
783                 assert (conf->instance != NULL);
784
785                 if (strcmp ("localhost", conf->instance) == 0)
786                 {
787                         sfree (conf->instance);
788                         conf->instance = NULL;
789                 }
790         }
791         else if (ci->values_num > 1)
792         {
793                 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
794                                 "one argument.");
795                 return (EINVAL);
796         }
797
798         for (i = 0; i < ci->children_num; i++)
799         {
800                 oconfig_item_t *child = ci->children + i;
801
802                 if (strcasecmp ("CollectCache", child->key) == 0)
803                         cf_util_get_boolean (child, &conf->collect_cache);
804                 else if (strcasecmp ("CollectConnections", child->key) == 0)
805                         cf_util_get_boolean (child, &conf->collect_connections);
806                 else if (strcasecmp ("CollectESI", child->key) == 0)
807                         cf_util_get_boolean (child, &conf->collect_esi);
808 #ifdef HAVE_VARNISH_V3
809                 else if (strcasecmp ("CollectDirectorDNS", child->key) == 0)
810                         cf_util_get_boolean (child, &conf->collect_dirdns);
811 #endif
812                 else if (strcasecmp ("CollectBackend", child->key) == 0)
813                         cf_util_get_boolean (child, &conf->collect_backend);
814                 else if (strcasecmp ("CollectFetch", child->key) == 0)
815                         cf_util_get_boolean (child, &conf->collect_fetch);
816                 else if (strcasecmp ("CollectHCB", child->key) == 0)
817                         cf_util_get_boolean (child, &conf->collect_hcb);
818                 else if (strcasecmp ("CollectObjects", child->key) == 0)
819                         cf_util_get_boolean (child, &conf->collect_objects);
820 #if HAVE_VARNISH_V2
821                 else if (strcasecmp ("CollectPurge", child->key) == 0)
822                         cf_util_get_boolean (child, &conf->collect_purge);
823 #else
824                 else if (strcasecmp ("CollectBan", child->key) == 0)
825                         cf_util_get_boolean (child, &conf->collect_ban);
826 #endif
827                 else if (strcasecmp ("CollectSession", child->key) == 0)
828                         cf_util_get_boolean (child, &conf->collect_session);
829                 else if (strcasecmp ("CollectSHM", child->key) == 0)
830                         cf_util_get_boolean (child, &conf->collect_shm);
831                 else if (strcasecmp ("CollectSMS", child->key) == 0)
832                         cf_util_get_boolean (child, &conf->collect_sms);
833 #if HAVE_VARNISH_V2
834                 else if (strcasecmp ("CollectSMA", child->key) == 0)
835                         cf_util_get_boolean (child, &conf->collect_sma);
836                 else if (strcasecmp ("CollectSM", child->key) == 0)
837                         cf_util_get_boolean (child, &conf->collect_sm);
838 #endif
839                 else if (strcasecmp ("CollectStruct", child->key) == 0)
840                         cf_util_get_boolean (child, &conf->collect_struct);
841                 else if (strcasecmp ("CollectTotals", child->key) == 0)
842                         cf_util_get_boolean (child, &conf->collect_totals);
843 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
844                 else if (strcasecmp ("CollectUptime", child->key) == 0)
845                         cf_util_get_boolean (child, &conf->collect_uptime);
846 #endif
847                 else if (strcasecmp ("CollectVCL", child->key) == 0)
848                         cf_util_get_boolean (child, &conf->collect_vcl);
849                 else if (strcasecmp ("CollectWorkers", child->key) == 0)
850                         cf_util_get_boolean (child, &conf->collect_workers);
851 #if HAVE_VARNISH_V4
852                 else if (strcasecmp ("CollectVSM", child->key) == 0)
853                         cf_util_get_boolean (child, &conf->collect_vsm);
854 #endif
855                 else
856                 {
857                         WARNING ("Varnish plugin: Ignoring unknown "
858                                         "configuration option: \"%s\". Did "
859                                         "you forget to add an <Instance /> "
860                                         "block around the configuration?",
861                                         child->key);
862                 }
863         }
864
865         if (!conf->collect_cache
866                         && !conf->collect_connections
867                         && !conf->collect_esi
868                         && !conf->collect_backend
869 #ifdef HAVE_VARNISH_V3
870                         && !conf->collect_dirdns
871 #endif
872                         && !conf->collect_fetch
873                         && !conf->collect_hcb
874                         && !conf->collect_objects
875 #if HAVE_VARNISH_V2
876                         && !conf->collect_purge
877 #else
878                         && !conf->collect_ban
879 #endif
880                         && !conf->collect_session
881                         && !conf->collect_shm
882                         && !conf->collect_sms
883 #if HAVE_VARNISH_V2
884                         && !conf->collect_sma
885                         && !conf->collect_sm
886 #endif
887                         && !conf->collect_struct
888                         && !conf->collect_totals
889 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
890                         && !conf->collect_uptime
891 #endif
892                         && !conf->collect_vcl
893                         && !conf->collect_workers
894 #if HAVE_VARNISH_V4
895                         && !conf->collect_vsm
896 #endif
897         )
898         {
899                 WARNING ("Varnish plugin: No metric has been configured for "
900                                 "instance \"%s\". Disabling this instance.",
901                                 (conf->instance == NULL) ? "localhost" : conf->instance);
902                 return (EINVAL);
903         }
904
905         ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
906                         (conf->instance == NULL) ? "localhost" : conf->instance);
907
908         ud.data = conf;
909         ud.free_func = varnish_config_free;
910
911         plugin_register_complex_read (/* group = */ "varnish",
912                         /* name      = */ callback_name,
913                         /* callback  = */ varnish_read,
914                         /* interval  = */ 0,
915                         /* user data = */ &ud);
916
917         have_instance = 1;
918
919         return (0);
920 } /* }}} int varnish_config_instance */
921
922 static int varnish_config (oconfig_item_t *ci) /* {{{ */
923 {
924         int i;
925
926         for (i = 0; i < ci->children_num; i++)
927         {
928                 oconfig_item_t *child = ci->children + i;
929
930                 if (strcasecmp ("Instance", child->key) == 0)
931                         varnish_config_instance (child);
932                 else
933                 {
934                         WARNING ("Varnish plugin: Ignoring unknown "
935                                         "configuration option: \"%s\"",
936                                         child->key);
937                 }
938         }
939
940         return (0);
941 } /* }}} int varnish_config */
942
943 void module_register (void) /* {{{ */
944 {
945         plugin_register_complex_config ("varnish", varnish_config);
946         plugin_register_init ("varnish", varnish_init);
947 } /* }}} */
948
949 /* vim: set sw=8 noet fdm=marker : */