corosync-notifyd.c 34 KB

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