corosync-notifyd.c 26 KB

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