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