5260619d295d1b979057fb8c02db89ceb9c1c290
[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 };
33 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
34
35 static char *host = NULL;
36
37 static int tt_config (const char *key, const char *value);
38 static int tt_read (void);
39 static void tt_submit(gauge_t rnum, const char *type);
40
41 void module_register (void)
42 {
43   plugin_register_config("tokyotyrant", tt_config, config_keys, config_keys_num);
44   plugin_register_read("tokyotyrant", tt_read);
45 }
46
47 static int tt_config (const char *key, const char *value)
48 {
49
50   if (strcasecmp ("Host", key) == 0)
51   {
52     if (host != NULL)
53       free (host);
54     host = strdup(value);
55   }
56   return (0);
57 }
58
59 static void printerr(TCRDB *rdb)
60 {
61   int ecode = tcrdbecode(rdb);
62   ERROR ("tokyotyrant plugin: error: %d, %s", ecode, tcrdberrmsg(ecode));
63 }
64
65 static int tt_read (void) {
66   gauge_t rnum, size;
67
68   TCRDB *rdb = tcrdbnew();
69
70   if (!tcrdbopen2(rdb, host))
71   {
72     printerr (rdb);
73     tcrdbdel (rdb);
74     return (1);
75   }
76
77   rnum = tcrdbrnum(rdb);
78   size = tcrdbsize(rdb);
79
80   if (!tcrdbclose(rdb))
81   {
82     printerr (rdb);
83     tcrdbdel (rdb);
84     return (1);
85   }
86   tt_submit (rnum, "records");
87   tt_submit (size, "file_size");
88
89   return (0);
90 }
91
92 static void tt_submit (gauge_t val, const char* type)
93 {
94   value_t values[1];
95   value_list_t vl = VALUE_LIST_INIT;
96
97   values[0].gauge = val;
98
99   vl.values = values;
100   vl.values_len = STATIC_ARRAY_SIZE (values);
101
102   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
103   sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
104   sstrncpy (vl.plugin_instance, host, sizeof (vl.plugin_instance));
105   sstrncpy (vl.type, type, sizeof (vl.type));
106
107   plugin_dispatch_values (&vl);
108 }