mon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/lcr/lcr_comp.h>
  42. #include <corosync/coroapi.h>
  43. #include <corosync/list.h>
  44. #include <corosync/logsys.h>
  45. #include <corosync/icmap.h>
  46. #include "../exec/fsm.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. /*
  134. * Dynamic loading descriptor
  135. */
  136. static struct corosync_service_engine *mon_get_service_engine_ver0 (void);
  137. static struct corosync_service_engine_iface_ver0 mon_service_engine_iface = {
  138. .corosync_get_service_engine_ver0 = mon_get_service_engine_ver0
  139. };
  140. static struct lcr_iface corosync_mon_ver0[1] = {
  141. {
  142. .name = "corosync_mon",
  143. .version = 0,
  144. .versions_replace = 0,
  145. .versions_replace_count = 0,
  146. .dependencies = 0,
  147. .dependency_count = 0,
  148. .constructor = NULL,
  149. .destructor = NULL,
  150. .interfaces = NULL,
  151. }
  152. };
  153. static struct lcr_comp mon_comp_ver0 = {
  154. .iface_count = 1,
  155. .ifaces = corosync_mon_ver0
  156. };
  157. static struct corosync_service_engine *mon_get_service_engine_ver0 (void)
  158. {
  159. return (&mon_service_engine);
  160. }
  161. #ifdef COROSYNC_SOLARIS
  162. void corosync_lcr_component_register (void);
  163. void corosync_lcr_component_register (void) {
  164. #else
  165. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  166. #endif
  167. lcr_interfaces_set (&corosync_mon_ver0[0], &mon_service_engine_iface);
  168. lcr_component_register (&mon_comp_ver0);
  169. }
  170. static const char * mon_res_state_to_str(struct cs_fsm* fsm,
  171. int32_t state)
  172. {
  173. switch (state) {
  174. case MON_S_STOPPED:
  175. return mon_stopped_str;
  176. break;
  177. case MON_S_RUNNING:
  178. return mon_running_str;
  179. break;
  180. case MON_S_FAILED:
  181. return mon_failed_str;
  182. break;
  183. }
  184. return NULL;
  185. }
  186. static const char * mon_res_event_to_str(struct cs_fsm* fsm,
  187. int32_t event)
  188. {
  189. switch (event) {
  190. case MON_E_CONFIG_CHANGED:
  191. return mon_config_changed_str;
  192. break;
  193. case MON_E_FAILURE:
  194. return mon_failure_str;
  195. break;
  196. }
  197. return NULL;
  198. }
  199. static void mon_fsm_state_set (struct cs_fsm* fsm,
  200. enum mon_resource_state next_state, struct resource_instance* inst)
  201. {
  202. enum mon_resource_state prev_state = fsm->curr_state;
  203. const char *state_str;
  204. char key_name[ICMAP_KEYNAME_MAXLEN];
  205. ENTER();
  206. cs_fsm_state_set(fsm, next_state, inst);
  207. if (prev_state == fsm->curr_state) {
  208. return;
  209. }
  210. state_str = mon_res_state_to_str(fsm, fsm->curr_state);
  211. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
  212. icmap_set_string(key_name, state_str);
  213. }
  214. static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
  215. {
  216. struct resource_instance * inst = (struct resource_instance *)data;
  217. uint64_t tmp_value;
  218. char key_name[ICMAP_KEYNAME_MAXLEN];
  219. int run_updater;
  220. ENTER();
  221. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
  222. if (icmap_get_uint64(key_name, &tmp_value) == CS_OK) {
  223. if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
  224. log_printf (LOGSYS_LEVEL_DEBUG,
  225. "poll_period changing from:%"PRIu64" to %"PRIu64".",
  226. inst->period, tmp_value);
  227. inst->period = tmp_value;
  228. } else {
  229. log_printf (LOGSYS_LEVEL_WARNING,
  230. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  231. tmp_value, inst->name);
  232. }
  233. }
  234. if (inst->timer_handle) {
  235. api->timer_delete(inst->timer_handle);
  236. inst->timer_handle = 0;
  237. }
  238. run_updater = 0;
  239. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "max");
  240. if (inst->max_type == ICMAP_VALUETYPE_INT32) {
  241. if (icmap_get_int32(key_name, &inst->max.int32) != CS_OK) {
  242. inst->max.int32 = INT32_MAX;
  243. mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
  244. } else {
  245. run_updater = 1;
  246. }
  247. }
  248. if (inst->max_type == ICMAP_VALUETYPE_DOUBLE) {
  249. if (icmap_get_double(key_name, &inst->max.dbl) != CS_OK) {
  250. inst->max.dbl = INT32_MAX;
  251. mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
  252. } else {
  253. run_updater = 1;
  254. }
  255. }
  256. if (run_updater) {
  257. mon_fsm_state_set (fsm, MON_S_RUNNING, inst);
  258. /*
  259. * run the updater, incase the period has shortened
  260. * and to start the timer.
  261. */
  262. inst->update_stats_fn (inst);
  263. }
  264. }
  265. void mon_resource_failed (struct cs_fsm* fsm, int32_t event, void * data)
  266. {
  267. struct resource_instance * inst = (struct resource_instance *)data;
  268. ENTER();
  269. mon_fsm_state_set (fsm, MON_S_FAILED, inst);
  270. }
  271. static int32_t percent_mem_used_get(void)
  272. {
  273. #if defined(HAVE_LIBSTATGRAB)
  274. sg_mem_stats *mem_stats;
  275. sg_swap_stats *swap_stats;
  276. long long total, freemem;
  277. mem_stats = sg_get_mem_stats();
  278. swap_stats = sg_get_swap_stats();
  279. if (mem_stats == NULL || swap_stats != NULL) {
  280. log_printf (LOGSYS_LEVEL_ERROR, "Unable to get memory stats: %s\n",
  281. sg_str_error(sg_get_error()));
  282. return -1;
  283. }
  284. total = mem_stats->total + swap_stats->total;
  285. freemem = mem_stats->free + swap_stats->free;
  286. return ((total - freemem) * 100) / total;
  287. #else
  288. #if defined(COROSYNC_LINUX)
  289. char *line_ptr;
  290. char line[512];
  291. unsigned long long value;
  292. FILE *f;
  293. long long total = 0;
  294. long long freemem = 0;
  295. if ((f = fopen("/proc/meminfo", "r")) == NULL) {
  296. return -1;
  297. }
  298. while ((line_ptr = fgets(line, sizeof(line), f)) != NULL) {
  299. if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) {
  300. continue;
  301. }
  302. value *= 1024;
  303. if (strncmp(line_ptr, "MemTotal:", 9) == 0) {
  304. total += value;
  305. } else if (strncmp(line_ptr, "MemFree:", 8) == 0) {
  306. freemem += value;
  307. } else if (strncmp(line_ptr, "SwapTotal:", 10) == 0) {
  308. total += value;
  309. } else if (strncmp(line_ptr, "SwapFree:", 9) == 0) {
  310. freemem += value;
  311. }
  312. }
  313. fclose(f);
  314. return ((total - freemem) * 100) / total;
  315. #else
  316. #error need libstatgrab or linux.
  317. #endif /* COROSYNC_LINUX */
  318. #endif /* HAVE_LIBSTATGRAB */
  319. }
  320. static void mem_update_stats_fn (void *data)
  321. {
  322. struct resource_instance * inst = (struct resource_instance *)data;
  323. int32_t new_value;
  324. uint64_t timestamp;
  325. char key_name[ICMAP_KEYNAME_MAXLEN];
  326. new_value = percent_mem_used_get();
  327. fprintf(stderr,"BLA = %u\n", new_value);
  328. if (new_value > 0) {
  329. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  330. icmap_set_uint32(key_name, new_value);
  331. timestamp = cs_timestamp_get();
  332. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  333. icmap_set_uint64(key_name, timestamp);
  334. if (new_value > inst->max.int32 && inst->fsm.curr_state != MON_S_FAILED) {
  335. cs_fsm_process (&inst->fsm, MON_E_FAILURE, inst);
  336. }
  337. }
  338. api->timer_add_duration(inst->period * MILLI_2_NANO_SECONDS,
  339. inst, inst->update_stats_fn, &inst->timer_handle);
  340. }
  341. static double min15_loadavg_get(void)
  342. {
  343. #if defined(HAVE_LIBSTATGRAB)
  344. sg_load_stats *load_stats;
  345. load_stats = sg_get_load_stats ();
  346. if (load_stats == NULL) {
  347. log_printf (LOGSYS_LEVEL_ERROR, "Unable to get load stats: %s\n",
  348. sg_str_error (sg_get_error()));
  349. return -1;
  350. }
  351. return load_stats->min15;
  352. #else
  353. #if defined(COROSYNC_LINUX)
  354. double loadav[3];
  355. if (getloadavg(loadav,3) < 0) {
  356. return -1;
  357. }
  358. return loadav[2];
  359. #else
  360. #error need libstatgrab or linux.
  361. #endif /* COROSYNC_LINUX */
  362. #endif /* HAVE_LIBSTATGRAB */
  363. }
  364. static void load_update_stats_fn (void *data)
  365. {
  366. struct resource_instance * inst = (struct resource_instance *)data;
  367. uint64_t timestamp;
  368. char key_name[ICMAP_KEYNAME_MAXLEN];
  369. double min15 = min15_loadavg_get();
  370. if (min15 > 0) {
  371. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  372. icmap_set_double(key_name, min15);
  373. timestamp = cs_timestamp_get();
  374. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  375. icmap_set_uint64(key_name, timestamp);
  376. if (min15 > inst->max.dbl && inst->fsm.curr_state != MON_S_FAILED) {
  377. cs_fsm_process (&inst->fsm, MON_E_FAILURE, &inst);
  378. }
  379. }
  380. api->timer_add_duration(inst->period * MILLI_2_NANO_SECONDS,
  381. inst, inst->update_stats_fn, &inst->timer_handle);
  382. }
  383. static void mon_key_changed_cb (
  384. int32_t event,
  385. const char *key_name,
  386. struct icmap_notify_value new_value,
  387. struct icmap_notify_value old_value,
  388. void *user_data)
  389. {
  390. struct resource_instance* inst = (struct resource_instance*)user_data;
  391. char *last_key_part;
  392. if (event == ICMAP_TRACK_DELETE && inst) {
  393. log_printf (LOGSYS_LEVEL_WARNING,
  394. "resource \"%s\" deleted from cmap!",
  395. inst->name);
  396. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
  397. }
  398. if (event == ICMAP_TRACK_MODIFY) {
  399. last_key_part = strrchr(key_name, '.');
  400. if (last_key_part == NULL)
  401. return ;
  402. last_key_part++;
  403. if (strcmp(last_key_part, "max") == 0 ||
  404. strcmp(last_key_part, "poll_period") == 0) {
  405. ENTER();
  406. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
  407. }
  408. }
  409. }
  410. static void mon_instance_init (struct resource_instance* inst)
  411. {
  412. uint64_t tmp_value;
  413. char key_name[ICMAP_KEYNAME_MAXLEN];
  414. icmap_track_t icmap_track;
  415. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
  416. if (inst->max_type == ICMAP_VALUETYPE_INT32) {
  417. icmap_set_int32(key_name, 0);
  418. } else {
  419. icmap_set_double(key_name, 0);
  420. }
  421. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
  422. icmap_set_uint64(key_name, 0);
  423. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
  424. icmap_set_string(key_name, mon_stopped_str);
  425. inst->fsm.name = inst->name;
  426. inst->fsm.curr_entry = 0;
  427. inst->fsm.curr_state = MON_S_STOPPED;
  428. inst->fsm.table = mon_fsm_table;
  429. inst->fsm.entries = sizeof(mon_fsm_table) / sizeof(struct cs_fsm_entry);
  430. inst->fsm.state_to_str = mon_res_state_to_str;
  431. inst->fsm.event_to_str = mon_res_event_to_str;
  432. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
  433. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  434. icmap_set_uint64(key_name, inst->period);
  435. }
  436. else {
  437. if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
  438. inst->period = tmp_value;
  439. } else {
  440. log_printf (LOGSYS_LEVEL_WARNING,
  441. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  442. tmp_value, inst->name);
  443. }
  444. }
  445. cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
  446. icmap_track_add(inst->icmap_path,
  447. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
  448. mon_key_changed_cb, inst, &icmap_track);
  449. }
  450. static int mon_exec_init_fn (
  451. struct corosync_api_v1 *corosync_api)
  452. {
  453. #ifdef HAVE_LIBSTATGRAB
  454. sg_init();
  455. #endif /* HAVE_LIBSTATGRAB */
  456. #ifdef COROSYNC_SOLARIS
  457. logsys_subsys_init();
  458. #endif
  459. api = corosync_api;
  460. mon_instance_init (&memory_used_inst);
  461. mon_instance_init (&load_15min_inst);
  462. return 0;
  463. }