corosync-notifyd.c 26 KB

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