157580Smjacob/* $FreeBSD: releng/10.3/share/examples/ses/srcs/chpmon.c 198934 2009-11-04 23:36:23Z delphij $ */
257580Smjacob/*
357580Smjacob * Copyright (c) 2000 by Matthew Jacob
457580Smjacob * All rights reserved.
557580Smjacob *
657580Smjacob * Redistribution and use in source and binary forms, with or without
757580Smjacob * modification, are permitted provided that the following conditions
857580Smjacob * are met:
957580Smjacob * 1. Redistributions of source code must retain the above copyright
1057580Smjacob *    notice, this list of conditions, and the following disclaimer,
1157580Smjacob *    without modification, immediately at the beginning of the file.
1257580Smjacob * 2. The name of the author may not be used to endorse or promote products
1357580Smjacob *    derived from this software without specific prior written permission.
1457580Smjacob *
1557580Smjacob * Alternatively, this software may be distributed under the terms of the
1657580Smjacob * the GNU Public License ("GPL").
1757580Smjacob *
1857580Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1957580Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2057580Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2157580Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2257580Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2357580Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2457580Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2557580Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2657580Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2757580Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2857580Smjacob * SUCH DAMAGE.
2957580Smjacob *
3057580Smjacob * Matthew Jacob
3157580Smjacob * Feral Software
3257580Smjacob * mjacob@feral.com
3357580Smjacob */
3457580Smjacob#include <unistd.h>
3557580Smjacob#include <stdlib.h>
3657580Smjacob#include <stdio.h>
3757580Smjacob#include <fcntl.h>
3857580Smjacob#include <errno.h>
3957580Smjacob#include <sys/ioctl.h>
4057580Smjacob#include "ses.h"
4157580Smjacob
4257580Smjacob/*
4357580Smjacob * Continuously monitor all named SES devices
4457580Smjacob * and turn all but INFO enclosure status
4557580Smjacob * values into CRITICAL enclosure status.
4657580Smjacob */
4757580Smjacob#define	BADSTAT	\
4857580Smjacob	(SES_ENCSTAT_UNRECOV|SES_ENCSTAT_CRITICAL|SES_ENCSTAT_NONCRITICAL)
4957580Smjacobint
50198934Sdelphijmain(int a, char **v)
5157580Smjacob{
5257580Smjacob	int fd, delay, dev;
5357580Smjacob	ses_encstat stat, *carray;
5457580Smjacob
5557580Smjacob	if (a < 3) {
5657580Smjacob		fprintf(stderr, "usage: %s polling-interval device "
5757580Smjacob		    "[ device ... ]\n", *v);
5857580Smjacob		return (1);
5957580Smjacob	}
6057580Smjacob	delay = atoi(v[1]);
6157580Smjacob	carray = malloc(a);
6257580Smjacob	if (carray == NULL) {
6357580Smjacob		perror("malloc");
6457580Smjacob		return (1);
6557580Smjacob	}
6657580Smjacob	bzero((void *)carray, a);
6757580Smjacob
6857580Smjacob	for (;;) {
6957580Smjacob		for (dev = 2; dev < a; dev++) {
7057580Smjacob			fd = open(v[dev], O_RDWR);
7157580Smjacob			if (fd < 0) {
7257580Smjacob				perror(v[dev]);
7357580Smjacob				continue;
7457580Smjacob			}
7557580Smjacob			/*
7657580Smjacob			 * First clear any enclosure status, in case it is
7757580Smjacob			 * a latched status.
7857580Smjacob			 */
7957580Smjacob			stat = 0;
8057580Smjacob			if (ioctl(fd, SESIOC_SETENCSTAT, (caddr_t) &stat) < 0) {
8157580Smjacob				fprintf(stderr, "%s: SESIOC_SETENCSTAT1: %s\n",
8257580Smjacob				    v[dev], strerror(errno));
8357580Smjacob				(void) close(fd);
8457580Smjacob				continue;
8557580Smjacob			}
8657580Smjacob			/*
8757580Smjacob			 * Now get the actual current enclosure status.
8857580Smjacob			 */
8957580Smjacob			if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &stat) < 0) {
9057580Smjacob				fprintf(stderr, "%s: SESIOC_GETENCSTAT: %s\n",
9157580Smjacob				    v[dev], strerror(errno));
9257580Smjacob				(void) close(fd);
9357580Smjacob				continue;
9457580Smjacob			}
9557580Smjacob
9657580Smjacob			if ((stat & BADSTAT) == 0) {
9757580Smjacob				if (carray[dev]) {
9857580Smjacob					fprintf(stdout, "%s: Clearing CRITICAL "
9957580Smjacob					    "condition\n", v[dev]);
10057580Smjacob					carray[dev] = 0;
10157580Smjacob				}
10257580Smjacob				(void) close(fd);
10357580Smjacob				continue;
10457580Smjacob			}
10557580Smjacob			carray[dev] = 1;
10657580Smjacob			fprintf(stdout, "%s: Setting CRITICAL from:", v[dev]);
10757580Smjacob			if (stat & SES_ENCSTAT_UNRECOV)
10857580Smjacob				fprintf(stdout, " UNRECOVERABLE");
10957580Smjacob
11057580Smjacob			if (stat & SES_ENCSTAT_CRITICAL)
11157580Smjacob				fprintf(stdout, " CRITICAL");
11257580Smjacob
11357580Smjacob			if (stat & SES_ENCSTAT_NONCRITICAL)
11457580Smjacob				fprintf(stdout, " NONCRITICAL");
11557580Smjacob			putchar('\n');
11657580Smjacob			stat = SES_ENCSTAT_CRITICAL;
11757580Smjacob			if (ioctl(fd, SESIOC_SETENCSTAT, (caddr_t) &stat) < 0) {
11857580Smjacob				fprintf(stderr, "%s: SESIOC_SETENCSTAT 2: %s\n",
11957580Smjacob				    v[dev], strerror(errno));
12057580Smjacob			}
12157580Smjacob			(void) close(fd);
12257580Smjacob		}
12357580Smjacob		sleep(delay);
12457580Smjacob	}
12557580Smjacob	/* NOTREACHED */
12657580Smjacob}
127