sesd.c revision 1.2
1/* $NetBSD: sesd.c,v 1.2 2000/02/22 06:06:08 mjacob Exp $ */
2/* $FreeBSD: $ */
3/* $OpenBSD: $ */
4/*
5 * Copyright (c) 2000 by Matthew Jacob
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions, and the following disclaimer,
13 *    without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * the GNU Public License ("GPL").
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * Matthew Jacob
33 * Feral Software
34 * mjacob@feral.com
35 */
36#include <unistd.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <fcntl.h>
40#include <errno.h>
41#include <string.h>
42#include <syslog.h>
43#include <sys/ioctl.h>
44#include SESINC
45
46#define	ALLSTAT (SES_ENCSTAT_UNRECOV | SES_ENCSTAT_CRITICAL | \
47	SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO)
48int main __P((int, char **));
49
50/*
51 * Monitor named SES devices and note (via syslog) any changes in status.
52 */
53
54int
55main(a, v)
56	int a;
57	char **v;
58{
59	static char *usage =
60	    "usage: %s [ -d ] [ -t pollinterval ] device [ device ]\n";
61	int fd, polltime, dev, devbase, nodaemon;
62	ses_encstat stat, *carray;
63
64	if (a < 2) {
65		fprintf(stderr, usage, *v);
66		return (1);
67	}
68
69	devbase = 1;
70
71	if (strcmp(v[1], "-d") == 0) {
72		nodaemon = 1;
73		devbase++;
74	} else {
75		nodaemon = 0;
76	}
77
78	if (a > 2 && strcmp(v[2], "-t") == 0) {
79		devbase += 2;
80		polltime = atoi(v[3]);
81	} else {
82		polltime = 30;
83	}
84
85	carray = malloc(a);
86	if (carray == NULL) {
87		perror("malloc");
88		return (1);
89	}
90	for (dev = devbase; dev < a; dev++)
91		carray[dev] = (ses_encstat) -1;
92
93	/*
94	 * Check to make sure we can open all devices
95	 */
96	for (dev = devbase; dev < a; dev++) {
97		fd = open(v[dev], O_RDWR);
98		if (fd < 0) {
99			perror(v[dev]);
100			return (1);
101		}
102		if (ioctl(fd, SESIOC_INIT, NULL) < 0) {
103			fprintf(stderr, "%s: SESIOC_INIT fails- %s\n",
104			    v[dev], strerror(errno));
105			return (1);
106		}
107		(void) close(fd);
108	}
109	if (nodaemon == 0) {
110		if (daemon(0, 0) < 0) {
111			perror("daemon");
112			return (1);
113		}
114		openlog("sesd", LOG_CONS, LOG_USER);
115	} else {
116		openlog("sesd", LOG_CONS|LOG_PERROR, LOG_USER);
117	}
118
119	for (;;) {
120		for (dev = devbase; dev < a; dev++) {
121			fd = open(v[dev], O_RDWR);
122			if (fd < 0) {
123				syslog(LOG_ERR, "%s: %m", v[dev]);
124				continue;
125			}
126
127			/*
128			 * Get the actual current enclosure status.
129			 */
130			if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &stat) < 0) {
131				syslog(LOG_ERR,
132				    "%s: SESIOC_GETENCSTAT- %m", v[dev]);
133				(void) close(fd);
134				continue;
135			}
136			(void) close(fd);
137
138			if (stat == carray[dev])
139				continue;
140
141			carray[dev] = stat;
142			if ((stat & ALLSTAT) == 0) {
143				syslog(LOG_NOTICE,
144				    "%s: Enclosure Status OK", v[dev]);
145			}
146			if (stat & SES_ENCSTAT_INFO) {
147				syslog(LOG_INFO,
148				    "%s: Enclosure Status Has Information",
149				    v[dev]);
150			}
151			if (stat & SES_ENCSTAT_NONCRITICAL) {
152				syslog(LOG_WARNING,
153				    "%s: Enclosure Non-Critical", v[dev]);
154			}
155			if (stat & SES_ENCSTAT_CRITICAL) {
156				syslog(LOG_CRIT,
157				    "%s: Enclosure Critical", v[dev]);
158			}
159			if (stat & SES_ENCSTAT_UNRECOV) {
160				syslog(LOG_ALERT,
161				    "%s: Enclosure Unrecoverable", v[dev]);
162			}
163		}
164		sleep(polltime);
165	}
166	/* NOTREACHED */
167}
168