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