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 **/
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>
29 static const char *config_keys[] =
30 {
31 "Host",
32 "Port"
33 };
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
36 static char *host = NULL;
37 static int port;
38 static char port_str[5];
40 static int tt_config (const char *key, const char *value)
41 {
42 if (strcasecmp ("Host", key) == 0)
43 {
44 if (host != NULL)
45 free (host);
46 host = strdup(value);
47 }
48 else if (strcasecmp ("Port", key) == 0)
49 {
50 port = atoi(value);
51 if ((port < 0) || (port > 65535))
52 {
53 ERROR ("tokyotyrant plugin: error: Port %s out of range", value);
54 return (-1);
55 }
56 ssnprintf(port_str, 5, "%i", port);
57 }
58 else
59 {
60 ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
61 return (-1);
62 }
64 return (0);
65 }
67 static void printerr(TCRDB *rdb)
68 {
69 int ecode = tcrdbecode(rdb);
70 ERROR ("tokyotyrant plugin: error: %d, %s",
71 ecode, tcrdberrmsg(ecode));
72 }
74 static void tt_submit (gauge_t val, const char* type)
75 {
76 value_t values[1];
77 value_list_t vl = VALUE_LIST_INIT;
79 values[0].gauge = val;
81 vl.values = values;
82 vl.values_len = STATIC_ARRAY_SIZE (values);
84 sstrncpy (vl.host, host, sizeof (vl.host));
85 sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
86 sstrncpy (vl.plugin_instance, port_str,
87 sizeof (vl.plugin_instance));
88 sstrncpy (vl.type, type, sizeof (vl.type));
90 plugin_dispatch_values (&vl);
91 }
93 static int tt_read (void) {
94 gauge_t rnum, size;
96 TCRDB *rdb = tcrdbnew();
98 if (!tcrdbopen(rdb, host, port))
99 {
100 printerr (rdb);
101 tcrdbdel (rdb);
102 return (1);
103 }
105 rnum = tcrdbrnum(rdb);
106 size = tcrdbsize(rdb);
107 tt_submit (rnum, "records");
108 tt_submit (size, "file_size");
110 if (!tcrdbclose(rdb))
111 {
112 printerr (rdb);
113 tcrdbdel (rdb);
114 return (1);
115 }
117 return (0);
118 }
120 void module_register (void)
121 {
122 plugin_register_config("tokyotyrant", tt_config,
123 config_keys, config_keys_num);
124 plugin_register_read("tokyotyrant", tt_read);
125 }
127 /* vim: set sw=8 ts=8 tw=78 : */