corosync-notifyd.c 27 KB

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