corosync-notifyd.c 27 KB

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