Varnish plugin: Added SMA monitor
[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                        N
96  * sm_nobj              outstanding allocations                   N
97  * sm_balloc            bytes allocated                           N
98  * sm_bfree             bytes free                                N
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                    N
105  * sms_nobj             SMS outstanding allocations               N
106  * sms_nbytes           SMS outstanding bytes                     N
107  * sms_balloc           SMS bytes allocated                       N
108  * sms_bfree            SMS bytes freed                           N
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
129 #include <varnish/varnishapi.h>
130
131 #define USER_CONFIG_INIT {0, 0, 0, 0, 0, 0, 0}
132 #define SET_MONITOR_FLAG(name, flag, value) if((strcasecmp(name, key) == 0) && IS_TRUE(value)) user_config.flag = 1
133
134 /* {{{ user_config_s */
135 struct user_config_s {
136         int monitor_cache;
137         int monitor_connections;
138         int monitor_esi;
139         int monitor_backend;
140         int monitor_fetch;
141         int monitor_hcb;
142         int monitor_shm;
143         int monitor_sma;
144 };
145
146 typedef struct user_config_s user_config_t; /* }}} */
147
148 /* {{{ Configuration directives */
149 static user_config_t user_config = USER_CONFIG_INIT;
150
151 static const char *config_keys[] =
152 {
153   "MonitorCache",
154   "MonitorConnections",
155   "MonitorESI",
156   "MonitorBackend",
157   "MonitorFetch",
158   "MonitorHCB",
159   "MonitorSHM",
160   "MonitorSMA"
161 };
162
163 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); /* }}} */
164
165 static int varnish_config(const char *key, const char *value) /* {{{ */
166 {
167         SET_MONITOR_FLAG("MonitorCache"      , monitor_cache      , value);
168         SET_MONITOR_FLAG("MonitorConnections", monitor_connections, value);
169         SET_MONITOR_FLAG("MonitorESI"        , monitor_esi        , value);
170         SET_MONITOR_FLAG("MonitorBackend"    , monitor_backend    , value);
171         SET_MONITOR_FLAG("MonitorFetch"      , monitor_fetch      , value);
172         SET_MONITOR_FLAG("MonitorHCB"        , monitor_hcb        , value);
173         SET_MONITOR_FLAG("MonitorSHM"        , monitor_shm        , value);
174         SET_MONITOR_FLAG("MonitorSMA"        , monitor_sma        , value);
175
176         return (0);
177 } /* }}} */
178
179 static void varnish_submit(const char *type, const char *type_instance, gauge_t value) /* {{{ */
180 {
181         value_t values[1];
182         value_list_t vl = VALUE_LIST_INIT;
183
184         values[0].gauge = value;
185         vl.values_len = 1;
186         vl.values = values;
187
188         sstrncpy(vl.host         , hostname_g   , sizeof(vl.host));
189         sstrncpy(vl.plugin       , "varnish"    , sizeof(vl.plugin));
190         sstrncpy(vl.type         , type         , sizeof(vl.type));
191         sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
192
193         plugin_dispatch_values(&vl);
194 } /* }}} */
195
196 static void varnish_monitor(struct varnish_stats *VSL_stats) /* {{{ */
197 {
198         if(user_config.monitor_cache == 1)
199         {
200                 varnish_submit("varnish_cache_ratio", "cache_hit"    , VSL_stats->cache_hit);     /* Cache hits          */
201                 varnish_submit("varnish_cache_ratio", "cache_miss"   , VSL_stats->cache_miss);    /* Cache misses        */
202                 varnish_submit("varnish_cache_ratio", "cache_hitpass", VSL_stats->cache_hitpass); /* Cache hits for pass */
203         }
204
205         if(user_config.monitor_connections == 1)
206         {
207                 varnish_submit("varnish_connections", "client_connections-accepted", VSL_stats->client_conn); /* Client connections accepted */
208                 varnish_submit("varnish_connections", "client_connections-dropped" , VSL_stats->client_drop); /* Connection dropped, no sess */
209                 varnish_submit("varnish_connections", "client_connections-received", VSL_stats->client_req);  /* Client requests received    */
210         }
211
212         if(user_config.monitor_esi == 1)
213         {
214                 varnish_submit("varnish_esi", "esi_parsed", VSL_stats->esi_parse);  /* Objects ESI parsed (unlock) */
215                 varnish_submit("varnish_esi", "esi_errors", VSL_stats->esi_errors); /* ESI parse errors (unlock)   */
216         }
217
218         if(user_config.monitor_backend == 1)
219         {
220                 varnish_submit("varnish_backend_connections", "backend_connections-success"      , VSL_stats->backend_conn);      /* Backend conn. success       */
221                 varnish_submit("varnish_backend_connections", "backend_connections-not-attempted", VSL_stats->backend_unhealthy); /* Backend conn. not attempted */
222                 varnish_submit("varnish_backend_connections", "backend_connections-too-many"     , VSL_stats->backend_busy);      /* Backend conn. too many      */
223                 varnish_submit("varnish_backend_connections", "backend_connections-failures"     , VSL_stats->backend_fail);      /* Backend conn. failures      */
224                 varnish_submit("varnish_backend_connections", "backend_connections-reuses"       , VSL_stats->backend_reuse);     /* Backend conn. reuses        */
225                 varnish_submit("varnish_backend_connections", "backend_connections-was-closed"   , VSL_stats->backend_toolate);   /* Backend conn. was closed    */
226                 varnish_submit("varnish_backend_connections", "backend_connections-recycles"     , VSL_stats->backend_recycle);   /* Backend conn. recycles      */
227                 varnish_submit("varnish_backend_connections", "backend_connections-unused"       , VSL_stats->backend_unused);    /* Backend conn. unused        */
228         }
229
230         if(user_config.monitor_fetch == 1)
231         {
232                 varnish_submit("varnish_fetch", "fetch_head"       , VSL_stats->fetch_head);    /* Fetch head                */
233                 varnish_submit("varnish_fetch", "fetch_length"     , VSL_stats->fetch_length);  /* Fetch with length         */
234                 varnish_submit("varnish_fetch", "fetch_chunked"    , VSL_stats->fetch_chunked); /* Fetch chunked             */
235                 varnish_submit("varnish_fetch", "fetch_eof"        , VSL_stats->fetch_eof);     /* Fetch EOF                 */
236                 varnish_submit("varnish_fetch", "fetch_bad-headers", VSL_stats->fetch_bad);     /* Fetch bad headers         */
237                 varnish_submit("varnish_fetch", "fetch_close"      , VSL_stats->fetch_close);   /* Fetch wanted close        */
238                 varnish_submit("varnish_fetch", "fetch_oldhttp"    , VSL_stats->fetch_oldhttp); /* Fetch pre HTTP/1.1 closed */
239                 varnish_submit("varnish_fetch", "fetch_zero"       , VSL_stats->fetch_zero);    /* Fetch zero len            */
240                 varnish_submit("varnish_fetch", "fetch_failed"     , VSL_stats->fetch_failed);  /* Fetch failed              */
241         }
242
243         if(user_config.monitor_hcb == 1)
244         {
245                 varnish_submit("varnish_hcb", "hcb_nolock", VSL_stats->hcb_nolock); /* HCB Lookups without lock */
246                 varnish_submit("varnish_hcb", "hcb_lock"  , VSL_stats->hcb_lock);   /* HCB Lookups with lock    */
247                 varnish_submit("varnish_hcb", "hcb_insert", VSL_stats->hcb_insert); /* HCB Inserts              */
248         }
249
250         if(user_config.monitor_shm == 1)
251         {
252                 varnish_submit("varnish_shm", "shm_records"   , VSL_stats->shm_records); /* SHM records                 */
253                 varnish_submit("varnish_shm", "shm_writes"    , VSL_stats->shm_writes);  /* SHM writes                  */
254                 varnish_submit("varnish_shm", "shm_flushes"   , VSL_stats->shm_flushes); /* SHM flushes due to overflow */
255                 varnish_submit("varnish_shm", "shm_contention", VSL_stats->shm_cont);    /* SHM MTX contention          */
256                 varnish_submit("varnish_shm", "shm_cycles"    , VSL_stats->shm_cycles);  /* SHM cycles through buffer   */
257         }
258
259         if(user_config.monitor_sma == 1)
260         {
261                 varnish_submit("varnish_sma", "sma_req"   , VSL_stats->sma_nreq);   /* SMA allocator requests      */
262                 varnish_submit("varnish_sma", "sma_nobj"  , VSL_stats->sma_nobj);   /* SMA outstanding allocations */
263                 varnish_submit("varnish_sma", "sma_nbytes", VSL_stats->sma_nbytes); /* SMA outstanding bytes       */
264                 varnish_submit("varnish_sma", "sma_balloc", VSL_stats->sma_balloc); /* SMA bytes allocated         */
265                 varnish_submit("varnish_sma", "sma_bfree" , VSL_stats->sma_bfree);  /* SMA bytes free              */
266         }
267 } /* }}} */
268
269 static int varnish_read(void) /* {{{ */
270 {
271         struct varnish_stats *VSL_stats;
272         const char *varnish_instance_name = NULL;
273
274         if ((VSL_stats = VSL_OpenStats(varnish_instance_name)) == NULL)
275         {
276                 ERROR("Varnish plugin : unable to load statistics");
277
278                 return (-1);
279         }
280
281         varnish_monitor(VSL_stats);
282
283     return (0);
284 } /* }}} */
285
286 void module_register (void) /* {{{ */
287 {
288         plugin_register_config("varnish", varnish_config, config_keys, config_keys_num);
289         plugin_register_read("varnish", varnish_read);
290 } /* }}} */
291
292 /* vim: set sw=8 noet fdm=marker : */