Print this page
OS-1840 fmdump shall emit JSON
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/fm/fmdump/common/fault.c
+++ new/usr/src/cmd/fm/fmdump/common/fault.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
23 24 */
24 25
25 26 #include <fmdump.h>
26 27 #include <stdio.h>
27 28 #include <strings.h>
29 +#include <alloca.h>
28 30
29 31 /*ARGSUSED*/
30 32 static int
31 33 flt_short(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
32 34 {
33 35 char buf[32], str[32];
34 36 char *class = NULL, *uuid = "-", *code = "-";
35 37
36 38 static const struct {
37 39 const char *class;
38 40 const char *tag;
39 41 } tags[] = {
40 42 { FM_LIST_SUSPECT_CLASS, "Diagnosed" },
41 43 { FM_LIST_REPAIRED_CLASS, "Repaired" },
42 44 { FM_LIST_RESOLVED_CLASS, "Resolved" },
43 45 { FM_LIST_UPDATED_CLASS, "Updated" },
44 46 { FM_LIST_ISOLATED_CLASS, "Isolated" },
45 47 };
46 48
47 49 (void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_UUID, &uuid);
48 50 (void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_DIAG_CODE, &code);
49 51
50 52 (void) nvlist_lookup_string(rp->rec_nvl, FM_CLASS, &class);
51 53 if (class != NULL) {
52 54 int i;
53 55
54 56 for (i = 0; i < sizeof (tags) / sizeof (tags[0]); i++) {
55 57 if (strcmp(class, tags[i].class) == 0) {
56 58 (void) snprintf(str, sizeof (str), "%s %s",
57 59 code, tags[i].tag);
58 60 code = str;
59 61 break;
60 62 }
61 63 }
62 64 }
63 65
64 66 fmdump_printf(fp, "%-20s %-32s %s\n",
65 67 fmdump_date(buf, sizeof (buf), rp), uuid, code);
66 68
67 69 return (0);
68 70 }
69 71
70 72 static int
71 73 flt_verb1(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
72 74 {
73 75 uint_t i, size = 0;
74 76 nvlist_t **nva;
75 77 uint8_t *ba;
76 78
77 79 (void) flt_short(lp, rp, fp);
78 80 (void) nvlist_lookup_uint32(rp->rec_nvl, FM_SUSPECT_FAULT_SZ, &size);
79 81
80 82 if (size != 0) {
81 83 (void) nvlist_lookup_nvlist_array(rp->rec_nvl,
82 84 FM_SUSPECT_FAULT_LIST, &nva, &size);
83 85 (void) nvlist_lookup_uint8_array(rp->rec_nvl,
84 86 FM_SUSPECT_FAULT_STATUS, &ba, &size);
85 87 }
86 88
87 89 for (i = 0; i < size; i++) {
88 90 char *class = NULL, *rname = NULL, *aname = NULL, *fname = NULL;
89 91 char *loc = NULL;
90 92 nvlist_t *fru, *asru, *rsrc;
91 93 uint8_t pct = 0;
92 94
93 95 (void) nvlist_lookup_uint8(nva[i], FM_FAULT_CERTAINTY, &pct);
94 96 (void) nvlist_lookup_string(nva[i], FM_CLASS, &class);
95 97
96 98 if (nvlist_lookup_nvlist(nva[i], FM_FAULT_FRU, &fru) == 0)
97 99 fname = fmdump_nvl2str(fru);
98 100
99 101 if (nvlist_lookup_nvlist(nva[i], FM_FAULT_ASRU, &asru) == 0)
100 102 aname = fmdump_nvl2str(asru);
101 103
102 104 if (nvlist_lookup_nvlist(nva[i], FM_FAULT_RESOURCE, &rsrc) == 0)
103 105 rname = fmdump_nvl2str(rsrc);
104 106
105 107 if (nvlist_lookup_string(nva[i], FM_FAULT_LOCATION, &loc)
106 108 == 0) {
107 109 if (fname && strncmp(fname, FM_FMRI_LEGACY_HC_PREFIX,
108 110 sizeof (FM_FMRI_LEGACY_HC_PREFIX)) == 0)
109 111 loc = fname + sizeof (FM_FMRI_LEGACY_HC_PREFIX);
110 112 }
111 113
112 114
113 115 fmdump_printf(fp, " %3u%% %s",
114 116 pct, class ? class : "-");
115 117
116 118 if (ba[i] & FM_SUSPECT_FAULTY)
117 119 fmdump_printf(fp, "\n\n");
118 120 else if (ba[i] & FM_SUSPECT_NOT_PRESENT)
119 121 fmdump_printf(fp, "\tRemoved\n\n");
120 122 else if (ba[i] & FM_SUSPECT_REPLACED)
121 123 fmdump_printf(fp, "\tReplaced\n\n");
122 124 else if (ba[i] & FM_SUSPECT_REPAIRED)
123 125 fmdump_printf(fp, "\tRepair Attempted\n\n");
124 126 else if (ba[i] & FM_SUSPECT_ACQUITTED)
125 127 fmdump_printf(fp, "\tAcquitted\n\n");
126 128 else
127 129 fmdump_printf(fp, "\n\n");
128 130
129 131 fmdump_printf(fp, " Problem in: %s\n",
130 132 rname ? rname : "-");
131 133
132 134 fmdump_printf(fp, " Affects: %s\n",
133 135 aname ? aname : "-");
134 136
135 137 fmdump_printf(fp, " FRU: %s\n",
136 138 fname ? fname : "-");
137 139
138 140 fmdump_printf(fp, " Location: %s\n\n",
139 141 loc ? loc : "-");
140 142
141 143 free(fname);
142 144 free(aname);
143 145 free(rname);
144 146 }
145 147
146 148 return (0);
147 149 }
148 150
149 151 static int
150 152 flt_verb23_cmn(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp,
151 153 nvlist_prtctl_t pctl)
152 154 {
153 155 const struct fmdump_fmt *efp = &fmdump_err_ops.do_formats[FMDUMP_VERB1];
154 156 const struct fmdump_fmt *ffp = &fmdump_flt_ops.do_formats[FMDUMP_VERB2];
155 157 uint_t i;
156 158 char buf[32], str[32];
157 159 char *class = NULL, *uuid = "-", *code = "-";
158 160
159 161 (void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_UUID, &uuid);
160 162 (void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_DIAG_CODE, &code);
161 163
162 164 (void) nvlist_lookup_string(rp->rec_nvl, FM_CLASS, &class);
163 165 if (class != NULL && strcmp(class, FM_LIST_REPAIRED_CLASS) == 0) {
164 166 (void) snprintf(str, sizeof (str), "%s %s", code, "Repaired");
165 167 code = str;
166 168 }
167 169 if (class != NULL && strcmp(class, FM_LIST_RESOLVED_CLASS) == 0) {
168 170 (void) snprintf(str, sizeof (str), "%s %s", code, "Resolved");
169 171 code = str;
170 172 }
171 173 if (class != NULL && strcmp(class, FM_LIST_UPDATED_CLASS) == 0) {
172 174 (void) snprintf(str, sizeof (str), "%s %s", code, "Updated");
173 175 code = str;
174 176 }
175 177
176 178 fmdump_printf(fp, "%s\n", ffp->do_hdr);
177 179 fmdump_printf(fp, "%-20s.%9.9llu %-32s %s\n",
178 180 fmdump_year(buf, sizeof (buf), rp), rp->rec_nsec, uuid, code);
179 181
180 182 if (rp->rec_nrefs != 0)
181 183 fmdump_printf(fp, "\n %s\n", efp->do_hdr);
182 184
183 185 for (i = 0; i < rp->rec_nrefs; i++) {
184 186 fmdump_printf(fp, " ");
185 187 efp->do_func(lp, &rp->rec_xrefs[i], fp);
186 188 }
187 189
188 190 fmdump_printf(fp, "\n");
189 191 if (pctl)
190 192 nvlist_prt(rp->rec_nvl, pctl);
191 193 else
192 194 nvlist_print(fp, rp->rec_nvl);
193 195 fmdump_printf(fp, "\n");
↓ open down ↓ |
156 lines elided |
↑ open up ↑ |
194 196
195 197 return (0);
196 198 }
197 199
198 200 static int
199 201 flt_verb2(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
200 202 {
201 203 return (flt_verb23_cmn(lp, rp, fp, NULL));
202 204 }
203 205
204 -
205 206 static int
206 207 flt_pretty(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
207 208 {
208 209 nvlist_prtctl_t pctl;
209 210 int rc;
210 211
211 212 if ((pctl = nvlist_prtctl_alloc()) != NULL) {
212 213 nvlist_prtctl_setdest(pctl, fp);
213 214 nvlist_prtctlop_nvlist(pctl, fmdump_render_nvlist, NULL);
214 215 }
215 216
216 217 rc = flt_verb23_cmn(lp, rp, fp, pctl);
217 218
218 219 nvlist_prtctl_free(pctl);
219 220 return (rc);
220 221 }
221 222
222 223 /*
223 224 * There is a lack of uniformity in how the various entries in our diagnosis
224 225 * are terminated. Some end with one newline, others with two. This makes the
225 226 * output of fmdump -m look a bit ugly. Therefore we postprocess the message
226 227 * before printing it, removing consecutive occurences of newlines.
227 228 */
228 229 static void
229 230 postprocess_msg(char *msg)
230 231 {
231 232 int i = 0, j = 0;
232 233 char *buf;
233 234
234 235 if ((buf = malloc(strlen(msg) + 1)) == NULL)
235 236 return;
236 237
237 238 buf[j++] = msg[i++];
238 239 for (i = 1; i < strlen(msg); i++) {
239 240 if (!(msg[i] == '\n' && msg[i - 1] == '\n'))
240 241 buf[j++] = msg[i];
241 242 }
242 243 buf[j] = '\0';
243 244 (void) strncpy(msg, buf, j+1);
244 245 free(buf);
245 246 }
246 247
247 248 /*ARGSUSED*/
248 249 static int
249 250 flt_msg(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
250 251 {
251 252 char *msg;
252 253
253 254 if ((msg = fmd_msg_gettext_nv(g_msg, NULL, rp->rec_nvl)) == NULL) {
254 255 (void) fprintf(stderr, "%s: failed to format message: %s\n",
255 256 g_pname, strerror(errno));
256 257 g_errs++;
257 258 return (-1);
258 259 } else {
259 260 postprocess_msg(msg);
260 261 fmdump_printf(fp, "%s\n", msg);
261 262 free(msg);
262 263 }
263 264
264 265 return (0);
265 266 }
266 267
267 268 const fmdump_ops_t fmdump_flt_ops = {
268 269 "fault", {
269 270 {
270 271 "TIME UUID SUNW-MSG-ID "
271 272 "EVENT",
272 273 (fmd_log_rec_f *)flt_short
273 274 }, {
274 275 "TIME UUID SUNW-MSG-ID "
275 276 "EVENT",
276 277 (fmd_log_rec_f *)flt_verb1
277 278 }, {
↓ open down ↓ |
63 lines elided |
↑ open up ↑ |
278 279 "TIME UUID"
279 280 " SUNW-MSG-ID",
280 281 (fmd_log_rec_f *)flt_verb2
281 282 }, {
282 283 "TIME UUID"
283 284 " SUNW-MSG-ID",
284 285 (fmd_log_rec_f *)flt_pretty
285 286 }, {
286 287 NULL,
287 288 (fmd_log_rec_f *)flt_msg
289 +}, {
290 +NULL,
291 +(fmd_log_rec_f *)fmdump_print_json
288 292 } }
289 293 };
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX