Changes suggested by Sebastian Harl.
[collectd.git] / src / tokyotyrant.c
1 /**
2  * collectd - src/tokyotyrant.c
3  * Copyright (C) 2009 Paul Sadauskas
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  *   Paul Sadauskas <psadauskas@gmail.com>
20  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_cache.h"
26 #include "utils_parse_option.h"
27 #include <tcrdb.h>
28
29 static const char *config_keys[] =
30 {
31         "Host",
32         "Port"
33 };
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
35
36 static char *host = NULL;
37
38 /* int is for opening connection, string is for plugin_instance */
39 static char *port_str = NULL;
40 static int   port;
41
42 static int tt_config (const char *key, const char *value)
43 {
44         if (strcasecmp ("Host", key) == 0)
45         {
46                 if (host != NULL)
47                         free (host);
48                 host = strdup(value);
49         }
50         else if (strcasecmp ("Port", key) == 0)
51         {
52                 if (port_str != NULL)
53                         free (port_str);
54                 port_str = strdup(value);
55
56                 port = atoi(value);
57
58                 if ((port < 0) || (port > 65535))
59                 {
60                         ERROR ("tokyotyrant plugin: error: Port %s out of range", value);
61                         return (-1);
62                 }
63         }
64         else
65         {
66                 ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
67                 return (-1);
68         }
69
70         return (0);
71 }
72
73 static void printerr(TCRDB *rdb)
74 {
75         int ecode = tcrdbecode(rdb);
76         ERROR ("tokyotyrant plugin: error: %d, %s",
77                         ecode, tcrdberrmsg(ecode));
78 }
79
80 static void tt_submit (gauge_t val, const char* type)
81 {
82         value_t values[1];
83         value_list_t vl = VALUE_LIST_INIT;
84
85         values[0].gauge = val;
86
87         vl.values = values;
88         vl.values_len = STATIC_ARRAY_SIZE (values);
89
90         sstrncpy (vl.host, host, sizeof (vl.host));
91         sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
92         sstrncpy (vl.plugin_instance, port_str,
93                         sizeof (vl.plugin_instance));
94         sstrncpy (vl.type, type, sizeof (vl.type));
95
96         plugin_dispatch_values (&vl);
97 }
98
99 static int tt_read (void) {
100         gauge_t rnum, size;
101
102         TCRDB *rdb = tcrdbnew();
103
104         if (!tcrdbopen(rdb, host, port))
105         {
106                 printerr (rdb);
107                 tcrdbdel (rdb);
108                 return (1);
109         }
110
111         rnum = tcrdbrnum(rdb);
112         size = tcrdbsize(rdb);
113         tt_submit (rnum, "records");
114         tt_submit (size, "file_size");
115
116         if (!tcrdbclose(rdb))
117         {
118                 printerr (rdb);
119                 tcrdbdel (rdb);
120                 return (1);
121         }
122
123         return (0);
124 }
125
126 void module_register (void)
127 {
128         plugin_register_config("tokyotyrant", tt_config,
129                         config_keys, config_keys_num);
130         plugin_register_read("tokyotyrant", tt_read);
131 }
132
133 /* vim: set sw=8 ts=8 tw=78 : */