1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <strings.h>
29 #include <fm/fmd_api.h>
30 #include <sys/fm/protocol.h>
31 #include <sys/fm/util.h>
32 #include <sys/sysevent.h>
33 
34 #include "fmevt.h"
35 
36 static evchan_t *fmevt_outbound_chan;
37 
38 static struct fmevt_outbound_stats {
39 	fmd_stat_t recv_calls;
40 	fmd_stat_t recv_list;
41 	fmd_stat_t recv_swevent;
42 	fmd_stat_t recv_other;
43 	fmd_stat_t fwd_success;
44 	fmd_stat_t fwd_failure;
45 } outbound_stats = {
46 	{ "outbound_recv_calls", FMD_TYPE_UINT64,
47 	    "total events received for forwarding" },
48 	{ "outbound_cat1class_list", FMD_TYPE_UINT64,
49 	    "events received matching list.*" },
50 	{ "outbound_cat1class_swevent", FMD_TYPE_UINT64,
51 	    "events received matching swevent.*" },
52 	{ "outbound_cat1class_other", FMD_TYPE_UINT64,
53 	    "events of other classes" },
54 	{ "outbound_fwd_success", FMD_TYPE_UINT64,
55 	    "events forwarded successfully" },
56 	{ "outbound_fwd_failure", FMD_TYPE_UINT64,
57 	    "events we failed to forward" }
58 };
59 
60 #define	BUMPSTAT(stat)	outbound_stats.stat.fmds_value.ui64++
61 
62 /*
63  * In the .conf file we subscribe to list.* and swevent.* event classes.
64  * Any additions to that set could cause some unexpected behaviour.
65  * For example adding fault.foo won't work (since we don't publish
66  * faults directly but only within a list.suspect) but we will get
67  * any list.* including fault.foo as a suspect.
68  */
69 /*ARGSUSED*/
70 void
71 fmevt_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
72 {
73 	BUMPSTAT(recv_calls);
74 
75 	if (strncmp(class, "list.", 5) == 0)
76 		BUMPSTAT(recv_list);
77 	else if (strncmp(class, "swevent.", 8) == 0)
78 		BUMPSTAT(recv_swevent);
79 	else
80 		BUMPSTAT(recv_other);
81 
82 	if (sysevent_evc_publish(fmevt_outbound_chan, class, "",
83 	    SUNW_VENDOR, FM_PUB, nvl, EVCH_SLEEP) == 0) {
84 		BUMPSTAT(fwd_success);
85 	} else {
86 		BUMPSTAT(fwd_failure);
87 		fmd_hdl_debug(hdl, "sysevent_evc_publish failed:");
88 	}
89 }
90 
91 void
92 fmevt_init_outbound(fmd_hdl_t *hdl)
93 {
94 	int32_t channel_depth;
95 	char *channel_name;
96 
97 	if (fmd_prop_get_int32(hdl, "protocol_forward_disable") == B_TRUE) {
98 		fmd_hdl_debug(hdl, "protocol forwarding disabled "
99 		    "through .conf file setting\n");
100 		return;
101 	}
102 
103 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (outbound_stats) /
104 	    sizeof (fmd_stat_t), (fmd_stat_t *)&outbound_stats);
105 
106 	/*
107 	 * Allow simulation environment to change outbound channel name.
108 	 */
109 	channel_name = fmd_prop_get_string(hdl, "outbound_channel");
110 
111 	if (sysevent_evc_bind(channel_name, &fmevt_outbound_chan,
112 	    EVCH_CREAT | EVCH_HOLD_PEND_INDEF) != 0) {
113 		fmd_hdl_abort(hdl, "Unable to bind channel %s",
114 		    channel_name);
115 		return;
116 	}
117 
118 	channel_depth = fmd_prop_get_int32(hdl, "outbound_channel_depth");
119 
120 	if (sysevent_evc_control(fmevt_outbound_chan, EVCH_SET_CHAN_LEN,
121 	    (uint32_t)channel_depth) != 0) {
122 		fmd_hdl_abort(hdl, "Unable to set depth of channel %s to %d",
123 		    channel_name, channel_depth);
124 	}
125 
126 	fmd_prop_free_string(hdl, channel_name);
127 }
128 
129 /*ARGSUSED*/
130 void
131 fmevt_fini_outbound(fmd_hdl_t *hdl)
132 {
133 	if (fmevt_outbound_chan != NULL) {
134 		(void) sysevent_evc_unbind(fmevt_outbound_chan);
135 		fmevt_outbound_chan = NULL;
136 	}
137 }
138