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