Code

517258e2cf3148bd1276c3d51811a67b75a191a3
[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  **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
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
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
43 #if HAVE_VARNISH_V2
44 #include <varnish/varnishapi.h>
45 typedef struct varnish_stats c_varnish_stats_t;
46 #endif
48 /* {{{ user_config_s */
49 struct user_config_s {
50         char *instance;
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; /* }}} */
84 static _Bool have_instance = 0;
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;
91         vl.values = &value;
92         vl.values_len = 1;
94         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
96         sstrncpy (vl.plugin, "varnish", sizeof (vl.plugin));
98         if (plugin_instance == NULL)
99                 plugin_instance = "default";
101         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
102                 "%s-%s", plugin_instance, category);
104         sstrncpy (vl.type, type, sizeof (vl.type));
106         if (type_instance != NULL)
107                 sstrncpy (vl.type_instance, type_instance,
108                                 sizeof (vl.type_instance));
110         return (plugin_dispatch_values (&vl));
111 } /* }}} int varnish_submit */
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)
117         value_t value;
119         value.gauge = (gauge_t) gauge_value;
121         return (varnish_submit (plugin_instance, category, type, type_instance, value));
122 } /* }}} int varnish_submit_gauge */
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)
128         value_t value;
130         value.derive = (derive_t) derive_value;
132         return (varnish_submit (plugin_instance, category, type, type_instance, value));
133 } /* }}} int varnish_submit_derive */
135 static void varnish_monitor (const user_config_t *conf, /* {{{ */
136                 const c_varnish_stats_t *stats)
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         }
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         }
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
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         }
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         }
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         }
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         }
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                 /* N purged objects              */
276                 varnish_submit_derive (conf->instance, "objects", "total_objects", "purged",             stats->n_obj_purged);
277 #else
278                 /* Objects sent with sendfile    */
279                 varnish_submit_derive (conf->instance, "objects", "total_objects", "sent_sendfile",      stats->n_objsendfile);
280                 /* Objects sent with write       */
281                 varnish_submit_derive (conf->instance, "objects", "total_objects", "sent_write",         stats->n_objwrite);
282                 /* Objects overflowing workspace */
283                 varnish_submit_derive (conf->instance, "objects", "total_objects", "workspace_overflow", stats->n_objoverflow);
284 #endif
285         }
287 #if HAVE_VARNISH_V2
288         if (conf->collect_purge)
289         {
290                 /* N total active purges      */
291                 varnish_submit_derive (conf->instance, "purge", "total_operations", "total",            stats->n_purge);
292                 /* N new purges added         */
293                 varnish_submit_derive (conf->instance, "purge", "total_operations", "added",            stats->n_purge_add);
294                 /* N old purges deleted       */
295                 varnish_submit_derive (conf->instance, "purge", "total_operations", "deleted",          stats->n_purge_retire);
296                 /* N objects tested           */
297                 varnish_submit_derive (conf->instance, "purge", "total_operations", "objects_tested",   stats->n_purge_obj_test);
298                 /* N regexps tested against   */
299                 varnish_submit_derive (conf->instance, "purge", "total_operations", "regexps_tested",   stats->n_purge_re_test);
300                 /* N duplicate purges removed */
301                 varnish_submit_derive (conf->instance, "purge", "total_operations", "duplicate",        stats->n_purge_dups);
302         }
303 #endif
304 #if HAVE_VARNISH_V3
305         if (conf->collect_ban)
306         {
307                 /* N total active bans      */
308                 varnish_submit_derive (conf->instance, "ban", "total_operations", "total",          stats->n_ban);
309                 /* N new bans added         */
310                 varnish_submit_derive (conf->instance, "ban", "total_operations", "added",          stats->n_ban_add);
311                 /* N old bans deleted       */
312                 varnish_submit_derive (conf->instance, "ban", "total_operations", "deleted",        stats->n_ban_retire);
313                 /* N objects tested         */
314                 varnish_submit_derive (conf->instance, "ban", "total_operations", "objects_tested", stats->n_ban_obj_test);
315                 /* N regexps tested against */
316                 varnish_submit_derive (conf->instance, "ban", "total_operations", "regexps_tested", stats->n_ban_re_test);
317                 /* N duplicate bans removed */
318                 varnish_submit_derive (conf->instance, "ban", "total_operations", "duplicate",      stats->n_ban_dups);
319         }
320 #endif
321 #if HAVE_VARNISH_V4
322         if (conf->collect_ban)
323         {
324                 /* N total active bans      */
325                 varnish_submit_derive (conf->instance, "ban", "total_operations", "total",          stats->bans);
326                 /* N new bans added         */
327                 varnish_submit_derive (conf->instance, "ban", "total_operations", "added",          stats->bans_added);
328                 /* N bans using obj */
329                 varnish_submit_derive (conf->instance, "ban", "total_operations", "obj",            stats->bans_obj);
330                 /* N bans using req */
331                 varnish_submit_derive (conf->instance, "ban", "total_operations", "req",            stats->bans_req);
332                 /* N new bans completed     */
333                 varnish_submit_derive (conf->instance, "ban", "total_operations", "completed",      stats->bans_completed);
334                 /* N old bans deleted       */
335                 varnish_submit_derive (conf->instance, "ban", "total_operations", "deleted",        stats->bans_deleted);
336                 /* N objects tested         */
337                 varnish_submit_derive (conf->instance, "ban", "total_operations", "tested",         stats->bans_tested);
338                 /* N duplicate bans removed */
339                 varnish_submit_derive (conf->instance, "ban", "total_operations", "duplicate",      stats->bans_dups);
340         }
341 #endif
343         if (conf->collect_session)
344         {
345                 /* Session Closed     */
346                 varnish_submit_derive (conf->instance, "session", "total_operations", "closed",    stats->sess_closed);
347                 /* Session Pipeline   */
348                 varnish_submit_derive (conf->instance, "session", "total_operations", "pipeline",  stats->sess_pipeline);
349                 /* Session Read Ahead */
350                 varnish_submit_derive (conf->instance, "session", "total_operations", "readahead", stats->sess_readahead);
351 #if HAVE_VARNISH_V4
352                 /* Sessions accepted */
353                 varnish_submit_derive (conf->instance, "session", "total_operations", "accepted",  stats->sess_conn);
354                 /* Sessions dropped for thread */
355                 varnish_submit_derive (conf->instance, "session", "total_operations", "dropped",   stats->sess_drop);
356                 /* Sessions accept failure */
357                 varnish_submit_derive (conf->instance, "session", "total_operations", "failed",    stats->sess_fail);
358                 /* Sessions pipe overflow */
359                 varnish_submit_derive (conf->instance, "session", "total_operations", "overflow",  stats->sess_pipe_overflow);
360                 /* Sessions queued for thread */
361                 varnish_submit_derive (conf->instance, "session", "total_operations", "queued",    stats->sess_queued);
362 #else
363                 /* Session Linger     */
364                 varnish_submit_derive (conf->instance, "session", "total_operations", "linger",    stats->sess_linger);
365 #endif
366                 /* Session herd       */
367                 varnish_submit_derive (conf->instance, "session", "total_operations", "herd",      stats->sess_herd);
368         }
370         if (conf->collect_shm)
371         {
372                 /* SHM records                 */
373                 varnish_submit_derive (conf->instance, "shm", "total_operations", "records"   , stats->shm_records);
374                 /* SHM writes                  */
375                 varnish_submit_derive (conf->instance, "shm", "total_operations", "writes"    , stats->shm_writes);
376                 /* SHM flushes due to overflow */
377                 varnish_submit_derive (conf->instance, "shm", "total_operations", "flushes"   , stats->shm_flushes);
378                 /* SHM MTX contention          */
379                 varnish_submit_derive (conf->instance, "shm", "total_operations", "contention", stats->shm_cont);
380                 /* SHM cycles through buffer   */
381                 varnish_submit_derive (conf->instance, "shm", "total_operations", "cycles"    , stats->shm_cycles);
382         }
384 #if HAVE_VARNISH_V2
385         if (conf->collect_sm)
386         {
387                 /* allocator requests */
388                 varnish_submit_derive (conf->instance, "sm", "total_requests", "nreq",         stats->sm_nreq);
389                 /* outstanding allocations */
390                 varnish_submit_gauge (conf->instance,  "sm", "requests", "outstanding",        stats->sm_nobj);
391                 /* bytes allocated */
392                 varnish_submit_derive (conf->instance,  "sm", "total_bytes", "allocated",      stats->sm_balloc);
393                 /* bytes free */
394                 varnish_submit_derive (conf->instance,  "sm", "total_bytes", "free",           stats->sm_bfree);
395         }
397         if (conf->collect_sma)
398         {
399                 /* SMA allocator requests */
400                 varnish_submit_derive (conf->instance, "sma", "total_requests", "nreq",    stats->sma_nreq);
401                 /* SMA outstanding allocations */
402                 varnish_submit_gauge (conf->instance,  "sma", "requests", "outstanding",   stats->sma_nobj);
403                 /* SMA outstanding bytes */
404                 varnish_submit_gauge (conf->instance,  "sma", "bytes", "outstanding",      stats->sma_nbytes);
405                 /* SMA bytes allocated */
406                 varnish_submit_derive (conf->instance,  "sma", "total_bytes", "allocated", stats->sma_balloc);
407                 /* SMA bytes free */
408                 varnish_submit_derive (conf->instance,  "sma", "total_bytes", "free" ,     stats->sma_bfree);
409         }
410 #endif
412         if (conf->collect_sms)
413         {
414                 /* SMS allocator requests */
415                 varnish_submit_derive (conf->instance, "sms", "total_requests", "allocator", stats->sms_nreq);
416                 /* SMS outstanding allocations */
417                 varnish_submit_gauge (conf->instance,  "sms", "requests", "outstanding",     stats->sms_nobj);
418                 /* SMS outstanding bytes */
419                 varnish_submit_gauge (conf->instance,  "sms", "bytes", "outstanding",        stats->sms_nbytes);
420                 /* SMS bytes allocated */
421                 varnish_submit_derive (conf->instance,  "sms", "total_bytes", "allocated",   stats->sms_balloc);
422                 /* SMS bytes freed */
423                 varnish_submit_derive (conf->instance,  "sms", "total_bytes", "free",        stats->sms_bfree);
424         }
426         if (conf->collect_struct)
427         {
428 #if !HAVE_VARNISH_V4
429                 /* N struct sess_mem       */
430                 varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess_mem",  stats->n_sess_mem);
431                 /* N struct sess           */
432                 varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess",      stats->n_sess);
433 #endif
434                 /* N struct object         */
435                 varnish_submit_gauge (conf->instance, "struct", "objects", "object",             stats->n_object);
436 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
437                 /* N unresurrected objects */
438                 varnish_submit_gauge (conf->instance, "struct", "objects", "vampireobject",      stats->n_vampireobject);
439                 /* N struct objectcore     */
440                 varnish_submit_gauge (conf->instance, "struct", "objects", "objectcore",         stats->n_objectcore);
441                 /* N struct waitinglist    */
442                 varnish_submit_gauge (conf->instance, "struct", "objects", "waitinglist",        stats->n_waitinglist);
443 #endif
444                 /* N struct objecthead     */
445                 varnish_submit_gauge (conf->instance, "struct", "objects", "objecthead",         stats->n_objecthead);
446 #ifdef HAVE_VARNISH_V2
447                 /* N struct smf            */
448                 varnish_submit_gauge (conf->instance, "struct", "objects", "smf",                stats->n_smf);
449                 /* N small free smf         */
450                 varnish_submit_gauge (conf->instance, "struct", "objects", "smf_frag",           stats->n_smf_frag);
451                 /* N large free smf         */
452                 varnish_submit_gauge (conf->instance, "struct", "objects", "smf_large",          stats->n_smf_large);
453                 /* N struct vbe_conn        */
454                 varnish_submit_gauge (conf->instance, "struct", "objects", "vbe_conn",           stats->n_vbe_conn);
455 #endif
456         }
458         if (conf->collect_totals)
459         {
460                 /* Total Sessions */
461                 varnish_submit_derive (conf->instance, "totals", "total_sessions", "sessions",  stats->s_sess);
462                 /* Total Requests */
463                 varnish_submit_derive (conf->instance, "totals", "total_requests", "requests",  stats->s_req);
464                 /* Total pipe */
465                 varnish_submit_derive (conf->instance, "totals", "total_operations", "pipe",    stats->s_pipe);
466                 /* Total pass */
467                 varnish_submit_derive (conf->instance, "totals", "total_operations", "pass",    stats->s_pass);
468                 /* Total fetch */
469                 varnish_submit_derive (conf->instance, "totals", "total_operations", "fetches", stats->s_fetch);
470 #if HAVE_VARNISH_V4
471                 /* Total synth */
472                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "synth",       stats->s_synth);
473                 /* Request header bytes */
474                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "req_header",  stats->s_req_hdrbytes);
475                 /* Request body byte */
476                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "req_body",    stats->s_req_bodybytes);
477                 /* Response header bytes */
478                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "resp_header", stats->s_resp_hdrbytes);
479                 /* Response body byte */
480                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "resp_body",   stats->s_resp_bodybytes);
481                 /* Pipe request header bytes */
482                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "pipe_header", stats->s_pipe_hdrbytes);
483                 /* Piped bytes from client */
484                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "pipe_in",     stats->s_pipe_in);
485                 /* Piped bytes to client */
486                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "pipe_out",    stats->s_pipe_out);
487                 /* Number of purge operations */
488                 varnish_submit_derive (conf->instance, "totals", "total_operations", "purges", stats->n_purges);
489 #else
490                 /* Total header bytes */
491                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "header-bytes", stats->s_hdrbytes);
492                 /* Total body byte */
493                 varnish_submit_derive (conf->instance, "totals", "total_bytes", "body-bytes",   stats->s_bodybytes);
494 #endif
495 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
496                 /* Gzip operations */
497                 varnish_submit_derive (conf->instance, "totals", "total_operations", "gzip",    stats->n_gzip);
498                 /* Gunzip operations */
499                 varnish_submit_derive (conf->instance, "totals", "total_operations", "gunzip",  stats->n_gunzip);
500 #endif
501         }
503 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
504         if (conf->collect_uptime)
505         {
506                 /* Client uptime */
507                 varnish_submit_gauge (conf->instance, "uptime", "uptime", "client_uptime", stats->uptime);
508         }
509 #endif
511         if (conf->collect_vcl)
512         {
513                 /* N vcl total     */
514                 varnish_submit_gauge (conf->instance, "vcl", "vcl", "total_vcl",     stats->n_vcl);
515                 /* N vcl available */
516                 varnish_submit_gauge (conf->instance, "vcl", "vcl", "avail_vcl",     stats->n_vcl_avail);
517                 /* N vcl discarded */
518                 varnish_submit_gauge (conf->instance, "vcl", "vcl", "discarded_vcl", stats->n_vcl_discard);
519 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
520                 /* Loaded VMODs */
521                 varnish_submit_gauge (conf->instance, "vcl", "objects", "vmod",      stats->vmods);
522 #endif
523         }
525         if (conf->collect_workers)
526         {
527 #ifdef HAVE_VARNISH_V4
528                 /* total number of threads */
529                 varnish_submit_gauge (conf->instance, "workers", "threads", "worker",             stats->threads);
530                 /* threads created */
531                 varnish_submit_derive (conf->instance, "workers", "total_threads", "created",     stats->threads_created);
532                 /* thread creation failed */
533                 varnish_submit_derive (conf->instance, "workers", "total_threads", "failed",      stats->threads_failed);
534                 /* threads hit max */
535                 varnish_submit_derive (conf->instance, "workers", "total_threads", "limited",     stats->threads_limited);
536                 /* threads destroyed */
537                 varnish_submit_derive (conf->instance, "workers", "total_threads", "dropped",     stats->threads_destroyed);
538                 /* length of session queue */
539                 varnish_submit_derive (conf->instance, "workers", "queue_length",  "threads",     stats->thread_queue_len);
540 #else
541                 /* worker threads */
542                 varnish_submit_gauge (conf->instance, "workers", "threads", "worker",             stats->n_wrk);
543                 /* worker threads created */
544                 varnish_submit_derive (conf->instance, "workers", "total_threads", "created",     stats->n_wrk_create);
545                 /* worker threads not created */
546                 varnish_submit_derive (conf->instance, "workers", "total_threads", "failed",      stats->n_wrk_failed);
547                 /* worker threads limited */
548                 varnish_submit_derive (conf->instance, "workers", "total_threads", "limited",     stats->n_wrk_max);
549                 /* dropped work requests */
550                 varnish_submit_derive (conf->instance, "workers", "total_requests", "dropped",    stats->n_wrk_drop);
551 #ifdef HAVE_VARNISH_V2
552                 /* queued work requests */
553                 varnish_submit_derive (conf->instance, "workers", "total_requests", "queued",     stats->n_wrk_queue);
554                 /* overflowed work requests */
555                 varnish_submit_derive (conf->instance, "workers", "total_requests", "overflowed", stats->n_wrk_overflow);
556 #else /* HAVE_VARNISH_V3 */
557                 /* queued work requests */
558                 varnish_submit_derive (conf->instance, "workers", "total_requests", "queued",       stats->n_wrk_queued);
559                 /* work request queue length */
560                 varnish_submit_derive (conf->instance, "workers", "total_requests", "queue_length", stats->n_wrk_lqueue);
561 #endif
562 #endif
563         }
564 } /* }}} void varnish_monitor */
566 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
567 static int varnish_read (user_data_t *ud) /* {{{ */
569         struct VSM_data *vd;
570         const c_varnish_stats_t *stats;
572         user_config_t *conf;
574         if ((ud == NULL) || (ud->data == NULL))
575                 return (EINVAL);
577         conf = ud->data;
579         vd = VSM_New();
580 #if HAVE_VARNISH_V3
581         VSC_Setup(vd);
582 #endif
584         if (conf->instance != NULL)
585         {
586                 int status;
588                 status = VSM_n_Arg (vd, conf->instance);
589                 if (status < 0)
590                 {
591                         ERROR ("varnish plugin: VSM_n_Arg (\"%s\") failed "
592                                         "with status %i.",
593                                         conf->instance, status);
594                         return (-1);
595                 }
596         }
598 #if HAVE_VARNISH_V3
599         if (VSC_Open (vd, /* diag = */ 1))
600 #else /* if HAVE_VARNISH_V4 */
601         if (VSM_Open (vd))
602 #endif
603         {
604                 ERROR ("varnish plugin: Unable to load statistics.");
606                 return (-1);
607         }
609 #if HAVE_VARNISH_V3
610         stats = VSC_Main(vd);
611 #else /* if HAVE_VARNISH_V4 */
612         stats = VSC_Main(vd, NULL);
613 #endif
615         varnish_monitor (conf, stats);
616         VSM_Close (vd);
618         return (0);
619 } /* }}} */
620 #else /* if HAVE_VARNISH_V2 */
621 static int varnish_read (user_data_t *ud) /* {{{ */
623         const c_varnish_stats_t *stats;
625         user_config_t *conf;
627         if ((ud == NULL) || (ud->data == NULL))
628                 return (EINVAL);
630         conf = ud->data;
632         stats = VSL_OpenStats (conf->instance);
633         if (stats == NULL)
634         {
635                 ERROR ("Varnish plugin : unable to load statistics");
637                 return (-1);
638         }
640         varnish_monitor (conf, stats);
642         return (0);
643 } /* }}} */
644 #endif
646 static void varnish_config_free (void *ptr) /* {{{ */
648         user_config_t *conf = ptr;
650         if (conf == NULL)
651                 return;
653         sfree (conf->instance);
654         sfree (conf);
655 } /* }}} */
657 static int varnish_config_apply_default (user_config_t *conf) /* {{{ */
659         if (conf == NULL)
660                 return (EINVAL);
662         conf->collect_backend     = 1;
663         conf->collect_cache       = 1;
664         conf->collect_connections = 1;
665 #ifdef HAVE_VARNISH_V3
666         conf->collect_dirdns      = 0;
667 #endif
668         conf->collect_esi         = 0;
669         conf->collect_fetch       = 0;
670         conf->collect_hcb         = 0;
671         conf->collect_objects     = 0;
672 #if HAVE_VARNISH_V2
673         conf->collect_purge       = 0;
674 #else
675         conf->collect_ban         = 0;
676 #endif
677         conf->collect_session     = 0;
678         conf->collect_shm         = 1;
679 #if HAVE_VARNISH_V2
680         conf->collect_sm          = 0;
681         conf->collect_sma         = 0;
682 #endif
683         conf->collect_sms         = 0;
684         conf->collect_struct      = 0;
685         conf->collect_totals      = 0;
686 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
687         conf->collect_uptime      = 0;
688 #endif
689         conf->collect_vcl         = 0;
690         conf->collect_workers     = 0;
692         return (0);
693 } /* }}} int varnish_config_apply_default */
695 static int varnish_init (void) /* {{{ */
697         user_config_t *conf;
698         user_data_t ud;
700         if (have_instance)
701                 return (0);
703         conf = malloc (sizeof (*conf));
704         if (conf == NULL)
705                 return (ENOMEM);
706         memset (conf, 0, sizeof (*conf));
708         /* Default settings: */
709         conf->instance = NULL;
711         varnish_config_apply_default (conf);
713         ud.data = conf;
714         ud.free_func = varnish_config_free;
716         plugin_register_complex_read (/* group = */ "varnish",
717                         /* name      = */ "varnish/localhost",
718                         /* callback  = */ varnish_read,
719                         /* interval  = */ NULL,
720                         /* user data = */ &ud);
722         return (0);
723 } /* }}} int varnish_init */
725 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
727         user_config_t *conf;
728         user_data_t ud;
729         char callback_name[DATA_MAX_NAME_LEN];
730         int i;
732         conf = malloc (sizeof (*conf));
733         if (conf == NULL)
734                 return (ENOMEM);
735         memset (conf, 0, sizeof (*conf));
736         conf->instance = NULL;
738         varnish_config_apply_default (conf);
740         if (ci->values_num == 1)
741         {
742                 int status;
744                 status = cf_util_get_string (ci, &conf->instance);
745                 if (status != 0)
746                 {
747                         sfree (conf);
748                         return (status);
749                 }
750                 assert (conf->instance != NULL);
752                 if (strcmp ("localhost", conf->instance) == 0)
753                 {
754                         sfree (conf->instance);
755                         conf->instance = NULL;
756                 }
757         }
758         else if (ci->values_num > 1)
759         {
760                 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
761                                 "one argument.");
762                 return (EINVAL);
763         }
765         for (i = 0; i < ci->children_num; i++)
766         {
767                 oconfig_item_t *child = ci->children + i;
769                 if (strcasecmp ("CollectCache", child->key) == 0)
770                         cf_util_get_boolean (child, &conf->collect_cache);
771                 else if (strcasecmp ("CollectConnections", child->key) == 0)
772                         cf_util_get_boolean (child, &conf->collect_connections);
773                 else if (strcasecmp ("CollectESI", child->key) == 0)
774                         cf_util_get_boolean (child, &conf->collect_esi);
775 #ifdef HAVE_VARNISH_V3
776                 else if (strcasecmp ("CollectDirectorDNS", child->key) == 0)
777                         cf_util_get_boolean (child, &conf->collect_dirdns);
778 #endif
779                 else if (strcasecmp ("CollectBackend", child->key) == 0)
780                         cf_util_get_boolean (child, &conf->collect_backend);
781                 else if (strcasecmp ("CollectFetch", child->key) == 0)
782                         cf_util_get_boolean (child, &conf->collect_fetch);
783                 else if (strcasecmp ("CollectHCB", child->key) == 0)
784                         cf_util_get_boolean (child, &conf->collect_hcb);
785                 else if (strcasecmp ("CollectObjects", child->key) == 0)
786                         cf_util_get_boolean (child, &conf->collect_objects);
787 #if HAVE_VARNISH_V2
788                 else if (strcasecmp ("CollectPurge", child->key) == 0)
789                         cf_util_get_boolean (child, &conf->collect_purge);
790 #else
791                 else if (strcasecmp ("CollectBan", child->key) == 0)
792                         cf_util_get_boolean (child, &conf->collect_ban);
793 #endif
794                 else if (strcasecmp ("CollectSession", child->key) == 0)
795                         cf_util_get_boolean (child, &conf->collect_session);
796                 else if (strcasecmp ("CollectSHM", child->key) == 0)
797                         cf_util_get_boolean (child, &conf->collect_shm);
798                 else if (strcasecmp ("CollectSMS", child->key) == 0)
799                         cf_util_get_boolean (child, &conf->collect_sms);
800 #if HAVE_VARNISH_V2
801                 else if (strcasecmp ("CollectSMA", child->key) == 0)
802                         cf_util_get_boolean (child, &conf->collect_sma);
803                 else if (strcasecmp ("CollectSM", child->key) == 0)
804                         cf_util_get_boolean (child, &conf->collect_sm);
805 #endif
806                 else if (strcasecmp ("CollectStruct", child->key) == 0)
807                         cf_util_get_boolean (child, &conf->collect_struct);
808                 else if (strcasecmp ("CollectTotals", child->key) == 0)
809                         cf_util_get_boolean (child, &conf->collect_totals);
810 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
811                 else if (strcasecmp ("CollectUptime", child->key) == 0)
812                         cf_util_get_boolean (child, &conf->collect_uptime);
813 #endif
814                 else if (strcasecmp ("CollectVCL", child->key) == 0)
815                         cf_util_get_boolean (child, &conf->collect_vcl);
816                 else if (strcasecmp ("CollectWorkers", child->key) == 0)
817                         cf_util_get_boolean (child, &conf->collect_workers);
818                 else
819                 {
820                         WARNING ("Varnish plugin: Ignoring unknown "
821                                         "configuration option: \"%s\". Did "
822                                         "you forget to add an <Instance /> "
823                                         "block around the configuration?",
824                                         child->key);
825                 }
826         }
828         if (!conf->collect_cache
829                         && !conf->collect_connections
830                         && !conf->collect_esi
831                         && !conf->collect_backend
832 #ifdef HAVE_VARNISH_V3
833                         && !conf->collect_dirdns
834 #endif
835                         && !conf->collect_fetch
836                         && !conf->collect_hcb
837                         && !conf->collect_objects
838 #if HAVE_VARNISH_V2
839                         && !conf->collect_purge
840 #else
841                         && !conf->collect_ban
842 #endif
843                         && !conf->collect_session
844                         && !conf->collect_shm
845                         && !conf->collect_sms
846 #if HAVE_VARNISH_V2
847                         && !conf->collect_sma
848                         && !conf->collect_sm
849 #endif
850                         && !conf->collect_struct
851                         && !conf->collect_totals
852 #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
853                         && !conf->collect_uptime
854 #endif
855                         && !conf->collect_vcl
856                         && !conf->collect_workers)
857         {
858                 WARNING ("Varnish plugin: No metric has been configured for "
859                                 "instance \"%s\". Disabling this instance.",
860                                 (conf->instance == NULL) ? "localhost" : conf->instance);
861                 return (EINVAL);
862         }
864         ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
865                         (conf->instance == NULL) ? "localhost" : conf->instance);
867         ud.data = conf;
868         ud.free_func = varnish_config_free;
870         plugin_register_complex_read (/* group = */ "varnish",
871                         /* name      = */ callback_name,
872                         /* callback  = */ varnish_read,
873                         /* interval  = */ NULL,
874                         /* user data = */ &ud);
876         have_instance = 1;
878         return (0);
879 } /* }}} int varnish_config_instance */
881 static int varnish_config (oconfig_item_t *ci) /* {{{ */
883         int i;
885         for (i = 0; i < ci->children_num; i++)
886         {
887                 oconfig_item_t *child = ci->children + i;
889                 if (strcasecmp ("Instance", child->key) == 0)
890                         varnish_config_instance (child);
891                 else
892                 {
893                         WARNING ("Varnish plugin: Ignoring unknown "
894                                         "configuration option: \"%s\"",
895                                         child->key);
896                 }
897         }
899         return (0);
900 } /* }}} int varnish_config */
902 void module_register (void) /* {{{ */
904         plugin_register_complex_config ("varnish", varnish_config);
905         plugin_register_init ("varnish", varnish_init);
906 } /* }}} */
908 /* vim: set sw=8 noet fdm=marker : */