corosync-notifyd.c 29 KB

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