4
0

corosync-notifyd.c 35 KB

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