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"
28 #include <tcrdb.h>
30 #define DEFAULT_HOST "127.0.0.1"
31 #define DEFAULT_PORT 1978
33 static const char *config_keys[] =
34 {
35 "Host",
36 "Port"
37 };
38 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
40 static char *config_host = NULL;
41 static char *config_port = NULL;
43 static TCRDB *rdb = NULL;
45 static int tt_config (const char *key, const char *value)
46 {
47 if (strcasecmp ("Host", key) == 0)
48 {
49 char *temp;
51 temp = strdup (value);
52 if (temp == NULL)
53 {
54 ERROR("tokyotyrant plugin: Host strdup failed.");
55 return (1);
56 }
57 sfree (config_host);
58 config_host = temp;
59 }
60 else if (strcasecmp ("Port", key) == 0)
61 {
62 char *temp;
64 temp = strdup (value);
65 if (temp == NULL)
66 {
67 ERROR("tokyotyrant plugin: Port strdup failed.");
68 return (1);
69 }
70 sfree (config_port);
71 config_port = temp;
72 }
73 else
74 {
75 ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
76 return (-1);
77 }
79 return (0);
80 }
82 static void printerr()
83 {
84 int ecode = tcrdbecode(rdb);
85 ERROR ("tokyotyrant plugin: error: %d, %s",
86 ecode, tcrdberrmsg(ecode));
87 }
89 static void tt_submit (gauge_t val, const char* type)
90 {
91 value_t values[1];
92 value_list_t vl = VALUE_LIST_INIT;
94 values[0].gauge = val;
96 vl.values = values;
97 vl.values_len = STATIC_ARRAY_SIZE (values);
99 sstrncpy (vl.host, config_host, sizeof (vl.host));
100 sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
101 sstrncpy (vl.plugin_instance, config_port,
102 sizeof (vl.plugin_instance));
103 sstrncpy (vl.type, type, sizeof (vl.type));
105 plugin_dispatch_values (&vl);
106 }
108 static void tt_open_db (void)
109 {
110 char* host = NULL;
111 int port = DEFAULT_PORT;
113 if (rdb != NULL)
114 return;
116 host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
118 if (config_port != NULL)
119 {
120 port = service_name_to_port_number (config_port);
121 if (port <= 0)
122 return;
123 }
125 rdb = tcrdbnew ();
126 if (rdb == NULL)
127 return;
128 else if (!tcrdbopen(rdb, host, port))
129 {
130 printerr ();
131 tcrdbdel (rdb);
132 rdb = NULL;
133 }
134 } /* void tt_open_db */
136 static int tt_read (void) {
137 gauge_t rnum, size;
139 tt_open_db ();
140 if (rdb == NULL)
141 return (-1);
143 rnum = tcrdbrnum(rdb);
144 tt_submit (rnum, "records");
146 size = tcrdbsize(rdb);
147 tt_submit (size, "file_size");
149 return (0);
150 }
152 static int tt_shutdown(void)
153 {
154 sfree(config_host);
155 sfree(config_port);
157 if (rdb != NULL)
158 {
159 if (!tcrdbclose(rdb))
160 {
161 printerr ();
162 tcrdbdel (rdb);
163 return (1);
164 }
165 tcrdbdel (rdb);
166 rdb = NULL;
167 }
169 return(0);
170 }
172 void module_register (void)
173 {
174 plugin_register_config("tokyotyrant", tt_config,
175 config_keys, config_keys_num);
176 plugin_register_read("tokyotyrant", tt_read);
177 plugin_register_shutdown("tokyotyrant", tt_shutdown);
178 }
180 /* vim: set sw=8 ts=8 tw=78 : */