荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: Mill (烂机一部!), 信区: Linux
标 题: 短电后自动down你的linux server(使用简易的UPS)
发信站: BBS 荔园晨风站 (Fri Nov 13 09:57:09 1998), 站内信件
pg.c - Power Guide of linux box
短电后自动down你的linux server(使用简易的UPS)
系统配置:1.要求有一个HUB和电源直接连接(不用UPS),如果停电
则这个HUB也停止工作了。
2.要保护的机器有一块网卡和1描述的HUB连接(双绞线)。
如果你的机器只有一块网卡和校网的HUB连接,那就再加
块卡吧。
3.你的机器必须用UPS(可以是没有串口的那种简易UPS)。
用法: 1.下面的源程序是为ne2000兼容卡写的。如果你的卡不是
ne2000兼容,那就只能由你自己来修改代码了(很简单的,hehe)。
2.如果你的卡恰好是ne2000兼容,也必须把代码中
#define BASEADDR 0x300
定义为你的网卡的I/O基址。
3.用 gcc -O1 -o pg pg.c编译
4.在 /etc/rc.d/rc.local中启动这个daemon.
原理: 定时检查网卡的状态,如果停电,那么HUB停止工作,网卡
lost carrier。这样,即使没有那种有串口的UPS,你的linux
也知道停电了。在第一次检测到网卡lost carrier后的3-5分钟
内,linux halt。如果在此期间,恢复供电则取消halt的操作。
移植: 如果你用的不是ne2000的卡,那只好劳您大驾改改程序了。主要是
修改两个常数的定义:
#define ENTSR_CRS 0x10 /* The carrier sense was lost. */
#define EN0_TSR 0x04 /* Transmit status reg RD */
这两个常数的值和网卡有关,可以在
/usr/src/linux/drivers/net目录下和你的卡对应的头文件中找到。
有些卡没有这个寄存器,但可以找到其他方法检验是否lost carrier。
--------------cut here
/*
* pg.c
* Power Guide v0.99 - Shutdown machine when power fails without Smart UPS
*
* by Alan Wang(wyong@huaguang.cs.zju.edu.cn) in Zhejiang Univ.
* 9/28/1998
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <asm/io.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <syslog.h>
#include <string.h>
#define BASEADDR 0x300
#define ENTSR_CRS 0x10 /* The carrier sense was lost. */
#define EN0_TSR 0x04 /* Transmit status reg RD */
/* Main program. */
int main()
{
int count = 0;
unsigned char txsr;
/* Start syslog. */
openlog("Power Guide", LOG_CONS|LOG_PERROR, LOG_DAEMON);
/* Daemonize. */
switch(fork()) {
case 0: /* Child */
closelog();
setsid();
break;
case -1: /* Error */
syslog(LOG_ERR, "can't fork.");
closelog();
exit(1);
default: /* Parent */
closelog();
exit(0);
}
/* Restart syslog. */
openlog("Power Guide", LOG_CONS, LOG_DAEMON);
if(ioperm(0x80,1,1)||ioperm(BASEADDR+EN0_TSR,1,1)){
perror("ioperm");
exit(1);
}
sleep(300); /* I do not know why? */
while(1) {
txsr = inb_p(0x300+EN0_TSR);
if (txsr & ENTSR_CRS){
count++;
if (count == 5){
syslog(LOG_EMERG,"Power FAILED, going to be DOWN!");
system("/sbin/shutdown now -h");
}
else {
syslog(LOG_EMERG,"Power MAYBE fail, warning - %d",count);
sleep(60);
continue;
}
}
/* Reset count and sleep 1 minute */
count = 0;
sleep(60);
}
/* Never happens */
return(0);
}
如有疑问请和wyong@huaguang.cs.zju.edu.cn 联系!
--
┏━━━━━━━━━━━━━┯┓
┃ 弃我去者,昨日之日不可留, ┕┫
┃ 乱我心者,今日之日多烦忧。 ┃
┗━━━━━━━━━━━━━━┛
※ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.0.167]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店