xref: /onnv-gate/usr/src/cmd/fm/fmadm/common/faulty.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <strings.h>
30*0Sstevel@tonic-gate #include <fmadm.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate static const char *
33*0Sstevel@tonic-gate rsrc_state_name(const fmd_adm_rsrcinfo_t *ari)
34*0Sstevel@tonic-gate {
35*0Sstevel@tonic-gate 	switch (ari->ari_flags & (FMD_ADM_RSRC_FAULTY|FMD_ADM_RSRC_UNUSABLE)) {
36*0Sstevel@tonic-gate 	default:
37*0Sstevel@tonic-gate 		return ("ok");
38*0Sstevel@tonic-gate 	case FMD_ADM_RSRC_FAULTY:
39*0Sstevel@tonic-gate 		return ("degraded");
40*0Sstevel@tonic-gate 	case FMD_ADM_RSRC_UNUSABLE:
41*0Sstevel@tonic-gate 		return ("unknown");
42*0Sstevel@tonic-gate 	case FMD_ADM_RSRC_FAULTY | FMD_ADM_RSRC_UNUSABLE:
43*0Sstevel@tonic-gate 		return ("faulted");
44*0Sstevel@tonic-gate 	}
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static const char faulty_line[] = "-------- "
48*0Sstevel@tonic-gate "----------------------------------------------------------------------";
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	IS_FAULTY(ari)	\
51*0Sstevel@tonic-gate 	(((ari)->ari_flags & (FMD_ADM_RSRC_FAULTY | \
52*0Sstevel@tonic-gate 	FMD_ADM_RSRC_INVISIBLE)) == FMD_ADM_RSRC_FAULTY)
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*ARGSUSED*/
55*0Sstevel@tonic-gate static int
56*0Sstevel@tonic-gate faulty_fmri(const fmd_adm_rsrcinfo_t *ari, void *opt_a)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	if (opt_a || IS_FAULTY(ari)) {
59*0Sstevel@tonic-gate 		(void) printf("%s\n%8s %s\n%8s %s\n",
60*0Sstevel@tonic-gate 		    faulty_line, rsrc_state_name(ari), ari->ari_fmri, "",
61*0Sstevel@tonic-gate 		    ari->ari_case ? ari->ari_case : "-");
62*0Sstevel@tonic-gate 	}
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	return (0);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*ARGSUSED*/
68*0Sstevel@tonic-gate static int
69*0Sstevel@tonic-gate faulty_uuid(const fmd_adm_rsrcinfo_t *ari, void *opt_a)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	if (opt_a || IS_FAULTY(ari)) {
72*0Sstevel@tonic-gate 		(void) printf("%s\n%8s %s\n%8s %s\n",
73*0Sstevel@tonic-gate 		    faulty_line, rsrc_state_name(ari), ari->ari_fmri, "",
74*0Sstevel@tonic-gate 		    ari->ari_uuid);
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	return (0);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int
81*0Sstevel@tonic-gate cmd_faulty(fmd_adm_t *adm, int argc, char *argv[])
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	fmd_adm_rsrc_f *func = faulty_fmri;
84*0Sstevel@tonic-gate 	int c, opt_a = 0;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ai")) != EOF) {
87*0Sstevel@tonic-gate 		switch (c) {
88*0Sstevel@tonic-gate 		case 'a':
89*0Sstevel@tonic-gate 			opt_a++;
90*0Sstevel@tonic-gate 			break;
91*0Sstevel@tonic-gate 		case 'i':
92*0Sstevel@tonic-gate 			func = faulty_uuid;
93*0Sstevel@tonic-gate 			break;
94*0Sstevel@tonic-gate 		default:
95*0Sstevel@tonic-gate 			return (FMADM_EXIT_USAGE);
96*0Sstevel@tonic-gate 		}
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if (optind < argc)
100*0Sstevel@tonic-gate 		return (FMADM_EXIT_USAGE);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (func == faulty_fmri)
103*0Sstevel@tonic-gate 		(void) printf("%8s %s\n", "STATE", "RESOURCE / UUID");
104*0Sstevel@tonic-gate 	else
105*0Sstevel@tonic-gate 		(void) printf("%8s %s\n", "STATE", "RESOURCE / CACHE-ID");
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (fmd_adm_rsrc_iter(adm, opt_a, func, (void *)opt_a) != 0)
108*0Sstevel@tonic-gate 		die("failed to retrieve resource data");
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	(void) printf("%s\n", faulty_line);
111*0Sstevel@tonic-gate 	return (FMADM_EXIT_SUCCESS);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate int
115*0Sstevel@tonic-gate cmd_flush(fmd_adm_t *adm, int argc, char *argv[])
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	int i, status = FMADM_EXIT_SUCCESS;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (argc < 2 || (i = getopt(argc, argv, "")) != EOF)
120*0Sstevel@tonic-gate 		return (FMADM_EXIT_USAGE);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
123*0Sstevel@tonic-gate 		if (fmd_adm_rsrc_flush(adm, argv[i]) != 0) {
124*0Sstevel@tonic-gate 			warn("failed to flush %s", argv[i]);
125*0Sstevel@tonic-gate 			status = FMADM_EXIT_ERROR;
126*0Sstevel@tonic-gate 		} else
127*0Sstevel@tonic-gate 			note("flushed resource history for %s\n", argv[i]);
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	return (status);
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate int
134*0Sstevel@tonic-gate cmd_repair(fmd_adm_t *adm, int argc, char *argv[])
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	int err;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	if (getopt(argc, argv, "") != EOF)
139*0Sstevel@tonic-gate 		return (FMADM_EXIT_USAGE);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (argc - optind != 1)
142*0Sstevel@tonic-gate 		return (FMADM_EXIT_USAGE);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/*
145*0Sstevel@tonic-gate 	 * For now, we assume that if the input string contains a colon, it is
146*0Sstevel@tonic-gate 	 * an FMRI and if it does not it is a UUID.  If things get more complex
147*0Sstevel@tonic-gate 	 * in the future with multiple UUID formats, an FMRI parser can be
148*0Sstevel@tonic-gate 	 * added here to differentiate the input argument appropriately.
149*0Sstevel@tonic-gate 	 */
150*0Sstevel@tonic-gate 	if (strchr(argv[optind], ':') != NULL)
151*0Sstevel@tonic-gate 		err = fmd_adm_rsrc_repair(adm, argv[optind]);
152*0Sstevel@tonic-gate 	else
153*0Sstevel@tonic-gate 		err = fmd_adm_case_repair(adm, argv[optind]);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	if (err != 0)
156*0Sstevel@tonic-gate 		die("failed to record repair to %s", argv[optind]);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	note("recorded repair to %s\n", argv[optind]);
159*0Sstevel@tonic-gate 	return (FMADM_EXIT_SUCCESS);
160*0Sstevel@tonic-gate }
161