Raspberry Pi树莓派看门狗解决方案.docx
- 文档编号:1149444
- 上传时间:2022-10-18
- 格式:DOCX
- 页数:11
- 大小:61.12KB
Raspberry Pi树莓派看门狗解决方案.docx
《Raspberry Pi树莓派看门狗解决方案.docx》由会员分享,可在线阅读,更多相关《Raspberry Pi树莓派看门狗解决方案.docx(11页珍藏版)》请在冰豆网上搜索。
RaspberryPi树莓派看门狗解决方案
1RaspberryPi树莓派看门狗功能解决方案
1.1解决方案概述
RaspberryPi树莓派系统在实际的应用过程中,特别是作为产品级应用中,设备系统的稳定可靠性是非常重要的,本方案以产品级应用为目标,针对树莓派系统设计专用于看门狗功能方案,以此保障树莓派系统的长期稳定运行,提升设备的稳定可靠性性。
本方案分别从硬件看门狗和软件看门狗两方面进行详细讲解实现的原理和过程。
硬件看门狗从系统层面为设备提供保障,软件看门狗从应用层面为设备提供保障,两者互为结合,共同发挥保障作用。
1.2硬件看门狗
看门狗,又叫watchdog,看门狗的作用就是防止程序发生死循环或者说程序跑飞。
硬件看门狗,是一个定时器电路,一般有一个输入,叫喂狗,每隔一段时间输出一个信号到喂狗端,给WDT清零,如果超过规定时间不喂狗(一般在程序跑飞时),定时超过,就会给出一个复位信号,使系统复位.防止系统死机。
在树莓派上设置看门狗,可实现树莓派在死机时,能够自动重启。
树莓派CPU芯片BCM2708中有硬件看门狗功能。
一、启动硬件看门狗模块(需要确认是否存在)
启用模块bcm2708_wdog
sudomodprobebcm2708_wdog
注意:
硬件看门狗芯片不一定命名为bcm2708_wdog,不同的硬件版本名字会有变化,如下所示。
sudomodprobebcm2708_wdog
echobcm2708_wdog|sudotee-a/etc/modules
异常:
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('PING','RESTART_ENABLE')
ifenable==0:
self.logger.info('Noneedcheck.')
returnenable=='1'
self.logger.info('Noneedcheck.')
returnFalse
defget_check_times(self):
ifnotos.path.exists(self.chk_ini):
return0
pingcf=Myparser()
pingcf.read(self.chk_ini)
ifpingcf.has_option('PING','TRY_TIMES'):
times=pingcf.get('PING','TRY_TIMES')
returnint(times)
return0
defset_check_times(self,times):
#ifnotos.path.exists(self.chk_ini):
#returnFalse
pingcf=Myparser()
pingcf.add_section('PING')
pingcf.set('PING','TRY_TIMES',times)
pingcf.write(open(self.chk_ini,'w'))
returnTrue
def_get_delay_limit(self):
delayThreshold=500
ifnotos.path.exists(self.ping_ini):
returndelayThreshold
pingcf=Myparser()
pingcf.read(self.ping_ini)
ifnotpingcf.has_option('PING','DELAY_VALUE'):
returndelayThreshold
tmp=pingcf.get('PING','DELAY_VALUE')
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
try:
ip=get_ip('eth0')
exceptIOError:
return'OFFLINE'
#hostname=socket.gethostname()
returnip
defcheck_linked(self,ifname='eth0'):
ret=self._get_ipaddr()
ifret=='OFFLINE':
self.logger.warning('Nowirelink')
returnFalse
else:
self.logger.info('Findwirelink')
returnTrue
defping_route(self,timeout=3):
route=ipconfig2.get_default_route('eth0')
try:
cmd="ping-c9%s"%route
ret=os.popen(cmd).read()
#printret
if'0received'inret:
#简单的判断,不要用含0的次数
self.logger.warning('P
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Raspberry Pi树莓派看门狗解决方案 Pi 树莓派 看门狗 解决方案
![提示](https://static.bdocx.com/images/bang_tan.gif)