corosync-notifyd.c 34 KB

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