Varnish plugin: Use the correct data sets when submitting the values.
[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  */
126 #include "collectd.h"
127 #include "common.h"
128 #include "plugin.h"
129 #include "configfile.h"
130
131 #include <varnish/varnishapi.h>
132
133 /* {{{ user_config_s */
134 struct user_config_s {
135         char *instance;
136
137         _Bool monitor_cache;
138         _Bool monitor_connections;
139         _Bool monitor_esi;
140         _Bool monitor_backend;
141         _Bool monitor_fetch;
142         _Bool monitor_hcb;
143         _Bool monitor_shm;
144         _Bool monitor_sma;
145         _Bool monitor_sms;
146         _Bool monitor_sm;
147 };
148 typedef struct user_config_s user_config_t; /* }}} */
149
150 static _Bool have_instance = 0;
151
152 static int varnish_submit (const char *plugin_instance, /* {{{ */
153                 const char *type, const char *type_instance, value_t value)
154 {
155         value_list_t vl = VALUE_LIST_INIT;
156
157         vl.values = &value;
158         vl.values_len = 1;
159
160         sstrncpy(vl.host, hostname_g, sizeof(vl.host));
161
162         sstrncpy(vl.plugin, "varnish", sizeof(vl.plugin));
163         if (plugin_instance != NULL)
164                 sstrncpy (vl.plugin_instance, plugin_instance,
165                                 sizeof (vl.plugin_instance));
166
167         sstrncpy(vl.type, type, sizeof(vl.type));
168         if (type_instance != NULL)
169                 sstrncpy(vl.type_instance, type_instance,
170                                 sizeof(vl.type_instance));
171
172         return (plugin_dispatch_values(&vl));
173 } /* }}} int varnish_submit */
174
175 static int varnish_submit_gauge (const char *plugin_instance, /* {{{ */
176                 const char *type, const char *type_instance,
177                 uint64_t gauge_value)
178 {
179         value_t value;
180
181         value.gauge = (gauge_t) gauge_value;
182
183         return (varnish_submit (plugin_instance, type, type_instance, value));
184 } /* }}} int varnish_submit_gauge */
185
186 static int varnish_submit_derive (const char *plugin_instance, /* {{{ */
187                 const char *type, const char *type_instance,
188                 uint64_t derive_value)
189 {
190         value_t value;
191
192         value.derive = (derive_t) derive_value;
193
194         return (varnish_submit (plugin_instance, type, type_instance, value));
195 } /* }}} int varnish_submit_derive */
196
197 static void varnish_monitor(const user_config_t *conf, struct varnish_stats *VSL_stats) /* {{{ */
198 {
199         if(conf->monitor_cache)
200         {
201                 /* Cache hits */
202                 varnish_submit_derive (conf->instance, "cache_result", "hit"    , VSL_stats->cache_hit);
203                 /* Cache misses */
204                 varnish_submit_derive (conf->instance, "cache_result", "miss"   , VSL_stats->cache_miss);
205                 /* Cache hits for pass */
206                 varnish_submit_derive (conf->instance, "cache_result", "hitpass", VSL_stats->cache_hitpass);
207         }
208
209         if(conf->monitor_connections)
210         {
211                 /* Client connections accepted */
212                 varnish_submit_derive (conf->instance, "connections", "client-accepted", VSL_stats->client_conn);
213                 /* Connection dropped, no sess */
214                 varnish_submit_derive (conf->instance, "connections", "client-dropped" , VSL_stats->client_drop);
215                 /* Client requests received    */
216                 varnish_submit_derive (conf->instance, "connections", "client-received", VSL_stats->client_req);
217         }
218
219         if(conf->monitor_esi)
220         {
221                 /* Objects ESI parsed (unlock) */
222                 varnish_submit_derive (conf->instance, "total_operations", "esi-parsed", VSL_stats->esi_parse);
223                 /* ESI parse errors (unlock)   */
224                 varnish_submit_derive (conf->instance, "total_operations", "esi-error", VSL_stats->esi_errors);
225         }
226
227         if(conf->monitor_backend)
228         {
229                 /* Backend conn. success       */
230                 varnish_submit_derive (conf->instance, "connections", "backend-success"      , VSL_stats->backend_conn);
231                 /* Backend conn. not attempted */
232                 varnish_submit_derive (conf->instance, "connections", "backend-not-attempted", VSL_stats->backend_unhealthy);
233                 /* Backend conn. too many      */
234                 varnish_submit_derive (conf->instance, "connections", "backend-too-many"     , VSL_stats->backend_busy);
235                 /* Backend conn. failures      */
236                 varnish_submit_derive (conf->instance, "connections", "backend-failures"     , VSL_stats->backend_fail);
237                 /* Backend conn. reuses        */
238                 varnish_submit_derive (conf->instance, "connections", "backend-reuses"       , VSL_stats->backend_reuse);
239                 /* Backend conn. was closed    */
240                 varnish_submit_derive (conf->instance, "connections", "backend-was-closed"   , VSL_stats->backend_toolate);
241                 /* Backend conn. recycles      */
242                 varnish_submit_derive (conf->instance, "connections", "backend-recycled"     , VSL_stats->backend_recycle);
243                 /* Backend conn. unused        */
244                 varnish_submit_derive (conf->instance, "connections", "backend-unused"       , VSL_stats->backend_unused);
245         }
246
247         if(conf->monitor_fetch)
248         {
249                 /* Fetch head                */
250                 varnish_submit_derive (conf->instance, "http_requests", "fetch-head"       , VSL_stats->fetch_head);
251                 /* Fetch with length         */
252                 varnish_submit_derive (conf->instance, "http_requests", "fetch-length"     , VSL_stats->fetch_length);
253                 /* Fetch chunked             */
254                 varnish_submit_derive (conf->instance, "http_requests", "fetch-chunked"    , VSL_stats->fetch_chunked);
255                 /* Fetch EOF                 */
256                 varnish_submit_derive (conf->instance, "http_requests", "fetch-eof"        , VSL_stats->fetch_eof);
257                 /* Fetch bad headers         */
258                 varnish_submit_derive (conf->instance, "http_requests", "fetch-bad_headers", VSL_stats->fetch_bad);
259                 /* Fetch wanted close        */
260                 varnish_submit_derive (conf->instance, "http_requests", "fetch-close"      , VSL_stats->fetch_close);
261                 /* Fetch pre HTTP/1.1 closed */
262                 varnish_submit_derive (conf->instance, "http_requests", "fetch-oldhttp"    , VSL_stats->fetch_oldhttp);
263                 /* Fetch zero len            */
264                 varnish_submit_derive (conf->instance, "http_requests", "fetch-zero"       , VSL_stats->fetch_zero);
265                 /* Fetch failed              */
266                 varnish_submit_derive (conf->instance, "http_requests", "fetch-failed"     , VSL_stats->fetch_failed);
267         }
268
269         if(conf->monitor_hcb)
270         {
271                 /* HCB Lookups without lock */
272                 varnish_submit_derive (conf->instance, "cache_operation", "lookup_nolock", VSL_stats->hcb_nolock);
273                 /* HCB Lookups with lock    */
274                 varnish_submit_derive (conf->instance, "cache_operation", "lookup_lock",   VSL_stats->hcb_lock);
275                 /* HCB Inserts              */
276                 varnish_submit_derive (conf->instance, "cache_operation", "insert",        VSL_stats->hcb_insert);
277         }
278
279         if(conf->monitor_shm)
280         {
281                 /* SHM records                 */
282                 varnish_submit_derive (conf->instance, "total_operations", "shmlog-records"   , VSL_stats->shm_records);
283                 /* SHM writes                  */
284                 varnish_submit_derive (conf->instance, "total_operations", "shmlog-writes"    , VSL_stats->shm_writes);
285                 /* SHM flushes due to overflow */
286                 varnish_submit_derive (conf->instance, "total_operations", "shmlog-flushes"   , VSL_stats->shm_flushes);
287                 /* SHM MTX contention          */
288                 varnish_submit_derive (conf->instance, "total_operations", "shmlog-contention", VSL_stats->shm_cont);
289                 /* SHM cycles through buffer   */
290                 varnish_submit_derive (conf->instance, "total_operations", "shmlog-cycles"    , VSL_stats->shm_cycles);
291         }
292
293         if(conf->monitor_sm)
294         {
295                 /* allocator requests */
296                 varnish_submit_derive (conf->instance, "total_requests", "storage-file",      VSL_stats->sm_nreq);
297                 /* outstanding allocations */
298                 varnish_submit_gauge (conf->instance, "requests", "storage-file-outstanding", VSL_stats->sm_nobj);
299                 /* bytes allocated */
300                 varnish_submit_gauge (conf->instance, "bytes", "storage-file-allocated",      VSL_stats->sm_balloc);
301                 /* bytes free */
302                 varnish_submit_gauge (conf->instance, "bytes", "storage-file-free",           VSL_stats->sm_bfree);
303         }
304
305         if(conf->monitor_sma)
306         {
307                 /* SMA allocator requests */
308                 varnish_submit_derive (conf->instance, "total_requests", "storage-mem",      VSL_stats->sma_nreq);
309                 /* SMA outstanding allocations */
310                 varnish_submit_gauge (conf->instance, "requests", "storage-mem-outstanding", VSL_stats->sma_nobj);
311                 /* SMA outstanding bytes */
312                 varnish_submit_gauge (conf->instance, "bytes", "storage-mem-outstanding",    VSL_stats->sma_nbytes);
313                 /* SMA bytes allocated */
314                 varnish_submit_gauge (conf->instance, "bytes", "storage-mem-allocated",      VSL_stats->sma_balloc);
315                 /* SMA bytes free */
316                 varnish_submit_gauge (conf->instance, "bytes", "storage-mem-free" ,          VSL_stats->sma_bfree);
317         }
318
319         if(conf->monitor_sms)
320         {
321                 /* SMS allocator requests */
322                 varnish_submit_derive (conf->instance, "total_requests", "storage-synth",      VSL_stats->sms_nreq);
323                 /* SMS outstanding allocations */
324                 varnish_submit_gauge (conf->instance, "requests", "storage-synth-outstanding", VSL_stats->sms_nobj);
325                 /* SMS outstanding bytes */
326                 varnish_submit_gauge (conf->instance, "bytes", "storage-synth-outstanding",    VSL_stats->sms_nbytes);
327                 /* SMS bytes allocated */
328                 varnish_submit_gauge (conf->instance, "bytes", "storage-synth-allocated",      VSL_stats->sms_balloc);
329                 /* SMS bytes freed */
330                 varnish_submit_gauge (conf->instance, "bytes", "storage-synth-free",           VSL_stats->sms_bfree);
331         }
332 } /* }}} void varnish_monitor */
333
334 static int varnish_read(user_data_t *ud) /* {{{ */
335 {
336         struct varnish_stats *VSL_stats;
337         user_config_t *conf;
338
339         if ((ud == NULL) || (ud->data == NULL))
340                 return (EINVAL);
341
342         conf = ud->data;
343
344         VSL_stats = VSL_OpenStats(conf->instance);
345         if (VSL_stats == NULL)
346         {
347                 ERROR("Varnish plugin : unable to load statistics");
348
349                 return (-1);
350         }
351
352         varnish_monitor(conf, VSL_stats);
353
354     return (0);
355 } /* }}} */
356
357 static void varnish_config_free (void *ptr) /* {{{ */
358 {
359         user_config_t *conf = ptr;
360
361         if (conf == NULL)
362                 return;
363
364         sfree (conf->instance);
365         sfree (conf);
366 } /* }}} */
367
368 static int varnish_init (void) /* {{{ */
369 {
370         user_config_t *conf;
371         user_data_t ud;
372
373         if (have_instance)
374                 return (0);
375
376         conf = malloc (sizeof (*conf));
377         if (conf == NULL)
378                 return (ENOMEM);
379         memset (conf, 0, sizeof (*conf));
380
381         /* Default settings: */
382         conf->instance = NULL;
383         conf->monitor_cache = 1;
384         conf->monitor_backend = 1;
385         conf->monitor_connections = 1;
386         conf->monitor_shm = 1;
387
388         ud.data = conf;
389         ud.free_func = varnish_config_free;
390
391         plugin_register_complex_read (/* group = */ "varnish",
392                         /* name      = */ "varnish/localhost",
393                         /* callback  = */ varnish_read,
394                         /* interval  = */ NULL,
395                         /* user data = */ &ud);
396
397         return (0);
398 } /* }}} int varnish_init */
399
400 static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
401 {
402         user_config_t *conf;
403         user_data_t ud;
404         char callback_name[DATA_MAX_NAME_LEN];
405         int i;
406
407         conf = malloc (sizeof (*conf));
408         if (conf == NULL)
409                 return (ENOMEM);
410         memset (conf, 0, sizeof (*conf));
411         conf->instance = NULL;
412
413         if (ci->values_num == 1)
414         {
415                 int status;
416
417                 status = cf_util_get_string (ci, &conf->instance);
418                 if (status != 0)
419                 {
420                         sfree (conf);
421                         return (status);
422                 }
423                 assert (conf->instance != NULL);
424
425                 if (strcmp ("localhost", conf->instance) == 0)
426                 {
427                         sfree (conf->instance);
428                         conf->instance = NULL;
429                 }
430         }
431         else if (ci->values_num > 1)
432         {
433                 WARNING ("Varnish plugin: \"Instance\" blocks accept only "
434                                 "one argument.");
435                 return (EINVAL);
436         }
437
438         for (i = 0; i < ci->children_num; i++)
439         {
440                 oconfig_item_t *child = ci->children + i;
441
442                 if (strcasecmp ("CollectCache", child->key) == 0)
443                         cf_util_get_boolean (child, &conf->monitor_cache);
444                 else if (strcasecmp ("CollectConnections", child->key) == 0)
445                         cf_util_get_boolean (child, &conf->monitor_connections);
446                 else if (strcasecmp ("CollectESI", child->key) == 0)
447                         cf_util_get_boolean (child, &conf->monitor_esi);
448                 else if (strcasecmp ("CollectBackend", child->key) == 0)
449                         cf_util_get_boolean (child, &conf->monitor_backend);
450                 else if (strcasecmp ("CollectFetch", child->key) == 0)
451                         cf_util_get_boolean (child, &conf->monitor_fetch);
452                 else if (strcasecmp ("CollectHCB", child->key) == 0)
453                         cf_util_get_boolean (child, &conf->monitor_hcb);
454                 else if (strcasecmp ("CollectSHM", child->key) == 0)
455                         cf_util_get_boolean (child, &conf->monitor_shm);
456                 else if (strcasecmp ("CollectSMA", child->key) == 0)
457                         cf_util_get_boolean (child, &conf->monitor_sma);
458                 else if (strcasecmp ("CollectSMS", child->key) == 0)
459                         cf_util_get_boolean (child, &conf->monitor_sms);
460                 else if (strcasecmp ("CollectSM", child->key) == 0)
461                         cf_util_get_boolean (child, &conf->monitor_sm);
462                 else
463                 {
464                         WARNING ("Varnish plugin: Ignoring unknown "
465                                         "configuration option: \"%s\"",
466                                         child->key);
467                 }
468         }
469
470         if (!conf->monitor_cache
471                         && !conf->monitor_connections
472                         && !conf->monitor_esi
473                         && !conf->monitor_backend
474                         && !conf->monitor_fetch
475                         && !conf->monitor_hcb
476                         && !conf->monitor_shm
477                         && !conf->monitor_sma
478                         && !conf->monitor_sms
479                         && !conf->monitor_sm)
480         {
481                 WARNING ("Varnish plugin: No metric has been configured for "
482                                 "instance \"%s\". Disabling this instance.",
483                                 (conf->instance == NULL) ? "localhost" : conf->instance);
484                 return (EINVAL);
485         }
486
487         ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
488                         (conf->instance == NULL) ? "localhost" : conf->instance);
489
490         ud.data = conf;
491         ud.free_func = varnish_config_free;
492
493         plugin_register_complex_read (/* group = */ "varnish",
494                         /* name      = */ callback_name,
495                         /* callback  = */ varnish_read,
496                         /* interval  = */ NULL,
497                         /* user data = */ &ud);
498
499         have_instance = 1;
500
501         return (0);
502 } /* }}} int varnish_config_instance */
503
504 static int varnish_config (oconfig_item_t *ci) /* {{{ */
505 {
506         int i;
507
508         for (i = 0; i < ci->children_num; i++)
509         {
510                 oconfig_item_t *child = ci->children + i;
511
512                 if (strcasecmp ("Instance", child->key) == 0)
513                         varnish_config_instance (child);
514                 else
515                 {
516                         WARNING ("Varnish plugin: Ignoring unknown "
517                                         "configuration option: \"%s\"",
518                                         child->key);
519                 }
520         }
521
522         return (0);
523 } /* }}} int varnish_config */
524
525 void module_register (void) /* {{{ */
526 {
527         plugin_register_complex_config("varnish", varnish_config);
528         plugin_register_init ("varnish", varnish_init);
529 } /* }}} */
530
531 /* vim: set sw=8 noet fdm=marker : */