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