mon.c 14 KB

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