mon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. #if defined(HAVE_LIBSTATGRAB)
  37. #include <statgrab.h>
  38. #endif
  39. #include <corosync/corotypes.h>
  40. #include <corosync/corodefs.h>
  41. #include <corosync/coroapi.h>
  42. #include <corosync/list.h>
  43. #include <corosync/logsys.h>
  44. #include <corosync/icmap.h>
  45. #include "fsm.h"
  46. #include "service.h"
  47. LOGSYS_DECLARE_SUBSYS ("MON");
  48. /*
  49. * Service Interfaces required by service_message_handler struct
  50. */
  51. static char *mon_exec_init_fn (struct corosync_api_v1 *corosync_api);
  52. static struct corosync_api_v1 *api;
  53. #define MON_DEFAULT_PERIOD 3000
  54. #define MON_MIN_PERIOD 500
  55. #define MON_MAX_PERIOD (120 * CS_TIME_MS_IN_SEC)
  56. struct corosync_service_engine mon_service_engine = {
  57. .name = "corosync resource monitoring service",
  58. .id = MON_SERVICE,
  59. .priority = 1,
  60. .private_data_size = 0,
  61. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  62. .lib_init_fn = NULL,
  63. .lib_exit_fn = NULL,
  64. .lib_engine = NULL,
  65. .lib_engine_count = 0,
  66. .exec_engine = NULL,
  67. .exec_engine_count = 0,
  68. .confchg_fn = NULL,
  69. .exec_init_fn = mon_exec_init_fn,
  70. .exec_dump_fn = NULL
  71. };
  72. static DECLARE_LIST_INIT (confchg_notify);
  73. struct resource_instance {
  74. const char *icmap_path;
  75. const char *name;
  76. corosync_timer_handle_t timer_handle;
  77. void (*update_stats_fn) (void *data);
  78. struct cs_fsm fsm;
  79. uint64_t period;
  80. icmap_value_types_t max_type;
  81. union {
  82. int32_t int32;
  83. double dbl;
  84. } max;
  85. };
  86. static void mem_update_stats_fn (void *data);
  87. static void load_update_stats_fn (void *data);
  88. static struct resource_instance memory_used_inst = {
  89. .name = "memory_used",
  90. .icmap_path = "resources.system.memory_used.",
  91. .update_stats_fn = mem_update_stats_fn,
  92. .max_type = ICMAP_VALUETYPE_INT32,
  93. .max.int32 = INT32_MAX,
  94. .period = MON_DEFAULT_PERIOD,
  95. };
  96. static struct resource_instance load_15min_inst = {
  97. .name = "load_15min",
  98. .icmap_path = "resources.system.load_15min.",
  99. .update_stats_fn = load_update_stats_fn,
  100. .max_type = ICMAP_VALUETYPE_DOUBLE,
  101. .max.dbl = INT32_MAX,
  102. .period = MON_DEFAULT_PERIOD,
  103. };
  104. /*
  105. * F S M
  106. */
  107. static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data);
  108. static void mon_resource_failed (struct cs_fsm* fsm, int32_t event, void * data);
  109. const char * mon_running_str = "running";
  110. const char * mon_failed_str = "failed";
  111. const char * mon_failure_str = "failure";
  112. const char * mon_stopped_str = "stopped";
  113. const char * mon_config_changed_str = "config_changed";
  114. enum mon_resource_state {
  115. MON_S_STOPPED,
  116. MON_S_RUNNING,
  117. MON_S_FAILED
  118. };
  119. enum mon_resource_event {
  120. MON_E_CONFIG_CHANGED,
  121. MON_E_FAILURE
  122. };
  123. struct cs_fsm_entry mon_fsm_table[] = {
  124. { MON_S_STOPPED, MON_E_CONFIG_CHANGED, mon_config_changed, {MON_S_STOPPED, MON_S_RUNNING, -1} },
  125. { MON_S_STOPPED, MON_E_FAILURE, NULL, {-1} },
  126. { MON_S_RUNNING, MON_E_CONFIG_CHANGED, mon_config_changed, {MON_S_RUNNING, MON_S_STOPPED, -1} },
  127. { MON_S_RUNNING, MON_E_FAILURE, mon_resource_failed, {MON_S_FAILED, -1} },
  128. { MON_S_FAILED, MON_E_CONFIG_CHANGED, mon_config_changed, {MON_S_RUNNING, MON_S_STOPPED, -1} },
  129. { MON_S_FAILED, MON_E_FAILURE, NULL, {-1} },
  130. };
  131. struct corosync_service_engine *mon_get_service_engine_ver0 (void)
  132. {
  133. return (&mon_service_engine);
  134. }
  135. static const char * mon_res_state_to_str(struct cs_fsm* fsm,
  136. int32_t state)
  137. {
  138. switch (state) {
  139. case MON_S_STOPPED:
  140. return mon_stopped_str;
  141. break;
  142. case MON_S_RUNNING:
  143. return mon_running_str;
  144. break;
  145. case MON_S_FAILED:
  146. return mon_failed_str;
  147. break;
  148. }
  149. return NULL;
  150. }
  151. static const char * mon_res_event_to_str(struct cs_fsm* fsm,
  152. int32_t event)
  153. {
  154. switch (event) {
  155. case MON_E_CONFIG_CHANGED:
  156. return mon_config_changed_str;
  157. break;
  158. case MON_E_FAILURE:
  159. return mon_failure_str;
  160. break;
  161. }
  162. return NULL;
  163. }
  164. static void mon_fsm_cb (struct cs_fsm *fsm, int cb_event, int32_t curr_state,
  165. int32_t next_state, int32_t fsm_event, void *data)
  166. {
  167. switch (cb_event) {
  168. case CS_FSM_CB_EVENT_PROCESS_NF:
  169. log_printf (LOGSYS_LEVEL_ERROR, "Fsm:%s could not find event \"%s\" in state \"%s\"",
  170. fsm->name, fsm->event_to_str(fsm, fsm_event), fsm->state_to_str(fsm, curr_state));
  171. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  172. break;
  173. case CS_FSM_CB_EVENT_STATE_SET:
  174. log_printf (LOGSYS_LEVEL_INFO, "Fsm:%s event \"%s\", state \"%s\" --> \"%s\"",
  175. fsm->name,
  176. fsm->event_to_str(fsm, fsm_event),
  177. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  178. fsm->state_to_str(fsm, next_state));
  179. break;
  180. case CS_FSM_CB_EVENT_STATE_SET_NF:
  181. log_printf (LOGSYS_LEVEL_CRIT, "Fsm:%s Can't change state from \"%s\" to \"%s\" (event was \"%s\")",
  182. fsm->name,
  183. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  184. fsm->state_to_str(fsm, next_state),
  185. fsm->event_to_str(fsm, fsm_event));
  186. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  187. break;
  188. default:
  189. log_printf (LOGSYS_LEVEL_CRIT, "Fsm: Can't find callback event!");
  190. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  191. break;
  192. }
  193. }
  194. static void mon_fsm_state_set (struct cs_fsm* fsm,
  195. enum mon_resource_state next_state, struct resource_instance* inst)
  196. {
  197. enum mon_resource_state prev_state = fsm->curr_state;
  198. const char *state_str;
  199. char key_name[ICMAP_KEYNAME_MAXLEN];
  200. ENTER();
  201. cs_fsm_state_set(fsm, next_state, inst, mon_fsm_cb);
  202. if (prev_state == fsm->curr_state) {
  203. return;
  204. }
  205. state_str = mon_res_state_to_str(fsm, fsm->curr_state);
  206. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
  207. icmap_set_string(key_name, state_str);
  208. }
  209. static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
  210. {
  211. struct resource_instance * inst = (struct resource_instance *)data;
  212. uint64_t tmp_value;
  213. char key_name[ICMAP_KEYNAME_MAXLEN];
  214. int run_updater;
  215. ENTER();
  216. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
  217. if (icmap_get_uint64(key_name, &tmp_value) == CS_OK) {
  218. if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
  219. log_printf (LOGSYS_LEVEL_DEBUG,
  220. "poll_period changing from:%"PRIu64" to %"PRIu64".",
  221. inst->period, tmp_value);
  222. inst->period = tmp_value;
  223. } else {
  224. log_printf (LOGSYS_LEVEL_WARNING,
  225. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  226. tmp_value, inst->name);
  227. }
  228. }
  229. if (inst->timer_handle) {
  230. api->timer_delete(inst->timer_handle);
  231. inst->timer_handle = 0;
  232. }
  233. run_updater = 0;
  234. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "max");
  235. if (inst->max_type == ICMAP_VALUETYPE_INT32) {
  236. if (icmap_get_int32(key_name, &inst->max.int32) != CS_OK) {
  237. inst->max.int32 = INT32_MAX;
  238. mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
  239. } else {
  240. run_updater = 1;
  241. }
  242. }
  243. if (inst->max_type == ICMAP_VALUETYPE_DOUBLE) {
  244. if (icmap_get_double(key_name, &inst->max.dbl) != CS_OK) {
  245. inst->max.dbl = INT32_MAX;
  246. mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
  247. } else {
  248. run_updater = 1;
  249. }
  250. }
  251. if (run_updater) {
  252. mon_fsm_state_set (fsm, MON_S_RUNNING, inst);
  253. /*
  254. * run the updater, incase the period has shortened
  255. * and to start the timer.
  256. */
  257. inst->update_stats_fn (inst);
  258. }
  259. }
  260. void mon_resource_failed (struct cs_fsm* fsm, int32_t event, void * data)
  261. {
  262. struct resource_instance * inst = (struct resource_instance *)data;
  263. ENTER();
  264. mon_fsm_state_set (fsm, MON_S_FAILED, inst);
  265. }
  266. static int32_t percent_mem_used_get(void)
  267. {
  268. #if defined(HAVE_LIBSTATGRAB)
  269. sg_mem_stats *mem_stats;
  270. sg_swap_stats *swap_stats;
  271. long long total, freemem;
  272. mem_stats = sg_get_mem_stats();
  273. swap_stats = sg_get_swap_stats();
  274. if (mem_stats == NULL || swap_stats != NULL) {
  275. log_printf (LOGSYS_LEVEL_ERROR, "Unable to get memory stats: %s",
  276. sg_str_error(sg_get_error()));
  277. return -1;
  278. }
  279. total = mem_stats->total + swap_stats->total;
  280. freemem = mem_stats->free + swap_stats->free;
  281. return ((total - freemem) * 100) / total;
  282. #else
  283. #if defined(COROSYNC_LINUX)
  284. char *line_ptr;
  285. char line[512];
  286. unsigned long long value;
  287. FILE *f;
  288. long long total = 0;
  289. long long freemem = 0;
  290. if ((f = fopen("/proc/meminfo", "r")) == NULL) {
  291. return -1;
  292. }
  293. while ((line_ptr = fgets(line, sizeof(line), f)) != NULL) {
  294. if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) {
  295. continue;
  296. }
  297. value *= 1024;
  298. if (strncmp(line_ptr, "MemTotal:", 9) == 0) {
  299. total += value;
  300. } else if (strncmp(line_ptr, "MemFree:", 8) == 0) {
  301. freemem += value;
  302. } else if (strncmp(line_ptr, "SwapTotal:", 10) == 0) {
  303. total += value;
  304. } else if (strncmp(line_ptr, "SwapFree:", 9) == 0) {
  305. freemem += value;
  306. }
  307. }
  308. fclose(f);
  309. return ((total - freemem) * 100) / total;
  310. #else
  311. #error need libstatgrab or linux.
  312. #endif /* COROSYNC_LINUX */
  313. #endif /* HAVE_LIBSTATGRAB */
  314. }
  315. static void mem_update_stats_fn (void *data)
  316. {
  317. struct resource_instance * inst = (struct resource_instance *)data;
  318. int32_t new_value;
  319. uint64_t timestamp;
  320. char key_name[ICMAP_KEYNAME_MAXLEN];
  321. new_value = percent_mem_used_get();
  322. if (new_value > 0) {
  323. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  324. icmap_set_uint32(key_name, new_value);
  325. timestamp = cs_timestamp_get();
  326. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  327. icmap_set_uint64(key_name, timestamp);
  328. if (new_value > inst->max.int32 && inst->fsm.curr_state != MON_S_FAILED) {
  329. cs_fsm_process (&inst->fsm, MON_E_FAILURE, inst, mon_fsm_cb);
  330. }
  331. }
  332. api->timer_add_duration(inst->period * MILLI_2_NANO_SECONDS,
  333. inst, inst->update_stats_fn, &inst->timer_handle);
  334. }
  335. static double min15_loadavg_get(void)
  336. {
  337. #if defined(HAVE_LIBSTATGRAB)
  338. sg_load_stats *load_stats;
  339. load_stats = sg_get_load_stats ();
  340. if (load_stats == NULL) {
  341. log_printf (LOGSYS_LEVEL_ERROR, "Unable to get load stats: %s",
  342. sg_str_error (sg_get_error()));
  343. return -1;
  344. }
  345. return load_stats->min15;
  346. #else
  347. #if defined(COROSYNC_LINUX)
  348. double loadav[3];
  349. if (getloadavg(loadav,3) < 0) {
  350. return -1;
  351. }
  352. return loadav[2];
  353. #else
  354. #error need libstatgrab or linux.
  355. #endif /* COROSYNC_LINUX */
  356. #endif /* HAVE_LIBSTATGRAB */
  357. }
  358. static void load_update_stats_fn (void *data)
  359. {
  360. struct resource_instance * inst = (struct resource_instance *)data;
  361. uint64_t timestamp;
  362. char key_name[ICMAP_KEYNAME_MAXLEN];
  363. double min15 = min15_loadavg_get();
  364. if (min15 > 0) {
  365. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  366. icmap_set_double(key_name, min15);
  367. timestamp = cs_timestamp_get();
  368. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  369. icmap_set_uint64(key_name, timestamp);
  370. if (min15 > inst->max.dbl && inst->fsm.curr_state != MON_S_FAILED) {
  371. cs_fsm_process (&inst->fsm, MON_E_FAILURE, &inst, mon_fsm_cb);
  372. }
  373. }
  374. api->timer_add_duration(inst->period * MILLI_2_NANO_SECONDS,
  375. inst, inst->update_stats_fn, &inst->timer_handle);
  376. }
  377. static void mon_key_changed_cb (
  378. int32_t event,
  379. const char *key_name,
  380. struct icmap_notify_value new_value,
  381. struct icmap_notify_value old_value,
  382. void *user_data)
  383. {
  384. struct resource_instance* inst = (struct resource_instance*)user_data;
  385. char *last_key_part;
  386. if (event == ICMAP_TRACK_DELETE && inst) {
  387. log_printf (LOGSYS_LEVEL_WARNING,
  388. "resource \"%s\" deleted from cmap!",
  389. inst->name);
  390. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst, mon_fsm_cb);
  391. }
  392. if (event == ICMAP_TRACK_MODIFY) {
  393. last_key_part = strrchr(key_name, '.');
  394. if (last_key_part == NULL)
  395. return ;
  396. last_key_part++;
  397. if (strcmp(last_key_part, "max") == 0 ||
  398. strcmp(last_key_part, "poll_period") == 0) {
  399. ENTER();
  400. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst, mon_fsm_cb);
  401. }
  402. }
  403. }
  404. static void mon_instance_init (struct resource_instance* inst)
  405. {
  406. uint64_t tmp_value;
  407. char key_name[ICMAP_KEYNAME_MAXLEN];
  408. icmap_track_t icmap_track = NULL;
  409. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  410. if (inst->max_type == ICMAP_VALUETYPE_INT32) {
  411. icmap_set_int32(key_name, 0);
  412. } else {
  413. icmap_set_double(key_name, 0);
  414. }
  415. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  416. icmap_set_uint64(key_name, 0);
  417. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
  418. icmap_set_string(key_name, mon_stopped_str);
  419. inst->fsm.name = inst->name;
  420. inst->fsm.curr_entry = 0;
  421. inst->fsm.curr_state = MON_S_STOPPED;
  422. inst->fsm.table = mon_fsm_table;
  423. inst->fsm.entries = sizeof(mon_fsm_table) / sizeof(struct cs_fsm_entry);
  424. inst->fsm.state_to_str = mon_res_state_to_str;
  425. inst->fsm.event_to_str = mon_res_event_to_str;
  426. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
  427. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  428. icmap_set_uint64(key_name, inst->period);
  429. }
  430. else {
  431. if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
  432. inst->period = tmp_value;
  433. } else {
  434. log_printf (LOGSYS_LEVEL_WARNING,
  435. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  436. tmp_value, inst->name);
  437. }
  438. }
  439. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst, mon_fsm_cb);
  440. icmap_track_add(inst->icmap_path,
  441. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
  442. mon_key_changed_cb, inst, &icmap_track);
  443. }
  444. static char *mon_exec_init_fn (struct corosync_api_v1 *corosync_api)
  445. {
  446. #ifdef HAVE_LIBSTATGRAB
  447. sg_init();
  448. #endif /* HAVE_LIBSTATGRAB */
  449. #ifdef COROSYNC_SOLARIS
  450. logsys_subsys_init();
  451. #endif
  452. api = corosync_api;
  453. mon_instance_init (&memory_used_inst);
  454. mon_instance_init (&load_15min_inst);
  455. return NULL;
  456. }