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