1*03831d35Sstevel /*
2*03831d35Sstevel  * CDDL HEADER START
3*03831d35Sstevel  *
4*03831d35Sstevel  * The contents of this file are subject to the terms of the
5*03831d35Sstevel  * Common Development and Distribution License (the "License").
6*03831d35Sstevel  * You may not use this file except in compliance with the License.
7*03831d35Sstevel  *
8*03831d35Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*03831d35Sstevel  * or http://www.opensolaris.org/os/licensing.
10*03831d35Sstevel  * See the License for the specific language governing permissions
11*03831d35Sstevel  * and limitations under the License.
12*03831d35Sstevel  *
13*03831d35Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
14*03831d35Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*03831d35Sstevel  * If applicable, add the following below this CDDL HEADER, with the
16*03831d35Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
17*03831d35Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
18*03831d35Sstevel  *
19*03831d35Sstevel  * CDDL HEADER END
20*03831d35Sstevel  */
21*03831d35Sstevel 
22*03831d35Sstevel /*
23*03831d35Sstevel  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*03831d35Sstevel  * Use is subject to license terms.
25*03831d35Sstevel  */
26*03831d35Sstevel 
27*03831d35Sstevel /*
28*03831d35Sstevel  * eventlog.c: support for the scadm loghistory option (to display the
29*03831d35Sstevel  * service processor log history)
30*03831d35Sstevel  */
31*03831d35Sstevel 
32*03831d35Sstevel #include <libintl.h>
33*03831d35Sstevel #include <stdio.h>
34*03831d35Sstevel #include <string.h>
35*03831d35Sstevel #include <time.h>  /* required by librsc.h */
36*03831d35Sstevel 
37*03831d35Sstevel #include "librsc.h"
38*03831d35Sstevel #include "adm.h"
39*03831d35Sstevel 
40*03831d35Sstevel #include "event_mess.h"
41*03831d35Sstevel #define	TAB '\t'
42*03831d35Sstevel #define	BACKSLASH_ESCAPE '\\'
43*03831d35Sstevel 
44*03831d35Sstevel /* #define DEBUG */
45*03831d35Sstevel 
46*03831d35Sstevel static char *
getEventLogMessage(int eventId)47*03831d35Sstevel getEventLogMessage(int eventId)
48*03831d35Sstevel {
49*03831d35Sstevel 	int	category;
50*03831d35Sstevel 	int	event;
51*03831d35Sstevel 	char	**alertCategory;
52*03831d35Sstevel 	char	*alertMessage;
53*03831d35Sstevel 
54*03831d35Sstevel 	category = eventId >> 16;
55*03831d35Sstevel 	event = eventId &0x0000ffff;
56*03831d35Sstevel 
57*03831d35Sstevel 	alertCategory = rsc_alerts[category];
58*03831d35Sstevel 	if (alertCategory) {
59*03831d35Sstevel 		alertMessage = alertCategory[event];
60*03831d35Sstevel 	} else {
61*03831d35Sstevel 		return (NULL);
62*03831d35Sstevel 	}
63*03831d35Sstevel 
64*03831d35Sstevel 	if (alertMessage) {
65*03831d35Sstevel 		return (alertMessage);
66*03831d35Sstevel 	} else {
67*03831d35Sstevel 		return (NULL);
68*03831d35Sstevel 	}
69*03831d35Sstevel }
70*03831d35Sstevel 
71*03831d35Sstevel /*
72*03831d35Sstevel  * getNextEventLogParam
73*03831d35Sstevel  *
74*03831d35Sstevel  *	Return the next message from a TAB delimited message parameter list.
75*03831d35Sstevel  *  Given a string message "mess1\tmess2\tmess3\t\t", this function will
76*03831d35Sstevel  *  return a ponter to "mess2" the first time it is called.
77*03831d35Sstevel  */
78*03831d35Sstevel static char *
getNextEventLogParam(char * mess)79*03831d35Sstevel getNextEventLogParam(char *mess)
80*03831d35Sstevel {
81*03831d35Sstevel 	char *p = mess;
82*03831d35Sstevel 
83*03831d35Sstevel 	do {
84*03831d35Sstevel 		/* ESCAPE means interpret the next character literally */
85*03831d35Sstevel 		if ((p != mess) && (*(p-1) == BACKSLASH_ESCAPE)) {
86*03831d35Sstevel 			p++;
87*03831d35Sstevel 			continue;
88*03831d35Sstevel 		}
89*03831d35Sstevel 
90*03831d35Sstevel 		if ((*p == TAB) && (*(p+1) == TAB)) {
91*03831d35Sstevel 			/* Double tab means end of list */
92*03831d35Sstevel 			return (NULL);
93*03831d35Sstevel 		}
94*03831d35Sstevel 		p++;
95*03831d35Sstevel 
96*03831d35Sstevel 	} while (*p != TAB);
97*03831d35Sstevel 
98*03831d35Sstevel 	/* return pointer to char after TAB */
99*03831d35Sstevel 	p++;
100*03831d35Sstevel 	return (p);
101*03831d35Sstevel 
102*03831d35Sstevel }
103*03831d35Sstevel 
104*03831d35Sstevel /*
105*03831d35Sstevel  * expandEventLogMessage
106*03831d35Sstevel  *
107*03831d35Sstevel  *	This function will expand the base message for the category/event
108*03831d35Sstevel  *  passed in with the TAB delimited parameters passed in via messParams.
109*03831d35Sstevel  * 	The expanded message will be returned in the buf character buffer.
110*03831d35Sstevel  */
111*03831d35Sstevel 
112*03831d35Sstevel static int
expandEventLogMessage(int eventId,char * messParams,size_t messParamsLen,char * buf)113*03831d35Sstevel expandEventLogMessage(int eventId, char *messParams, size_t messParamsLen,
114*03831d35Sstevel     char *buf)
115*03831d35Sstevel {
116*03831d35Sstevel 
117*03831d35Sstevel 	char	*alertMessage;
118*03831d35Sstevel 	char	*s;
119*03831d35Sstevel 	char	*d;
120*03831d35Sstevel 	char	*param;
121*03831d35Sstevel 
122*03831d35Sstevel 	/* Get Alert message from internal tables */
123*03831d35Sstevel 	alertMessage = getEventLogMessage(eventId);
124*03831d35Sstevel 	if (alertMessage == NULL) {
125*03831d35Sstevel 		(void) strcpy(buf, "Unknown alert");
126*03831d35Sstevel 		return (strlen("Unknown alert"));
127*03831d35Sstevel 	}
128*03831d35Sstevel 
129*03831d35Sstevel 	/* No message parameters to copy */
130*03831d35Sstevel 	if (messParamsLen == 0) {
131*03831d35Sstevel 		(void) strcpy(buf, alertMessage);
132*03831d35Sstevel 		return (strlen(buf));
133*03831d35Sstevel 	}
134*03831d35Sstevel 
135*03831d35Sstevel 	/* A %s in the base message means we expand with a parameter */
136*03831d35Sstevel 	if (strstr(alertMessage, "%s")) {
137*03831d35Sstevel 		s = alertMessage;
138*03831d35Sstevel 		d = buf;
139*03831d35Sstevel 		param = messParams;
140*03831d35Sstevel 
141*03831d35Sstevel 		do {
142*03831d35Sstevel 			if ((*s == '%') && (*(s+1) == 's')) {
143*03831d35Sstevel 				if (param) {
144*03831d35Sstevel 					char *p = param;
145*03831d35Sstevel 
146*03831d35Sstevel 					while ((*p) && (*p != TAB)) {
147*03831d35Sstevel 						*d++ = *p++;
148*03831d35Sstevel 					}
149*03831d35Sstevel 				}
150*03831d35Sstevel 				/* Get next parameter on list for next %s */
151*03831d35Sstevel 				param = getNextEventLogParam(param);
152*03831d35Sstevel 				s += 2;
153*03831d35Sstevel 			}
154*03831d35Sstevel 		} while ((*d++ = *s++));
155*03831d35Sstevel 
156*03831d35Sstevel 	} else {
157*03831d35Sstevel 		/* If no %s tokens to expand, just copy message */
158*03831d35Sstevel 		(void) strcpy(buf, alertMessage);
159*03831d35Sstevel 	}
160*03831d35Sstevel 
161*03831d35Sstevel 	return (strlen(buf));
162*03831d35Sstevel 
163*03831d35Sstevel }
164*03831d35Sstevel 
165*03831d35Sstevel static void
ADM_Process_old_event_log()166*03831d35Sstevel ADM_Process_old_event_log()
167*03831d35Sstevel {
168*03831d35Sstevel 	char			timebuf[32];
169*03831d35Sstevel 	char			messBuff[256];
170*03831d35Sstevel 	char			eventMsgBuf[256];
171*03831d35Sstevel 	rscp_msg_t		Message;
172*03831d35Sstevel 	struct timespec		Timeout;
173*03831d35Sstevel 	dp_get_event_log_r_t	*rscReply;
174*03831d35Sstevel 	char			*datap;
175*03831d35Sstevel 	dp_event_log_entry_t	entry;
176*03831d35Sstevel 	int			i, len, entryhdrsize;
177*03831d35Sstevel 
178*03831d35Sstevel 	ADM_Start();
179*03831d35Sstevel 
180*03831d35Sstevel 	Message.type = DP_GET_EVENT_LOG;
181*03831d35Sstevel 	Message.len = 0;
182*03831d35Sstevel 	Message.data = NULL;
183*03831d35Sstevel 	ADM_Send(&Message);
184*03831d35Sstevel 
185*03831d35Sstevel 	Timeout.tv_nsec = 0;
186*03831d35Sstevel 	Timeout.tv_sec  = ADM_TIMEOUT;
187*03831d35Sstevel 	ADM_Recv(&Message, &Timeout,
188*03831d35Sstevel 	    DP_GET_EVENT_LOG_R, sizeof (*rscReply));
189*03831d35Sstevel 
190*03831d35Sstevel 	/* Print the event log messages */
191*03831d35Sstevel 	rscReply = (dp_get_event_log_r_t *)Message.data;
192*03831d35Sstevel 	datap = (char *)rscReply->data;
193*03831d35Sstevel 	for (i = 0; i < rscReply->entry_count; i++) {
194*03831d35Sstevel 		entryhdrsize = sizeof (entry) - sizeof (entry.param);
195*03831d35Sstevel 		(void) memcpy(&entry, datap, entryhdrsize);
196*03831d35Sstevel 		datap += entryhdrsize;
197*03831d35Sstevel 		(void) memcpy(&entry.param, datap, entry.paramLen);
198*03831d35Sstevel 		(void) strftime(timebuf, sizeof (timebuf), "%b %d %H:%M:%S",
199*03831d35Sstevel 		    gmtime((time_t *)&entry.eventTime));
200*03831d35Sstevel 		(void) sprintf(messBuff, "%s : %08lx: \"", timebuf,
201*03831d35Sstevel 		    entry.eventId);
202*03831d35Sstevel 		len = expandEventLogMessage(entry.eventId, entry.param,
203*03831d35Sstevel 		    entry.paramLen, eventMsgBuf);
204*03831d35Sstevel 		(void) strncat(messBuff, eventMsgBuf, len);
205*03831d35Sstevel 		(void) strcat(messBuff, "\"\r\n");
206*03831d35Sstevel 		(void) printf(messBuff);
207*03831d35Sstevel 		datap += entry.paramLen;
208*03831d35Sstevel 	}
209*03831d35Sstevel 
210*03831d35Sstevel 	ADM_Free(&Message);
211*03831d35Sstevel }
212*03831d35Sstevel 
213*03831d35Sstevel static int
ADM_Process_new_event_log(int all)214*03831d35Sstevel ADM_Process_new_event_log(int all)
215*03831d35Sstevel {
216*03831d35Sstevel 	char			timebuf[32];
217*03831d35Sstevel 	char			messBuff[256];
218*03831d35Sstevel 	char			eventMsgBuf[256];
219*03831d35Sstevel 	rscp_msg_t		Message;
220*03831d35Sstevel 	struct timespec		Timeout;
221*03831d35Sstevel 	dp_get_event_log2_r_t	*rscReply;
222*03831d35Sstevel 	char			*datap;
223*03831d35Sstevel 	dp_event_log_entry_t	entry;
224*03831d35Sstevel 	int			i, len, entryhdrsize, sent_ok;
225*03831d35Sstevel 	rsci64			events_remaining, seqno;
226*03831d35Sstevel 	rsci16			request_size, returned_events;
227*03831d35Sstevel 	dp_get_event_log2_t	rscCmd;
228*03831d35Sstevel 
229*03831d35Sstevel 	ADM_Start();
230*03831d35Sstevel 
231*03831d35Sstevel 	/*
232*03831d35Sstevel 	 * Start by sending a zero-length request to ALOM, so that
233*03831d35Sstevel 	 * we can learn the length of the console log.  We expect
234*03831d35Sstevel 	 * ALOM to return the length of the entire log.  We get
235*03831d35Sstevel 	 * a snapshot of the length of the log here - it may however
236*03831d35Sstevel 	 * continue to grow as we're reading it.  We read only as
237*03831d35Sstevel 	 * much of the log as we get in this snapshot.
238*03831d35Sstevel 	 *
239*03831d35Sstevel 	 * If the command fails, we quietly return failure here so
240*03831d35Sstevel 	 * that the caller can re-try with the old/legacy command.
241*03831d35Sstevel 	 */
242*03831d35Sstevel 	rscCmd.start_seq = 0;
243*03831d35Sstevel 	rscCmd.length = 0;
244*03831d35Sstevel 	Message.type = DP_GET_EVENT_LOG2;
245*03831d35Sstevel 	Message.len = sizeof (rscCmd);
246*03831d35Sstevel 	Message.data = (char *)&rscCmd;
247*03831d35Sstevel 	if (ADM_Send_ret(&Message) != 0) {
248*03831d35Sstevel 		return (1);
249*03831d35Sstevel 	}
250*03831d35Sstevel 
251*03831d35Sstevel 	Timeout.tv_nsec = 0;
252*03831d35Sstevel 	Timeout.tv_sec  = ADM_TIMEOUT;
253*03831d35Sstevel 	ADM_Recv(&Message, &Timeout,
254*03831d35Sstevel 	    DP_GET_EVENT_LOG2_R, sizeof (*rscReply));
255*03831d35Sstevel 
256*03831d35Sstevel 	rscReply = (dp_get_event_log2_r_t *)Message.data;
257*03831d35Sstevel 
258*03831d35Sstevel 	/*
259*03831d35Sstevel 	 * Fetch an fixed number of events from the end of
260*03831d35Sstevel 	 * the log if at least that many exist, and we were not
261*03831d35Sstevel 	 * asked to fetch all the events.
262*03831d35Sstevel 	 */
263*03831d35Sstevel 	if ((all == 0) &&
264*03831d35Sstevel 	    (rscReply->remaining_log_events > DEFAULT_NUM_EVENTS)) {
265*03831d35Sstevel 		events_remaining = DEFAULT_NUM_EVENTS;
266*03831d35Sstevel 		seqno = (rscReply->remaining_log_events +
267*03831d35Sstevel 		    rscReply->next_seq) - events_remaining;
268*03831d35Sstevel 	} else {
269*03831d35Sstevel 		events_remaining = rscReply->remaining_log_events;
270*03831d35Sstevel 		seqno = rscReply->next_seq;
271*03831d35Sstevel 	}
272*03831d35Sstevel 	request_size = sizeof (rscReply->buffer);
273*03831d35Sstevel 	ADM_Free(&Message);
274*03831d35Sstevel 
275*03831d35Sstevel 	/*
276*03831d35Sstevel 	 * This loop runs as long as there is data in the log, or until
277*03831d35Sstevel 	 * we hit the default limit (above).  It's possible that ALOM may
278*03831d35Sstevel 	 * shrink the log - we need to account for this.  If ALOM returns
279*03831d35Sstevel 	 * no data, we bail out.
280*03831d35Sstevel 	 */
281*03831d35Sstevel 	while (events_remaining) {
282*03831d35Sstevel 		rscCmd.start_seq = seqno;
283*03831d35Sstevel 		rscCmd.length = request_size;
284*03831d35Sstevel 		Message.type = DP_GET_EVENT_LOG2;
285*03831d35Sstevel 		Message.len = sizeof (rscCmd);
286*03831d35Sstevel 		Message.data = (char *)&rscCmd;
287*03831d35Sstevel 		ADM_Send(&Message);
288*03831d35Sstevel 
289*03831d35Sstevel 		Timeout.tv_nsec = 0;
290*03831d35Sstevel 		Timeout.tv_sec  = ADM_TIMEOUT;
291*03831d35Sstevel 		ADM_Recv(&Message, &Timeout,
292*03831d35Sstevel 		    DP_GET_EVENT_LOG2_R, sizeof (*rscReply));
293*03831d35Sstevel 
294*03831d35Sstevel 		rscReply = (dp_get_event_log2_r_t *)Message.data;
295*03831d35Sstevel 
296*03831d35Sstevel 		/* If ALOM returns zero events, we're done. */
297*03831d35Sstevel 		returned_events = rscReply->num_events;
298*03831d35Sstevel 		if (returned_events == 0) {
299*03831d35Sstevel 			ADM_Free(&Message);
300*03831d35Sstevel 			break;
301*03831d35Sstevel 		}
302*03831d35Sstevel 
303*03831d35Sstevel 		/*
304*03831d35Sstevel 		 * if the event at the original sequence number is no
305*03831d35Sstevel 		 * longer in the log, print a message
306*03831d35Sstevel 		 */
307*03831d35Sstevel 		if (seqno + returned_events < rscReply->next_seq) {
308*03831d35Sstevel 			printf(gettext("\nscadm: lost %d events\n"),
309*03831d35Sstevel 			    rscReply->next_seq - (seqno + returned_events));
310*03831d35Sstevel 		}
311*03831d35Sstevel 
312*03831d35Sstevel 		/*
313*03831d35Sstevel 		 * get ready for next main loop iteration
314*03831d35Sstevel 		 */
315*03831d35Sstevel 		seqno = rscReply->next_seq;
316*03831d35Sstevel 		events_remaining -= returned_events;
317*03831d35Sstevel 
318*03831d35Sstevel 		/* Print the event log messages */
319*03831d35Sstevel 		datap = rscReply->buffer;
320*03831d35Sstevel 
321*03831d35Sstevel 		for (i = 0; i < returned_events; i++) {
322*03831d35Sstevel 			entryhdrsize = sizeof (entry) - sizeof (entry.param);
323*03831d35Sstevel 			(void) memcpy(&entry, datap, entryhdrsize);
324*03831d35Sstevel 			datap += entryhdrsize;
325*03831d35Sstevel 			(void) memcpy(&entry.param, datap, entry.paramLen);
326*03831d35Sstevel 			(void) strftime(timebuf, sizeof (timebuf),
327*03831d35Sstevel 			    "%b %d %H:%M:%S",
328*03831d35Sstevel 			    gmtime((time_t *)&entry.eventTime));
329*03831d35Sstevel 			(void) sprintf(messBuff, "%s : %08lx: \"", timebuf,
330*03831d35Sstevel 			    entry.eventId);
331*03831d35Sstevel 			len = expandEventLogMessage(entry.eventId, entry.param,
332*03831d35Sstevel 			    entry.paramLen, eventMsgBuf);
333*03831d35Sstevel 			(void) strncat(messBuff, eventMsgBuf, len);
334*03831d35Sstevel 			(void) strcat(messBuff, "\"\r\n");
335*03831d35Sstevel 			(void) printf(messBuff);
336*03831d35Sstevel 			datap += entry.paramLen;
337*03831d35Sstevel 		}
338*03831d35Sstevel 
339*03831d35Sstevel 		ADM_Free(&Message);
340*03831d35Sstevel 	}
341*03831d35Sstevel 	return (0);
342*03831d35Sstevel }
343*03831d35Sstevel 
344*03831d35Sstevel void
ADM_Process_event_log(int all)345*03831d35Sstevel ADM_Process_event_log(int all)
346*03831d35Sstevel {
347*03831d35Sstevel 	if (ADM_Process_new_event_log(all) != 0) {
348*03831d35Sstevel 		ADM_Process_old_event_log();
349*03831d35Sstevel 	}
350*03831d35Sstevel }
351