corosync-notifyd.c 33 KB

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