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