corosync-notifyd.c 33 KB

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