corosync-notifyd.c 27 KB

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