Varnish plugin: Added HCB monitoring
[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 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <varnish/varnishapi.h>
27
28 #define USER_CONFIG_INIT {0, 0, 0, 0, 0,0}
29 #define SET_MONITOR_FLAG(name, flag, value) if((strcasecmp(name, key) == 0) && IS_TRUE(value)) user_config.flag = 1
30
31 /* {{{ user_config_s */
32 struct user_config_s {
33         int monitor_cache;
34         int monitor_connections;
35         int monitor_esi;
36         int monitor_backend;
37         int monitor_fetch;
38         int monitor_hcb;
39 };
40
41 typedef struct user_config_s user_config_t; /* }}} */
42
43 /* {{{ Configuration directives */
44 static user_config_t user_config = USER_CONFIG_INIT;
45
46 static const char *config_keys[] =
47 {
48   "MonitorCache",
49   "MonitorConnections",
50   "MonitorESI",
51   "MonitorBackend",
52   "MonitorFetch",
53   "MonitorHCB"
54 };
55
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); /* }}} */
57
58 static int varnish_config(const char *key, const char *value) /* {{{ */
59 {
60         SET_MONITOR_FLAG("MonitorCache", monitor_cache, value);
61         SET_MONITOR_FLAG("MonitorConnections", monitor_connections, value);
62         SET_MONITOR_FLAG("MonitorESI", monitor_esi, value);
63         SET_MONITOR_FLAG("MonitorBackend", monitor_backend, value);
64         SET_MONITOR_FLAG("MonitorFetch", monitor_fetch, value);
65         SET_MONITOR_FLAG("MonitorHCB", monitor_hcb, value);
66
67         return (0);
68 } /* }}} */
69
70 static void varnish_submit(const char *type, const char *type_instance, gauge_t value) /* {{{ */
71 {
72         value_t values[1];
73         value_list_t vl = VALUE_LIST_INIT;
74
75         values[0].gauge = value;
76         vl.values_len = 1;
77         vl.values = values;
78
79         sstrncpy(vl.host         , hostname_g   , sizeof(vl.host));
80         sstrncpy(vl.plugin       , "varnish"    , sizeof(vl.plugin));
81         sstrncpy(vl.type         , type         , sizeof(vl.type));
82         sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
83
84         plugin_dispatch_values(&vl);
85 } /* }}} */
86
87 static void varnish_monitor(struct varnish_stats *VSL_stats) /* {{{ */
88 {
89         if(user_config.monitor_cache == 1)
90         {
91                 varnish_submit("varnish_cache_ratio", "cache_hit"    , VSL_stats->cache_hit);     /* Cache hits          */
92                 varnish_submit("varnish_cache_ratio", "cache_miss"   , VSL_stats->cache_miss);    /* Cache misses        */
93                 varnish_submit("varnish_cache_ratio", "cache_hitpass", VSL_stats->cache_hitpass); /* Cache hits for pass */
94         }
95
96         if(user_config.monitor_connections == 1)
97         {
98                 varnish_submit("varnish_connections", "client_connections-accepted", VSL_stats->client_conn); /* Client connections accepted */
99                 varnish_submit("varnish_connections", "client_connections-dropped" , VSL_stats->client_drop); /* Connection dropped, no sess */
100                 varnish_submit("varnish_connections", "client_connections-received", VSL_stats->client_req);  /* Client requests received    */
101         }
102
103         if(user_config.monitor_esi == 1)
104         {
105                 varnish_submit("varnish_esi", "esi_parsed", VSL_stats->esi_parse);  /* Objects ESI parsed (unlock) */
106                 varnish_submit("varnish_esi", "esi_errors", VSL_stats->esi_errors); /* ESI parse errors (unlock)   */
107         }
108
109         if(user_config.monitor_backend == 1)
110         {
111                 varnish_submit("varnish_backend_connections", "backend_connections-success"      , VSL_stats->backend_conn);      /* Backend conn. success       */
112                 varnish_submit("varnish_backend_connections", "backend_connections-not-attempted", VSL_stats->backend_unhealthy); /* Backend conn. not attempted */
113                 varnish_submit("varnish_backend_connections", "backend_connections-too-many"     , VSL_stats->backend_busy);      /* Backend conn. too many      */
114                 varnish_submit("varnish_backend_connections", "backend_connections-failures"     , VSL_stats->backend_fail);      /* Backend conn. failures      */
115                 varnish_submit("varnish_backend_connections", "backend_connections-reuses"       , VSL_stats->backend_reuse);     /* Backend conn. reuses        */
116                 varnish_submit("varnish_backend_connections", "backend_connections-was-closed"   , VSL_stats->backend_toolate);   /* Backend conn. was closed    */
117                 varnish_submit("varnish_backend_connections", "backend_connections-recycles"     , VSL_stats->backend_recycle);   /* Backend conn. recycles      */
118                 varnish_submit("varnish_backend_connections", "backend_connections-unused"       , VSL_stats->backend_unused);    /* Backend conn. unused        */
119         }
120
121         if(user_config.monitor_fetch == 1)
122         {
123                 varnish_submit("varnish_fetch", "fetch_head"       , VSL_stats->fetch_head);    /* Fetch head                */
124                 varnish_submit("varnish_fetch", "fetch_length"     , VSL_stats->fetch_length);  /* Fetch with length         */
125                 varnish_submit("varnish_fetch", "fetch_chunked"    , VSL_stats->fetch_chunked); /* Fetch chunked             */
126                 varnish_submit("varnish_fetch", "fetch_eof"        , VSL_stats->fetch_eof);     /* Fetch EOF                 */
127                 varnish_submit("varnish_fetch", "fetch_bad-headers", VSL_stats->fetch_bad);     /* Fetch bad headers         */
128                 varnish_submit("varnish_fetch", "fetch_close"      , VSL_stats->fetch_close);   /* Fetch wanted close        */
129                 varnish_submit("varnish_fetch", "fetch_oldhttp"    , VSL_stats->fetch_oldhttp); /* Fetch pre HTTP/1.1 closed */
130                 varnish_submit("varnish_fetch", "fetch_zero"       , VSL_stats->fetch_zero);    /* Fetch zero len            */
131                 varnish_submit("varnish_fetch", "fetch_failed"     , VSL_stats->fetch_failed);  /* Fetch failed              */
132         }
133
134         if(user_config.monitor_hcb == 1)
135         {
136                 varnish_submit("varnish_hcb", "hcb_nolock", VSL_stats->hcb_nolock); /* HCB Lookups without lock */
137                 varnish_submit("varnish_hcb", "hcb_lock"  , VSL_stats->hcb_lock);   /* HCB Lookups with lock    */
138                 varnish_submit("varnish_hcb", "hcb_insert", VSL_stats->hcb_insert); /* HCB Inserts              */
139         }
140 } /* }}} */
141
142 static int varnish_read(void) /* {{{ */
143 {
144         struct varnish_stats *VSL_stats;
145         const char *varnish_instance_name = NULL;
146
147         if ((VSL_stats = VSL_OpenStats(varnish_instance_name)) == NULL)
148         {
149                 ERROR("Varnish plugin : unable to load statistics");
150
151                 return (-1);
152         }
153
154         varnish_monitor(VSL_stats);
155
156     return (0);
157 } /* }}} */
158
159 void module_register (void) /* {{{ */
160 {
161         plugin_register_config("varnish", varnish_config, config_keys, config_keys_num);
162         plugin_register_read("varnish", varnish_read);
163 } /* }}} */
164
165 /* vim: set sw=8 noet fdm=marker : */