PageRenderTime 69ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/table.c

http://github.com/octo/collectd
C | 502 lines | 365 code | 96 blank | 41 comment | 120 complexity | ac0282a824b1d01fdda83d710b3e4b3d MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * collectd - src/table.c
  3. * Copyright (C) 2009 Sebastian Harl
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Sebastian Harl <sh at tokkee.org>
  25. **/
  26. /*
  27. * This module provides generic means to parse and dispatch tabular data.
  28. */
  29. #include "collectd.h"
  30. #include "utils/common/common.h"
  31. #include "plugin.h"
  32. #define log_err(...) ERROR("table plugin: " __VA_ARGS__)
  33. #define log_warn(...) WARNING("table plugin: " __VA_ARGS__)
  34. /*
  35. * private data types
  36. */
  37. typedef struct {
  38. char *type;
  39. char *instance_prefix;
  40. size_t *instances;
  41. size_t instances_num;
  42. size_t *values;
  43. size_t values_num;
  44. const data_set_t *ds;
  45. } tbl_result_t;
  46. typedef struct {
  47. char *file;
  48. char *sep;
  49. char *plugin_name;
  50. char *instance;
  51. tbl_result_t *results;
  52. size_t results_num;
  53. size_t max_colnum;
  54. } tbl_t;
  55. static void tbl_result_setup(tbl_result_t *res) {
  56. res->type = NULL;
  57. res->instance_prefix = NULL;
  58. res->instances = NULL;
  59. res->instances_num = 0;
  60. res->values = NULL;
  61. res->values_num = 0;
  62. res->ds = NULL;
  63. } /* tbl_result_setup */
  64. static void tbl_result_clear(tbl_result_t *res) {
  65. if (res == NULL) {
  66. return;
  67. }
  68. sfree(res->type);
  69. sfree(res->instance_prefix);
  70. sfree(res->instances);
  71. res->instances_num = 0;
  72. sfree(res->values);
  73. res->values_num = 0;
  74. res->ds = NULL;
  75. } /* tbl_result_clear */
  76. static void tbl_setup(tbl_t *tbl, char *file) {
  77. tbl->file = sstrdup(file);
  78. tbl->sep = NULL;
  79. tbl->plugin_name = NULL;
  80. tbl->instance = NULL;
  81. tbl->results = NULL;
  82. tbl->results_num = 0;
  83. tbl->max_colnum = 0;
  84. } /* tbl_setup */
  85. static void tbl_clear(tbl_t *tbl) {
  86. if (tbl == NULL) {
  87. return;
  88. }
  89. sfree(tbl->file);
  90. sfree(tbl->sep);
  91. sfree(tbl->plugin_name);
  92. sfree(tbl->instance);
  93. /* (tbl->results == NULL) -> (tbl->results_num == 0) */
  94. assert((tbl->results != NULL) || (tbl->results_num == 0));
  95. for (size_t i = 0; i < tbl->results_num; ++i)
  96. tbl_result_clear(tbl->results + i);
  97. sfree(tbl->results);
  98. tbl->results_num = 0;
  99. tbl->max_colnum = 0;
  100. } /* tbl_clear */
  101. static tbl_t *tables;
  102. static size_t tables_num;
  103. /*
  104. * configuration handling
  105. */
  106. static int tbl_config_append_array_i(char *name, size_t **var, size_t *len,
  107. oconfig_item_t *ci) {
  108. if (ci->values_num < 1) {
  109. log_err("\"%s\" expects at least one argument.", name);
  110. return 1;
  111. }
  112. size_t num = ci->values_num;
  113. for (size_t i = 0; i < num; ++i) {
  114. if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
  115. log_err("\"%s\" expects numerical arguments only.", name);
  116. return 1;
  117. }
  118. }
  119. size_t *tmp = realloc(*var, ((*len) + num) * sizeof(**var));
  120. if (tmp == NULL) {
  121. log_err("realloc failed: %s.", STRERRNO);
  122. return -1;
  123. }
  124. *var = tmp;
  125. for (size_t i = 0; i < num; ++i) {
  126. (*var)[*len] = (size_t)ci->values[i].value.number;
  127. (*len)++;
  128. }
  129. return 0;
  130. } /* tbl_config_append_array_s */
  131. static int tbl_config_result(tbl_t *tbl, oconfig_item_t *ci) {
  132. if (ci->values_num != 0) {
  133. log_err("<Result> does not expect any arguments.");
  134. return 1;
  135. }
  136. tbl_result_t *res =
  137. realloc(tbl->results, (tbl->results_num + 1) * sizeof(*tbl->results));
  138. if (res == NULL) {
  139. log_err("realloc failed: %s.", STRERRNO);
  140. return -1;
  141. }
  142. tbl->results = res;
  143. res = tbl->results + tbl->results_num;
  144. tbl_result_setup(res);
  145. for (int i = 0; i < ci->children_num; ++i) {
  146. oconfig_item_t *c = ci->children + i;
  147. if (strcasecmp(c->key, "Type") == 0)
  148. cf_util_get_string(c, &res->type);
  149. else if (strcasecmp(c->key, "InstancePrefix") == 0)
  150. cf_util_get_string(c, &res->instance_prefix);
  151. else if (strcasecmp(c->key, "InstancesFrom") == 0)
  152. tbl_config_append_array_i(c->key, &res->instances, &res->instances_num,
  153. c);
  154. else if (strcasecmp(c->key, "ValuesFrom") == 0)
  155. tbl_config_append_array_i(c->key, &res->values, &res->values_num, c);
  156. else
  157. log_warn("Ignoring unknown config key \"%s\" "
  158. " in <Result>.",
  159. c->key);
  160. }
  161. int status = 0;
  162. if (res->type == NULL) {
  163. log_err("No \"Type\" option specified for <Result> in table \"%s\".",
  164. tbl->file);
  165. status = 1;
  166. }
  167. if (res->values == NULL) {
  168. log_err("No \"ValuesFrom\" option specified for <Result> in table \"%s\".",
  169. tbl->file);
  170. status = 1;
  171. }
  172. if (status != 0) {
  173. tbl_result_clear(res);
  174. return status;
  175. }
  176. tbl->results_num++;
  177. return 0;
  178. } /* tbl_config_result */
  179. static int tbl_config_table(oconfig_item_t *ci) {
  180. if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
  181. log_err("<Table> expects a single string argument.");
  182. return 1;
  183. }
  184. tbl_t *tbl = realloc(tables, (tables_num + 1) * sizeof(*tables));
  185. if (tbl == NULL) {
  186. log_err("realloc failed: %s.", STRERRNO);
  187. return -1;
  188. }
  189. tables = tbl;
  190. tbl = tables + tables_num;
  191. tbl_setup(tbl, ci->values[0].value.string);
  192. for (int i = 0; i < ci->children_num; i++) {
  193. oconfig_item_t *c = ci->children + i;
  194. if (strcasecmp(c->key, "Separator") == 0)
  195. cf_util_get_string(c, &tbl->sep);
  196. else if (strcasecmp(c->key, "Plugin") == 0)
  197. cf_util_get_string(c, &tbl->plugin_name);
  198. else if (strcasecmp(c->key, "Instance") == 0)
  199. cf_util_get_string(c, &tbl->instance);
  200. else if (strcasecmp(c->key, "Result") == 0)
  201. tbl_config_result(tbl, c);
  202. else
  203. log_warn("Ignoring unknown config key \"%s\" "
  204. "in <Table %s>.",
  205. c->key, tbl->file);
  206. }
  207. int status = 0;
  208. if (tbl->sep == NULL) {
  209. log_err("Table \"%s\" does not specify any separator.", tbl->file);
  210. status = 1;
  211. } else {
  212. strunescape(tbl->sep, strlen(tbl->sep) + 1);
  213. }
  214. if (tbl->instance == NULL) {
  215. tbl->instance = sstrdup(tbl->file);
  216. replace_special(tbl->instance, strlen(tbl->instance));
  217. }
  218. if (tbl->results == NULL) {
  219. assert(tbl->results_num == 0);
  220. log_err("Table \"%s\" does not specify any (valid) results.", tbl->file);
  221. status = 1;
  222. }
  223. if (status != 0) {
  224. tbl_clear(tbl);
  225. return status;
  226. }
  227. for (size_t i = 0; i < tbl->results_num; ++i) {
  228. tbl_result_t *res = tbl->results + i;
  229. for (size_t j = 0; j < res->instances_num; ++j)
  230. if (res->instances[j] > tbl->max_colnum)
  231. tbl->max_colnum = res->instances[j];
  232. for (size_t j = 0; j < res->values_num; ++j)
  233. if (res->values[j] > tbl->max_colnum)
  234. tbl->max_colnum = res->values[j];
  235. }
  236. tables_num++;
  237. return 0;
  238. } /* tbl_config_table */
  239. static int tbl_config(oconfig_item_t *ci) {
  240. for (int i = 0; i < ci->children_num; ++i) {
  241. oconfig_item_t *c = ci->children + i;
  242. if (strcasecmp(c->key, "Table") == 0)
  243. tbl_config_table(c);
  244. else
  245. log_warn("Ignoring unknown config key \"%s\".", c->key);
  246. }
  247. return 0;
  248. } /* tbl_config */
  249. /*
  250. * result handling
  251. */
  252. static int tbl_prepare(tbl_t *tbl) {
  253. for (size_t i = 0; i < tbl->results_num; ++i) {
  254. tbl_result_t *res = tbl->results + i;
  255. res->ds = plugin_get_ds(res->type);
  256. if (res->ds == NULL) {
  257. log_err("Unknown type \"%s\". See types.db(5) for details.", res->type);
  258. return -1;
  259. }
  260. if (res->values_num != res->ds->ds_num) {
  261. log_err("Invalid type \"%s\". Expected %" PRIsz " data source%s, "
  262. "got %" PRIsz ".",
  263. res->type, res->values_num, (1 == res->values_num) ? "" : "s",
  264. res->ds->ds_num);
  265. return -1;
  266. }
  267. }
  268. return 0;
  269. } /* tbl_prepare */
  270. static int tbl_finish(tbl_t *tbl) {
  271. for (size_t i = 0; i < tbl->results_num; ++i)
  272. tbl->results[i].ds = NULL;
  273. return 0;
  274. } /* tbl_finish */
  275. static int tbl_result_dispatch(tbl_t *tbl, tbl_result_t *res, char **fields,
  276. size_t fields_num) {
  277. value_list_t vl = VALUE_LIST_INIT;
  278. value_t values[res->values_num];
  279. assert(res->ds);
  280. assert(res->values_num == res->ds->ds_num);
  281. for (size_t i = 0; i < res->values_num; ++i) {
  282. assert(res->values[i] < fields_num);
  283. char *value = fields[res->values[i]];
  284. if (parse_value(value, &values[i], res->ds->ds[i].type) != 0)
  285. return -1;
  286. }
  287. vl.values = values;
  288. vl.values_len = STATIC_ARRAY_SIZE(values);
  289. sstrncpy(vl.plugin, (tbl->plugin_name != NULL) ? tbl->plugin_name : "table",
  290. sizeof(vl.plugin));
  291. sstrncpy(vl.plugin_instance, tbl->instance, sizeof(vl.plugin_instance));
  292. sstrncpy(vl.type, res->type, sizeof(vl.type));
  293. if (res->instances_num == 0) {
  294. if (res->instance_prefix)
  295. sstrncpy(vl.type_instance, res->instance_prefix,
  296. sizeof(vl.type_instance));
  297. } else {
  298. char *instances[res->instances_num];
  299. char instances_str[DATA_MAX_NAME_LEN];
  300. for (size_t i = 0; i < res->instances_num; ++i) {
  301. assert(res->instances[i] < fields_num);
  302. instances[i] = fields[res->instances[i]];
  303. }
  304. strjoin(instances_str, sizeof(instances_str), instances,
  305. STATIC_ARRAY_SIZE(instances), "-");
  306. instances_str[sizeof(instances_str) - 1] = '\0';
  307. int r;
  308. if (res->instance_prefix == NULL)
  309. r = snprintf(vl.type_instance, sizeof(vl.type_instance), "%s",
  310. instances_str);
  311. else
  312. r = snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s",
  313. res->instance_prefix, instances_str);
  314. if ((size_t)r >= sizeof(vl.type_instance))
  315. log_warn("Truncated type instance: %s.", vl.type_instance);
  316. }
  317. plugin_dispatch_values(&vl);
  318. return 0;
  319. } /* tbl_result_dispatch */
  320. static int tbl_parse_line(tbl_t *tbl, char *line, size_t len) {
  321. char *fields[tbl->max_colnum + 1];
  322. size_t i = 0;
  323. char *ptr = line;
  324. char *saveptr = NULL;
  325. while ((fields[i] = strtok_r(ptr, tbl->sep, &saveptr)) != NULL) {
  326. ptr = NULL;
  327. i++;
  328. if (i > tbl->max_colnum)
  329. break;
  330. }
  331. if (i <= tbl->max_colnum) {
  332. log_warn("Not enough columns in line "
  333. "(expected at least %" PRIsz ", got %" PRIsz ").",
  334. tbl->max_colnum + 1, i);
  335. return -1;
  336. }
  337. for (i = 0; i < tbl->results_num; ++i)
  338. if (tbl_result_dispatch(tbl, tbl->results + i, fields,
  339. STATIC_ARRAY_SIZE(fields)) != 0) {
  340. log_err("Failed to dispatch result.");
  341. continue;
  342. }
  343. return 0;
  344. } /* tbl_parse_line */
  345. static int tbl_read_table(tbl_t *tbl) {
  346. char buf[4096];
  347. FILE *fh = fopen(tbl->file, "r");
  348. if (fh == NULL) {
  349. log_err("Failed to open file \"%s\": %s.", tbl->file, STRERRNO);
  350. return -1;
  351. }
  352. buf[sizeof(buf) - 1] = '\0';
  353. while (fgets(buf, sizeof(buf), fh) != NULL) {
  354. if (buf[sizeof(buf) - 1] != '\0') {
  355. buf[sizeof(buf) - 1] = '\0';
  356. log_warn("Table %s: Truncated line: %s", tbl->file, buf);
  357. }
  358. if (tbl_parse_line(tbl, buf, sizeof(buf)) != 0) {
  359. log_warn("Table %s: Failed to parse line: %s", tbl->file, buf);
  360. continue;
  361. }
  362. }
  363. if (ferror(fh) != 0) {
  364. log_err("Failed to read from file \"%s\": %s.", tbl->file, STRERRNO);
  365. fclose(fh);
  366. return -1;
  367. }
  368. fclose(fh);
  369. return 0;
  370. } /* tbl_read_table */
  371. /*
  372. * collectd callbacks
  373. */
  374. static int tbl_read(void) {
  375. int status = -1;
  376. if (tables_num == 0)
  377. return 0;
  378. for (size_t i = 0; i < tables_num; ++i) {
  379. tbl_t *tbl = tables + i;
  380. if (tbl_prepare(tbl) != 0) {
  381. log_err("Failed to prepare and parse table \"%s\".", tbl->file);
  382. continue;
  383. }
  384. if (tbl_read_table(tbl) == 0)
  385. status = 0;
  386. tbl_finish(tbl);
  387. }
  388. return status;
  389. } /* tbl_read */
  390. static int tbl_shutdown(void) {
  391. for (size_t i = 0; i < tables_num; ++i)
  392. tbl_clear(&tables[i]);
  393. sfree(tables);
  394. return 0;
  395. } /* tbl_shutdown */
  396. static int tbl_init(void) {
  397. if (tables_num == 0)
  398. return 0;
  399. plugin_register_read("table", tbl_read);
  400. plugin_register_shutdown("table", tbl_shutdown);
  401. return 0;
  402. } /* tbl_init */
  403. void module_register(void) {
  404. plugin_register_complex_config("table", tbl_config);
  405. plugin_register_init("table", tbl_init);
  406. } /* module_register */