aa2044471de2a83ad84cb6a208b3cd5eafb96694
[collectd.git] / src / varnish.c
1 /**
2  * collectd - src/varnish.c
3  * Copyright (C) 2010 Jerome Renard
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Jerome Renard <jerome.renard@gmail.com>
20  **/
21
22 /**
23  * Current list of what is monitored and what is not monitored (yet)
24  *
25  * Field name           Description                           Monitored
26  * ----------           -----------                           ---------
27  * uptime               Child uptime                              N
28  * client_conn          Client connections accepted               Y
29  * client_drop          Connection dropped, no sess               Y
30  * client_req           Client requests received                  Y
31  * cache_hit            Cache hits                                Y
32  * cache_hitpass        Cache hits for pass                       Y
33  * cache_miss           Cache misses                              Y
34  * backend_conn         Backend conn. success                     Y
35  * backend_unhealthy    Backend conn. not attempted               Y
36  * backend_busy         Backend conn. too many                    Y
37  * backend_fail         Backend conn. failures                    Y
38  * backend_reuse        Backend conn. reuses                      Y
39  * backend_toolate      Backend conn. was closed                  Y
40  * backend_recycle      Backend conn. recycles                    Y
41  * backend_unused       Backend conn. unused                      Y
42  * fetch_head           Fetch head                                Y
43  * fetch_length         Fetch with Length                         Y
44  * fetch_chunked        Fetch chunked                             Y
45  * fetch_eof            Fetch EOF                                 Y
46  * fetch_bad            Fetch had bad headers                     Y
47  * fetch_close          Fetch wanted close                        Y
48  * fetch_oldhttp        Fetch pre HTTP/1.1 closed                 Y
49  * fetch_zero           Fetch zero len                            Y
50  * fetch_failed         Fetch failed                              Y
51  * n_sess_mem           N struct sess_mem                         N
52  * n_sess               N struct sess                             N
53  * n_object             N struct object                           N
54  * n_vampireobject      N unresurrected objects                   N
55  * n_objectcore         N struct objectcore                       N
56  * n_objecthead         N struct objecthead                       N
57  * n_smf                N struct smf                              N
58  * n_smf_frag           N small free smf                          N
59  * n_smf_large          N large free smf                          N
60  * n_vbe_conn           N struct vbe_conn                         N
61  * n_wrk                N worker threads                          N
62  * n_wrk_create         N worker threads created                  N
63  * n_wrk_failed         N worker threads not created              N
64  * n_wrk_max            N worker threads limited                  N
65  * n_wrk_queue          N queued work requests                    N
66  * n_wrk_overflow       N overflowed work requests                N
67  * n_wrk_drop           N dropped work requests                   N
68  * n_backend            N backends                                N
69  * n_expired            N expired objects                         N
70  * n_lru_nuked          N LRU nuked objects                       N
71  * n_lru_saved          N LRU saved objects                       N
72  * n_lru_moved          N LRU moved objects                       N
73  * n_deathrow           N objects on deathrow                     N
74  * losthdr              HTTP header overflows                     N
75  * n_objsendfile        Objects sent with sendfile                N
76  * n_objwrite           Objects sent with write                   N
77  * n_objoverflow        Objects overflowing workspace             N
78  * s_sess               Total Sessions                            N
79  * s_req                Total Requests                            N
80  * s_pipe               Total pipe                                N
81  * s_pass               Total pass                                N
82  * s_fetch              Total fetch                               N
83  * s_hdrbytes           Total header bytes                        N
84  * s_bodybytes          Total body bytes                          N
85  * sess_closed          Session Closed                            N
86  * sess_pipeline        Session Pipeline                          N
87  * sess_readahead       Session Read Ahead                        N
88  * sess_linger          Session Linger                            N
89  * sess_herd            Session herd                              N
90  * shm_records          SHM records                               Y
91  * shm_writes           SHM writes                                Y
92  * shm_flushes          SHM flushes due to overflow               Y
93  * shm_cont             SHM MTX contention                        Y
94  * shm_cycles           SHM cycles through buffer                 Y
95  * sm_nreq              allocator requests                        Y
96  * sm_nobj              outstanding allocations                   Y
97  * sm_balloc            bytes allocated                           Y
98  * sm_bfree             bytes free                                Y
99  * sma_nreq             SMA allocator requests                    Y
100  * sma_nobj             SMA outstanding allocations               Y
101  * sma_nbytes           SMA outstanding bytes                     Y
102  * sma_balloc           SMA bytes allocated                       Y
103  * sma_bfree            SMA bytes free                            Y
104  * sms_nreq             SMS allocator requests                    Y
105  * sms_nobj             SMS outstanding allocations               Y
106  * sms_nbytes           SMS outstanding bytes                     Y
107  * sms_balloc           SMS bytes allocated                       Y
108  * sms_bfree            SMS bytes freed                           Y
109  * backend_req          Backend requests made                     N
110  * n_vcl                N vcl total                               N
111  * n_vcl_avail          N vcl available                           N
112  * n_vcl_discard        N vcl discarded                           N
113  * n_purge              N total active purges                     N
114  * n_purge_add          N new purges added                        N
115  * n_purge_retire       N old purges deleted                      N
116  * n_purge_obj_test     N objects tested                          N
117  * n_purge_re_test      N regexps tested against                  N
118  * n_purge_dups         N duplicate purges removed                N
119  * hcb_nolock           HCB Lookups without lock                  Y
120  * hcb_lock             HCB Lookups with lock                     Y
121  * hcb_insert           HCB Inserts                               Y
122  * esi_parse            Objects ESI parsed (unlock)               Y
123  * esi_errors           ESI parse errors (unlock)                 Y
124  */
125 #include "collectd.h"
126 #include "common.h"
127 #include "plugin.h"
128 #include "configfile.h"
129
130 #include <varnish/varnishapi.h>
131
132 #define USER_CONFIG_INIT {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
133 #define SET_MONITOR_FLAG(name, flag, value) if((strcasecmp(name, key) == 0) && IS_TRUE(value)) user_config.flag = 1
134
135 /* {{{ user_config_s */
136 struct user_config_s {
137         char *instance;
138
139         _Bool monitor_cache;
140         _Bool monitor_connections;
141         _Bool monitor_esi;
142         _Bool monitor_backend;
143         _Bool monitor_fetch;
144         _Bool monitor_hcb;
145         _Bool monitor_shm;
146         _Bool monitor_sma;
147         _Bool monitor_sms;
148         _Bool monitor_sm;
149 };
150 typedef struct user_config_s user_config_t; /* }}} */
151
152 static _Bool have_instance = 0;
153
154 static void varnish_submit(const char *type, const char *type_instance, gauge_t value) /* {{{ */
155 {
156         value_t values[1];
157         value_list_t vl = VALUE_LIST_INIT;
158
159         values[0].gauge = value;
160         vl.values_len = 1;
161         vl.values = values;
162
163         sstrncpy(vl.host         , hostname_g   , sizeof(vl.host));
164         sstrncpy(vl.plugin       , "varnish"    , sizeof(vl.plugin));
165         sstrncpy(vl.type         , type         , sizeof(vl.type));
166         sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
167
168         plugin_dispatch_values(&vl);
169 } /* }}} */
170
171 static void varnish_monitor(const user_config_t *conf, struct varnish_stats *VSL_stats) /* {{{ */
172 {
173         if(conf->monitor_cache)
174         {
175                 varnish_submit("varnish_cache_ratio", "cache_hit"    , VSL_stats->cache_hit);     /* Cache hits          */
176                 varnish_submit("varnish_cache_ratio", "cache_miss"   , VSL_stats->cache_miss);    /* Cache misses        */
177                 varnish_submit("varnish_cache_ratio", "cache_hitpass", VSL_stats->cache_hitpass); /* Cache hits for pass */
178         }
179
180         if(conf->monitor_connections)
181         {
182                 varnish_submit("varnish_connections", "client_connections-accepted", VSL_stats->client_conn); /* Client connections accepted */
183                 varnish_submit("varnish_connections", "client_connections-dropped" , VSL_stats->client_drop); /* Connection dropped, no sess */
184                 varnish_submit("varnish_connections", "client_connections-received", VSL_stats->client_req);  /* Client requests received    */
185         }
186
187         if(conf->monitor_esi)
188         {
189                 varnish_submit("varnish_esi", "esi_parsed", VSL_stats->esi_parse);  /* Objects ESI parsed (unlock) */
190                 varnish_submit("varnish_esi", "esi_errors", VSL_stats->esi_errors); /* ESI parse errors (unlock)   */
191         }
192
193         if(conf->monitor_backend)
194         {
195                 varnish_submit("varnish_backend_connections", "backend_connections-success"      , VSL_stats->backend_conn);      /* Backend conn. success       */
196                 varnish_submit("varnish_backend_connections", "backend_connections-not-attempted", VSL_stats->backend_unhealthy); /* Backend conn. not attempted */
197                 varnish_submit("varnish_backend_connections", "backend_connections-too-many"     , VSL_stats->backend_busy);      /* Backend conn. too many      */
198                 varnish_submit("varnish_backend_connections", "backend_connections-failures"     , VSL_stats->backend_fail);      /* Backend conn. failures      */
199                 varnish_submit("varnish_backend_connections", "backend_connections-reuses"       , VSL_stats->backend_reuse);     /* Backend conn. reuses        */
200                 varnish_submit("varnish_backend_connections", "backend_connections-was-closed"   , VSL_stats->backend_toolate);   /* Backend conn. was closed    */
201                 varnish_submit("varnish_backend_connections", "backend_connections-recycles"     , VSL_stats->backend_recycle);   /* Backend conn. recycles      */
202                 varnish_submit("varnish_backend_connections", "backend_connections-unused"       , VSL_stats->backend_unused);    /* Backend conn. unused        */
203         }
204
205         if(conf->monitor_fetch)
206         {
207                 varnish_submit("varnish_fetch", "fetch_head"       , VSL_stats->fetch_head);    /* Fetch head                */
208                 varnish_submit("varnish_fetch", "fetch_length"     , VSL_stats->fetch_length);  /* Fetch with length         */
209                 varnish_submit("varnish_fetch", "fetch_chunked"    , VSL_stats->fetch_chunked); /* Fetch chunked             */
210                 varnish_submit("varnish_fetch", "fetch_eof"        , VSL_stats->fetch_eof);     /* Fetch EOF                 */
211                 varnish_submit("varnish_fetch", "fetch_bad-headers", VSL_stats->fetch_bad);     /* Fetch bad headers         */
212                 varnish_submit("varnish_fetch", "fetch_close"      , VSL_stats->fetch_close);   /* Fetch wanted close        */
213                 varnish_submit("varnish_fetch", "fetch_oldhttp"    , VSL_stats->fetch_oldhttp); /* Fetch pre HTTP/1.1 closed */
214                 varnish_submit("varnish_fetch", "fetch_zero"       , VSL_stats->fetch_zero);    /* Fetch zero len            */
215                 varnish_submit("varnish_fetch", "fetch_failed"     , VSL_stats->fetch_failed);  /* Fetch failed              */
216         }
217
218         if(conf->monitor_hcb)
219         {
220                 varnish_submit("varnish_hcb", "hcb_nolock", VSL_stats->hcb_nolock); /* HCB Lookups without lock */
221                 varnish_submit("varnish_hcb", "hcb_lock"  , VSL_stats->hcb_lock);   /* HCB Lookups with lock    */
222                 varnish_submit("varnish_hcb", "hcb_insert", VSL_stats->hcb_insert); /* HCB Inserts              */
223         }
224
225         if(conf->monitor_shm)
226         {
227                 varnish_submit("varnish_shm", "shm_records"   , VSL_stats->shm_records); /* SHM records                 */
228                 varnish_submit("varnish_shm", "shm_writes"    , VSL_stats->shm_writes);  /* SHM writes                  */
229                 varnish_submit("varnish_shm", "shm_flushes"   , VSL_stats->shm_flushes); /* SHM flushes due to overflow */
230                 varnish_submit("varnish_shm", "shm_contention", VSL_stats->shm_cont);    /* SHM MTX contention          */
231                 varnish_submit("varnish_shm", "shm_cycles"    , VSL_stats->shm_cycles);  /* SHM cycles through buffer   */
232         }
233
234         if(conf->monitor_sma)
235         {
236                 varnish_submit("varnish_sma", "sma_req"   , VSL_stats->sma_nreq);   /* SMA allocator requests      */
237                 varnish_submit("varnish_sma", "sma_nobj"  , VSL_stats->sma_nobj);   /* SMA outstanding allocations */
238                 varnish_submit("varnish_sma", "sma_nbytes", VSL_stats->sma_nbytes); /* SMA outstanding bytes       */
239                 varnish_submit("varnish_sma", "sma_balloc", VSL_stats->sma_balloc); /* SMA bytes allocated         */
240                 varnish_submit("varnish_sma", "sma_bfree" , VSL_stats->sma_bfree);  /* SMA bytes free              */
241         }
242
243         if(conf->monitor_sms)
244         {
245                 varnish_submit("varnish_sms", "sms_nreq"  , VSL_stats->sms_nreq);   /* SMS allocator requests      */
246                 varnish_submit("varnish_sms", "sms_nobj"  , VSL_stats->sms_nobj);   /* SMS outstanding allocations */
247                 varnish_submit("varnish_sms", "sms_nbytes", VSL_stats->sms_nbytes); /* SMS outstanding bytes       */
248                 varnish_submit("varnish_sms", "sms_balloc", VSL_stats->sms_balloc); /* SMS bytes allocated         */
249                 varnish_submit("varnish_sms", "sms_bfree" , VSL_stats->sms_bfree);  /* SMS bytes freed             */
250         }
251
252         if(conf->monitor_sm)
253         {
254                 varnish_submit("varnish_sm", "sm_nreq"  , VSL_stats->sm_nreq);   /* allocator requests      */
255                 varnish_submit("varnish_sm", "sm_nobj"  , VSL_stats->sm_nobj);   /* outstanding allocations */
256                 varnish_submit("varnish_sm", "sm_balloc", VSL_stats->sm_balloc); /* bytes allocated         */
257                 varnish_submit("varnish_sm", "sm_bfree" , VSL_stats->sm_bfree);  /* bytes free              */
258         }
259 } /* }}} */
260
261 static int varnish_read(user_data_t *ud) /* {{{ */
262 {
263         struct varnish_stats *VSL_stats;
264         user_config_t *conf;
265
266         if ((ud == NULL) || (ud->data == NULL))
267                 return (EINVAL);
268
269         conf = ud->data;
270
271         VSL_stats = VSL_OpenStats(conf->instance);
272         if (VSL_stats == NULL)
273         {
274                 ERROR("Varnish plugin : unable to load statistics");
275
276                 return (-1);
277         }
278
279         varnish_monitor(conf, VSL_stats);
280
281     return (0);
282 } /* }}} */
283
284 static void varnish_config_free (void *ptr) /* {{{ */
285 {
286         user_config_t *conf = ptr;
287
288         if (conf == NULL)
289                 return;
290
291         sfree (conf->instance);
292         sfree (conf);
293 } /* }}} */
294
295 static int varnish_init (void) /* {{{ */
296 {
297         user_config_t *conf;
298         user_data_t ud;
299
300         if (have_instance)
301                 return (0);
302
303         conf = malloc (sizeof (*conf));
304         if (conf == NULL)
305                 return (ENOMEM);
306         memset (conf, 0, sizeof (*conf));
307
308         /* Default settings: */
309         conf->instance = NULL;
310         conf->monitor_cache = 1;
311         conf->monitor_connections = 1;
312
313         ud.data = conf;
314         ud.free_func = varnish_config_free;
315
316         plugin_register_complex_read (/* group = */ "varnish",
317                         /* name      = */ "varnish/localhost",
318                         /* callback  = */ varnish_read,
319                         /* interval  = */ NULL,
320                         /* user data = */ &ud);
321
322         return (0);
323 } /* }}} int varnish_init */
324
325 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
326 {
327         user_config_t *conf;
328         user_data_t ud;
329         char callback_name[DATA_MAX_NAME_LEN];
330         int i;
331
332         conf = malloc (sizeof (*conf));
333         if (conf == NULL)
334                 return (ENOMEM);
335         memset (conf, 0, sizeof (*conf));
336         conf->instance = NULL;
337
338         if (ci->values_num == 1)
339         {
340                 int status;
341
342                 status = cf_util_get_string (ci, &conf->instance);
343                 if (status != 0)
344                 {
345                         sfree (conf);
346                         return (status);
347                 }
348                 assert (conf->instance != NULL);
349
350                 if (strcmp ("localhost", conf->instance) == 0)
351                 {
352                         sfree (conf->instance);
353                         conf->instance = NULL;
354                 }
355         }
356         else if (ci->values_num > 1)
357         {
358                 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
359                                 "one argument.");
360                 return (EINVAL);
361         }
362
363         for (i = 0; i < ci->children_num; i++)
364         {
365                 oconfig_item_t *child = ci->children + i;
366
367                 if (strcasecmp ("MonitorCache", child->key) == 0)
368                         cf_util_get_boolean (child, &conf->monitor_cache);
369                 else if (strcasecmp ("MonitorConnections", child->key) == 0)
370                         cf_util_get_boolean (child, &conf->monitor_connections);
371                 else if (strcasecmp ("MonitorESI", child->key) == 0)
372                         cf_util_get_boolean (child, &conf->monitor_esi);
373                 else if (strcasecmp ("MonitorBackend", child->key) == 0)
374                         cf_util_get_boolean (child, &conf->monitor_backend);
375                 else if (strcasecmp ("MonitorFetch", child->key) == 0)
376                         cf_util_get_boolean (child, &conf->monitor_fetch);
377                 else if (strcasecmp ("MonitorHCB", child->key) == 0)
378                         cf_util_get_boolean (child, &conf->monitor_hcb);
379                 else if (strcasecmp ("MonitorSHM", child->key) == 0)
380                         cf_util_get_boolean (child, &conf->monitor_shm);
381                 else if (strcasecmp ("MonitorSMA", child->key) == 0)
382                         cf_util_get_boolean (child, &conf->monitor_sma);
383                 else if (strcasecmp ("MonitorSMS", child->key) == 0)
384                         cf_util_get_boolean (child, &conf->monitor_sms);
385                 else if (strcasecmp ("MonitorSM", child->key) == 0)
386                         cf_util_get_boolean (child, &conf->monitor_sm);
387                 else
388                 {
389                         WARNING ("Varnish plugin: Ignoring unknown "
390                                         "configuration option: \"%s\"",
391                                         child->key);
392                 }
393         }
394
395         if (!conf->monitor_cache
396                         && !conf->monitor_connections
397                         && !conf->monitor_esi
398                         && !conf->monitor_backend
399                         && !conf->monitor_fetch
400                         && !conf->monitor_hcb
401                         && !conf->monitor_shm
402                         && !conf->monitor_sma
403                         && !conf->monitor_sms
404                         && !conf->monitor_sm)
405         {
406                 WARNING ("Varnish plugin: No metric has been configured for "
407                                 "instance \"%s\". Disabling this instance.",
408                                 (conf->instance == NULL) ? "localhost" : conf->instance);
409                 return (EINVAL);
410         }
411
412         ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
413                         (conf->instance == NULL) ? "localhost" : conf->instance);
414
415         ud.data = conf;
416         ud.free_func = varnish_config_free;
417
418         plugin_register_complex_read (/* group = */ "varnish",
419                         /* name      = */ callback_name,
420                         /* callback  = */ varnish_read,
421                         /* interval  = */ NULL,
422                         /* user data = */ &ud);
423
424         have_instance = 1;
425
426         return (0);
427 } /* }}} int varnish_config_instance */
428
429 static int varnish_config (oconfig_item_t *ci) /* {{{ */
430 {
431         int i;
432
433         for (i = 0; i < ci->children_num; i++)
434         {
435                 oconfig_item_t *child = ci->children + i;
436
437                 if (strcasecmp ("Instance", child->key) == 0)
438                         varnish_config_instance (child);
439                 else
440                 {
441                         WARNING ("Varnish plugin: Ignoring unknown "
442                                         "configuration option: \"%s\"",
443                                         child->key);
444                 }
445         }
446
447         return (0);
448 } /* }}} int varnish_config */
449
450 void module_register (void) /* {{{ */
451 {
452         plugin_register_complex_config("varnish", varnish_config);
453         plugin_register_init ("varnish", varnish_init);
454 } /* }}} */
455
456 /* vim: set sw=8 noet fdm=marker : */