corosync-notifyd.c 35 KB

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