2 * collectd - src/varnish.c
3 * Copyright (C) 2010 Jerome Renard
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.
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.
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
19 * Jerome Renard <jerome.renard@gmail.com>
26 #include <varnish/varnishapi.h>
28 #define USER_CONFIG_INIT {0, 0, 0, 0}
29 #define SET_MONITOR_FLAG(name, flag, value) if((strcasecmp(name, key) == 0) && IS_TRUE(value)) user_config.flag = 1
31 /* {{{ user_config_s */
32 struct user_config_s {
34 int monitor_connections;
39 typedef struct user_config_s user_config_t; /* }}} */
41 /* {{{ Configuration directives */
42 static user_config_t user_config = USER_CONFIG_INIT;
44 static const char *config_keys[] =
52 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); /* }}} */
54 static int varnish_config(const char *key, const char *value) /* {{{ */
56 SET_MONITOR_FLAG("MonitorCache", monitor_cache, value);
57 SET_MONITOR_FLAG("MonitorConnections", monitor_connections, value);
58 SET_MONITOR_FLAG("MonitorESI", monitor_esi, value);
59 SET_MONITOR_FLAG("MonitorBackend", monitor_backend, value);
64 static void varnish_submit(const char *type, const char *type_instance, gauge_t value) /* {{{ */
67 value_list_t vl = VALUE_LIST_INIT;
69 values[0].gauge = value;
73 sstrncpy(vl.host , hostname_g , sizeof(vl.host));
74 sstrncpy(vl.plugin , "varnish" , sizeof(vl.plugin));
75 sstrncpy(vl.type , type , sizeof(vl.type));
76 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
78 plugin_dispatch_values(&vl);
81 static void varnish_monitor(struct varnish_stats *VSL_stats) /* {{{ */
83 if(user_config.monitor_cache == 1)
85 varnish_submit("varnish_cache_ratio", "cache_hit" , VSL_stats->cache_hit); /* Cache hits */
86 varnish_submit("varnish_cache_ratio", "cache_miss" , VSL_stats->cache_miss); /* Cache misses */
87 varnish_submit("varnish_cache_ratio", "cache_hitpass", VSL_stats->cache_hitpass); /* Cache hits for pass */
90 if(user_config.monitor_connections == 1)
92 varnish_submit("varnish_connections", "client_connections-accepted", VSL_stats->client_conn); /* Client connections accepted */
93 varnish_submit("varnish_connections", "client_connections-dropped" , VSL_stats->client_drop); /* Connection dropped, no sess */
94 varnish_submit("varnish_connections", "client_connections-received", VSL_stats->client_req); /* Client requests received */
97 if(user_config.monitor_esi == 1)
99 varnish_submit("varnish_esi", "esi_parsed", VSL_stats->esi_parse); /* Objects ESI parsed (unlock) */
100 varnish_submit("varnish_esi", "esi_errors", VSL_stats->esi_errors); /* ESI parse errors (unlock) */
103 if(user_config.monitor_backend == 1)
105 varnish_submit("varnish_backend_connections", "backend_connections-success" , VSL_stats->backend_conn); /* Backend conn. success */
106 varnish_submit("varnish_backend_connections", "backend_connections-not-attempted", VSL_stats->backend_unhealthy); /* Backend conn. not attempted */
107 varnish_submit("varnish_backend_connections", "backend_connections-too-many" , VSL_stats->backend_busy); /* Backend conn. too many */
108 varnish_submit("varnish_backend_connections", "backend_connections-failures" , VSL_stats->backend_fail); /* Backend conn. failures */
109 varnish_submit("varnish_backend_connections", "backend_connections-reuses" , VSL_stats->backend_reuse); /* Backend conn. reuses */
110 varnish_submit("varnish_backend_connections", "backend_connections-was-closed" , VSL_stats->backend_toolate); /* Backend conn. was closed */
111 varnish_submit("varnish_backend_connections", "backend_connections-recycles" , VSL_stats->backend_recycle); /* Backend conn. recycles */
112 varnish_submit("varnish_backend_connections", "backend_connections-unused" , VSL_stats->backend_unused); /* Backend conn. unused */
116 static int varnish_read(void) /* {{{ */
118 struct varnish_stats *VSL_stats;
119 const char *varnish_instance_name = NULL;
121 if ((VSL_stats = VSL_OpenStats(varnish_instance_name)) == NULL)
123 ERROR("Varnish plugin : unable to load statistics");
128 varnish_monitor(VSL_stats);
133 void module_register (void) /* {{{ */
135 plugin_register_config("varnish", varnish_config, config_keys, config_keys_num);
136 plugin_register_read("varnish", varnish_read);
139 /* vim: set sw=8 noet fdm=marker : */