corosync-notifyd.c 26 KB

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