corosync-notifyd.c 33 KB

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