corosync-notifyd.c 26 KB

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