corosync-notifyd.c 31 KB

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