mon.c 14 KB

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