4
0

mon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2010-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <unistd.h>
  36. #include <statgrab.h>
  37. #include <corosync/corotypes.h>
  38. #include <corosync/corodefs.h>
  39. #include <corosync/coroapi.h>
  40. #include <qb/qblist.h>
  41. #include <corosync/logsys.h>
  42. #include <corosync/icmap.h>
  43. #include "fsm.h"
  44. #include "service.h"
  45. LOGSYS_DECLARE_SUBSYS ("MON");
  46. /*
  47. * Service Interfaces required by service_message_handler struct
  48. */
  49. static char *mon_exec_init_fn (struct corosync_api_v1 *corosync_api);
  50. static struct corosync_api_v1 *api;
  51. #define MON_DEFAULT_PERIOD 3000
  52. #define MON_MIN_PERIOD 500
  53. #define MON_MAX_PERIOD (120 * CS_TIME_MS_IN_SEC)
  54. struct corosync_service_engine mon_service_engine = {
  55. .name = "corosync resource monitoring service",
  56. .id = MON_SERVICE,
  57. .priority = 1,
  58. .private_data_size = 0,
  59. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  60. .lib_init_fn = NULL,
  61. .lib_exit_fn = NULL,
  62. .lib_engine = NULL,
  63. .lib_engine_count = 0,
  64. .exec_engine = NULL,
  65. .exec_engine_count = 0,
  66. .confchg_fn = NULL,
  67. .exec_init_fn = mon_exec_init_fn,
  68. .exec_dump_fn = NULL
  69. };
  70. static QB_LIST_DECLARE (confchg_notify);
  71. struct resource_instance {
  72. const char *icmap_path;
  73. const char *name;
  74. corosync_timer_handle_t timer_handle;
  75. void (*update_stats_fn) (void *data);
  76. struct cs_fsm fsm;
  77. uint64_t period;
  78. icmap_value_types_t max_type;
  79. union {
  80. int32_t int32;
  81. double dbl;
  82. } max;
  83. };
  84. static void mem_update_stats_fn (void *data);
  85. static void load_update_stats_fn (void *data);
  86. static struct resource_instance memory_used_inst = {
  87. .name = "memory_used",
  88. .icmap_path = "resources.system.memory_used.",
  89. .update_stats_fn = mem_update_stats_fn,
  90. .max_type = ICMAP_VALUETYPE_INT32,
  91. .max.int32 = INT32_MAX,
  92. .period = MON_DEFAULT_PERIOD,
  93. };
  94. static struct resource_instance load_15min_inst = {
  95. .name = "load_15min",
  96. .icmap_path = "resources.system.load_15min.",
  97. .update_stats_fn = load_update_stats_fn,
  98. .max_type = ICMAP_VALUETYPE_DOUBLE,
  99. .max.dbl = INT32_MAX,
  100. .period = MON_DEFAULT_PERIOD,
  101. };
  102. /*
  103. * F S M
  104. */
  105. static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data);
  106. static void mon_resource_failed (struct cs_fsm* fsm, int32_t event, void * data);
  107. const char * mon_running_str = "running";
  108. const char * mon_failed_str = "failed";
  109. const char * mon_failure_str = "failure";
  110. const char * mon_stopped_str = "stopped";
  111. const char * mon_config_changed_str = "config_changed";
  112. enum mon_resource_state {
  113. MON_S_STOPPED,
  114. MON_S_RUNNING,
  115. MON_S_FAILED
  116. };
  117. enum mon_resource_event {
  118. MON_E_CONFIG_CHANGED,
  119. MON_E_FAILURE
  120. };
  121. struct cs_fsm_entry mon_fsm_table[] = {
  122. { MON_S_STOPPED, MON_E_CONFIG_CHANGED, mon_config_changed, {MON_S_STOPPED, MON_S_RUNNING, -1} },
  123. { MON_S_STOPPED, MON_E_FAILURE, NULL, {-1} },
  124. { MON_S_RUNNING, MON_E_CONFIG_CHANGED, mon_config_changed, {MON_S_RUNNING, MON_S_STOPPED, -1} },
  125. { MON_S_RUNNING, MON_E_FAILURE, mon_resource_failed, {MON_S_FAILED, -1} },
  126. { MON_S_FAILED, MON_E_CONFIG_CHANGED, mon_config_changed, {MON_S_RUNNING, MON_S_STOPPED, -1} },
  127. { MON_S_FAILED, MON_E_FAILURE, NULL, {-1} },
  128. };
  129. struct corosync_service_engine *mon_get_service_engine_ver0 (void)
  130. {
  131. return (&mon_service_engine);
  132. }
  133. static const char * mon_res_state_to_str(struct cs_fsm* fsm,
  134. int32_t state)
  135. {
  136. switch (state) {
  137. case MON_S_STOPPED:
  138. return mon_stopped_str;
  139. break;
  140. case MON_S_RUNNING:
  141. return mon_running_str;
  142. break;
  143. case MON_S_FAILED:
  144. return mon_failed_str;
  145. break;
  146. }
  147. return NULL;
  148. }
  149. static const char * mon_res_event_to_str(struct cs_fsm* fsm,
  150. int32_t event)
  151. {
  152. switch (event) {
  153. case MON_E_CONFIG_CHANGED:
  154. return mon_config_changed_str;
  155. break;
  156. case MON_E_FAILURE:
  157. return mon_failure_str;
  158. break;
  159. }
  160. return NULL;
  161. }
  162. static void mon_fsm_cb (struct cs_fsm *fsm, int cb_event, int32_t curr_state,
  163. int32_t next_state, int32_t fsm_event, void *data)
  164. {
  165. switch (cb_event) {
  166. case CS_FSM_CB_EVENT_PROCESS_NF:
  167. log_printf (LOGSYS_LEVEL_ERROR, "Fsm:%s could not find event \"%s\" in state \"%s\"",
  168. fsm->name, fsm->event_to_str(fsm, fsm_event), fsm->state_to_str(fsm, curr_state));
  169. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  170. break;
  171. case CS_FSM_CB_EVENT_STATE_SET:
  172. log_printf (LOGSYS_LEVEL_INFO, "Fsm:%s event \"%s\", state \"%s\" --> \"%s\"",
  173. fsm->name,
  174. fsm->event_to_str(fsm, fsm_event),
  175. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  176. fsm->state_to_str(fsm, next_state));
  177. break;
  178. case CS_FSM_CB_EVENT_STATE_SET_NF:
  179. log_printf (LOGSYS_LEVEL_CRIT, "Fsm:%s Can't change state from \"%s\" to \"%s\" (event was \"%s\")",
  180. fsm->name,
  181. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  182. fsm->state_to_str(fsm, next_state),
  183. fsm->event_to_str(fsm, fsm_event));
  184. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  185. break;
  186. default:
  187. log_printf (LOGSYS_LEVEL_CRIT, "Fsm: Can't find callback event!");
  188. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  189. break;
  190. }
  191. }
  192. static void mon_fsm_state_set (struct cs_fsm* fsm,
  193. enum mon_resource_state next_state, struct resource_instance* inst)
  194. {
  195. enum mon_resource_state prev_state = fsm->curr_state;
  196. const char *state_str;
  197. char key_name[ICMAP_KEYNAME_MAXLEN];
  198. ENTER();
  199. cs_fsm_state_set(fsm, next_state, inst, mon_fsm_cb);
  200. if (prev_state == fsm->curr_state) {
  201. return;
  202. }
  203. state_str = mon_res_state_to_str(fsm, fsm->curr_state);
  204. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
  205. icmap_set_string(key_name, state_str);
  206. }
  207. static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
  208. {
  209. struct resource_instance * inst = (struct resource_instance *)data;
  210. char *tmp_str;
  211. uint64_t tmp_value;
  212. char key_name[ICMAP_KEYNAME_MAXLEN];
  213. int run_updater;
  214. int scanf_res = 0;
  215. int32_t i32;
  216. double dbl;
  217. ENTER();
  218. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
  219. if (icmap_get_string(key_name, &tmp_str) == CS_OK) {
  220. scanf_res = sscanf(tmp_str, "%"PRIu64, &tmp_value);
  221. if (scanf_res != 1) {
  222. log_printf (LOGSYS_LEVEL_WARNING,
  223. "Could NOT use poll_period: %s (not uint64 type) for resource %s",
  224. tmp_str, inst->name);
  225. }
  226. free(tmp_str);
  227. if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
  228. log_printf (LOGSYS_LEVEL_DEBUG,
  229. "poll_period changing from:%"PRIu64" to %"PRIu64".",
  230. inst->period, tmp_value);
  231. inst->period = tmp_value;
  232. } else {
  233. log_printf (LOGSYS_LEVEL_WARNING,
  234. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  235. tmp_value, inst->name);
  236. }
  237. }
  238. if (inst->timer_handle) {
  239. api->timer_delete(inst->timer_handle);
  240. inst->timer_handle = 0;
  241. }
  242. run_updater = 0;
  243. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "max");
  244. if (icmap_get_string(key_name, &tmp_str) == CS_OK) {
  245. if (inst->max_type == ICMAP_VALUETYPE_INT32) {
  246. if (sscanf(tmp_str, "%"PRId32, &i32) != 1) {
  247. inst->max.int32 = INT32_MAX;
  248. mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
  249. } else {
  250. inst->max.int32 = i32;
  251. run_updater = 1;
  252. }
  253. }
  254. if (inst->max_type == ICMAP_VALUETYPE_DOUBLE) {
  255. if (sscanf(tmp_str, "%lf", &dbl) != 1) {
  256. inst->max.dbl = INT32_MAX;
  257. mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
  258. } else {
  259. inst->max.dbl = dbl;
  260. run_updater = 1;
  261. }
  262. }
  263. free(tmp_str);
  264. }
  265. if (run_updater) {
  266. mon_fsm_state_set (fsm, MON_S_RUNNING, inst);
  267. /*
  268. * run the updater, incase the period has shortened
  269. * and to start the timer.
  270. */
  271. inst->update_stats_fn (inst);
  272. }
  273. }
  274. void mon_resource_failed (struct cs_fsm* fsm, int32_t event, void * data)
  275. {
  276. struct resource_instance * inst = (struct resource_instance *)data;
  277. ENTER();
  278. mon_fsm_state_set (fsm, MON_S_FAILED, inst);
  279. }
  280. static int32_t percent_mem_used_get(void)
  281. {
  282. sg_mem_stats *mem_stats;
  283. sg_swap_stats *swap_stats;
  284. long long total, freemem;
  285. #ifdef HAVE_LIBSTATGRAB_GE_090
  286. mem_stats = sg_get_mem_stats(NULL);
  287. swap_stats = sg_get_swap_stats(NULL);
  288. #else
  289. mem_stats = sg_get_mem_stats();
  290. swap_stats = sg_get_swap_stats();
  291. #endif
  292. if (mem_stats == NULL || swap_stats == NULL) {
  293. log_printf (LOGSYS_LEVEL_ERROR, "Unable to get memory stats: %s",
  294. sg_str_error(sg_get_error()));
  295. return -1;
  296. }
  297. total = mem_stats->total + swap_stats->total;
  298. freemem = mem_stats->free + swap_stats->free;
  299. return ((total - freemem) * 100) / total;
  300. }
  301. static void mem_update_stats_fn (void *data)
  302. {
  303. struct resource_instance * inst = (struct resource_instance *)data;
  304. int32_t new_value;
  305. uint64_t timestamp;
  306. char key_name[ICMAP_KEYNAME_MAXLEN];
  307. new_value = percent_mem_used_get();
  308. if (new_value > 0) {
  309. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  310. icmap_set_uint32(key_name, new_value);
  311. timestamp = cs_timestamp_get();
  312. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  313. icmap_set_uint64(key_name, timestamp);
  314. if (new_value > inst->max.int32 && inst->fsm.curr_state != MON_S_FAILED) {
  315. cs_fsm_process (&inst->fsm, MON_E_FAILURE, inst, mon_fsm_cb);
  316. }
  317. }
  318. api->timer_add_duration(inst->period * MILLI_2_NANO_SECONDS,
  319. inst, inst->update_stats_fn, &inst->timer_handle);
  320. }
  321. static double min15_loadavg_get(void)
  322. {
  323. sg_load_stats *load_stats;
  324. #ifdef HAVE_LIBSTATGRAB_GE_090
  325. load_stats = sg_get_load_stats (NULL);
  326. #else
  327. load_stats = sg_get_load_stats ();
  328. #endif
  329. if (load_stats == NULL) {
  330. log_printf (LOGSYS_LEVEL_ERROR, "Unable to get load stats: %s",
  331. sg_str_error (sg_get_error()));
  332. return -1;
  333. }
  334. return load_stats->min15;
  335. }
  336. static void load_update_stats_fn (void *data)
  337. {
  338. struct resource_instance * inst = (struct resource_instance *)data;
  339. uint64_t timestamp;
  340. char key_name[ICMAP_KEYNAME_MAXLEN];
  341. double min15 = min15_loadavg_get();
  342. if (min15 > 0) {
  343. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  344. icmap_set_double(key_name, min15);
  345. timestamp = cs_timestamp_get();
  346. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  347. icmap_set_uint64(key_name, timestamp);
  348. if (min15 > inst->max.dbl && inst->fsm.curr_state != MON_S_FAILED) {
  349. cs_fsm_process (&inst->fsm, MON_E_FAILURE, inst, mon_fsm_cb);
  350. }
  351. }
  352. api->timer_add_duration(inst->period * MILLI_2_NANO_SECONDS,
  353. inst, inst->update_stats_fn, &inst->timer_handle);
  354. }
  355. static void mon_key_changed_cb (
  356. int32_t event,
  357. const char *key_name,
  358. struct icmap_notify_value new_value,
  359. struct icmap_notify_value old_value,
  360. void *user_data)
  361. {
  362. struct resource_instance* inst = (struct resource_instance*)user_data;
  363. char *last_key_part;
  364. if (event == ICMAP_TRACK_DELETE && inst) {
  365. log_printf (LOGSYS_LEVEL_WARNING,
  366. "resource \"%s\" deleted from cmap!",
  367. inst->name);
  368. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst, mon_fsm_cb);
  369. }
  370. if (event == ICMAP_TRACK_MODIFY) {
  371. last_key_part = strrchr(key_name, '.');
  372. if (last_key_part == NULL)
  373. return ;
  374. last_key_part++;
  375. if (strcmp(last_key_part, "max") == 0 ||
  376. strcmp(last_key_part, "poll_period") == 0) {
  377. ENTER();
  378. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst, mon_fsm_cb);
  379. }
  380. }
  381. }
  382. static void mon_instance_init (struct resource_instance* inst)
  383. {
  384. uint64_t tmp_value;
  385. char key_name[ICMAP_KEYNAME_MAXLEN];
  386. icmap_track_t icmap_track = NULL;
  387. char *tmp_str;
  388. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  389. if (inst->max_type == ICMAP_VALUETYPE_INT32) {
  390. icmap_set_int32(key_name, 0);
  391. } else {
  392. icmap_set_double(key_name, 0);
  393. }
  394. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  395. icmap_set_uint64(key_name, 0);
  396. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
  397. icmap_set_string(key_name, mon_stopped_str);
  398. inst->fsm.name = inst->name;
  399. inst->fsm.curr_entry = 0;
  400. inst->fsm.curr_state = MON_S_STOPPED;
  401. inst->fsm.table = mon_fsm_table;
  402. inst->fsm.entries = sizeof(mon_fsm_table) / sizeof(struct cs_fsm_entry);
  403. inst->fsm.state_to_str = mon_res_state_to_str;
  404. inst->fsm.event_to_str = mon_res_event_to_str;
  405. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
  406. if (icmap_get_string(key_name, &tmp_str) != CS_OK ||
  407. sscanf(tmp_str, "%"PRIu64, &tmp_value) != 1) {
  408. icmap_set_uint64(key_name, inst->period);
  409. }
  410. else {
  411. if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
  412. inst->period = tmp_value;
  413. } else {
  414. log_printf (LOGSYS_LEVEL_WARNING,
  415. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  416. tmp_value, inst->name);
  417. }
  418. free(tmp_str);
  419. }
  420. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst, mon_fsm_cb);
  421. icmap_track_add(inst->icmap_path,
  422. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
  423. mon_key_changed_cb, inst, &icmap_track);
  424. }
  425. static char *mon_exec_init_fn (struct corosync_api_v1 *corosync_api)
  426. {
  427. #ifdef HAVE_LIBSTATGRAB_GE_090
  428. sg_init(1);
  429. #else
  430. sg_init();
  431. #endif
  432. api = corosync_api;
  433. mon_instance_init (&memory_used_inst);
  434. mon_instance_init (&load_15min_inst);
  435. return NULL;
  436. }