corosync-notifyd.c 29 KB

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