| 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 <[email protected]> |
| 20 |
**/ |
| 21 |
|
| 22 |
#include "collectd.h" |
| 23 |
|
| 24 |
#include "plugin.h" |
| 25 |
#include "utils/common/common.h" |
| 26 |
#include "utils_cache.h" |
| 27 |
|
| 28 |
#include <tcrdb.h> |
| 29 |
|
| 30 |
#define DEFAULT_HOST "127.0.0.1" |
| 31 |
#define DEFAULT_PORT 1978 |
| 32 |
|
| 33 |
static const char *config_keys[] = {"Host", "Port"}; |
| 34 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 35 |
|
| 36 |
static char *config_host; |
| 37 |
static char *config_port; |
| 38 |
|
| 39 |
static TCRDB *rdb; |
| 40 |
|
| 41 |
static int tt_config(const char *key, const char *value) { |
| 42 |
if (strcasecmp("Host", key) == 0) { |
| 43 |
char *temp; |
| 44 |
|
| 45 |
temp = strdup(value); |
| 46 |
if (temp == NULL) { |
| 47 |
ERROR("tokyotyrant plugin: Host strdup failed."); |
| 48 |
return 1; |
| 49 |
} |
| 50 |
sfree(config_host); |
| 51 |
config_host = temp; |
| 52 |
} else if (strcasecmp("Port", key) == 0) { |
| 53 |
char *temp; |
| 54 |
|
| 55 |
temp = strdup(value); |
| 56 |
if (temp == NULL) { |
| 57 |
ERROR("tokyotyrant plugin: Port strdup failed."); |
| 58 |
return 1; |
| 59 |
} |
| 60 |
sfree(config_port); |
| 61 |
config_port = temp; |
| 62 |
} else { |
| 63 |
ERROR("tokyotyrant plugin: error: unrecognized configuration key %s", key); |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static void printerr(void) { |
| 71 |
int ecode = tcrdbecode(rdb); |
| 72 |
ERROR("tokyotyrant plugin: error: %d, %s", ecode, tcrdberrmsg(ecode)); |
| 73 |
} |
| 74 |
|
| 75 |
static void tt_submit(gauge_t value, const char *type) { |
| 76 |
value_list_t vl = VALUE_LIST_INIT; |
| 77 |
|
| 78 |
vl.values = &(value_t){.gauge = value}; |
| 79 |
vl.values_len = 1; |
| 80 |
|
| 81 |
sstrncpy(vl.host, config_host, sizeof(vl.host)); |
| 82 |
sstrncpy(vl.plugin, "tokyotyrant", sizeof(vl.plugin)); |
| 83 |
sstrncpy(vl.plugin_instance, config_port, sizeof(vl.plugin_instance)); |
| 84 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 85 |
|
| 86 |
plugin_dispatch_values(&vl); |
| 87 |
} |
| 88 |
|
| 89 |
static void tt_open_db(void) { |
| 90 |
const char *host; |
| 91 |
int port = DEFAULT_PORT; |
| 92 |
|
| 93 |
if (rdb != NULL) |
| 94 |
return; |
| 95 |
|
| 96 |
host = ((config_host != NULL) ? config_host : DEFAULT_HOST); |
| 97 |
|
| 98 |
if (config_port != NULL) { |
| 99 |
port = service_name_to_port_number(config_port); |
| 100 |
if (port <= 0) |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
rdb = tcrdbnew(); |
| 105 |
if (rdb == NULL) |
| 106 |
return; |
| 107 |
else if (!tcrdbopen(rdb, host, port)) { |
| 108 |
printerr(); |
| 109 |
tcrdbdel(rdb); |
| 110 |
rdb = NULL; |
| 111 |
} |
| 112 |
} /* void tt_open_db */ |
| 113 |
|
| 114 |
static int tt_read(void) { |
| 115 |
gauge_t rnum, size; |
| 116 |
|
| 117 |
tt_open_db(); |
| 118 |
if (rdb == NULL) |
| 119 |
return -1; |
| 120 |
|
| 121 |
rnum = tcrdbrnum(rdb); |
| 122 |
tt_submit(rnum, "records"); |
| 123 |
|
| 124 |
size = tcrdbsize(rdb); |
| 125 |
tt_submit(size, "file_size"); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static int tt_shutdown(void) { |
| 131 |
sfree(config_host); |
| 132 |
sfree(config_port); |
| 133 |
|
| 134 |
if (rdb != NULL) { |
| 135 |
if (!tcrdbclose(rdb)) { |
| 136 |
printerr(); |
| 137 |
tcrdbdel(rdb); |
| 138 |
return 1; |
| 139 |
} |
| 140 |
tcrdbdel(rdb); |
| 141 |
rdb = NULL; |
| 142 |
} |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
void module_register(void) { |
| 148 |
plugin_register_config("tokyotyrant", tt_config, config_keys, |
| 149 |
config_keys_num); |
| 150 |
plugin_register_read("tokyotyrant", tt_read); |
| 151 |
plugin_register_shutdown("tokyotyrant", tt_shutdown); |
| 152 |
} |