corosync-notifyd.c 27 KB

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