corosync-notifyd.c 26 KB

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