4
0

corosync-notifyd.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Copyright (c) 2011-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/select.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/types.h>
  39. #include <netdb.h>
  40. #include <arpa/inet.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <errno.h>
  44. #include <unistd.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <poll.h>
  48. #include <signal.h>
  49. #include <qb/qbdefs.h>
  50. #include <qb/qbloop.h>
  51. #include <qb/qblog.h>
  52. #include <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. uint32_t quorum_type;
  308. int fd;
  309. quorum_callbacks_t quorum_callbacks = {
  310. .quorum_notify_fn = _cs_quorum_notification,
  311. };
  312. rc = quorum_initialize (&quorum_handle, &quorum_callbacks,
  313. &quorum_type);
  314. if (rc != CS_OK) {
  315. qb_log(LOG_ERR, "Could not connect to corosync(quorum)");
  316. return;
  317. }
  318. quorum_fd_get(quorum_handle, &fd);
  319. qb_loop_poll_add(main_loop, QB_LOOP_MED, fd, POLLIN|POLLNVAL, NULL,
  320. _cs_quorum_dispatch);
  321. rc = quorum_trackstart(quorum_handle, CS_TRACK_CHANGES);
  322. if (rc != CS_OK) {
  323. qb_log(LOG_ERR, "Could not start tracking");
  324. return;
  325. }
  326. }
  327. static void
  328. _cs_quorum_finalize(void)
  329. {
  330. quorum_finalize (quorum_handle);
  331. }
  332. #ifdef HAVE_DBUS
  333. /*
  334. * dbus notifications
  335. */
  336. static void
  337. _cs_dbus_auto_flush(void)
  338. {
  339. dbus_connection_ref(db);
  340. dbus_connection_read_write(db, 500);
  341. dbus_connection_unref(db);
  342. }
  343. static void
  344. _cs_dbus_release(void)
  345. {
  346. DBusError err;
  347. if (!db)
  348. return;
  349. dbus_error_init(&err);
  350. dbus_bus_release_name(db, DBUS_CS_NAME, &err);
  351. dbus_error_free(&err);
  352. dbus_connection_unref(db);
  353. db = NULL;
  354. }
  355. static void
  356. _cs_dbus_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  357. {
  358. DBusMessage *msg = NULL;
  359. if (err_set) {
  360. qb_log(LOG_ERR, "%s", _err);
  361. err_set = 0;
  362. }
  363. if (!db) {
  364. goto out_free;
  365. }
  366. if (dbus_connection_get_is_connected(db) != TRUE) {
  367. err_set = 1;
  368. snprintf(_err, sizeof(_err), "DBus connection lost");
  369. _cs_dbus_release();
  370. goto out_unlock;
  371. }
  372. _cs_dbus_auto_flush();
  373. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  374. DBUS_CS_IFACE,
  375. "QuorumStateChange"))) {
  376. qb_log(LOG_ERR, "error creating dbus signal");
  377. goto out_unlock;
  378. }
  379. if (!dbus_message_append_args(msg,
  380. DBUS_TYPE_STRING, &nodename,
  381. DBUS_TYPE_UINT32, &nodeid,
  382. DBUS_TYPE_STRING, &state,
  383. DBUS_TYPE_INVALID)) {
  384. qb_log(LOG_ERR, "error adding args to quorum signal");
  385. goto out_unlock;
  386. }
  387. dbus_connection_send(db, msg, NULL);
  388. out_unlock:
  389. if (msg) {
  390. dbus_message_unref(msg);
  391. }
  392. out_free:
  393. return;
  394. }
  395. static void
  396. _cs_dbus_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  397. {
  398. DBusMessage *msg = NULL;
  399. if (err_set) {
  400. qb_log(LOG_ERR, "%s", _err);
  401. err_set = 0;
  402. }
  403. if (!db) {
  404. goto out_free;
  405. }
  406. if (dbus_connection_get_is_connected(db) != TRUE) {
  407. err_set = 1;
  408. snprintf(_err, sizeof(_err), "DBus connection lost");
  409. _cs_dbus_release();
  410. goto out_unlock;
  411. }
  412. _cs_dbus_auto_flush();
  413. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  414. DBUS_CS_IFACE,
  415. "NodeStateChange"))) {
  416. qb_log(LOG_ERR, "error creating NodeStateChange signal");
  417. goto out_unlock;
  418. }
  419. if (!dbus_message_append_args(msg,
  420. DBUS_TYPE_STRING, &nodename,
  421. DBUS_TYPE_UINT32, &nodeid,
  422. DBUS_TYPE_STRING, &ip,
  423. DBUS_TYPE_STRING, &state,
  424. DBUS_TYPE_INVALID)) {
  425. qb_log(LOG_ERR, "error adding args to NodeStateChange signal");
  426. goto out_unlock;
  427. }
  428. dbus_connection_send(db, msg, NULL);
  429. out_unlock:
  430. if (msg) {
  431. dbus_message_unref(msg);
  432. }
  433. out_free:
  434. return;
  435. }
  436. static void
  437. _cs_dbus_application_connection_event(char *nodename, uint32_t nodeid, char *app_name, const char *state)
  438. {
  439. DBusMessage *msg = NULL;
  440. if (err_set) {
  441. qb_log(LOG_ERR, "%s", _err);
  442. err_set = 0;
  443. }
  444. if (!db) {
  445. goto out_free;
  446. }
  447. if (dbus_connection_get_is_connected(db) != TRUE) {
  448. err_set = 1;
  449. snprintf(_err, sizeof(_err), "DBus connection lost");
  450. _cs_dbus_release();
  451. goto out_unlock;
  452. }
  453. _cs_dbus_auto_flush();
  454. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  455. DBUS_CS_IFACE,
  456. "ConnectionStateChange"))) {
  457. qb_log(LOG_ERR, "error creating ConnectionStateChange signal");
  458. goto out_unlock;
  459. }
  460. if (!dbus_message_append_args(msg,
  461. DBUS_TYPE_STRING, &nodename,
  462. DBUS_TYPE_UINT32, &nodeid,
  463. DBUS_TYPE_STRING, &app_name,
  464. DBUS_TYPE_STRING, &state,
  465. DBUS_TYPE_INVALID)) {
  466. qb_log(LOG_ERR, "error adding args to ConnectionStateChange signal");
  467. goto out_unlock;
  468. }
  469. dbus_connection_send(db, msg, NULL);
  470. out_unlock:
  471. if (msg) {
  472. dbus_message_unref(msg);
  473. }
  474. out_free:
  475. return;
  476. }
  477. static void
  478. _cs_dbus_rrp_faulty_event(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state)
  479. {
  480. DBusMessage *msg = NULL;
  481. if (err_set) {
  482. qb_log(LOG_ERR, "%s", _err);
  483. err_set = 0;
  484. }
  485. if (!db) {
  486. goto out_free;
  487. }
  488. if (dbus_connection_get_is_connected(db) != TRUE) {
  489. err_set = 1;
  490. snprintf(_err, sizeof(_err), "DBus connection lost");
  491. _cs_dbus_release();
  492. goto out_unlock;
  493. }
  494. _cs_dbus_auto_flush();
  495. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  496. DBUS_CS_IFACE,
  497. "QuorumStateChange"))) {
  498. qb_log(LOG_ERR, "error creating dbus signal");
  499. goto out_unlock;
  500. }
  501. if (!dbus_message_append_args(msg,
  502. DBUS_TYPE_STRING, &nodename,
  503. DBUS_TYPE_UINT32, &nodeid,
  504. DBUS_TYPE_UINT32, &iface_no,
  505. DBUS_TYPE_STRING, &state,
  506. DBUS_TYPE_INVALID)) {
  507. qb_log(LOG_ERR, "error adding args to rrp signal");
  508. goto out_unlock;
  509. }
  510. dbus_connection_send(db, msg, NULL);
  511. out_unlock:
  512. if (msg) {
  513. dbus_message_unref(msg);
  514. }
  515. out_free:
  516. return;
  517. }
  518. static void
  519. _cs_dbus_init(void)
  520. {
  521. DBusConnection *dbc = NULL;
  522. DBusError err;
  523. dbus_error_init(&err);
  524. dbc = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
  525. if (!dbc) {
  526. snprintf(_err, sizeof(_err),
  527. "dbus_bus_get: %s", err.message);
  528. err_set = 1;
  529. dbus_error_free(&err);
  530. return;
  531. }
  532. dbus_connection_set_exit_on_disconnect(dbc, FALSE);
  533. db = dbc;
  534. notifiers[num_notifiers].node_membership_fn =
  535. _cs_dbus_node_membership_event;
  536. notifiers[num_notifiers].node_quorum_fn =
  537. _cs_dbus_node_quorum_event;
  538. notifiers[num_notifiers].application_connection_fn =
  539. _cs_dbus_application_connection_event;
  540. notifiers[num_notifiers].rrp_faulty_fn =
  541. _cs_dbus_rrp_faulty_event;
  542. num_notifiers++;
  543. }
  544. #endif /* HAVE_DBUS */
  545. #ifdef ENABLE_SNMP
  546. static netsnmp_session *snmp_init (const char *target)
  547. {
  548. static netsnmp_session *session = NULL;
  549. #ifndef NETSNMPV54
  550. char default_port[128];
  551. snprintf (default_port, sizeof (default_port), "%s:162", target);
  552. #endif
  553. if (session) {
  554. return (session);
  555. }
  556. if (target == NULL) {
  557. return NULL;
  558. }
  559. session = malloc (sizeof (netsnmp_session));
  560. snmp_sess_init (session);
  561. session->version = SNMP_VERSION_2c;
  562. session->callback = NULL;
  563. session->callback_magic = NULL;
  564. session = snmp_add(session,
  565. #ifdef NETSNMPV54
  566. netsnmp_transport_open_client ("snmptrap", target),
  567. #else
  568. netsnmp_tdomain_transport (default_port, 0, "udp"),
  569. #endif
  570. NULL, NULL);
  571. if (session == NULL) {
  572. qb_log(LOG_ERR, 0, "Could not create snmp transport");
  573. }
  574. return (session);
  575. }
  576. static inline void add_field (
  577. netsnmp_pdu *trap_pdu,
  578. u_char asn_type,
  579. const char *prefix,
  580. void *value,
  581. size_t value_size)
  582. {
  583. oid _oid[MAX_OID_LEN];
  584. size_t _oid_len = MAX_OID_LEN;
  585. if (snmp_parse_oid(prefix, _oid, &_oid_len)) {
  586. snmp_pdu_add_variable (trap_pdu, _oid, _oid_len, asn_type, (u_char *) value, value_size);
  587. }
  588. }
  589. static void
  590. _cs_snmp_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  591. {
  592. int ret;
  593. char csysuptime[CS_TIMESTAMP_STR_LEN];
  594. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  595. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  596. time_t now = time (NULL);
  597. netsnmp_pdu *trap_pdu;
  598. netsnmp_session *session = snmp_init (snmp_manager);
  599. if (session == NULL) {
  600. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  601. return ;
  602. }
  603. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  604. if (!trap_pdu) {
  605. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  606. return ;
  607. }
  608. /* send uptime */
  609. snprintf (csysuptime, CS_TIMESTAMP_STR_LEN, "%ld", now);
  610. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  611. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_NODE);
  612. /* Add extries to the trap */
  613. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  614. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  615. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_ADDR, (void*)ip, strlen (ip));
  616. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_STATUS, (void*)state, strlen (state));
  617. /* Send and cleanup */
  618. ret = snmp_send (session, trap_pdu);
  619. if (ret == 0) {
  620. /* error */
  621. qb_log(LOG_ERR, "Could not send SNMP trap");
  622. snmp_free_pdu (trap_pdu);
  623. }
  624. }
  625. static void
  626. _cs_snmp_node_quorum_event(char *nodename, uint32_t nodeid,
  627. const char *state)
  628. {
  629. int ret;
  630. char csysuptime[20];
  631. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  632. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  633. time_t now = time (NULL);
  634. netsnmp_pdu *trap_pdu;
  635. netsnmp_session *session = snmp_init (snmp_manager);
  636. if (session == NULL) {
  637. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  638. return ;
  639. }
  640. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  641. if (!trap_pdu) {
  642. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  643. return ;
  644. }
  645. /* send uptime */
  646. sprintf (csysuptime, "%ld", now);
  647. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  648. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_QUORUM);
  649. /* Add extries to the trap */
  650. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  651. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  652. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_QUORUM, (void*)state, strlen (state));
  653. /* Send and cleanup */
  654. ret = snmp_send (session, trap_pdu);
  655. if (ret == 0) {
  656. /* error */
  657. qb_log(LOG_ERR, "Could not send SNMP trap");
  658. snmp_free_pdu (trap_pdu);
  659. }
  660. }
  661. static void
  662. _cs_snmp_rrp_faulty_event(char *nodename, uint32_t nodeid,
  663. uint32_t iface_no, const char *state)
  664. {
  665. int ret;
  666. char csysuptime[20];
  667. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  668. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  669. time_t now = time (NULL);
  670. netsnmp_pdu *trap_pdu;
  671. netsnmp_session *session = snmp_init (snmp_manager);
  672. if (session == NULL) {
  673. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  674. return ;
  675. }
  676. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  677. if (!trap_pdu) {
  678. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  679. return ;
  680. }
  681. /* send uptime */
  682. sprintf (csysuptime, "%ld", now);
  683. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  684. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_RRP);
  685. /* Add extries to the trap */
  686. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  687. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  688. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_RRP_IFACE_NO, (void*)&iface_no, sizeof (iface_no));
  689. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_RRP_STATUS, (void*)state, strlen (state));
  690. /* Send and cleanup */
  691. ret = snmp_send (session, trap_pdu);
  692. if (ret == 0) {
  693. /* error */
  694. qb_log(LOG_ERR, "Could not send SNMP trap");
  695. snmp_free_pdu (trap_pdu);
  696. }
  697. }
  698. static void
  699. _cs_snmp_init(void)
  700. {
  701. if (snmp_manager == NULL) {
  702. snmp_manager = (char*)local_host;
  703. }
  704. notifiers[num_notifiers].node_membership_fn =
  705. _cs_snmp_node_membership_event;
  706. notifiers[num_notifiers].node_quorum_fn =
  707. _cs_snmp_node_quorum_event;
  708. notifiers[num_notifiers].application_connection_fn = NULL;
  709. notifiers[num_notifiers].rrp_faulty_fn =
  710. _cs_snmp_rrp_faulty_event;
  711. num_notifiers++;
  712. }
  713. #endif /* ENABLE_SNMP */
  714. static void
  715. _cs_syslog_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  716. {
  717. qb_log(LOG_NOTICE, "%s[%d] ip:%s %s", nodename, nodeid, ip, state);
  718. }
  719. static void
  720. _cs_syslog_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  721. {
  722. if (strcmp(state, "quorate") == 0) {
  723. qb_log(LOG_NOTICE, "%s[%d] is now %s", nodename, nodeid, state);
  724. } else {
  725. qb_log(LOG_NOTICE, "%s[%d] has lost quorum", nodename, nodeid);
  726. }
  727. }
  728. static void
  729. _cs_syslog_application_connection_event(char *nodename, uint32_t nodeid, char* app_name, const char *state)
  730. {
  731. if (strcmp(state, "connected") == 0) {
  732. qb_log(LOG_NOTICE, "%s[%d] %s is now %s to corosync", nodename, nodeid, app_name, state);
  733. } else {
  734. qb_log(LOG_NOTICE, "%s[%d] %s is now %s from corosync", nodename, nodeid, app_name, state);
  735. }
  736. }
  737. static void
  738. _cs_syslog_rrp_faulty_event(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state)
  739. {
  740. qb_log(LOG_NOTICE, "%s[%d] interface %u is now %s", nodename, nodeid, iface_no, state);
  741. }
  742. static void
  743. _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  744. {
  745. int i;
  746. for (i = 0; i < num_notifiers; i++) {
  747. if (notifiers[i].node_membership_fn) {
  748. notifiers[i].node_membership_fn(nodename, nodeid, state, ip);
  749. }
  750. }
  751. }
  752. static void
  753. _cs_local_node_info_get(char **nodename, uint32_t *nodeid)
  754. {
  755. cs_error_t rc;
  756. corosync_cfg_handle_t cfg_handle;
  757. if (local_nodeid == 0) {
  758. rc = corosync_cfg_initialize(&cfg_handle, NULL);
  759. if (rc != CS_OK) {
  760. syslog (LOG_ERR, "Failed to initialize the cfg API. Error %d\n", rc);
  761. exit (EXIT_FAILURE);
  762. }
  763. rc = corosync_cfg_local_get (cfg_handle, &local_nodeid);
  764. corosync_cfg_finalize(cfg_handle);
  765. if (rc != CS_OK) {
  766. local_nodeid = 0;
  767. strncpy(local_nodename, "localhost", sizeof (local_nodename));
  768. local_nodename[sizeof (local_nodename) - 1] = '\0';
  769. } else {
  770. gethostname(local_nodename, CS_MAX_NAME_LENGTH);
  771. }
  772. }
  773. *nodeid = local_nodeid;
  774. *nodename = local_nodename;
  775. }
  776. static void
  777. _cs_node_quorum_event(const char *state)
  778. {
  779. int i;
  780. char *nodename;
  781. uint32_t nodeid;
  782. _cs_local_node_info_get(&nodename, &nodeid);
  783. for (i = 0; i < num_notifiers; i++) {
  784. if (notifiers[i].node_quorum_fn) {
  785. notifiers[i].node_quorum_fn(nodename, nodeid, state);
  786. }
  787. }
  788. }
  789. static void
  790. _cs_application_connection_event(char *app_name, const char *state)
  791. {
  792. int i;
  793. char *nodename;
  794. uint32_t nodeid;
  795. _cs_local_node_info_get(&nodename, &nodeid);
  796. for (i = 0; i < num_notifiers; i++) {
  797. if (notifiers[i].application_connection_fn) {
  798. notifiers[i].application_connection_fn(nodename, nodeid, app_name, state);
  799. }
  800. }
  801. }
  802. static void
  803. _cs_rrp_faulty_event(uint32_t iface_no, const char *state)
  804. {
  805. int i;
  806. char *nodename;
  807. uint32_t nodeid;
  808. _cs_local_node_info_get(&nodename, &nodeid);
  809. for (i = 0; i < num_notifiers; i++) {
  810. if (notifiers[i].application_connection_fn) {
  811. notifiers[i].rrp_faulty_fn(nodename, nodeid, iface_no, state);
  812. }
  813. }
  814. }
  815. static int32_t
  816. sig_exit_handler(int32_t num, void *data)
  817. {
  818. qb_loop_stop(main_loop);
  819. return 0;
  820. }
  821. static void
  822. _cs_cmap_init(void)
  823. {
  824. cs_error_t rc;
  825. int cmap_fd = 0;
  826. cmap_track_handle_t track_handle;
  827. rc = cmap_initialize (&cmap_handle);
  828. if (rc != CS_OK) {
  829. qb_log(LOG_ERR, "Failed to initialize the cmap API. Error %d", rc);
  830. exit (EXIT_FAILURE);
  831. }
  832. cmap_fd_get(cmap_handle, &cmap_fd);
  833. qb_loop_poll_add(main_loop, QB_LOOP_MED, cmap_fd, POLLIN|POLLNVAL, NULL,
  834. _cs_cmap_dispatch);
  835. rc = cmap_track_add(cmap_handle, "runtime.connections.",
  836. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX,
  837. _cs_cmap_connections_key_changed,
  838. NULL,
  839. &track_handle);
  840. if (rc != CS_OK) {
  841. qb_log(LOG_ERR,
  842. "Failed to track the connections key. Error %d", rc);
  843. exit (EXIT_FAILURE);
  844. }
  845. rc = cmap_track_add(cmap_handle, "runtime.totem.pg.mrp.srp.members.",
  846. CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  847. _cs_cmap_members_key_changed,
  848. NULL,
  849. &track_handle);
  850. if (rc != CS_OK) {
  851. qb_log(LOG_ERR,
  852. "Failed to track the members key. Error %d", rc);
  853. exit (EXIT_FAILURE);
  854. }
  855. rc = cmap_track_add(cmap_handle, "runtime.totem.pg.mrp.rrp.",
  856. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  857. _cs_cmap_rrp_faulty_key_changed,
  858. NULL,
  859. &track_handle);
  860. if (rc != CS_OK) {
  861. qb_log(LOG_ERR,
  862. "Failed to track the rrp key. Error %d", rc);
  863. exit (EXIT_FAILURE);
  864. }
  865. }
  866. static void
  867. _cs_cmap_finalize(void)
  868. {
  869. cmap_finalize (cmap_handle);
  870. }
  871. static void
  872. _cs_check_config(void)
  873. {
  874. if (conf[CS_NTF_LOG] == QB_FALSE &&
  875. conf[CS_NTF_STDOUT] == QB_FALSE &&
  876. conf[CS_NTF_SNMP] == QB_FALSE &&
  877. conf[CS_NTF_DBUS] == QB_FALSE) {
  878. qb_log(LOG_ERR, "no event type enabled, see corosync-notifyd -h, exiting.");
  879. exit(EXIT_FAILURE);
  880. }
  881. #ifndef ENABLE_SNMP
  882. if (conf[CS_NTF_SNMP]) {
  883. qb_log(LOG_ERR, "Not compiled with SNMP support enabled, exiting.");
  884. exit(EXIT_FAILURE);
  885. }
  886. #endif
  887. #ifndef HAVE_DBUS
  888. if (conf[CS_NTF_DBUS]) {
  889. qb_log(LOG_ERR, "Not compiled with DBus support enabled, exiting.");
  890. exit(EXIT_FAILURE);
  891. }
  892. #endif
  893. if (conf[CS_NTF_STDOUT] && !conf[CS_NTF_FG]) {
  894. qb_log(LOG_ERR, "configured to print to stdout and run in the background, exiting");
  895. exit(EXIT_FAILURE);
  896. }
  897. if (conf[CS_NTF_SNMP] && conf[CS_NTF_DBUS]) {
  898. qb_log(LOG_ERR, "configured to send snmp traps and dbus signals - are you sure?.");
  899. }
  900. }
  901. static void
  902. _cs_usage(void)
  903. {
  904. fprintf(stderr, "usage:\n"\
  905. " -f : Start application in foreground.\n"\
  906. " -l : Log all events.\n"\
  907. " -o : Print events to stdout (turns on -l).\n"\
  908. " -s : Send SNMP traps on all events.\n"\
  909. " -m : SNMP Manager IP address (defaults to localhost).\n"\
  910. " -d : Send DBUS signals on all events.\n"\
  911. " -h : Print this help\n\n");
  912. }
  913. int
  914. main(int argc, char *argv[])
  915. {
  916. int ch;
  917. conf[CS_NTF_FG] = QB_FALSE;
  918. conf[CS_NTF_LOG] = QB_FALSE;
  919. conf[CS_NTF_STDOUT] = QB_FALSE;
  920. conf[CS_NTF_SNMP] = QB_FALSE;
  921. conf[CS_NTF_DBUS] = QB_FALSE;
  922. while ((ch = getopt (argc, argv, "floshdm:")) != EOF) {
  923. switch (ch) {
  924. case 'f':
  925. conf[CS_NTF_FG] = QB_TRUE;
  926. break;
  927. case 'l':
  928. conf[CS_NTF_LOG] = QB_TRUE;
  929. break;
  930. case 'm':
  931. conf[CS_NTF_SNMP] = QB_TRUE;
  932. strncpy(snmp_manager_buf, optarg, sizeof (snmp_manager_buf));
  933. snmp_manager_buf[sizeof (snmp_manager_buf) - 1] = '\0';
  934. snmp_manager = snmp_manager_buf;
  935. break;
  936. case 'o':
  937. conf[CS_NTF_LOG] = QB_TRUE;
  938. conf[CS_NTF_STDOUT] = QB_TRUE;
  939. break;
  940. case 's':
  941. conf[CS_NTF_SNMP] = QB_TRUE;
  942. break;
  943. case 'd':
  944. conf[CS_NTF_DBUS] = QB_TRUE;
  945. break;
  946. case 'h':
  947. default:
  948. _cs_usage();
  949. return EXIT_FAILURE;
  950. }
  951. }
  952. qb_log_init("notifyd", LOG_DAEMON, LOG_INFO);
  953. if (conf[CS_NTF_STDOUT]) {
  954. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  955. QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
  956. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, conf[CS_NTF_STDOUT]);
  957. }
  958. _cs_check_config();
  959. if (!conf[CS_NTF_FG]) {
  960. if (daemon(0, 0) < 0)
  961. {
  962. perror("daemon() failed");
  963. return EXIT_FAILURE;
  964. }
  965. }
  966. num_notifiers = 0;
  967. if (conf[CS_NTF_LOG]) {
  968. notifiers[num_notifiers].node_membership_fn =
  969. _cs_syslog_node_membership_event;
  970. notifiers[num_notifiers].node_quorum_fn =
  971. _cs_syslog_node_quorum_event;
  972. notifiers[num_notifiers].application_connection_fn =
  973. _cs_syslog_application_connection_event;
  974. notifiers[num_notifiers].rrp_faulty_fn =
  975. _cs_syslog_rrp_faulty_event;
  976. num_notifiers++;
  977. }
  978. main_loop = qb_loop_create();
  979. _cs_cmap_init();
  980. _cs_quorum_init();
  981. #ifdef HAVE_DBUS
  982. if (conf[CS_NTF_DBUS]) {
  983. _cs_dbus_init();
  984. }
  985. #endif /* HAVE_DBUS */
  986. #ifdef ENABLE_SNMP
  987. if (conf[CS_NTF_SNMP]) {
  988. _cs_snmp_init();
  989. }
  990. #endif /* ENABLE_SNMP */
  991. qb_loop_signal_add(main_loop,
  992. QB_LOOP_HIGH,
  993. SIGINT,
  994. NULL,
  995. sig_exit_handler,
  996. NULL);
  997. qb_loop_signal_add(main_loop,
  998. QB_LOOP_HIGH,
  999. SIGQUIT,
  1000. NULL,
  1001. sig_exit_handler,
  1002. NULL);
  1003. qb_loop_signal_add(main_loop,
  1004. QB_LOOP_HIGH,
  1005. SIGTERM,
  1006. NULL,
  1007. sig_exit_handler,
  1008. NULL);
  1009. qb_loop_run(main_loop);
  1010. #ifdef HAVE_DBUS
  1011. if (conf[CS_NTF_DBUS]) {
  1012. _cs_dbus_release();
  1013. }
  1014. #endif /* HAVE_DBUS */
  1015. _cs_quorum_finalize();
  1016. _cs_cmap_finalize();
  1017. return 0;
  1018. }