|
|
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.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>
#include <paths.h>
#ifndef SIGPWR
# define SIGPWR SIGUSR1
#endif
#define PWRSTAT "/etc/powerstatus"
/* Tell init that the power has gone (1), is back (0),
or the UPS batteries are low (2). */
void powerfail(int event)
{
int fd;
/* Create an info file for init. */
unlink (PWRSTAT);
if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
switch (event)
{
case 0 :
write(fd, "OK\n", 3);
break;
case 1:
write(fd, "WAIT\n", 5);
break;
case 2:
default:
write(fd, "FAIL\n", 4);
break;
}
close(fd);
}
kill(1, SIGPWR);
}
/* Main program. */
int main(int argc, char *argv[])
{
int DCD;
if (argc < 2) {
fprintf(stderr, "Usage: turn [0|1]\n");
exit(1);
}/* Start syslog. */
openlog("turn", LOG_CONS, LOG_DAEMON);
DCD = atoi(argv[1]);
if(DCD != 0) powerfail(0); else powerfail(1);
}
以上是在192.168.7.112上的
turn.c程式
gcc turn.c -o turn
以下是/etc/inittab中的几行
pf::powerfail:/sbin/shutdown -f -h +30 " ower Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c " ower Restored; Shutdown Cancelled"
在有电源检测的服务器上
shd
/usr/bin/ssh root@192.168.7.112 /root/turn 0
/sbin/shutdown -f -h +30 " ower Failure; System Shutting Down"
备注:可以用ssh给别人发送执行命令。。。
shc
/usr/bin/ssh root@192.168.7.112 /root/turn 1
/sbin/shutdown -c " ower Restored; Shutdown Cancelled"
当然服务器上有如些一服务在运行。。见
powerd.c
这一套是公司的一个牛人做的。。。硬件我就不懂了。。。用RSA232与电脑联接。。
还有是一个非UPS上的220V-->。。。设备很小。。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|