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