corosync-notifyd.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /*
  2. * Copyright (c) 2011-2017 Red Hat, Inc.
  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/qbmap.h>
  52. #include <qb/qblog.h>
  53. #include <corosync/corotypes.h>
  54. #include <corosync/cfg.h>
  55. #include <corosync/quorum.h>
  56. #include <corosync/cmap.h>
  57. #ifdef HAVE_LIBSYSTEMD
  58. #include <systemd/sd-daemon.h>
  59. #endif
  60. /*
  61. * generic declarations
  62. */
  63. enum {
  64. CS_NTF_LOG,
  65. CS_NTF_STDOUT,
  66. CS_NTF_SNMP,
  67. CS_NTF_DBUS,
  68. CS_NTF_FG,
  69. CS_NTF_MAX,
  70. };
  71. static int conf[CS_NTF_MAX];
  72. static int32_t _cs_is_quorate = 0;
  73. typedef void (*node_membership_fn_t)(char *nodename, uint32_t nodeid, char *state, char* ip);
  74. typedef void (*node_quorum_fn_t)(char *nodename, uint32_t nodeid, const char *state);
  75. typedef void (*application_connection_fn_t)(char *nodename, uint32_t nodeid, char *app_name, const char *state);
  76. typedef void (*link_faulty_fn_t)(char *nodename, uint32_t local_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state);
  77. struct notify_callbacks {
  78. node_membership_fn_t node_membership_fn;
  79. node_quorum_fn_t node_quorum_fn;
  80. application_connection_fn_t application_connection_fn;
  81. link_faulty_fn_t link_faulty_fn;
  82. };
  83. struct track_item {
  84. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  85. cmap_track_handle_t track_handle;
  86. };
  87. #define MAX_NOTIFIERS 5
  88. static int num_notifiers = 0;
  89. static struct notify_callbacks notifiers[MAX_NOTIFIERS];
  90. static uint32_t local_nodeid = 0;
  91. static char local_nodename[CS_MAX_NAME_LENGTH];
  92. static qb_loop_t *main_loop;
  93. static quorum_handle_t quorum_handle;
  94. static qb_map_t *tracker_map;
  95. static void _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip);
  96. static void _cs_node_quorum_event(const char *state);
  97. static void _cs_application_connection_event(char *app_name, const char *state);
  98. static void _cs_link_faulty_event(uint32_t nodeid, uint32_t iface_no, const char *state);
  99. #ifdef HAVE_DBUS
  100. #include <dbus/dbus.h>
  101. /*
  102. * dbus
  103. */
  104. #define DBUS_CS_NAME "org.corosync"
  105. #define DBUS_CS_IFACE "org.corosync"
  106. #define DBUS_CS_PATH "/org/corosync"
  107. static DBusConnection *db = NULL;
  108. static char _err[512];
  109. static int err_set = 0;
  110. static void _cs_dbus_init(void);
  111. #endif /* HAVE_DBUS */
  112. #ifdef ENABLE_SNMP
  113. #include <net-snmp/net-snmp-config.h>
  114. #include <net-snmp/snmpv3_api.h>
  115. #include <net-snmp/agent/agent_trap.h>
  116. #include <net-snmp/library/mib.h>
  117. #include <net-snmp/library/snmp_api.h>
  118. #include <net-snmp/library/snmp_client.h>
  119. #include <net-snmp/library/snmp_debug.h>
  120. enum snmp_node_status {
  121. SNMP_NODE_STATUS_UNKNOWN = 0,
  122. SNMP_NODE_STATUS_JOINED = 1,
  123. SNMP_NODE_STATUS_LEFT = 2
  124. };
  125. #define SNMP_OID_COROSYNC "1.3.6.1.4.1.35488"
  126. #define SNMP_OID_OBJECT_ROOT SNMP_OID_COROSYNC ".1"
  127. #define SNMP_OID_OBJECT_NODE_NAME SNMP_OID_OBJECT_ROOT ".1"
  128. #define SNMP_OID_OBJECT_NODE_ID SNMP_OID_OBJECT_ROOT ".2"
  129. #define SNMP_OID_OBJECT_NODE_STATUS SNMP_OID_OBJECT_ROOT ".3"
  130. #define SNMP_OID_OBJECT_NODE_ADDR SNMP_OID_OBJECT_ROOT ".4"
  131. #define SNMP_OID_OBJECT_LOCAL_NODE_ID SNMP_OID_OBJECT_ROOT ".5"
  132. #define SNMP_OID_OBJECT_RINGSEQ SNMP_OID_OBJECT_ROOT ".20"
  133. #define SNMP_OID_OBJECT_QUORUM SNMP_OID_OBJECT_ROOT ".21"
  134. #define SNMP_OID_OBJECT_APP_NAME SNMP_OID_OBJECT_ROOT ".40"
  135. #define SNMP_OID_OBJECT_APP_STATUS SNMP_OID_OBJECT_ROOT ".41"
  136. #define SNMP_OID_OBJECT_LINK_IFACE_NO SNMP_OID_OBJECT_ROOT ".60"
  137. #define SNMP_OID_OBJECT_LINK_STATUS SNMP_OID_OBJECT_ROOT ".61"
  138. #define SNMP_OID_TRAPS_ROOT SNMP_OID_COROSYNC ".0"
  139. #define SNMP_OID_TRAPS_NODE SNMP_OID_TRAPS_ROOT ".1"
  140. #define SNMP_OID_TRAPS_QUORUM SNMP_OID_TRAPS_ROOT ".2"
  141. #define SNMP_OID_TRAPS_APP SNMP_OID_TRAPS_ROOT ".3"
  142. #define SNMP_OID_TRAPS_LINK SNMP_OID_TRAPS_ROOT ".4"
  143. #define CS_TIMESTAMP_STR_LEN 20
  144. static const char *local_host = "localhost";
  145. #endif /* ENABLE_SNMP */
  146. static char snmp_manager_buf[CS_MAX_NAME_LENGTH];
  147. static char *snmp_manager = NULL;
  148. static char snmp_community_buf[CS_MAX_NAME_LENGTH];
  149. static char *snmp_community = NULL;
  150. #define CMAP_MAX_RETRIES 10
  151. /*
  152. * cmap
  153. */
  154. static cmap_handle_t cmap_handle;
  155. static cmap_handle_t stats_handle;
  156. static int32_t _cs_ip_to_hostname(char* ip, char* name_out)
  157. {
  158. struct sockaddr_in sa;
  159. int rc;
  160. if (strchr(ip, ':') == NULL) {
  161. sa.sin_family = AF_INET;
  162. } else {
  163. sa.sin_family = AF_INET6;
  164. }
  165. rc = inet_pton(sa.sin_family, ip, &sa.sin_addr);
  166. if (rc == 0) {
  167. return -EINVAL;
  168. }
  169. rc = getnameinfo((struct sockaddr*)&sa, sizeof(sa),
  170. name_out, CS_MAX_NAME_LENGTH, NULL, 0, 0);
  171. if (rc != 0) {
  172. qb_log(LOG_ERR, "error looking up %s : %s", ip, gai_strerror(rc));
  173. return -EINVAL;
  174. }
  175. return 0;
  176. }
  177. static void _cs_cmap_members_key_changed (
  178. cmap_handle_t cmap_handle_c,
  179. cmap_track_handle_t cmap_track_handle,
  180. int32_t event,
  181. const char *key_name,
  182. struct cmap_notify_value new_value,
  183. struct cmap_notify_value old_value,
  184. void *user_data)
  185. {
  186. char nodename[CS_MAX_NAME_LENGTH];
  187. char* open_bracket = NULL;
  188. char* close_bracket = NULL;
  189. int res;
  190. uint32_t nodeid;
  191. char *ip_str;
  192. char tmp_key[CMAP_KEYNAME_MAXLEN];
  193. cs_error_t err;
  194. int no_retries;
  195. if (event != CMAP_TRACK_ADD && event != CMAP_TRACK_MODIFY) {
  196. return ;
  197. }
  198. if (NULL == key_name) {
  199. qb_log(LOG_ERR, "key_name: nil");
  200. }
  201. res = sscanf(key_name, "runtime.members.%u.%s", &nodeid, tmp_key);
  202. if (res != 2)
  203. return ;
  204. if (strcmp(tmp_key, "status") != 0) {
  205. return ;
  206. }
  207. res = snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "runtime.members.%u.ip", nodeid);
  208. if (res <= 0 || res >= CMAP_KEYNAME_MAXLEN) {
  209. qb_log(LOG_ERR, "temp_key: failed, res: %d, nodeid: %u", res, nodeid);
  210. return ;
  211. }
  212. no_retries = 0;
  213. while ((err = cmap_get_string(cmap_handle, tmp_key, &ip_str)) == CS_ERR_TRY_AGAIN &&
  214. no_retries++ < CMAP_MAX_RETRIES) {
  215. sleep(1);
  216. }
  217. if (err != CS_OK) {
  218. return ;
  219. }
  220. /*
  221. * We want the ip out of: "r(0) ip(192.168.100.92)"
  222. */
  223. open_bracket = strrchr(ip_str, '(');
  224. if (NULL == open_bracket) {
  225. qb_log(LOG_ERR, "ip_str: %s", ip_str);
  226. free(ip_str);
  227. return ;
  228. }
  229. open_bracket++;
  230. close_bracket = strchr(open_bracket, ')');
  231. if (NULL == close_bracket) {
  232. qb_log(LOG_ERR, "open_bracket: %s", open_bracket);
  233. free(ip_str);
  234. return ;
  235. }
  236. *close_bracket = '\0';
  237. res = _cs_ip_to_hostname(open_bracket, nodename);
  238. if (res) {
  239. strncpy(nodename, open_bracket, CS_MAX_NAME_LENGTH-1);
  240. }
  241. _cs_node_membership_event(nodename, nodeid, (char *)new_value.data, open_bracket);
  242. free(ip_str);
  243. }
  244. static void _cs_cmap_connections_key_changed (
  245. cmap_handle_t cmap_handle_c,
  246. cmap_track_handle_t cmap_track_handle,
  247. int32_t event,
  248. const char *key_name,
  249. struct cmap_notify_value new_value,
  250. struct cmap_notify_value old_value,
  251. void *user_data)
  252. {
  253. char obj_name[CS_MAX_NAME_LENGTH];
  254. char conn_str[CMAP_KEYNAME_MAXLEN];
  255. char tmp_key[CMAP_KEYNAME_MAXLEN];
  256. int service, pid;
  257. int res;
  258. res = sscanf(key_name, "stats.ipcs.service%d.%d.%[^.].%s", &service,&pid, conn_str, tmp_key);
  259. if (res != 4) {
  260. return ;
  261. }
  262. if (strcmp(tmp_key, "procname") != 0) {
  263. return ;
  264. }
  265. if (snprintf(obj_name, CS_MAX_NAME_LENGTH, "%s.%d.%s", conn_str, pid,
  266. (char*)new_value.data) >= CS_MAX_NAME_LENGTH) {
  267. /*
  268. * This should never happen
  269. */
  270. qb_log(LOG_ERR, "Can't snprintf obj_name");
  271. return ;
  272. }
  273. if (event == CMAP_TRACK_ADD) {
  274. _cs_application_connection_event(obj_name, "connected");
  275. }
  276. if (event == CMAP_TRACK_DELETE) {
  277. _cs_application_connection_event(obj_name, "disconnected");
  278. }
  279. }
  280. static void _cs_cmap_link_faulty_key_changed (
  281. cmap_handle_t cmap_handle_c,
  282. cmap_track_handle_t cmap_track_handle,
  283. int32_t event,
  284. const char *key_name,
  285. struct cmap_notify_value new_value,
  286. struct cmap_notify_value old_value,
  287. void *user_data)
  288. {
  289. uint32_t iface_no;
  290. uint32_t nodeid;
  291. int res;
  292. int no_retries;
  293. uint8_t connected;
  294. cs_error_t err;
  295. res = sscanf(key_name, "stats.knet.node%u.link%u.connected", &nodeid, &iface_no);
  296. if (res != 2) {
  297. return ;
  298. }
  299. no_retries = 0;
  300. while ((err = cmap_get_uint8(stats_handle, key_name, &connected)) == CS_ERR_TRY_AGAIN &&
  301. no_retries++ < CMAP_MAX_RETRIES) {
  302. sleep(1);
  303. }
  304. if (err != CS_OK) {
  305. return ;
  306. }
  307. if (connected) {
  308. _cs_link_faulty_event(nodeid, iface_no, "operational");
  309. } else {
  310. _cs_link_faulty_event(nodeid, iface_no, "disconnected");
  311. }
  312. }
  313. static void _cs_cmap_link_added_removed (
  314. cmap_handle_t cmap_handle_c,
  315. cmap_track_handle_t track_handle,
  316. int32_t event,
  317. const char *key_name,
  318. struct cmap_notify_value new_value,
  319. struct cmap_notify_value old_value,
  320. void *user_data)
  321. {
  322. struct track_item *track_item;
  323. /* Add/remove a tracker for a new/removed knet link */
  324. if (strstr(key_name, ".connected")) {
  325. if (event == CMAP_TRACK_ADD) {
  326. track_item = malloc(sizeof(struct track_item));
  327. if (!track_item) {
  328. return;
  329. }
  330. cmap_track_add(stats_handle, key_name, CMAP_TRACK_MODIFY, _cs_cmap_link_faulty_key_changed, NULL, &track_handle);
  331. strcpy(track_item->key_name, key_name);
  332. track_item->track_handle = track_handle;
  333. qb_map_put(tracker_map, track_item->key_name, track_item);
  334. } else {
  335. track_item = qb_map_get(tracker_map, key_name);
  336. if (track_item) {
  337. cmap_track_delete(stats_handle, track_item->track_handle);
  338. qb_map_rm(tracker_map, track_item->key_name);
  339. free(track_item);
  340. }
  341. }
  342. }
  343. }
  344. static int
  345. _cs_cmap_dispatch(int fd, int revents, void *data)
  346. {
  347. cs_error_t err;
  348. err = cmap_dispatch(*(cmap_handle_t *)data, CS_DISPATCH_ONE);
  349. if (err != CS_OK && err != CS_ERR_TRY_AGAIN && err != CS_ERR_TIMEOUT &&
  350. err != CS_ERR_QUEUE_FULL) {
  351. qb_log(LOG_ERR, "Could not dispatch cmap events. Error %u", err);
  352. qb_loop_stop(main_loop);
  353. return -1;
  354. }
  355. return 0;
  356. }
  357. static void _cs_quorum_notification(quorum_handle_t handle,
  358. uint32_t quorate, uint64_t ring_seq,
  359. uint32_t view_list_entries, uint32_t *view_list)
  360. {
  361. if (_cs_is_quorate == quorate) {
  362. return;
  363. }
  364. _cs_is_quorate = quorate;
  365. if (quorate) {
  366. _cs_node_quorum_event("quorate");
  367. } else {
  368. _cs_node_quorum_event("not quorate");
  369. }
  370. }
  371. static int
  372. _cs_quorum_dispatch(int fd, int revents, void *data)
  373. {
  374. cs_error_t err;
  375. err = quorum_dispatch(quorum_handle, CS_DISPATCH_ONE);
  376. if (err != CS_OK && err != CS_ERR_TRY_AGAIN && err != CS_ERR_TIMEOUT &&
  377. err != CS_ERR_QUEUE_FULL) {
  378. qb_log(LOG_ERR, "Could not dispatch quorum events. Error %u", err);
  379. qb_loop_stop(main_loop);
  380. return -1;
  381. }
  382. return 0;
  383. }
  384. static void
  385. _cs_quorum_init(void)
  386. {
  387. cs_error_t rc;
  388. uint32_t quorum_type;
  389. int fd;
  390. quorum_callbacks_t quorum_callbacks = {
  391. .quorum_notify_fn = _cs_quorum_notification,
  392. };
  393. rc = quorum_initialize (&quorum_handle, &quorum_callbacks,
  394. &quorum_type);
  395. if (rc != CS_OK) {
  396. qb_log(LOG_ERR, "Could not connect to corosync(quorum)");
  397. return;
  398. }
  399. quorum_fd_get(quorum_handle, &fd);
  400. qb_loop_poll_add(main_loop, QB_LOOP_MED, fd, POLLIN|POLLNVAL, NULL,
  401. _cs_quorum_dispatch);
  402. rc = quorum_trackstart(quorum_handle, CS_TRACK_CHANGES);
  403. if (rc != CS_OK) {
  404. qb_log(LOG_ERR, "Could not start tracking");
  405. return;
  406. }
  407. }
  408. static void
  409. _cs_quorum_finalize(void)
  410. {
  411. quorum_finalize (quorum_handle);
  412. }
  413. #ifdef HAVE_DBUS
  414. /*
  415. * dbus notifications
  416. */
  417. static void
  418. _cs_dbus_auto_flush(void)
  419. {
  420. dbus_connection_ref(db);
  421. while (dbus_connection_get_dispatch_status(db) == DBUS_DISPATCH_DATA_REMAINS) {
  422. dbus_connection_dispatch(db);
  423. }
  424. while (dbus_connection_has_messages_to_send(db)) {
  425. dbus_connection_flush(db);
  426. }
  427. dbus_connection_unref(db);
  428. }
  429. static void
  430. _cs_dbus_release(void)
  431. {
  432. DBusError err;
  433. if (!db)
  434. return;
  435. dbus_error_init(&err);
  436. dbus_bus_release_name(db, DBUS_CS_NAME, &err);
  437. dbus_error_free(&err);
  438. dbus_connection_unref(db);
  439. db = NULL;
  440. }
  441. static void
  442. _cs_dbus_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  443. {
  444. DBusMessage *msg = NULL;
  445. if (err_set) {
  446. qb_log(LOG_ERR, "%s", _err);
  447. err_set = 0;
  448. }
  449. if (!db) {
  450. goto out_free;
  451. }
  452. if (dbus_connection_get_is_connected(db) != TRUE) {
  453. err_set = 1;
  454. snprintf(_err, sizeof(_err), "DBus connection lost");
  455. _cs_dbus_release();
  456. goto out_unlock;
  457. }
  458. _cs_dbus_auto_flush();
  459. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  460. DBUS_CS_IFACE,
  461. "QuorumStateChange"))) {
  462. qb_log(LOG_ERR, "error creating dbus signal");
  463. goto out_unlock;
  464. }
  465. if (!dbus_message_append_args(msg,
  466. DBUS_TYPE_STRING, &nodename,
  467. DBUS_TYPE_UINT32, &nodeid,
  468. DBUS_TYPE_STRING, &state,
  469. DBUS_TYPE_INVALID)) {
  470. qb_log(LOG_ERR, "error adding args to quorum signal");
  471. goto out_unlock;
  472. }
  473. dbus_connection_send(db, msg, NULL);
  474. out_unlock:
  475. if (msg) {
  476. dbus_message_unref(msg);
  477. }
  478. out_free:
  479. return;
  480. }
  481. static void
  482. _cs_dbus_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  483. {
  484. DBusMessage *msg = NULL;
  485. if (err_set) {
  486. qb_log(LOG_ERR, "%s", _err);
  487. err_set = 0;
  488. }
  489. if (!db) {
  490. goto out_free;
  491. }
  492. if (dbus_connection_get_is_connected(db) != TRUE) {
  493. err_set = 1;
  494. snprintf(_err, sizeof(_err), "DBus connection lost");
  495. _cs_dbus_release();
  496. goto out_unlock;
  497. }
  498. _cs_dbus_auto_flush();
  499. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  500. DBUS_CS_IFACE,
  501. "NodeStateChange"))) {
  502. qb_log(LOG_ERR, "error creating NodeStateChange signal");
  503. goto out_unlock;
  504. }
  505. if (!dbus_message_append_args(msg,
  506. DBUS_TYPE_STRING, &nodename,
  507. DBUS_TYPE_UINT32, &nodeid,
  508. DBUS_TYPE_STRING, &ip,
  509. DBUS_TYPE_STRING, &state,
  510. DBUS_TYPE_INVALID)) {
  511. qb_log(LOG_ERR, "error adding args to NodeStateChange signal");
  512. goto out_unlock;
  513. }
  514. dbus_connection_send(db, msg, NULL);
  515. out_unlock:
  516. if (msg) {
  517. dbus_message_unref(msg);
  518. }
  519. out_free:
  520. return;
  521. }
  522. static void
  523. _cs_dbus_application_connection_event(char *nodename, uint32_t nodeid, char *app_name, const char *state)
  524. {
  525. DBusMessage *msg = NULL;
  526. if (err_set) {
  527. qb_log(LOG_ERR, "%s", _err);
  528. err_set = 0;
  529. }
  530. if (!db) {
  531. goto out_free;
  532. }
  533. if (dbus_connection_get_is_connected(db) != TRUE) {
  534. err_set = 1;
  535. snprintf(_err, sizeof(_err), "DBus connection lost");
  536. _cs_dbus_release();
  537. goto out_unlock;
  538. }
  539. _cs_dbus_auto_flush();
  540. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  541. DBUS_CS_IFACE,
  542. "ConnectionStateChange"))) {
  543. qb_log(LOG_ERR, "error creating ConnectionStateChange signal");
  544. goto out_unlock;
  545. }
  546. if (!dbus_message_append_args(msg,
  547. DBUS_TYPE_STRING, &nodename,
  548. DBUS_TYPE_UINT32, &nodeid,
  549. DBUS_TYPE_STRING, &app_name,
  550. DBUS_TYPE_STRING, &state,
  551. DBUS_TYPE_INVALID)) {
  552. qb_log(LOG_ERR, "error adding args to ConnectionStateChange signal");
  553. goto out_unlock;
  554. }
  555. dbus_connection_send(db, msg, NULL);
  556. out_unlock:
  557. if (msg) {
  558. dbus_message_unref(msg);
  559. }
  560. out_free:
  561. return;
  562. }
  563. static void
  564. _cs_dbus_link_faulty_event(char *nodename, uint32_t local_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state)
  565. {
  566. DBusMessage *msg = NULL;
  567. if (err_set) {
  568. qb_log(LOG_ERR, "%s", _err);
  569. err_set = 0;
  570. }
  571. if (!db) {
  572. goto out_free;
  573. }
  574. if (dbus_connection_get_is_connected(db) != TRUE) {
  575. err_set = 1;
  576. snprintf(_err, sizeof(_err), "DBus connection lost");
  577. _cs_dbus_release();
  578. goto out_unlock;
  579. }
  580. _cs_dbus_auto_flush();
  581. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  582. DBUS_CS_IFACE,
  583. "QuorumStateChange"))) {
  584. qb_log(LOG_ERR, "error creating dbus signal");
  585. goto out_unlock;
  586. }
  587. if (!dbus_message_append_args(msg,
  588. DBUS_TYPE_STRING, &nodename,
  589. DBUS_TYPE_UINT32, &local_nodeid,
  590. DBUS_TYPE_UINT32, &nodeid,
  591. DBUS_TYPE_UINT32, &iface_no,
  592. DBUS_TYPE_STRING, &state,
  593. DBUS_TYPE_INVALID)) {
  594. qb_log(LOG_ERR, "error adding args to link signal");
  595. goto out_unlock;
  596. }
  597. dbus_connection_send(db, msg, NULL);
  598. out_unlock:
  599. if (msg) {
  600. dbus_message_unref(msg);
  601. }
  602. out_free:
  603. return;
  604. }
  605. static void
  606. _cs_dbus_init(void)
  607. {
  608. DBusConnection *dbc = NULL;
  609. DBusError err;
  610. dbus_error_init(&err);
  611. dbc = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
  612. if (!dbc) {
  613. snprintf(_err, sizeof(_err),
  614. "dbus_bus_get: %s", err.message);
  615. err_set = 1;
  616. dbus_error_free(&err);
  617. return;
  618. }
  619. dbus_connection_set_exit_on_disconnect(dbc, FALSE);
  620. db = dbc;
  621. notifiers[num_notifiers].node_membership_fn =
  622. _cs_dbus_node_membership_event;
  623. notifiers[num_notifiers].node_quorum_fn =
  624. _cs_dbus_node_quorum_event;
  625. notifiers[num_notifiers].application_connection_fn =
  626. _cs_dbus_application_connection_event;
  627. notifiers[num_notifiers].link_faulty_fn =
  628. _cs_dbus_link_faulty_event;
  629. num_notifiers++;
  630. }
  631. #endif /* HAVE_DBUS */
  632. #ifdef ENABLE_SNMP
  633. static netsnmp_session *snmp_init (const char *target)
  634. {
  635. static netsnmp_session *session = NULL;
  636. #ifndef NETSNMPV54
  637. char default_port[128];
  638. snprintf (default_port, sizeof (default_port), "%s:162", target);
  639. #endif
  640. if (session) {
  641. return (session);
  642. }
  643. if (target == NULL) {
  644. return NULL;
  645. }
  646. session = malloc (sizeof (netsnmp_session));
  647. snmp_sess_init (session);
  648. session->version = SNMP_VERSION_2c;
  649. session->callback = NULL;
  650. session->callback_magic = NULL;
  651. if (snmp_community) {
  652. session->community = (u_char *)snmp_community;
  653. session->community_len = strlen(snmp_community_buf);
  654. }
  655. session = snmp_add(session,
  656. #ifdef NETSNMPV54
  657. netsnmp_transport_open_client ("snmptrap", target),
  658. #else
  659. netsnmp_tdomain_transport (default_port, 0, "udp"),
  660. #endif
  661. NULL, NULL);
  662. if (session == NULL) {
  663. qb_log(LOG_ERR, 0, "Could not create snmp transport");
  664. }
  665. return (session);
  666. }
  667. static inline void add_field (
  668. netsnmp_pdu *trap_pdu,
  669. u_char asn_type,
  670. const char *prefix,
  671. void *value,
  672. size_t value_size)
  673. {
  674. oid _oid[MAX_OID_LEN];
  675. size_t _oid_len = MAX_OID_LEN;
  676. if (snmp_parse_oid(prefix, _oid, &_oid_len)) {
  677. snmp_pdu_add_variable (trap_pdu, _oid, _oid_len, asn_type, (u_char *) value, value_size);
  678. }
  679. }
  680. static void
  681. _cs_snmp_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  682. {
  683. int ret;
  684. char csysuptime[CS_TIMESTAMP_STR_LEN];
  685. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  686. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  687. time_t now = time (NULL);
  688. netsnmp_pdu *trap_pdu;
  689. netsnmp_session *session = snmp_init (snmp_manager);
  690. if (session == NULL) {
  691. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  692. return ;
  693. }
  694. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  695. if (!trap_pdu) {
  696. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  697. return ;
  698. }
  699. /* send uptime */
  700. snprintf (csysuptime, CS_TIMESTAMP_STR_LEN, "%ld", now);
  701. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  702. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_NODE);
  703. /* Add extries to the trap */
  704. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  705. add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  706. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_ADDR, (void*)ip, strlen (ip));
  707. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_STATUS, (void*)state, strlen (state));
  708. /* Send and cleanup */
  709. ret = snmp_send (session, trap_pdu);
  710. if (ret == 0) {
  711. /* error */
  712. qb_log(LOG_ERR, "Could not send SNMP trap");
  713. snmp_free_pdu (trap_pdu);
  714. }
  715. }
  716. static void
  717. _cs_snmp_node_quorum_event(char *nodename, uint32_t nodeid,
  718. const char *state)
  719. {
  720. int ret;
  721. char csysuptime[20];
  722. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  723. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  724. time_t now = time (NULL);
  725. netsnmp_pdu *trap_pdu;
  726. netsnmp_session *session = snmp_init (snmp_manager);
  727. if (session == NULL) {
  728. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  729. return ;
  730. }
  731. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  732. if (!trap_pdu) {
  733. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  734. return ;
  735. }
  736. /* send uptime */
  737. sprintf (csysuptime, "%ld", now);
  738. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  739. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_QUORUM);
  740. /* Add extries to the trap */
  741. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  742. add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  743. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_QUORUM, (void*)state, strlen (state));
  744. /* Send and cleanup */
  745. ret = snmp_send (session, trap_pdu);
  746. if (ret == 0) {
  747. /* error */
  748. qb_log(LOG_ERR, "Could not send SNMP trap");
  749. snmp_free_pdu (trap_pdu);
  750. }
  751. }
  752. static void
  753. _cs_snmp_link_faulty_event(char *nodename, uint32_t local_nodeid, uint32_t nodeid,
  754. uint32_t iface_no, const char *state)
  755. {
  756. int ret;
  757. char csysuptime[20];
  758. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  759. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  760. time_t now = time (NULL);
  761. netsnmp_pdu *trap_pdu;
  762. netsnmp_session *session = snmp_init (snmp_manager);
  763. if (session == NULL) {
  764. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  765. return ;
  766. }
  767. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  768. if (!trap_pdu) {
  769. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  770. return ;
  771. }
  772. /* send uptime */
  773. sprintf (csysuptime, "%ld", now);
  774. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  775. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_LINK);
  776. /* Add extries to the trap */
  777. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  778. add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_LOCAL_NODE_ID, (void*)&local_nodeid, sizeof (local_nodeid));
  779. add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  780. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_LINK_IFACE_NO, (void*)&iface_no, sizeof (iface_no));
  781. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_LINK_STATUS, (void*)state, strlen (state));
  782. /* Send and cleanup */
  783. ret = snmp_send (session, trap_pdu);
  784. if (ret == 0) {
  785. /* error */
  786. qb_log(LOG_ERR, "Could not send SNMP trap");
  787. snmp_free_pdu (trap_pdu);
  788. }
  789. }
  790. static void
  791. _cs_snmp_init(void)
  792. {
  793. if (snmp_manager == NULL) {
  794. snmp_manager = (char*)local_host;
  795. }
  796. notifiers[num_notifiers].node_membership_fn =
  797. _cs_snmp_node_membership_event;
  798. notifiers[num_notifiers].node_quorum_fn =
  799. _cs_snmp_node_quorum_event;
  800. notifiers[num_notifiers].application_connection_fn = NULL;
  801. notifiers[num_notifiers].link_faulty_fn =
  802. _cs_snmp_link_faulty_event;
  803. num_notifiers++;
  804. }
  805. #endif /* ENABLE_SNMP */
  806. static void
  807. _cs_syslog_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  808. {
  809. qb_log(LOG_NOTICE, "%s[%d] ip:%s %s", nodename, nodeid, ip, state);
  810. }
  811. static void
  812. _cs_syslog_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  813. {
  814. if (strcmp(state, "quorate") == 0) {
  815. qb_log(LOG_NOTICE, "%s[%d] is now %s", nodename, nodeid, state);
  816. } else {
  817. qb_log(LOG_NOTICE, "%s[%d] has lost quorum", nodename, nodeid);
  818. }
  819. }
  820. static void
  821. _cs_syslog_application_connection_event(char *nodename, uint32_t nodeid, char* app_name, const char *state)
  822. {
  823. if (strcmp(state, "connected") == 0) {
  824. qb_log(LOG_NOTICE, "%s[%d] %s is now %s to corosync", nodename, nodeid, app_name, state);
  825. } else {
  826. qb_log(LOG_NOTICE, "%s[%d] %s is now %s from corosync", nodename, nodeid, app_name, state);
  827. }
  828. }
  829. static void
  830. _cs_syslog_link_faulty_event(char *nodename, uint32_t our_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state)
  831. {
  832. qb_log(LOG_NOTICE, "%s[%d] link %u to node %u is now %s", nodename, our_nodeid, iface_no, nodeid, state);
  833. }
  834. static void
  835. _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  836. {
  837. int i;
  838. for (i = 0; i < num_notifiers; i++) {
  839. if (notifiers[i].node_membership_fn) {
  840. notifiers[i].node_membership_fn(nodename, nodeid, state, ip);
  841. }
  842. }
  843. }
  844. static void
  845. _cs_local_node_info_get(char **nodename, uint32_t *nodeid)
  846. {
  847. cs_error_t rc;
  848. corosync_cfg_handle_t cfg_handle;
  849. if (local_nodeid == 0) {
  850. rc = corosync_cfg_initialize(&cfg_handle, NULL);
  851. if (rc != CS_OK) {
  852. syslog (LOG_ERR, "Failed to initialize the cfg API. Error %d\n", rc);
  853. exit (EXIT_FAILURE);
  854. }
  855. rc = corosync_cfg_local_get (cfg_handle, &local_nodeid);
  856. corosync_cfg_finalize(cfg_handle);
  857. if (rc != CS_OK) {
  858. local_nodeid = 0;
  859. strncpy(local_nodename, "localhost", sizeof (local_nodename));
  860. local_nodename[sizeof (local_nodename) - 1] = '\0';
  861. } else {
  862. gethostname(local_nodename, CS_MAX_NAME_LENGTH);
  863. }
  864. }
  865. *nodeid = local_nodeid;
  866. *nodename = local_nodename;
  867. }
  868. static void
  869. _cs_node_quorum_event(const char *state)
  870. {
  871. int i;
  872. char *nodename;
  873. uint32_t nodeid;
  874. _cs_local_node_info_get(&nodename, &nodeid);
  875. for (i = 0; i < num_notifiers; i++) {
  876. if (notifiers[i].node_quorum_fn) {
  877. notifiers[i].node_quorum_fn(nodename, nodeid, state);
  878. }
  879. }
  880. }
  881. static void
  882. _cs_application_connection_event(char *app_name, const char *state)
  883. {
  884. int i;
  885. char *nodename;
  886. uint32_t nodeid;
  887. _cs_local_node_info_get(&nodename, &nodeid);
  888. for (i = 0; i < num_notifiers; i++) {
  889. if (notifiers[i].application_connection_fn) {
  890. notifiers[i].application_connection_fn(nodename, nodeid, app_name, state);
  891. }
  892. }
  893. }
  894. static void
  895. _cs_link_faulty_event(uint32_t nodeid, uint32_t iface_no, const char *state)
  896. {
  897. int i;
  898. char *nodename;
  899. uint32_t our_nodeid;
  900. _cs_local_node_info_get(&nodename, &our_nodeid);
  901. for (i = 0; i < num_notifiers; i++) {
  902. if (notifiers[i].link_faulty_fn) {
  903. notifiers[i].link_faulty_fn(nodename, our_nodeid, nodeid, iface_no, state);
  904. }
  905. }
  906. }
  907. static int32_t
  908. sig_exit_handler(int32_t num, void *data)
  909. {
  910. qb_loop_stop(main_loop);
  911. return 0;
  912. }
  913. static void track_link_updown_events(void)
  914. {
  915. cmap_iter_handle_t iter_handle;
  916. cmap_track_handle_t track_handle;
  917. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  918. size_t value_len;
  919. cmap_value_types_t type;
  920. cs_error_t err;
  921. struct track_item *track_item;
  922. err = cmap_iter_init(stats_handle, "stats.knet.", &iter_handle);
  923. if (err != CS_OK) {
  924. fprintf (stderr, "Failed to initialize knet stats iterator. Error %s\n", cs_strerror(err));
  925. exit (EXIT_FAILURE);
  926. }
  927. while ((err = cmap_iter_next(stats_handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  928. if (strstr(key_name, ".connected")) {
  929. track_item = malloc(sizeof(struct track_item));
  930. if (!track_item) {
  931. return;
  932. }
  933. if ((err = cmap_track_add(stats_handle, key_name, CMAP_TRACK_MODIFY, _cs_cmap_link_faulty_key_changed, NULL, &track_handle)) != CS_OK) {
  934. fprintf (stderr, "Failed to add tracker for %s. Error %s\n", key_name, cs_strerror(err));
  935. exit (EXIT_FAILURE);
  936. }
  937. strcpy(track_item->key_name, key_name);
  938. track_item->track_handle = track_handle;
  939. qb_map_put(tracker_map, track_item->key_name, track_item);
  940. }
  941. }
  942. cmap_iter_finalize(stats_handle, iter_handle);
  943. }
  944. static void
  945. _cs_cmap_init(void)
  946. {
  947. cs_error_t rc = CS_OK;
  948. int cmap_fd = 0;
  949. int stats_fd = 0;
  950. cmap_track_handle_t track_handle;
  951. tracker_map = qb_trie_create();
  952. if (!tracker_map) {
  953. qb_log(LOG_ERR, "Failed to initialize the track map. Error %d", rc);
  954. exit (EXIT_FAILURE);
  955. }
  956. rc = cmap_initialize_map (&cmap_handle, CMAP_MAP_ICMAP);
  957. if (rc != CS_OK) {
  958. qb_log(LOG_ERR, "Failed to initialize the cmap API. Error %d", rc);
  959. exit (EXIT_FAILURE);
  960. }
  961. cmap_fd_get(cmap_handle, &cmap_fd);
  962. qb_loop_poll_add(main_loop, QB_LOOP_MED, cmap_fd, POLLIN|POLLNVAL, (void*)&cmap_handle,
  963. _cs_cmap_dispatch);
  964. rc = cmap_initialize_map (&stats_handle, CMAP_MAP_STATS);
  965. if (rc != CS_OK) {
  966. qb_log(LOG_ERR, "Failed to initialize the cmap stats API. Error %d", rc);
  967. exit (EXIT_FAILURE);
  968. }
  969. cmap_fd_get(stats_handle, &stats_fd);
  970. qb_loop_poll_add(main_loop, QB_LOOP_MED, stats_fd, POLLIN|POLLNVAL, (void*)&stats_handle,
  971. _cs_cmap_dispatch);
  972. rc = cmap_track_add(cmap_handle, "runtime.members.",
  973. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  974. _cs_cmap_members_key_changed,
  975. NULL,
  976. &track_handle);
  977. if (rc != CS_OK) {
  978. qb_log(LOG_ERR,
  979. "Failed to track the members key. Error %d", rc);
  980. exit (EXIT_FAILURE);
  981. }
  982. rc = cmap_track_add(stats_handle, "stats.ipcs.",
  983. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX,
  984. _cs_cmap_connections_key_changed,
  985. NULL,
  986. &track_handle);
  987. if (rc != CS_OK) {
  988. qb_log(LOG_ERR,
  989. "Failed to track the connections key. Error %d", rc);
  990. exit (EXIT_FAILURE);
  991. }
  992. rc = cmap_track_add(stats_handle, "stats.knet.",
  993. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX,
  994. _cs_cmap_link_added_removed,
  995. NULL,
  996. &track_handle);
  997. if (rc != CS_OK) {
  998. qb_log(LOG_ERR,
  999. "Failed to track the knet link status key. Error %d", rc);
  1000. exit (EXIT_FAILURE);
  1001. }
  1002. track_link_updown_events();
  1003. }
  1004. static void
  1005. _cs_cmap_finalize(void)
  1006. {
  1007. cmap_finalize (cmap_handle);
  1008. cmap_finalize (stats_handle);
  1009. }
  1010. static void
  1011. _cs_check_config(void)
  1012. {
  1013. if (conf[CS_NTF_LOG] == QB_FALSE &&
  1014. conf[CS_NTF_STDOUT] == QB_FALSE &&
  1015. conf[CS_NTF_SNMP] == QB_FALSE &&
  1016. conf[CS_NTF_DBUS] == QB_FALSE) {
  1017. qb_log(LOG_ERR, "no event type enabled, see corosync-notifyd -h, exiting.");
  1018. exit(EXIT_FAILURE);
  1019. }
  1020. #ifndef ENABLE_SNMP
  1021. if (conf[CS_NTF_SNMP]) {
  1022. qb_log(LOG_ERR, "Not compiled with SNMP support enabled, exiting.");
  1023. exit(EXIT_FAILURE);
  1024. }
  1025. #endif
  1026. #ifndef HAVE_DBUS
  1027. if (conf[CS_NTF_DBUS]) {
  1028. qb_log(LOG_ERR, "Not compiled with DBus support enabled, exiting.");
  1029. exit(EXIT_FAILURE);
  1030. }
  1031. #endif
  1032. if (conf[CS_NTF_STDOUT] && !conf[CS_NTF_FG]) {
  1033. qb_log(LOG_ERR, "configured to print to stdout and run in the background, exiting");
  1034. exit(EXIT_FAILURE);
  1035. }
  1036. if (conf[CS_NTF_SNMP] && conf[CS_NTF_DBUS]) {
  1037. qb_log(LOG_ERR, "configured to send snmp traps and dbus signals - are you sure?.");
  1038. }
  1039. }
  1040. static void
  1041. _cs_usage(void)
  1042. {
  1043. fprintf(stderr, "usage:\n"\
  1044. " -c : SNMP Community name.\n"\
  1045. " -f : Start application in foreground.\n"\
  1046. " -l : Log all events.\n"\
  1047. " -o : Print events to stdout (turns on -l).\n"\
  1048. " -s : Send SNMP traps on all events.\n"\
  1049. " -m : Set the SNMP Manager IP address (defaults to localhost).\n"\
  1050. " -d : Send DBUS signals on all events.\n"\
  1051. " -h : Print this help.\n\n");
  1052. }
  1053. int
  1054. main(int argc, char *argv[])
  1055. {
  1056. int ch;
  1057. conf[CS_NTF_FG] = QB_FALSE;
  1058. conf[CS_NTF_LOG] = QB_FALSE;
  1059. conf[CS_NTF_STDOUT] = QB_FALSE;
  1060. conf[CS_NTF_SNMP] = QB_FALSE;
  1061. conf[CS_NTF_DBUS] = QB_FALSE;
  1062. while ((ch = getopt (argc, argv, "c:floshdm:")) != EOF) {
  1063. switch (ch) {
  1064. case 'c':
  1065. strncpy(snmp_community_buf, optarg, sizeof (snmp_community_buf));
  1066. snmp_community_buf[sizeof (snmp_community_buf) - 1] = '\0';
  1067. snmp_community = snmp_community_buf;
  1068. break;
  1069. case 'f':
  1070. conf[CS_NTF_FG] = QB_TRUE;
  1071. break;
  1072. case 'l':
  1073. conf[CS_NTF_LOG] = QB_TRUE;
  1074. break;
  1075. case 'm':
  1076. conf[CS_NTF_SNMP] = QB_TRUE;
  1077. strncpy(snmp_manager_buf, optarg, sizeof (snmp_manager_buf));
  1078. snmp_manager_buf[sizeof (snmp_manager_buf) - 1] = '\0';
  1079. snmp_manager = snmp_manager_buf;
  1080. break;
  1081. case 'o':
  1082. conf[CS_NTF_LOG] = QB_TRUE;
  1083. conf[CS_NTF_STDOUT] = QB_TRUE;
  1084. break;
  1085. case 's':
  1086. conf[CS_NTF_SNMP] = QB_TRUE;
  1087. break;
  1088. case 'd':
  1089. conf[CS_NTF_DBUS] = QB_TRUE;
  1090. break;
  1091. case 'h':
  1092. default:
  1093. _cs_usage();
  1094. return EXIT_FAILURE;
  1095. }
  1096. }
  1097. qb_log_init("notifyd", LOG_DAEMON, LOG_INFO);
  1098. if (conf[CS_NTF_STDOUT]) {
  1099. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  1100. QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
  1101. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, conf[CS_NTF_STDOUT]);
  1102. }
  1103. _cs_check_config();
  1104. if (!conf[CS_NTF_FG]) {
  1105. if (daemon(0, 0) < 0)
  1106. {
  1107. perror("daemon() failed");
  1108. return EXIT_FAILURE;
  1109. }
  1110. }
  1111. num_notifiers = 0;
  1112. if (conf[CS_NTF_LOG]) {
  1113. notifiers[num_notifiers].node_membership_fn =
  1114. _cs_syslog_node_membership_event;
  1115. notifiers[num_notifiers].node_quorum_fn =
  1116. _cs_syslog_node_quorum_event;
  1117. notifiers[num_notifiers].application_connection_fn =
  1118. _cs_syslog_application_connection_event;
  1119. notifiers[num_notifiers].link_faulty_fn =
  1120. _cs_syslog_link_faulty_event;
  1121. num_notifiers++;
  1122. }
  1123. main_loop = qb_loop_create();
  1124. _cs_cmap_init();
  1125. _cs_quorum_init();
  1126. #ifdef HAVE_DBUS
  1127. if (conf[CS_NTF_DBUS]) {
  1128. _cs_dbus_init();
  1129. }
  1130. #endif /* HAVE_DBUS */
  1131. #ifdef ENABLE_SNMP
  1132. if (conf[CS_NTF_SNMP]) {
  1133. _cs_snmp_init();
  1134. }
  1135. #endif /* ENABLE_SNMP */
  1136. qb_loop_signal_add(main_loop,
  1137. QB_LOOP_HIGH,
  1138. SIGINT,
  1139. NULL,
  1140. sig_exit_handler,
  1141. NULL);
  1142. qb_loop_signal_add(main_loop,
  1143. QB_LOOP_HIGH,
  1144. SIGQUIT,
  1145. NULL,
  1146. sig_exit_handler,
  1147. NULL);
  1148. qb_loop_signal_add(main_loop,
  1149. QB_LOOP_HIGH,
  1150. SIGTERM,
  1151. NULL,
  1152. sig_exit_handler,
  1153. NULL);
  1154. #ifdef HAVE_LIBSYSTEMD
  1155. sd_notify (0, "READY=1");
  1156. #endif
  1157. qb_loop_run(main_loop);
  1158. #ifdef HAVE_DBUS
  1159. if (conf[CS_NTF_DBUS]) {
  1160. _cs_dbus_release();
  1161. }
  1162. #endif /* HAVE_DBUS */
  1163. _cs_quorum_finalize();
  1164. _cs_cmap_finalize();
  1165. return 0;
  1166. }