corosync-notifyd.c 34 KB

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