corosync-notifyd.c 25 KB

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