Raspberry Pi树莓派看门狗解决方案Word格式.docx
《Raspberry Pi树莓派看门狗解决方案Word格式.docx》由会员分享,可在线阅读,更多相关《Raspberry Pi树莓派看门狗解决方案Word格式.docx(11页珍藏版)》请在冰豆网上搜索。
modprobe:
FATAL:
Modulebcm2708_wdognotfound.
出现上面错误请使用下面的命令:
原因是名称修改了
sudomodprobebcm2835_wdt
echobcm2835_wdt|sudotee-a/etc/modules
编辑/etc/modules,实现启动时自动加载bcm2708_wdog模块
sudonano/etc/modules
在文件最后一行,添加一行
bcm2708_wdog
启用模块后,则系统增加了一个设备/dev/watchdog
每10秒必须喂一下这个狗
二、配置watchdog
安装watchdog
sudoapt-getinstallwatchdog
启动watchdog软件
sudo/etc/init.d/watchdogstart
编辑/etc/watchdog.conf配置文件
sudonano/etc/watchdog.conf
在文件中有很多预设的定义,初始是都被注释掉了
找到#watchdog-device=/dev/watchdog这一行
删掉行首的#号
除了这个外,watchdog.conf还可以定义,如用ping不到某主机则重启等,其它具体参数请参考下图进行修改。
重启watchdog软件
sudo/etc/init.d/watchdogrestart
OK了
三、测试
最后,确定没有重要的任务在执行,且已去激活交换分区(swappartitionorfile)
输入下述命令(forkbomb),测试一下看门狗
:
(){:
|:
&
};
1.3软件看门狗
软件看门狗,就是在应用层通过应用程序的方式实现系统关键资源及运行状态的定期监测与控制功能,以达到看门狗的功能效果。
本方案提供的软件看门狗分别从常出现的网络故障以及U盘故障两方面进行设计和实现,其它功能的实现方法可以类同参考,举一反三即可。
1.3.1网络故障看门狗
在产品级的树莓派产品应用过程中,特别是作为一个网络设备产品时,有可能会出现应用层面的网络故障,这时候如果对设备进行网络重启或设备重启操作,是有可能修复的。
本方案设计的网络故障看门狗基本实现思路如下:
1、制作网络故障检测脚本,主动去PING网关设备10次;
2、统计10次PING操作的执行情况,包括成功次数、平均延时等
3、综合统计数据情况,如果达到异常阈值,比如延时过大、丢包次数过多等,则进行重启网卡或系统。
4、多次重复执行失败后,则可以根据实际需要进行故障报警通知等操作。
5、网络故障检测脚本由crontab定时任务定期启动执行,达到持续定期检测的目的。
以下为网络故障检测脚本代码,仅供参考:
classCheckNetwork():
def__init__(self):
self.logger=None
self._initLogger(checkfile.getPythonLogDir()+'
pingTest.log'
)
self.ping_ini='
/home/pi/tde/params/pingTest.ini'
self.chk_ini=checkfile.getGlobalStatusDir()+'
pingCheck.log'
self._delay_val=self._get_delay_limit()
def_initLogger(self,logfile):
self.logger=logging.getLogger('
pingTest'
self.logger.setLevel(logging.DEBUG)
ifos.path.exists(logfile):
os.system('
sudochmod-R777%s'
%(logfile))
#setsplitteris100k,andonly1backup
fh=logging.handlers.RotatingFileHandler(logfile,
maxBytes=1*10**5,backupCount=1)
fh.setLevel(logging.INFO)
formatter=logging.Formatter('
%(asctime)s-%(name)s-%(levelname)s-%(message)s'
)
fh.setFormatter(formatter)
console=logging.StreamHandler()
console.setLevel(logging.DEBUG)
self.logger.addHandler(fh)
self.logger.addHandler(console)
defneed_check(self):
ifnotos.path.exists(self.ping_ini):
self.logger.info('
Noneedcheck.'
returnFalse
pingcf=Myparser()
pingcf.read(self.ping_ini)
ifpingcf.has_option('
PING'
'
RESTART_ENABLE'
):
enable=pingcf.get('
ifenable==0:
returnenable=='
1'
defget_check_times(self):
ifnotos.path.exists(self.chk_ini):
return0
pingcf.read(self.chk_ini)
TRY_TIMES'
times=pingcf.get('
returnint(times)
defset_check_times(self,times):
#ifnotos.path.exists(self.chk_ini):
#returnFalse
pingcf.add_section('
pingcf.set('
times)
pingcf.write(open(self.chk_ini,'
w'
))
returnTrue
def_get_delay_limit(self):
delayThreshold=500
returndelayThreshold
ifnotpingcf.has_option('
DELAY_VALUE'
tmp=pingcf.get('
try:
delayThreshold=float(tmp)
exceptException,e:
self.logger.error('
Errordelaythreshold.Usedefault!
'
return500
print'
delaythreshold:
delayThreshold
returnfloat(delayThreshold)
def_get_ipaddr(self):
defget_ip(ifname):
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ipaddr=socket.inet_ntoa(fcntl.ioctl(
sock.fileno(),
0x8915,#SIOCGIFADDR
struct.pack('
256s'
ifname[:
15])
)[20:
24]
)
sock.close()
returnipaddr
ip=get_ip('
eth0'
exceptIOError:
return'
OFFLINE'
#hostname=socket.gethostname()
returnip
defcheck_linked(self,ifname='
ret=self._get_ipaddr()
ifret=='
self.logger.warning('
Nowirelink'
else:
Findwirelink'
defping_route(self,timeout=3):
route=ipconfig2.get_default_route('
cmd="
ping-c9%s"
%route
ret=os.popen(cmd).read()
#printret
if'
0received'
inret:
#简单的判断,不要用含0的次数
P