xref: /freebsd/share/examples/ses/srcs/chpmon.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
1daf1cffcSMatt Jacob /*
2daf1cffcSMatt Jacob  * Copyright (c) 2000 by Matthew Jacob
3daf1cffcSMatt Jacob  * All rights reserved.
4daf1cffcSMatt Jacob  *
5daf1cffcSMatt Jacob  * Redistribution and use in source and binary forms, with or without
6daf1cffcSMatt Jacob  * modification, are permitted provided that the following conditions
7daf1cffcSMatt Jacob  * are met:
8daf1cffcSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
9daf1cffcSMatt Jacob  *    notice, this list of conditions, and the following disclaimer,
10daf1cffcSMatt Jacob  *    without modification, immediately at the beginning of the file.
11daf1cffcSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
12daf1cffcSMatt Jacob  *    derived from this software without specific prior written permission.
13daf1cffcSMatt Jacob  *
14daf1cffcSMatt Jacob  * Alternatively, this software may be distributed under the terms of the
15daf1cffcSMatt Jacob  * the GNU Public License ("GPL").
16daf1cffcSMatt Jacob  *
17daf1cffcSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18daf1cffcSMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19daf1cffcSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20daf1cffcSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21daf1cffcSMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22daf1cffcSMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23daf1cffcSMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24daf1cffcSMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25daf1cffcSMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26daf1cffcSMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27daf1cffcSMatt Jacob  * SUCH DAMAGE.
28daf1cffcSMatt Jacob  *
29daf1cffcSMatt Jacob  * Matthew Jacob
30daf1cffcSMatt Jacob  * Feral Software
31daf1cffcSMatt Jacob  * mjacob@feral.com
32daf1cffcSMatt Jacob  */
33daf1cffcSMatt Jacob #include <unistd.h>
34daf1cffcSMatt Jacob #include <stdlib.h>
35daf1cffcSMatt Jacob #include <stdio.h>
36daf1cffcSMatt Jacob #include <fcntl.h>
37daf1cffcSMatt Jacob #include <errno.h>
38daf1cffcSMatt Jacob #include <sys/ioctl.h>
39daf1cffcSMatt Jacob #include "ses.h"
40daf1cffcSMatt Jacob 
41daf1cffcSMatt Jacob /*
42daf1cffcSMatt Jacob  * Continuously monitor all named SES devices
43daf1cffcSMatt Jacob  * and turn all but INFO enclosure status
44daf1cffcSMatt Jacob  * values into CRITICAL enclosure status.
45daf1cffcSMatt Jacob  */
46daf1cffcSMatt Jacob #define	BADSTAT	\
47daf1cffcSMatt Jacob 	(SES_ENCSTAT_UNRECOV|SES_ENCSTAT_CRITICAL|SES_ENCSTAT_NONCRITICAL)
48daf1cffcSMatt Jacob int
main(int a,char ** v)492804a96aSXin LI main(int a, char **v)
50daf1cffcSMatt Jacob {
51daf1cffcSMatt Jacob 	int fd, delay, dev;
52daf1cffcSMatt Jacob 	ses_encstat stat, *carray;
53daf1cffcSMatt Jacob 
54daf1cffcSMatt Jacob 	if (a < 3) {
55daf1cffcSMatt Jacob 		fprintf(stderr, "usage: %s polling-interval device "
56daf1cffcSMatt Jacob 		    "[ device ... ]\n", *v);
57daf1cffcSMatt Jacob 		return (1);
58daf1cffcSMatt Jacob 	}
59daf1cffcSMatt Jacob 	delay = atoi(v[1]);
60daf1cffcSMatt Jacob 	carray = malloc(a);
61daf1cffcSMatt Jacob 	if (carray == NULL) {
62daf1cffcSMatt Jacob 		perror("malloc");
63daf1cffcSMatt Jacob 		return (1);
64daf1cffcSMatt Jacob 	}
65daf1cffcSMatt Jacob 	bzero((void *)carray, a);
66daf1cffcSMatt Jacob 
67daf1cffcSMatt Jacob 	for (;;) {
68daf1cffcSMatt Jacob 		for (dev = 2; dev < a; dev++) {
69daf1cffcSMatt Jacob 			fd = open(v[dev], O_RDWR);
70daf1cffcSMatt Jacob 			if (fd < 0) {
71daf1cffcSMatt Jacob 				perror(v[dev]);
72daf1cffcSMatt Jacob 				continue;
73daf1cffcSMatt Jacob 			}
74daf1cffcSMatt Jacob 			/*
75daf1cffcSMatt Jacob 			 * First clear any enclosure status, in case it is
76daf1cffcSMatt Jacob 			 * a latched status.
77daf1cffcSMatt Jacob 			 */
78daf1cffcSMatt Jacob 			stat = 0;
79daf1cffcSMatt Jacob 			if (ioctl(fd, SESIOC_SETENCSTAT, (caddr_t) &stat) < 0) {
80daf1cffcSMatt Jacob 				fprintf(stderr, "%s: SESIOC_SETENCSTAT1: %s\n",
81daf1cffcSMatt Jacob 				    v[dev], strerror(errno));
82daf1cffcSMatt Jacob 				(void) close(fd);
83daf1cffcSMatt Jacob 				continue;
84daf1cffcSMatt Jacob 			}
85daf1cffcSMatt Jacob 			/*
86daf1cffcSMatt Jacob 			 * Now get the actual current enclosure status.
87daf1cffcSMatt Jacob 			 */
88daf1cffcSMatt Jacob 			if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &stat) < 0) {
89daf1cffcSMatt Jacob 				fprintf(stderr, "%s: SESIOC_GETENCSTAT: %s\n",
90daf1cffcSMatt Jacob 				    v[dev], strerror(errno));
91daf1cffcSMatt Jacob 				(void) close(fd);
92daf1cffcSMatt Jacob 				continue;
93daf1cffcSMatt Jacob 			}
94daf1cffcSMatt Jacob 
95daf1cffcSMatt Jacob 			if ((stat & BADSTAT) == 0) {
96daf1cffcSMatt Jacob 				if (carray[dev]) {
97daf1cffcSMatt Jacob 					fprintf(stdout, "%s: Clearing CRITICAL "
98daf1cffcSMatt Jacob 					    "condition\n", v[dev]);
99daf1cffcSMatt Jacob 					carray[dev] = 0;
100daf1cffcSMatt Jacob 				}
101daf1cffcSMatt Jacob 				(void) close(fd);
102daf1cffcSMatt Jacob 				continue;
103daf1cffcSMatt Jacob 			}
104daf1cffcSMatt Jacob 			carray[dev] = 1;
105daf1cffcSMatt Jacob 			fprintf(stdout, "%s: Setting CRITICAL from:", v[dev]);
106daf1cffcSMatt Jacob 			if (stat & SES_ENCSTAT_UNRECOV)
107daf1cffcSMatt Jacob 				fprintf(stdout, " UNRECOVERABLE");
108daf1cffcSMatt Jacob 
109daf1cffcSMatt Jacob 			if (stat & SES_ENCSTAT_CRITICAL)
110daf1cffcSMatt Jacob 				fprintf(stdout, " CRITICAL");
111daf1cffcSMatt Jacob 
112daf1cffcSMatt Jacob 			if (stat & SES_ENCSTAT_NONCRITICAL)
113daf1cffcSMatt Jacob 				fprintf(stdout, " NONCRITICAL");
114daf1cffcSMatt Jacob 			putchar('\n');
115daf1cffcSMatt Jacob 			stat = SES_ENCSTAT_CRITICAL;
116daf1cffcSMatt Jacob 			if (ioctl(fd, SESIOC_SETENCSTAT, (caddr_t) &stat) < 0) {
117daf1cffcSMatt Jacob 				fprintf(stderr, "%s: SESIOC_SETENCSTAT 2: %s\n",
118daf1cffcSMatt Jacob 				    v[dev], strerror(errno));
119daf1cffcSMatt Jacob 			}
120daf1cffcSMatt Jacob 			(void) close(fd);
121daf1cffcSMatt Jacob 		}
122daf1cffcSMatt Jacob 		sleep(delay);
123daf1cffcSMatt Jacob 	}
124daf1cffcSMatt Jacob 	/* NOTREACHED */
125daf1cffcSMatt Jacob }
126