corosync-notifyd.c 29 KB

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