Complete the collectd.conf man page.
[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}
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 };
37
38 typedef struct user_config_s user_config_t; /* }}} */
39
40 /* {{{ Configuration directives */
41 static user_config_t user_config = USER_CONFIG_INIT;
42
43 static const char *config_keys[] =
44 {
45   "MonitorCache",
46   "MonitorConnections",
47   "MonitorESI"
48 };
49
50 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); /* }}} */
51
52 static int varnish_config(const char *key, const char *value) /* {{{ */
53 {
54         SET_MONITOR_FLAG("MonitorCache", monitor_cache, value);
55         SET_MONITOR_FLAG("MonitorConnections", monitor_connections, value);
56         SET_MONITOR_FLAG("MonitorESI", monitor_esi, value);
57
58         return (0);
59 } /* }}} */
60
61 static void varnish_submit(const char *type, const char *type_instance, gauge_t value) /* {{{ */
62 {
63         value_t values[1];
64         value_list_t vl = VALUE_LIST_INIT;
65
66         values[0].gauge = value;
67         vl.values_len = 1;
68         vl.values = values;
69
70         sstrncpy(vl.host         , hostname_g   , sizeof(vl.host));
71         sstrncpy(vl.plugin       , "varnish"    , sizeof(vl.plugin));
72         sstrncpy(vl.type         , type         , sizeof(vl.type));
73         sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
74
75         plugin_dispatch_values(&vl);
76 } /* }}} */
77
78 static void varnish_monitor(struct varnish_stats *VSL_stats) /* {{{ */
79 {
80         if(user_config.monitor_cache == 1)
81         {
82                 varnish_submit("varnish_cache_ratio", "cache_hit"    , VSL_stats->cache_hit);
83                 varnish_submit("varnish_cache_ratio", "cache_miss"   , VSL_stats->cache_miss);
84                 varnish_submit("varnish_cache_ratio", "cache_hitpass", VSL_stats->cache_hitpass);
85         }
86
87         if(user_config.monitor_connections == 1)
88         {
89                 varnish_submit("varnish_connections", "client_connections-accepted", VSL_stats->client_conn);
90                 varnish_submit("varnish_connections", "client_connections-dropped" , VSL_stats->client_drop);
91                 varnish_submit("varnish_connections", "client_connections-received", VSL_stats->client_req);
92         }
93
94         if(user_config.monitor_esi == 1)
95         {
96                 varnish_submit("varnish_esi", "esi_parsed", VSL_stats->esi_parse);
97                 varnish_submit("varnish_esi", "esi_errors", VSL_stats->esi_errors);
98         }
99 } /* }}} */
100
101 static int varnish_read(void) /* {{{ */
102 {
103         struct varnish_stats *VSL_stats;
104         const char *varnish_instance_name = NULL;
105
106         if ((VSL_stats = VSL_OpenStats(varnish_instance_name)) == NULL)
107         {
108                 ERROR("Varnish plugin : unable to load statistics");
109
110                 return (-1);
111         }
112
113         varnish_monitor(VSL_stats);
114
115     return (0);
116 } /* }}} */
117
118 void module_register (void) /* {{{ */
119 {
120         plugin_register_config("varnish", varnish_config, config_keys, config_keys_num);
121         plugin_register_read("varnish", varnish_read);
122 } /* }}} */
123
124 /* vim: set sw=8 noet fdm=marker : */