corosync-notifyd.c 35 KB

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