You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stat_convert.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamadm.h"
  18. #include "lua/lua_common.h"
  19. #include "contrib/uthash/utlist.h"
  20. /* Common */
  21. static char *config_file = NULL;
  22. static char *symbol_ham = NULL;
  23. static char *symbol_spam = NULL;
  24. static double expire = 0.0;
  25. /* Inputs */
  26. static char *spam_db = NULL;
  27. static char *ham_db = NULL;
  28. static char *cache_db = NULL;
  29. /* Outputs */
  30. static char *redis_host = NULL;
  31. static char *redis_db = NULL;
  32. static char *redis_username = NULL;
  33. static char *redis_password = NULL;
  34. static gboolean reset_previous = FALSE;
  35. static void rspamadm_statconvert(int argc, char **argv,
  36. const struct rspamadm_command *cmd);
  37. static const char *rspamadm_statconvert_help(gboolean full_help,
  38. const struct rspamadm_command *cmd);
  39. struct rspamadm_command statconvert_command = {
  40. .name = "statconvert",
  41. .flags = 0,
  42. .help = rspamadm_statconvert_help,
  43. .run = rspamadm_statconvert,
  44. .lua_subrs = NULL,
  45. };
  46. static GOptionEntry entries[] = {
  47. {"config", 'c', 0, G_OPTION_ARG_FILENAME, &config_file,
  48. "Config file to read data from", NULL},
  49. {"reset", 'r', 0, G_OPTION_ARG_NONE, &reset_previous,
  50. "Reset previous data instead of appending values", NULL},
  51. {"expire", 'e', 0, G_OPTION_ARG_DOUBLE, &expire,
  52. "Set expiration in seconds (can be fractional)", NULL},
  53. {"symbol-spam", 0, 0, G_OPTION_ARG_STRING, &symbol_spam,
  54. "Symbol for spam (e.g. BAYES_SPAM)", NULL},
  55. {"symbol-ham", 0, 0, G_OPTION_ARG_STRING, &symbol_ham,
  56. "Symbol for ham (e.g. BAYES_HAM)", NULL},
  57. {"spam-db", 0, 0, G_OPTION_ARG_STRING, &spam_db,
  58. "Input spam file (sqlite3)", NULL},
  59. {"ham-db", 0, 0, G_OPTION_ARG_STRING, &ham_db,
  60. "Input ham file (sqlite3)", NULL},
  61. {"cache", 0, 0, G_OPTION_ARG_FILENAME, &cache_db,
  62. "Input learn cache", NULL},
  63. {"redis-host", 'h', 0, G_OPTION_ARG_STRING, &redis_host,
  64. "Output redis ip (in format ip:port)", NULL},
  65. {"redis-username", 'u', 0, G_OPTION_ARG_STRING, &redis_username,
  66. "Username to connect to redis", NULL},
  67. {"redis-password", 'p', 0, G_OPTION_ARG_STRING, &redis_password,
  68. "Password to connect to redis", NULL},
  69. {"redis-db", 'd', 0, G_OPTION_ARG_STRING, &redis_db,
  70. "Redis database (should be numeric)", NULL},
  71. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
  72. static const char *
  73. rspamadm_statconvert_help(gboolean full_help, const struct rspamadm_command *cmd)
  74. {
  75. const char *help_str;
  76. if (full_help) {
  77. help_str = "Convert statistics from sqlite3 to redis\n\n"
  78. "Usage: rspamadm statconvert -c /etc/rspamd.conf [-r]\n"
  79. "Where options are:\n\n"
  80. "-c: config file to read data from\n"
  81. "-r: reset previous data instead of increasing values\n"
  82. "-e: set expire to that amount of seconds\n"
  83. "** Or specify options directly **\n"
  84. "--redis-host: output redis ip (in format ip:port)\n"
  85. "--redis-db: output redis database\n"
  86. "--redis-username: redis username\n"
  87. "--redis-password: redis password\n"
  88. "--cache: sqlite3 file for learn cache\n"
  89. "--spam-db: sqlite3 input file for spam data\n"
  90. "--ham-db: sqlite3 input file for ham data\n"
  91. "--symbol-spam: symbol in redis for spam (e.g. BAYES_SPAM)\n"
  92. "--symbol-ham: symbol in redis for ham (e.g. BAYES_HAM)\n";
  93. }
  94. else {
  95. help_str = "Convert statistics from sqlite3 to redis";
  96. }
  97. return help_str;
  98. }
  99. static void
  100. rspamadm_statconvert(int argc, char **argv, const struct rspamadm_command *cmd)
  101. {
  102. GOptionContext *context;
  103. GError *error = NULL;
  104. ucl_object_t *obj;
  105. context = g_option_context_new(
  106. "statconvert - converts statistics from sqlite3 to redis");
  107. g_option_context_set_summary(context,
  108. "Summary:\n Rspamd administration utility version " RVERSION
  109. "\n Release id: " RID);
  110. g_option_context_add_main_entries(context, entries, NULL);
  111. g_option_context_set_ignore_unknown_options(context, TRUE);
  112. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  113. rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message);
  114. g_error_free(error);
  115. g_option_context_free(context);
  116. exit(EXIT_FAILURE);
  117. }
  118. g_option_context_free(context);
  119. if (config_file) {
  120. /* Load config file, assuming that it has all information required */
  121. struct ucl_parser *parser;
  122. parser = ucl_parser_new(0);
  123. rspamd_ucl_add_conf_variables(parser, ucl_vars);
  124. if (!ucl_parser_add_file(parser, config_file)) {
  125. msg_err("ucl parser error: %s", ucl_parser_get_error(parser));
  126. ucl_parser_free(parser);
  127. exit(EXIT_FAILURE);
  128. }
  129. obj = ucl_parser_get_object(parser);
  130. ucl_parser_free(parser);
  131. }
  132. else {
  133. /* We need to get all information from the command line */
  134. ucl_object_t *classifier, *statfile_ham, *statfile_spam, *tmp, *redis;
  135. /* Check arguments sanity */
  136. if (spam_db == NULL) {
  137. msg_err("No spam-db specified");
  138. exit(EXIT_FAILURE);
  139. }
  140. if (ham_db == NULL) {
  141. msg_err("No ham-db specified");
  142. exit(EXIT_FAILURE);
  143. }
  144. if (redis_host == NULL) {
  145. msg_err("No redis-host specified");
  146. exit(EXIT_FAILURE);
  147. }
  148. if (symbol_ham == NULL) {
  149. msg_err("No symbol-ham specified");
  150. exit(EXIT_FAILURE);
  151. }
  152. if (symbol_spam == NULL) {
  153. msg_err("No symbol-spam specified");
  154. exit(EXIT_FAILURE);
  155. }
  156. obj = ucl_object_typed_new(UCL_OBJECT);
  157. classifier = ucl_object_typed_new(UCL_OBJECT);
  158. ucl_object_insert_key(obj, classifier, "classifier", 0, false);
  159. /* Now we need to create "bayes" key in it */
  160. tmp = ucl_object_typed_new(UCL_OBJECT);
  161. ucl_object_insert_key(classifier, tmp, "bayes", 0, false);
  162. classifier = tmp;
  163. ucl_object_insert_key(classifier, ucl_object_fromstring("sqlite3"),
  164. "backend", 0, false);
  165. if (cache_db != NULL) {
  166. ucl_object_t *cache;
  167. cache = ucl_object_typed_new(UCL_OBJECT);
  168. ucl_object_insert_key(cache, ucl_object_fromstring("sqlite3"),
  169. "type", 0, false);
  170. ucl_object_insert_key(cache, ucl_object_fromstring(cache_db),
  171. "file", 0, false);
  172. ucl_object_insert_key(classifier, cache, "cache", 0, false);
  173. }
  174. statfile_ham = ucl_object_typed_new(UCL_OBJECT);
  175. ucl_object_insert_key(statfile_ham, ucl_object_fromstring(symbol_ham),
  176. "symbol", 0, false);
  177. ucl_object_insert_key(statfile_ham, ucl_object_frombool(false),
  178. "spam", 0, false);
  179. ucl_object_insert_key(statfile_ham, ucl_object_fromstring(ham_db),
  180. "db", 0, false);
  181. statfile_spam = ucl_object_typed_new(UCL_OBJECT);
  182. ucl_object_insert_key(statfile_spam, ucl_object_fromstring(symbol_spam),
  183. "symbol", 0, false);
  184. ucl_object_insert_key(statfile_spam, ucl_object_frombool(true),
  185. "spam", 0, false);
  186. ucl_object_insert_key(statfile_spam, ucl_object_fromstring(spam_db),
  187. "db", 0, false);
  188. DL_APPEND(statfile_ham, statfile_spam);
  189. ucl_object_insert_key(classifier, statfile_ham,
  190. "statfile", 0, false);
  191. /* Deal with redis */
  192. redis = ucl_object_typed_new(UCL_OBJECT);
  193. ucl_object_insert_key(obj, redis, "redis", 0, false);
  194. ucl_object_insert_key(redis, ucl_object_fromstring(redis_host),
  195. "servers", 0, false);
  196. if (redis_db) {
  197. ucl_object_insert_key(redis, ucl_object_fromstring(redis_db),
  198. "dbname", 0, false);
  199. }
  200. if (redis_username) {
  201. ucl_object_insert_key(redis, ucl_object_fromstring(redis_username),
  202. "username", 0, false);
  203. }
  204. if (redis_password) {
  205. ucl_object_insert_key(redis, ucl_object_fromstring(redis_password),
  206. "password", 0, false);
  207. }
  208. }
  209. ucl_object_insert_key(obj, ucl_object_frombool(reset_previous),
  210. "reset_previous", 0, false);
  211. if (expire != 0) {
  212. ucl_object_insert_key(obj, ucl_object_fromdouble(expire),
  213. "expire", 0, false);
  214. }
  215. rspamadm_execute_lua_ucl_subr(argc,
  216. argv,
  217. obj,
  218. "stat_convert",
  219. TRUE);
  220. ucl_object_unref(obj);
  221. }