xref: /titanic_41/usr/src/cmd/lvm/util/medstat.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1992-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * mediator status utility.
31  */
32 
33 #include <meta.h>
34 #include <sdssc.h>
35 
36 static void
37 usage(
38 	mdsetname_t	*sp,
39 	char		*string)
40 {
41 	if ((string != NULL) && (*string != '\0'))
42 		md_eprintf("%s\n", string);
43 	(void) fprintf(stderr, gettext(
44 	    "usage:	%s [-q] -s setname\n"),
45 	myname);
46 	md_exit(sp, (string == NULL) ? 0 : 1);
47 }
48 
49 /*
50  * parse args and do it
51  */
52 int
53 main(
54 	int			argc,
55 	char			*argv[]
56 )
57 {
58 	int			c;
59 	char			*sname = MD_LOCAL_NAME;
60 	md_error_t		status = mdnullerror;
61 	md_error_t		*ep = &status;
62 	mdsetname_t		*sp = NULL;
63 	md_set_desc		*sd;
64 	int			i;
65 	int			max_meds;
66 	md_h_t			mdh;
67 	med_data_t		medd;
68 	int			medok = 0;
69 	int			golden = 0;
70 	int			verbose = 1;
71 	int			error;
72 
73 	/*
74 	 * Get the locale set up before calling any other routines
75 	 * with messages to ouput.  Just in case we're not in a build
76 	 * environment, make sure that TEXT_DOMAIN gets set to
77 	 * something.
78 	 */
79 #if !defined(TEXT_DOMAIN)
80 #define	TEXT_DOMAIN "SYS_TEST"
81 #endif
82 	(void) setlocale(LC_ALL, "");
83 	(void) textdomain(TEXT_DOMAIN);
84 
85 	if ((sdssc_bind_library() == SDSSC_OKAY) &&
86 		(sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
87 		    &error) == SDSSC_PROXY_DONE))
88 			exit(error);
89 
90 	/* initialize */
91 	if (md_init(argc, argv, 0, 1, ep) != 0) {
92 		mde_perror(ep, "");
93 		md_exit(sp, 1);
94 	}
95 
96 	optind = 1;
97 	opterr = 1;
98 	while ((c = getopt(argc, argv, "qs:?")) != -1) {
99 		switch (c) {
100 		    case 'q':
101 			    verbose = 0;
102 			    break;
103 		    case 's':
104 			    sname = optarg;
105 			    break;
106 		    case '?':
107 			    if (optopt == '?')
108 				    usage(sp, NULL);
109 			    /*FALLTHROUGH*/
110 		    default:
111 			usage(sp, gettext("unknown command"));
112 		}
113 	}
114 
115 	/* must have set for everything else */
116 	if (strcmp(sname, MD_LOCAL_NAME) == 0)
117 		usage(sp, gettext("setname must be specified"));
118 
119 	/* snarf MDDB */
120 	if (meta_setup_db_locations(ep) != 0) {
121 		mde_perror(ep, "");
122 		md_exit(sp, 1);
123 	}
124 
125 	if ((sp = metasetname(sname, ep)) == NULL) {
126 		mde_perror(ep, "");
127 		md_exit(sp, 1);
128 	}
129 
130 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
131 		mde_perror(ep, "");
132 		md_exit(sp, 1);
133 	}
134 
135 	if (sd->sd_med.n_cnt == 0) {
136 		if (verbose)
137 			(void) printf(gettext(
138 			    "No mediator hosts configured for set \"%s\".\n"),
139 			    sname);
140 		md_exit(sp, 2);
141 	}
142 
143 	if ((max_meds = get_max_meds(ep)) == 0)
144 		return (-1);
145 
146 	if (verbose)
147 		(void) printf("%8.8s\t\t%6.6s\t%6.6s\n",
148 		    gettext("Mediator"), gettext("Status"),
149 		    gettext("Golden"));
150 
151 	for (i = 0; i < max_meds; i++) {
152 
153 		if (sd->sd_med.n_lst[i].a_cnt == 0)
154 			continue;
155 
156 		(void) memset(&medd, '\0', sizeof (medd));
157 		(void) memset(&mdh, '\0', sizeof (mdh));
158 		mdh = sd->sd_med.n_lst[i];	/* structure assignment */
159 
160 		if (verbose)
161 			(void) printf("%-17.17s\t",
162 			    sd->sd_med.n_lst[i].a_nm[0]);
163 
164 		if (clnt_med_get_data(&mdh, sp, &medd, ep) == -1) {
165 			if (mdanyrpcerror(ep)) {
166 				if (verbose)
167 					(void) printf("%s\n",
168 					    gettext("Unreachable"));
169 				continue;
170 			} else if (mdiserror(ep, MDE_MED_ERROR)) {
171 				if (verbose)
172 					(void) printf("%s\n",
173 					    gettext("Bad"));
174 			} else {
175 				if (verbose)
176 					(void) printf("%s\n",
177 					    gettext("Fatal"));
178 			}
179 			mde_perror(ep, "");
180 			if (mdiserror(ep, MDE_MED_ERROR))
181 				continue;
182 			md_exit(sp, 1);
183 		}
184 
185 		if (verbose)
186 			(void) printf("%s", gettext("Ok"));
187 
188 		if (medd.med_dat_fl & MED_DFL_GOLDEN) {
189 			if (verbose)
190 				(void) printf("\t%s",
191 				    gettext("Yes"));
192 			golden++;
193 		} else {
194 			if (verbose)
195 				(void) printf("\t%s",
196 				    gettext("No"));
197 		}
198 
199 		if (verbose)
200 			(void) printf("\n");
201 
202 		medok++;
203 	}
204 
205 	if (golden)
206 		md_exit(sp, 0);
207 
208 	if (medok < ((sd->sd_med.n_cnt / 2) + 1))
209 		md_exit(sp, 1);
210 
211 	md_exit(sp, 0);
212 	/*NOTREACHED*/
213 	return (0);
214 }
215