4
0

corosync-notifyd.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Copyright (c) 2011-2017 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 <sys/select.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/types.h>
  39. #include <netdb.h>
  40. #include <arpa/inet.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <errno.h>
  44. #include <unistd.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <poll.h>
  48. #include <signal.h>
  49. #include <qb/qbdefs.h>
  50. #include <qb/qbloop.h>
  51. #include <qb/qbmap.h>
  52. #include <qb/qblog.h>
  53. #include <corosync/corotypes.h>
  54. #include <corosync/cfg.h>
  55. #include <corosync/quorum.h>
  56. #include <corosync/cmap.h>
  57. #ifdef HAVE_LIBSYSTEMD
  58. #include <systemd/sd-daemon.h>
  59. #endif
  60. /*
  61. * generic declarations
  62. */
  63. enum {
  64. CS_NTF_LOG,
  65. CS_NTF_STDOUT,
  66. CS_NTF_SNMP,
  67. CS_NTF_DBUS,
  68. CS_NTF_FG,
  69. CS_NTF_NODNS,
  70. CS_NTF_MAX,
  71. };
  72. static int conf[CS_NTF_MAX];
  73. static int exit_code = 0;
  74. static int32_t _cs_is_quorate = 0;
  75. typedef void (*node_membership_fn_t)(char *nodename, uint32_t nodeid, char *state, char* ip);
  76. typedef void (*node_quorum_fn_t)(char *nodename, uint32_t nodeid, const char *state);
  77. typedef void (*application_connection_fn_t)(char *nodename, uint32_t nodeid, char *app_name, const char *state);
  78. typedef void (*link_faulty_fn_t)(char *nodename, uint32_t local_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state);
  79. struct notify_callbacks {
  80. node_membership_fn_t node_membership_fn;
  81. node_quorum_fn_t node_quorum_fn;
  82. application_connection_fn_t application_connection_fn;
  83. link_faulty_fn_t link_faulty_fn;
  84. };
  85. struct track_item {
  86. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  87. cmap_track_handle_t track_handle;
  88. };
  89. #define MAX_NOTIFIERS 5
  90. static int num_notifiers = 0;
  91. static struct notify_callbacks notifiers[MAX_NOTIFIERS];
  92. /*
  93. * Global variable with local nodeid
  94. */
  95. static uint32_t g_local_nodeid = 0;
  96. static char local_nodename[CS_MAX_NAME_LENGTH];
  97. static qb_loop_t *main_loop;
  98. static quorum_handle_t quorum_handle;
  99. static qb_map_t *tracker_map;
  100. static void _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip);
  101. static void _cs_node_quorum_event(const char *state);
  102. static void _cs_application_connection_event(char *app_name, const char *state);
  103. static void _cs_link_faulty_event(uint32_t nodeid, uint32_t iface_no, const char *state);
  104. #ifdef HAVE_DBUS
  105. #include <dbus/dbus.h>
  106. /*
  107. * dbus
  108. */
  109. #define DBUS_CS_NAME "org.corosync"
  110. #define DBUS_CS_IFACE "org.corosync"
  111. #define DBUS_CS_PATH "/org/corosync"
  112. static DBusConnection *db = NULL;
  113. static char _err[512];
  114. static int err_set = 0;
  115. static void _cs_dbus_init(void);
  116. #endif /* HAVE_DBUS */
  117. #ifdef ENABLE_SNMP
  118. #include <net-snmp/net-snmp-config.h>
  119. #include <net-snmp/snmpv3_api.h>
  120. #include <net-snmp/agent/agent_trap.h>
  121. #include <net-snmp/library/mib.h>
  122. #include <net-snmp/library/snmp_api.h>
  123. #include <net-snmp/library/snmp_client.h>
  124. #include <net-snmp/library/snmp_debug.h>
  125. enum snmp_node_status {
  126. SNMP_NODE_STATUS_UNKNOWN = 0,
  127. SNMP_NODE_STATUS_JOINED = 1,
  128. SNMP_NODE_STATUS_LEFT = 2
  129. };
  130. #define SNMP_OID_COROSYNC "1.3.6.1.4.1.35488"
  131. #define SNMP_OID_OBJECT_ROOT SNMP_OID_COROSYNC ".1"
  132. #define SNMP_OID_OBJECT_NODE_NAME SNMP_OID_OBJECT_ROOT ".1"
  133. #define SNMP_OID_OBJECT_NODE_ID SNMP_OID_OBJECT_ROOT ".2"
  134. #define SNMP_OID_OBJECT_NODE_STATUS SNMP_OID_OBJECT_ROOT ".3"
  135. #define SNMP_OID_OBJECT_NODE_ADDR SNMP_OID_OBJECT_ROOT ".4"
  136. #define SNMP_OID_OBJECT_LOCAL_NODE_ID SNMP_OID_OBJECT_ROOT ".5"
  137. #define SNMP_OID_OBJECT_RINGSEQ SNMP_OID_OBJECT_ROOT ".20"
  138. #define SNMP_OID_OBJECT_QUORUM SNMP_OID_OBJECT_ROOT ".21"
  139. #define SNMP_OID_OBJECT_APP_NAME SNMP_OID_OBJECT_ROOT ".40"
  140. #define SNMP_OID_OBJECT_APP_STATUS SNMP_OID_OBJECT_ROOT ".41"
  141. #define SNMP_OID_OBJECT_LINK_IFACE_NO SNMP_OID_OBJECT_ROOT ".60"
  142. #define SNMP_OID_OBJECT_LINK_STATUS SNMP_OID_OBJECT_ROOT ".61"
  143. #define SNMP_OID_TRAPS_ROOT SNMP_OID_COROSYNC ".0"
  144. #define SNMP_OID_TRAPS_NODE SNMP_OID_TRAPS_ROOT ".1"
  145. #define SNMP_OID_TRAPS_QUORUM SNMP_OID_TRAPS_ROOT ".2"
  146. #define SNMP_OID_TRAPS_APP SNMP_OID_TRAPS_ROOT ".3"
  147. #define SNMP_OID_TRAPS_LINK SNMP_OID_TRAPS_ROOT ".4"
  148. #define CS_TIMESTAMP_STR_LEN 20
  149. static const char *local_host = "localhost";
  150. #endif /* ENABLE_SNMP */
  151. static char snmp_manager_buf[CS_MAX_NAME_LENGTH];
  152. static char *snmp_manager = NULL;
  153. static char snmp_community_buf[CS_MAX_NAME_LENGTH];
  154. static char *snmp_community = NULL;
  155. #define CMAP_MAX_RETRIES 10
  156. /*
  157. * cmap
  158. */
  159. static cmap_handle_t cmap_handle;
  160. static cmap_handle_t stats_handle;
  161. static cmap_track_handle_t cmap_track_handle_runtime_members_key_changed;
  162. static cmap_track_handle_t cmap_track_handle_stats_ipcs_key_changed;
  163. static cmap_track_handle_t cmap_track_handle_stats_knet_key_changed;
  164. static int32_t _cs_ip_to_hostname(char* ip, char* name_out)
  165. {
  166. struct sockaddr_in sa;
  167. int rc;
  168. if (strchr(ip, ':') == NULL) {
  169. sa.sin_family = AF_INET;
  170. } else {
  171. sa.sin_family = AF_INET6;
  172. }
  173. rc = inet_pton(sa.sin_family, ip, &sa.sin_addr);
  174. if (rc == 0) {
  175. return -EINVAL;
  176. }
  177. rc = getnameinfo((struct sockaddr*)&sa, sizeof(sa),
  178. name_out, CS_MAX_NAME_LENGTH, NULL, 0, 0);
  179. if (rc != 0) {
  180. qb_log(LOG_ERR, "error looking up %s : %s", ip, gai_strerror(rc));
  181. return -EINVAL;
  182. }
  183. return 0;
  184. }
  185. static void _cs_cmap_members_key_changed (
  186. cmap_handle_t cmap_handle_c,
  187. cmap_track_handle_t cmap_track_handle,
  188. int32_t event,
  189. const char *key_name,
  190. struct cmap_notify_value new_value,
  191. struct cmap_notify_value old_value,
  192. void *user_data)
  193. {
  194. char nodename[CS_MAX_NAME_LENGTH];
  195. char* open_bracket = NULL;
  196. char* close_bracket = NULL;
  197. int res;
  198. uint32_t nodeid;
  199. char *ip_str;
  200. char tmp_key[CMAP_KEYNAME_MAXLEN];
  201. cs_error_t err;
  202. int no_retries;
  203. if (event != CMAP_TRACK_ADD && event != CMAP_TRACK_MODIFY) {
  204. return ;
  205. }
  206. if (NULL == key_name) {
  207. qb_log(LOG_ERR, "key_name: nil");
  208. return ;
  209. }
  210. res = sscanf(key_name, "runtime.members.%u.%s", &nodeid, tmp_key);
  211. if (res != 2)
  212. return ;
  213. if (strcmp(tmp_key, "status") != 0) {
  214. return ;
  215. }
  216. res = snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "runtime.members.%u.ip", nodeid);
  217. if (res <= 0 || res >= CMAP_KEYNAME_MAXLEN) {
  218. qb_log(LOG_ERR, "temp_key: failed, res: %d, nodeid: " CS_PRI_NODE_ID, res, nodeid);
  219. return ;
  220. }
  221. no_retries = 0;
  222. while ((err = cmap_get_string(cmap_handle, tmp_key, &ip_str)) == CS_ERR_TRY_AGAIN &&
  223. no_retries++ < CMAP_MAX_RETRIES) {
  224. sleep(1);
  225. }
  226. if (err != CS_OK) {
  227. return ;
  228. }
  229. /*
  230. * We want the ip out of: "r(0) ip(192.168.100.92)"
  231. */
  232. open_bracket = strrchr(ip_str, '(');
  233. if (NULL == open_bracket) {
  234. qb_log(LOG_ERR, "ip_str: %s", ip_str);
  235. free(ip_str);
  236. return ;
  237. }
  238. open_bracket++;
  239. close_bracket = strchr(open_bracket, ')');
  240. if (NULL == close_bracket) {
  241. qb_log(LOG_ERR, "open_bracket: %s", open_bracket);
  242. free(ip_str);
  243. return ;
  244. }
  245. *close_bracket = '\0';
  246. if(conf[CS_NTF_NODNS]) {
  247. strncpy(nodename, open_bracket, CS_MAX_NAME_LENGTH-1);
  248. } else {
  249. res = _cs_ip_to_hostname(open_bracket, nodename);
  250. if (res) {
  251. strncpy(nodename, open_bracket, CS_MAX_NAME_LENGTH-1);
  252. }
  253. }
  254. _cs_node_membership_event(nodename, nodeid, (char *)new_value.data, open_bracket);
  255. free(ip_str);
  256. }
  257. static void _cs_cmap_connections_key_changed (
  258. cmap_handle_t cmap_handle_c,
  259. cmap_track_handle_t cmap_track_handle,
  260. int32_t event,
  261. const char *key_name,
  262. struct cmap_notify_value new_value,
  263. struct cmap_notify_value old_value,
  264. void *user_data)
  265. {
  266. char obj_name[CS_MAX_NAME_LENGTH];
  267. char conn_str[CMAP_KEYNAME_MAXLEN];
  268. char tmp_key[CMAP_KEYNAME_MAXLEN];
  269. int service, pid;
  270. int res;
  271. res = sscanf(key_name, "stats.ipcs.service%d.%d.%[^.].%s", &service,&pid, conn_str, tmp_key);
  272. if (res != 4) {
  273. return ;
  274. }
  275. if (strcmp(tmp_key, "procname") != 0) {
  276. return ;
  277. }
  278. if (snprintf(obj_name, CS_MAX_NAME_LENGTH, "%s.%d.%s", conn_str, pid,
  279. (char*)new_value.data) >= CS_MAX_NAME_LENGTH) {
  280. /*
  281. * This should never happen
  282. */
  283. qb_log(LOG_ERR, "Can't snprintf obj_name");
  284. return ;
  285. }
  286. if (event == CMAP_TRACK_ADD) {
  287. _cs_application_connection_event(obj_name, "connected");
  288. }
  289. if (event == CMAP_TRACK_DELETE) {
  290. _cs_application_connection_event(obj_name, "disconnected");
  291. }
  292. }
  293. static void _cs_cmap_link_faulty_key_changed (
  294. cmap_handle_t cmap_handle_c,
  295. cmap_track_handle_t cmap_track_handle,
  296. int32_t event,
  297. const char *key_name,
  298. struct cmap_notify_value new_value,
  299. struct cmap_notify_value old_value,
  300. void *user_data)
  301. {
  302. uint32_t iface_no;
  303. uint32_t nodeid;
  304. int res;
  305. int no_retries;
  306. uint8_t connected;
  307. cs_error_t err;
  308. res = sscanf(key_name, "stats.knet.node%u.link%u.connected", &nodeid, &iface_no);
  309. if (res != 2) {
  310. return ;
  311. }
  312. no_retries = 0;
  313. while ((err = cmap_get_uint8(stats_handle, key_name, &connected)) == CS_ERR_TRY_AGAIN &&
  314. no_retries++ < CMAP_MAX_RETRIES) {
  315. sleep(1);
  316. }
  317. if (err != CS_OK) {
  318. return ;
  319. }
  320. if (connected) {
  321. _cs_link_faulty_event(nodeid, iface_no, "operational");
  322. } else {
  323. _cs_link_faulty_event(nodeid, iface_no, "disconnected");
  324. }
  325. }
  326. static void _cs_cmap_link_added_removed (
  327. cmap_handle_t cmap_handle_c,
  328. cmap_track_handle_t track_handle,
  329. int32_t event,
  330. const char *key_name,
  331. struct cmap_notify_value new_value,
  332. struct cmap_notify_value old_value,
  333. void *user_data)
  334. {
  335. struct track_item *track_item;
  336. cs_error_t err;
  337. /* Add/remove a tracker for a new/removed knet link */
  338. if (strstr(key_name, ".connected")) {
  339. if (event == CMAP_TRACK_ADD) {
  340. assert(strlen(key_name) < sizeof(track_item->key_name));
  341. track_item = malloc(sizeof(struct track_item));
  342. if (!track_item) {
  343. qb_log(LOG_WARNING, "Can't alloc track_item for new/removed knet link");
  344. return;
  345. }
  346. err = cmap_track_add(stats_handle, key_name, CMAP_TRACK_MODIFY,
  347. _cs_cmap_link_faulty_key_changed, NULL, &track_handle);
  348. if (err != CS_OK) {
  349. qb_log(LOG_WARNING, "Can't add tracker for new/removed knet link");
  350. free(track_item);
  351. return ;
  352. }
  353. strcpy(track_item->key_name, key_name);
  354. track_item->track_handle = track_handle;
  355. qb_map_put(tracker_map, track_item->key_name, track_item);
  356. } else {
  357. track_item = qb_map_get(tracker_map, key_name);
  358. if (track_item) {
  359. cmap_track_delete(stats_handle, track_item->track_handle);
  360. qb_map_rm(tracker_map, track_item->key_name);
  361. free(track_item);
  362. }
  363. }
  364. }
  365. }
  366. static int
  367. _cs_cmap_dispatch(int fd, int revents, void *data)
  368. {
  369. cs_error_t err;
  370. err = cmap_dispatch(*(cmap_handle_t *)data, CS_DISPATCH_ONE);
  371. if (err != CS_OK && err != CS_ERR_TRY_AGAIN && err != CS_ERR_TIMEOUT &&
  372. err != CS_ERR_QUEUE_FULL) {
  373. qb_log(LOG_ERR, "Could not dispatch cmap events. Error %u", err);
  374. qb_loop_stop(main_loop);
  375. exit_code = 1;
  376. return -1;
  377. }
  378. return 0;
  379. }
  380. static void _cs_quorum_notification(quorum_handle_t handle,
  381. uint32_t quorate, uint64_t ring_seq,
  382. uint32_t view_list_entries, uint32_t *view_list)
  383. {
  384. if (_cs_is_quorate == quorate) {
  385. return;
  386. }
  387. _cs_is_quorate = quorate;
  388. if (quorate) {
  389. _cs_node_quorum_event("quorate");
  390. } else {
  391. _cs_node_quorum_event("not quorate");
  392. }
  393. }
  394. static int
  395. _cs_quorum_dispatch(int fd, int revents, void *data)
  396. {
  397. cs_error_t err;
  398. err = quorum_dispatch(quorum_handle, CS_DISPATCH_ONE);
  399. if (err != CS_OK && err != CS_ERR_TRY_AGAIN && err != CS_ERR_TIMEOUT &&
  400. err != CS_ERR_QUEUE_FULL) {
  401. qb_log(LOG_ERR, "Could not dispatch quorum events. Error %u", err);
  402. qb_loop_stop(main_loop);
  403. exit_code = 1;
  404. return -1;
  405. }
  406. return 0;
  407. }
  408. static void
  409. _cs_quorum_init(void)
  410. {
  411. cs_error_t rc;
  412. uint32_t quorum_type;
  413. int fd;
  414. quorum_callbacks_t quorum_callbacks = {
  415. .quorum_notify_fn = _cs_quorum_notification,
  416. };
  417. rc = quorum_initialize (&quorum_handle, &quorum_callbacks,
  418. &quorum_type);
  419. if (rc != CS_OK) {
  420. qb_log(LOG_ERR, "Could not connect to corosync(quorum)");
  421. return;
  422. }
  423. quorum_fd_get(quorum_handle, &fd);
  424. qb_loop_poll_add(main_loop, QB_LOOP_MED, fd, POLLIN|POLLNVAL, NULL,
  425. _cs_quorum_dispatch);
  426. rc = quorum_trackstart(quorum_handle, CS_TRACK_CHANGES);
  427. if (rc != CS_OK) {
  428. qb_log(LOG_ERR, "Could not start tracking");
  429. return;
  430. }
  431. }
  432. static void
  433. _cs_quorum_finalize(void)
  434. {
  435. quorum_finalize (quorum_handle);
  436. }
  437. #ifdef HAVE_DBUS
  438. /*
  439. * dbus notifications
  440. */
  441. static void
  442. _cs_dbus_auto_flush(void)
  443. {
  444. dbus_connection_ref(db);
  445. while (dbus_connection_get_dispatch_status(db) == DBUS_DISPATCH_DATA_REMAINS) {
  446. dbus_connection_dispatch(db);
  447. }
  448. while (dbus_connection_has_messages_to_send(db)) {
  449. dbus_connection_flush(db);
  450. }
  451. dbus_connection_unref(db);
  452. }
  453. static void
  454. _cs_dbus_release(void)
  455. {
  456. DBusError err;
  457. if (!db)
  458. return;
  459. dbus_error_init(&err);
  460. dbus_bus_release_name(db, DBUS_CS_NAME, &err);
  461. dbus_error_free(&err);
  462. dbus_connection_unref(db);
  463. db = NULL;
  464. }
  465. static void
  466. _cs_dbus_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  467. {
  468. DBusMessage *msg = NULL;
  469. if (err_set) {
  470. qb_log(LOG_ERR, "%s", _err);
  471. err_set = 0;
  472. }
  473. if (!db) {
  474. goto out_free;
  475. }
  476. if (dbus_connection_get_is_connected(db) != TRUE) {
  477. err_set = 1;
  478. snprintf(_err, sizeof(_err), "DBus connection lost");
  479. _cs_dbus_release();
  480. goto out_unlock;
  481. }
  482. _cs_dbus_auto_flush();
  483. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  484. DBUS_CS_IFACE,
  485. "QuorumStateChange"))) {
  486. qb_log(LOG_ERR, "error creating dbus signal");
  487. goto out_unlock;
  488. }
  489. if (!dbus_message_append_args(msg,
  490. DBUS_TYPE_STRING, &nodename,
  491. DBUS_TYPE_UINT32, &nodeid,
  492. DBUS_TYPE_STRING, &state,
  493. DBUS_TYPE_INVALID)) {
  494. qb_log(LOG_ERR, "error adding args to quorum signal");
  495. goto out_unlock;
  496. }
  497. dbus_connection_send(db, msg, NULL);
  498. out_unlock:
  499. if (msg) {
  500. dbus_message_unref(msg);
  501. }
  502. out_free:
  503. return;
  504. }
  505. static void
  506. _cs_dbus_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  507. {
  508. DBusMessage *msg = NULL;
  509. if (err_set) {
  510. qb_log(LOG_ERR, "%s", _err);
  511. err_set = 0;
  512. }
  513. if (!db) {
  514. goto out_free;
  515. }
  516. if (dbus_connection_get_is_connected(db) != TRUE) {
  517. err_set = 1;
  518. snprintf(_err, sizeof(_err), "DBus connection lost");
  519. _cs_dbus_release();
  520. goto out_unlock;
  521. }
  522. _cs_dbus_auto_flush();
  523. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  524. DBUS_CS_IFACE,
  525. "NodeStateChange"))) {
  526. qb_log(LOG_ERR, "error creating NodeStateChange signal");
  527. goto out_unlock;
  528. }
  529. if (!dbus_message_append_args(msg,
  530. DBUS_TYPE_STRING, &nodename,
  531. DBUS_TYPE_UINT32, &nodeid,
  532. DBUS_TYPE_STRING, &ip,
  533. DBUS_TYPE_STRING, &state,
  534. DBUS_TYPE_INVALID)) {
  535. qb_log(LOG_ERR, "error adding args to NodeStateChange signal");
  536. goto out_unlock;
  537. }
  538. dbus_connection_send(db, msg, NULL);
  539. out_unlock:
  540. if (msg) {
  541. dbus_message_unref(msg);
  542. }
  543. out_free:
  544. return;
  545. }
  546. static void
  547. _cs_dbus_application_connection_event(char *nodename, uint32_t nodeid, char *app_name, const char *state)
  548. {
  549. DBusMessage *msg = NULL;
  550. if (err_set) {
  551. qb_log(LOG_ERR, "%s", _err);
  552. err_set = 0;
  553. }
  554. if (!db) {
  555. goto out_free;
  556. }
  557. if (dbus_connection_get_is_connected(db) != TRUE) {
  558. err_set = 1;
  559. snprintf(_err, sizeof(_err), "DBus connection lost");
  560. _cs_dbus_release();
  561. goto out_unlock;
  562. }
  563. _cs_dbus_auto_flush();
  564. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  565. DBUS_CS_IFACE,
  566. "ConnectionStateChange"))) {
  567. qb_log(LOG_ERR, "error creating ConnectionStateChange signal");
  568. goto out_unlock;
  569. }
  570. if (!dbus_message_append_args(msg,
  571. DBUS_TYPE_STRING, &nodename,
  572. DBUS_TYPE_UINT32, &nodeid,
  573. DBUS_TYPE_STRING, &app_name,
  574. DBUS_TYPE_STRING, &state,
  575. DBUS_TYPE_INVALID)) {
  576. qb_log(LOG_ERR, "error adding args to ConnectionStateChange signal");
  577. goto out_unlock;
  578. }
  579. dbus_connection_send(db, msg, NULL);
  580. out_unlock:
  581. if (msg) {
  582. dbus_message_unref(msg);
  583. }
  584. out_free:
  585. return;
  586. }
  587. static void
  588. _cs_dbus_link_faulty_event(char *nodename, uint32_t local_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state)
  589. {
  590. DBusMessage *msg = NULL;
  591. if (err_set) {
  592. qb_log(LOG_ERR, "%s", _err);
  593. err_set = 0;
  594. }
  595. if (!db) {
  596. goto out_free;
  597. }
  598. if (dbus_connection_get_is_connected(db) != TRUE) {
  599. err_set = 1;
  600. snprintf(_err, sizeof(_err), "DBus connection lost");
  601. _cs_dbus_release();
  602. goto out_unlock;
  603. }
  604. _cs_dbus_auto_flush();
  605. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  606. DBUS_CS_IFACE,
  607. "QuorumStateChange"))) {
  608. qb_log(LOG_ERR, "error creating dbus signal");
  609. goto out_unlock;
  610. }
  611. if (!dbus_message_append_args(msg,
  612. DBUS_TYPE_STRING, &nodename,
  613. DBUS_TYPE_UINT32, &local_nodeid,
  614. DBUS_TYPE_UINT32, &nodeid,
  615. DBUS_TYPE_UINT32, &iface_no,
  616. DBUS_TYPE_STRING, &state,
  617. DBUS_TYPE_INVALID)) {
  618. qb_log(LOG_ERR, "error adding args to link signal");
  619. goto out_unlock;
  620. }
  621. dbus_connection_send(db, msg, NULL);
  622. out_unlock:
  623. if (msg) {
  624. dbus_message_unref(msg);
  625. }
  626. out_free:
  627. return;
  628. }
  629. static void
  630. _cs_dbus_init(void)
  631. {
  632. DBusConnection *dbc = NULL;
  633. DBusError err;
  634. dbus_error_init(&err);
  635. dbc = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
  636. if (!dbc) {
  637. snprintf(_err, sizeof(_err),
  638. "dbus_bus_get: %s", err.message);
  639. err_set = 1;
  640. dbus_error_free(&err);
  641. return;
  642. }
  643. dbus_connection_set_exit_on_disconnect(dbc, FALSE);
  644. db = dbc;
  645. notifiers[num_notifiers].node_membership_fn =
  646. _cs_dbus_node_membership_event;
  647. notifiers[num_notifiers].node_quorum_fn =
  648. _cs_dbus_node_quorum_event;
  649. notifiers[num_notifiers].application_connection_fn =
  650. _cs_dbus_application_connection_event;
  651. notifiers[num_notifiers].link_faulty_fn =
  652. _cs_dbus_link_faulty_event;
  653. num_notifiers++;
  654. }
  655. #endif /* HAVE_DBUS */
  656. #ifdef ENABLE_SNMP
  657. static netsnmp_session *_cs_snmp_session_init (const char *target)
  658. {
  659. static netsnmp_session *session = NULL;
  660. #ifndef NETSNMPV54
  661. char default_port[128];
  662. snprintf (default_port, sizeof (default_port), "%s:162", target);
  663. #endif
  664. if (session) {
  665. return (session);
  666. }
  667. if (target == NULL) {
  668. return NULL;
  669. }
  670. session = malloc (sizeof (netsnmp_session));
  671. snmp_sess_init (session);
  672. session->version = SNMP_VERSION_2c;
  673. session->callback = NULL;
  674. session->callback_magic = NULL;
  675. if (snmp_community) {
  676. session->community = (u_char *)snmp_community;
  677. session->community_len = strlen(snmp_community_buf);
  678. }
  679. session = snmp_add(session,
  680. #ifdef NETSNMPV54
  681. netsnmp_transport_open_client ("snmptrap", target),
  682. #else
  683. netsnmp_tdomain_transport (default_port, 0, "udp"),
  684. #endif
  685. NULL, NULL);
  686. if (session == NULL) {
  687. qb_log(LOG_ERR, 0, "Could not create snmp transport");
  688. }
  689. return (session);
  690. }
  691. static void _cs_snmp_add_field (
  692. netsnmp_pdu *trap_pdu,
  693. u_char asn_type,
  694. const char *prefix,
  695. void *value,
  696. size_t value_size)
  697. {
  698. oid _oid[MAX_OID_LEN];
  699. size_t _oid_len = MAX_OID_LEN;
  700. if (snmp_parse_oid(prefix, _oid, &_oid_len)) {
  701. snmp_pdu_add_variable (trap_pdu, _oid, _oid_len, asn_type, (u_char *) value, value_size);
  702. }
  703. }
  704. static netsnmp_pdu *_cs_snmp_trap_pdu_init (const char *trap_oid)
  705. {
  706. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  707. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  708. char csysuptime[CS_TIMESTAMP_STR_LEN];
  709. time_t now;
  710. struct tm *now_tm;
  711. netsnmp_pdu *trap_pdu;
  712. now = time (NULL);
  713. if (now == ((time_t)-1)) {
  714. qb_log(LOG_NOTICE, "Failed to get timestamp.");
  715. return (NULL);
  716. }
  717. /* format uptime */
  718. now_tm = localtime(&now);
  719. if (now_tm == NULL || strftime (csysuptime, sizeof(csysuptime), "%s", now_tm) == 0) {
  720. qb_log(LOG_NOTICE, "Failed to format timestamp.");
  721. return (NULL);
  722. }
  723. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  724. if (!trap_pdu) {
  725. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  726. return (NULL);
  727. }
  728. /* send uptime */
  729. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  730. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', trap_oid);
  731. return (trap_pdu);
  732. }
  733. static void
  734. _cs_snmp_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  735. {
  736. int ret;
  737. netsnmp_pdu *trap_pdu;
  738. netsnmp_session *session = _cs_snmp_session_init (snmp_manager);
  739. if (session == NULL) {
  740. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  741. return ;
  742. }
  743. trap_pdu = _cs_snmp_trap_pdu_init(SNMP_OID_TRAPS_NODE);
  744. if (trap_pdu == NULL) {
  745. return ;
  746. }
  747. /* Add extries to the trap */
  748. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  749. _cs_snmp_add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  750. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_ADDR, (void*)ip, strlen (ip));
  751. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_STATUS, (void*)state, strlen (state));
  752. /* Send and cleanup */
  753. ret = snmp_send (session, trap_pdu);
  754. if (ret == 0) {
  755. /* error */
  756. qb_log(LOG_ERR, "Could not send SNMP trap");
  757. snmp_free_pdu (trap_pdu);
  758. }
  759. }
  760. static void
  761. _cs_snmp_node_quorum_event(char *nodename, uint32_t nodeid,
  762. const char *state)
  763. {
  764. int ret;
  765. netsnmp_pdu *trap_pdu;
  766. netsnmp_session *session = _cs_snmp_session_init (snmp_manager);
  767. if (session == NULL) {
  768. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  769. return ;
  770. }
  771. trap_pdu = _cs_snmp_trap_pdu_init(SNMP_OID_TRAPS_QUORUM);
  772. if (trap_pdu == NULL) {
  773. return ;
  774. }
  775. /* Add extries to the trap */
  776. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  777. _cs_snmp_add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  778. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_QUORUM, (void*)state, strlen (state));
  779. /* Send and cleanup */
  780. ret = snmp_send (session, trap_pdu);
  781. if (ret == 0) {
  782. /* error */
  783. qb_log(LOG_ERR, "Could not send SNMP trap");
  784. snmp_free_pdu (trap_pdu);
  785. }
  786. }
  787. static void
  788. _cs_snmp_link_faulty_event(char *nodename, uint32_t local_nodeid, uint32_t nodeid,
  789. uint32_t iface_no, const char *state)
  790. {
  791. int ret;
  792. netsnmp_pdu *trap_pdu;
  793. netsnmp_session *session = _cs_snmp_session_init (snmp_manager);
  794. if (session == NULL) {
  795. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  796. return ;
  797. }
  798. trap_pdu = _cs_snmp_trap_pdu_init(SNMP_OID_TRAPS_LINK);
  799. if (trap_pdu == NULL) {
  800. return ;
  801. }
  802. /* Add extries to the trap */
  803. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  804. _cs_snmp_add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_LOCAL_NODE_ID, (void*)&local_nodeid, sizeof (local_nodeid));
  805. _cs_snmp_add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  806. _cs_snmp_add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_LINK_IFACE_NO, (void*)&iface_no, sizeof (iface_no));
  807. _cs_snmp_add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_LINK_STATUS, (void*)state, strlen (state));
  808. /* Send and cleanup */
  809. ret = snmp_send (session, trap_pdu);
  810. if (ret == 0) {
  811. /* error */
  812. qb_log(LOG_ERR, "Could not send SNMP trap");
  813. snmp_free_pdu (trap_pdu);
  814. }
  815. }
  816. static void
  817. _cs_snmp_init(void)
  818. {
  819. if (snmp_manager == NULL) {
  820. snmp_manager = (char*)local_host;
  821. }
  822. notifiers[num_notifiers].node_membership_fn =
  823. _cs_snmp_node_membership_event;
  824. notifiers[num_notifiers].node_quorum_fn =
  825. _cs_snmp_node_quorum_event;
  826. notifiers[num_notifiers].application_connection_fn = NULL;
  827. notifiers[num_notifiers].link_faulty_fn =
  828. _cs_snmp_link_faulty_event;
  829. num_notifiers++;
  830. }
  831. #endif /* ENABLE_SNMP */
  832. static void
  833. _cs_syslog_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  834. {
  835. qb_log(LOG_NOTICE, "%s[" CS_PRI_NODE_ID "] ip:%s %s", nodename, nodeid, ip, state);
  836. }
  837. static void
  838. _cs_syslog_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  839. {
  840. if (strcmp(state, "quorate") == 0) {
  841. qb_log(LOG_NOTICE, "%s[" CS_PRI_NODE_ID "] is now %s", nodename, nodeid, state);
  842. } else {
  843. qb_log(LOG_NOTICE, "%s[" CS_PRI_NODE_ID "] has lost quorum", nodename, nodeid);
  844. }
  845. }
  846. static void
  847. _cs_syslog_application_connection_event(char *nodename, uint32_t nodeid, char* app_name, const char *state)
  848. {
  849. if (strcmp(state, "connected") == 0) {
  850. qb_log(LOG_NOTICE, "%s[" CS_PRI_NODE_ID "] %s is now %s to corosync", nodename, nodeid, app_name, state);
  851. } else {
  852. qb_log(LOG_NOTICE, "%s[" CS_PRI_NODE_ID "] %s is now %s from corosync", nodename, nodeid, app_name, state);
  853. }
  854. }
  855. static void
  856. _cs_syslog_link_faulty_event(char *nodename, uint32_t our_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state)
  857. {
  858. qb_log(LOG_NOTICE, "%s[" CS_PRI_NODE_ID "] link %u to node " CS_PRI_NODE_ID " is now %s", nodename, our_nodeid, iface_no, nodeid, state);
  859. }
  860. static void
  861. _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  862. {
  863. int i;
  864. for (i = 0; i < num_notifiers; i++) {
  865. if (notifiers[i].node_membership_fn) {
  866. notifiers[i].node_membership_fn(nodename, nodeid, state, ip);
  867. }
  868. }
  869. }
  870. static void
  871. _cs_local_node_info_get(char **nodename, uint32_t *nodeid)
  872. {
  873. cs_error_t rc;
  874. corosync_cfg_handle_t cfg_handle;
  875. if (g_local_nodeid == 0) {
  876. rc = corosync_cfg_initialize(&cfg_handle, NULL);
  877. if (rc != CS_OK) {
  878. syslog (LOG_ERR, "Failed to initialize the cfg API. Error %d\n", rc);
  879. exit (EXIT_FAILURE);
  880. }
  881. rc = corosync_cfg_local_get (cfg_handle, &g_local_nodeid);
  882. corosync_cfg_finalize(cfg_handle);
  883. if (rc != CS_OK) {
  884. g_local_nodeid = 0;
  885. strncpy(local_nodename, "localhost", sizeof (local_nodename));
  886. local_nodename[sizeof (local_nodename) - 1] = '\0';
  887. } else {
  888. gethostname(local_nodename, CS_MAX_NAME_LENGTH);
  889. }
  890. }
  891. *nodeid = g_local_nodeid;
  892. *nodename = local_nodename;
  893. }
  894. static void
  895. _cs_node_quorum_event(const char *state)
  896. {
  897. int i;
  898. char *nodename;
  899. uint32_t nodeid;
  900. _cs_local_node_info_get(&nodename, &nodeid);
  901. for (i = 0; i < num_notifiers; i++) {
  902. if (notifiers[i].node_quorum_fn) {
  903. notifiers[i].node_quorum_fn(nodename, nodeid, state);
  904. }
  905. }
  906. }
  907. static void
  908. _cs_application_connection_event(char *app_name, const char *state)
  909. {
  910. int i;
  911. char *nodename;
  912. uint32_t nodeid;
  913. _cs_local_node_info_get(&nodename, &nodeid);
  914. for (i = 0; i < num_notifiers; i++) {
  915. if (notifiers[i].application_connection_fn) {
  916. notifiers[i].application_connection_fn(nodename, nodeid, app_name, state);
  917. }
  918. }
  919. }
  920. static void
  921. _cs_link_faulty_event(uint32_t nodeid, uint32_t iface_no, const char *state)
  922. {
  923. int i;
  924. char *nodename;
  925. uint32_t our_nodeid;
  926. _cs_local_node_info_get(&nodename, &our_nodeid);
  927. for (i = 0; i < num_notifiers; i++) {
  928. if (notifiers[i].link_faulty_fn) {
  929. notifiers[i].link_faulty_fn(nodename, our_nodeid, nodeid, iface_no, state);
  930. }
  931. }
  932. }
  933. static int32_t
  934. sig_exit_handler(int32_t num, void *data)
  935. {
  936. qb_loop_stop(main_loop);
  937. return 0;
  938. }
  939. static void track_link_updown_events(void)
  940. {
  941. cmap_iter_handle_t iter_handle;
  942. cmap_track_handle_t track_handle;
  943. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  944. size_t value_len;
  945. cmap_value_types_t type;
  946. cs_error_t err;
  947. struct track_item *track_item;
  948. err = cmap_iter_init(stats_handle, "stats.knet.", &iter_handle);
  949. if (err != CS_OK) {
  950. fprintf (stderr, "Failed to initialize knet stats iterator. Error %s\n", cs_strerror(err));
  951. exit (EXIT_FAILURE);
  952. }
  953. while ((err = cmap_iter_next(stats_handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  954. if (strstr(key_name, ".connected")) {
  955. track_item = malloc(sizeof(struct track_item));
  956. if (!track_item) {
  957. return;
  958. }
  959. if ((err = cmap_track_add(stats_handle, key_name, CMAP_TRACK_MODIFY, _cs_cmap_link_faulty_key_changed, NULL, &track_handle)) != CS_OK) {
  960. fprintf (stderr, "Failed to add tracker for %s. Error %s\n", key_name, cs_strerror(err));
  961. exit (EXIT_FAILURE);
  962. }
  963. strcpy(track_item->key_name, key_name);
  964. track_item->track_handle = track_handle;
  965. qb_map_put(tracker_map, track_item->key_name, track_item);
  966. }
  967. }
  968. cmap_iter_finalize(stats_handle, iter_handle);
  969. }
  970. static void
  971. _cs_cmap_init(void)
  972. {
  973. cs_error_t rc = CS_OK;
  974. int cmap_fd = 0;
  975. int stats_fd = 0;
  976. tracker_map = qb_trie_create();
  977. if (!tracker_map) {
  978. qb_log(LOG_ERR, "Failed to initialize the track map. Error %d", rc);
  979. exit (EXIT_FAILURE);
  980. }
  981. rc = cmap_initialize_map (&cmap_handle, CMAP_MAP_ICMAP);
  982. if (rc != CS_OK) {
  983. qb_log(LOG_ERR, "Failed to initialize the cmap API. Error %d", rc);
  984. exit (EXIT_FAILURE);
  985. }
  986. cmap_fd_get(cmap_handle, &cmap_fd);
  987. qb_loop_poll_add(main_loop, QB_LOOP_MED, cmap_fd, POLLIN|POLLNVAL, (void*)&cmap_handle,
  988. _cs_cmap_dispatch);
  989. rc = cmap_initialize_map (&stats_handle, CMAP_MAP_STATS);
  990. if (rc != CS_OK) {
  991. qb_log(LOG_ERR, "Failed to initialize the cmap stats API. Error %d", rc);
  992. exit (EXIT_FAILURE);
  993. }
  994. cmap_fd_get(stats_handle, &stats_fd);
  995. qb_loop_poll_add(main_loop, QB_LOOP_MED, stats_fd, POLLIN|POLLNVAL, (void*)&stats_handle,
  996. _cs_cmap_dispatch);
  997. rc = cmap_track_add(cmap_handle, "runtime.members.",
  998. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  999. _cs_cmap_members_key_changed,
  1000. NULL,
  1001. &cmap_track_handle_runtime_members_key_changed);
  1002. if (rc != CS_OK) {
  1003. qb_log(LOG_ERR,
  1004. "Failed to track the members key. Error %d", rc);
  1005. exit (EXIT_FAILURE);
  1006. }
  1007. rc = cmap_track_add(stats_handle, "stats.ipcs.",
  1008. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX,
  1009. _cs_cmap_connections_key_changed,
  1010. NULL,
  1011. &cmap_track_handle_stats_ipcs_key_changed);
  1012. if (rc != CS_OK) {
  1013. qb_log(LOG_ERR,
  1014. "Failed to track the connections key. Error %d", rc);
  1015. exit (EXIT_FAILURE);
  1016. }
  1017. rc = cmap_track_add(stats_handle, "stats.knet.",
  1018. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX,
  1019. _cs_cmap_link_added_removed,
  1020. NULL,
  1021. &cmap_track_handle_stats_knet_key_changed);
  1022. if (rc != CS_OK) {
  1023. qb_log(LOG_ERR,
  1024. "Failed to track the knet link status key. Error %d", rc);
  1025. exit (EXIT_FAILURE);
  1026. }
  1027. track_link_updown_events();
  1028. }
  1029. static void
  1030. _cs_cmap_finalize(void)
  1031. {
  1032. struct qb_map_iter *map_iter;
  1033. struct track_item *track_item;
  1034. map_iter = qb_map_iter_create(tracker_map);
  1035. while (qb_map_iter_next(map_iter, (void **)&track_item) != NULL) {
  1036. cmap_track_delete(stats_handle, track_item->track_handle);
  1037. free(track_item);
  1038. }
  1039. qb_map_iter_free(map_iter);
  1040. cmap_track_delete(cmap_handle, cmap_track_handle_runtime_members_key_changed);
  1041. cmap_track_delete(stats_handle, cmap_track_handle_stats_ipcs_key_changed);
  1042. cmap_track_delete(stats_handle, cmap_track_handle_stats_knet_key_changed);
  1043. cmap_finalize (cmap_handle);
  1044. cmap_finalize (stats_handle);
  1045. }
  1046. static void
  1047. _cs_check_config(void)
  1048. {
  1049. if (conf[CS_NTF_LOG] == QB_FALSE &&
  1050. conf[CS_NTF_STDOUT] == QB_FALSE &&
  1051. conf[CS_NTF_SNMP] == QB_FALSE &&
  1052. conf[CS_NTF_DBUS] == QB_FALSE) {
  1053. qb_log(LOG_ERR, "no event type enabled, see corosync-notifyd -h, exiting.");
  1054. exit(EXIT_FAILURE);
  1055. }
  1056. #ifndef ENABLE_SNMP
  1057. if (conf[CS_NTF_SNMP]) {
  1058. qb_log(LOG_ERR, "Not compiled with SNMP support enabled, exiting.");
  1059. exit(EXIT_FAILURE);
  1060. }
  1061. #endif
  1062. #ifndef HAVE_DBUS
  1063. if (conf[CS_NTF_DBUS]) {
  1064. qb_log(LOG_ERR, "Not compiled with DBus support enabled, exiting.");
  1065. exit(EXIT_FAILURE);
  1066. }
  1067. #endif
  1068. if (conf[CS_NTF_STDOUT] && !conf[CS_NTF_FG]) {
  1069. qb_log(LOG_ERR, "configured to print to stdout and run in the background, exiting");
  1070. exit(EXIT_FAILURE);
  1071. }
  1072. if (conf[CS_NTF_SNMP] && conf[CS_NTF_DBUS]) {
  1073. qb_log(LOG_ERR, "configured to send snmp traps and dbus signals - are you sure?.");
  1074. }
  1075. }
  1076. static void
  1077. _cs_usage(void)
  1078. {
  1079. fprintf(stderr, "usage:\n"\
  1080. " -c : SNMP Community name.\n"\
  1081. " -f : Start application in foreground.\n"\
  1082. " -l : Log all events.\n"\
  1083. " -o : Print events to stdout (turns on -l).\n"\
  1084. " -s : Send SNMP traps on all events.\n"\
  1085. " -m : Set the SNMP Manager IP address (defaults to localhost).\n"\
  1086. " -n : No reverse DNS lookup on cmap member change events.\n"\
  1087. " -d : Send DBUS signals on all events.\n"\
  1088. " -h : Print this help.\n\n");
  1089. }
  1090. int
  1091. main(int argc, char *argv[])
  1092. {
  1093. int ch;
  1094. conf[CS_NTF_FG] = QB_FALSE;
  1095. conf[CS_NTF_LOG] = QB_FALSE;
  1096. conf[CS_NTF_STDOUT] = QB_FALSE;
  1097. conf[CS_NTF_SNMP] = QB_FALSE;
  1098. conf[CS_NTF_DBUS] = QB_FALSE;
  1099. while ((ch = getopt (argc, argv, "c:floshdnm:")) != EOF) {
  1100. switch (ch) {
  1101. case 'c':
  1102. strncpy(snmp_community_buf, optarg, sizeof (snmp_community_buf));
  1103. snmp_community_buf[sizeof (snmp_community_buf) - 1] = '\0';
  1104. snmp_community = snmp_community_buf;
  1105. break;
  1106. case 'f':
  1107. conf[CS_NTF_FG] = QB_TRUE;
  1108. break;
  1109. case 'l':
  1110. conf[CS_NTF_LOG] = QB_TRUE;
  1111. break;
  1112. case 'm':
  1113. conf[CS_NTF_SNMP] = QB_TRUE;
  1114. strncpy(snmp_manager_buf, optarg, sizeof (snmp_manager_buf));
  1115. snmp_manager_buf[sizeof (snmp_manager_buf) - 1] = '\0';
  1116. snmp_manager = snmp_manager_buf;
  1117. break;
  1118. case 'n':
  1119. conf[CS_NTF_NODNS] = QB_TRUE;
  1120. break;
  1121. case 'o':
  1122. conf[CS_NTF_LOG] = QB_TRUE;
  1123. conf[CS_NTF_STDOUT] = QB_TRUE;
  1124. break;
  1125. case 's':
  1126. conf[CS_NTF_SNMP] = QB_TRUE;
  1127. break;
  1128. case 'd':
  1129. conf[CS_NTF_DBUS] = QB_TRUE;
  1130. break;
  1131. case 'h':
  1132. default:
  1133. _cs_usage();
  1134. return EXIT_FAILURE;
  1135. }
  1136. }
  1137. qb_log_init("notifyd", LOG_DAEMON, LOG_INFO);
  1138. if (conf[CS_NTF_STDOUT]) {
  1139. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  1140. QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
  1141. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, conf[CS_NTF_STDOUT]);
  1142. }
  1143. _cs_check_config();
  1144. if (!conf[CS_NTF_FG]) {
  1145. if (daemon(0, 0) < 0)
  1146. {
  1147. perror("daemon() failed");
  1148. return EXIT_FAILURE;
  1149. }
  1150. }
  1151. num_notifiers = 0;
  1152. if (conf[CS_NTF_LOG]) {
  1153. notifiers[num_notifiers].node_membership_fn =
  1154. _cs_syslog_node_membership_event;
  1155. notifiers[num_notifiers].node_quorum_fn =
  1156. _cs_syslog_node_quorum_event;
  1157. notifiers[num_notifiers].application_connection_fn =
  1158. _cs_syslog_application_connection_event;
  1159. notifiers[num_notifiers].link_faulty_fn =
  1160. _cs_syslog_link_faulty_event;
  1161. num_notifiers++;
  1162. }
  1163. main_loop = qb_loop_create();
  1164. _cs_cmap_init();
  1165. _cs_quorum_init();
  1166. #ifdef HAVE_DBUS
  1167. if (conf[CS_NTF_DBUS]) {
  1168. _cs_dbus_init();
  1169. }
  1170. #endif /* HAVE_DBUS */
  1171. #ifdef ENABLE_SNMP
  1172. if (conf[CS_NTF_SNMP]) {
  1173. _cs_snmp_init();
  1174. }
  1175. #endif /* ENABLE_SNMP */
  1176. qb_loop_signal_add(main_loop,
  1177. QB_LOOP_HIGH,
  1178. SIGINT,
  1179. NULL,
  1180. sig_exit_handler,
  1181. NULL);
  1182. qb_loop_signal_add(main_loop,
  1183. QB_LOOP_HIGH,
  1184. SIGQUIT,
  1185. NULL,
  1186. sig_exit_handler,
  1187. NULL);
  1188. qb_loop_signal_add(main_loop,
  1189. QB_LOOP_HIGH,
  1190. SIGTERM,
  1191. NULL,
  1192. sig_exit_handler,
  1193. NULL);
  1194. #ifdef HAVE_LIBSYSTEMD
  1195. sd_notify (0, "READY=1");
  1196. #endif
  1197. qb_loop_run(main_loop);
  1198. #ifdef HAVE_DBUS
  1199. if (conf[CS_NTF_DBUS]) {
  1200. _cs_dbus_release();
  1201. }
  1202. #endif /* HAVE_DBUS */
  1203. _cs_quorum_finalize();
  1204. _cs_cmap_finalize();
  1205. return (exit_code);
  1206. }