解决方案

Linux watchdog配置

seo靠我 2023-09-24 10:22:41

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

前言一、watchdog是什么?1.硬件看门狗2.软件看门狗 二、使用步骤1.硬件看门狗2.软件看门狗 总结

前言

好久没写文章了,SEO靠我最近遇到一个蛋疼的问题,Linux内核假死的情况,简而言之就是内核在工作的过程中突然进入一种未知状态,不能正常工作了。watchdog主要有两种:第一种是硬件支持的,第二种是纯软件的。今天我们主要讲第SEO靠我二种,纯软件实现的。

一、watchdog是什么?

1.硬件看门狗

看门狗,又叫 watchdog,从本质上来说就是一个定时器电路,一般有一个输入和一个输出,其中输入叫做喂狗,输出一般连接到另外一个部分的复SEO靠我位端,一般是连接到单片机。 看门狗的功能是定期的查看芯片内部的情况,一旦发生错误就向芯片发出重启信号。看门狗命令在程序的中断中拥有最高的优先级。

注意:硬件看门狗本质上是电路,是物理层面的东西,本质上是SEO靠我不会受到干扰的。

2.软件看门狗

watchdog - a software watchdog daemon

其实就是一个后台服务,一直循环遍历任务,接下来我们详细展开。

二、使用步骤

1.硬件看门狗

以UbunSEO靠我tu-18.04为例,其他的系统请自行研究。我手上用的是orangepi-4,它的系统里自带硬件看门狗,具体查看方法是:

ls /dev/watchdog

如果配置了看门狗的系统会在dev下面有一个watSEO靠我chdog的设备文件,我们操作这个设备文件就可以了。

#include <stdio.h> #include <stdlib.h> #include <string.h>SEO靠我 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #iSEO靠我nclude <fcntl.h> #include <sys/ioctl.h> #include <errno.h> #include <sys/timSEO靠我e.h> #include <unistd.h> #include <time.h> #include <getopt.h> #inclSEO靠我ude <sys/signal.h> #include <termios.h>struct watchdog_info{unsigned int options; //options SEO靠我the card/driver supprots 19unsigned int firmware_version; //firmcard version of the cardunsigned chaSEO靠我r identity[32]; //identity of the board 21};#define WATCHDOG_IOCTL_BASE W #define WDIOC_GETSSEO靠我UPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info) #define WDIOC_SETTIMEOUT _IOWR(WATSEO靠我CHDOG_IOCTL_BASE, 6, int) #define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int) //27 SEO靠我 #define WDIOS_DISABLECARD 0x0001 #define WDIOS_ENABLECARD 0x0002 #define WDIOCSEO靠我_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int) #define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASEO靠我SE, 5, int)int Getch (void) //无回显的从屏幕输入字符,来达到喂狗的目的{int ch;struct termios oldt, newt; //终端设备结构体tcgetaSEO靠我ttr(STDIN_FILENO, &oldt); //获得终端属性newt = oldt;newt.c_lflag &= ~(ECHO|ICANON); //设置无回显属性tcsetattr(STDSEO靠我IN_FILENO, TCSANOW, &newt); //设置新的终端属性ch = getchar(); //从键盘输入一个数据tcsetattr(STDIN_FILENO, TCSANOW, &oSEO靠我ldt); //恢复终端设备初始设置return ch;}//suspend some seconds int zsleep(int millisecond){unsigned lonSEO靠我g usec;usec=1000*millisecond;usleep(usec); //usleep(1)睡眠一微秒(10E-6),这里也就是0.1s } int ISEO靠我nit() {int fd;//open device filefd = open("/dev/watchdog",O_RDWR); //打开看门狗设备if(fd < 0){printSEO靠我f("device open fail\n");return -1;}printf("open success\n");return fd; }int main(int argc,chSEO靠我ar **argv) {int fd,ch;int i,j;char c;struct watchdog_info wi;if(argc != 2){printf("Usage : .SEO靠我/watchdog 10\n");return -1;}fd=Init(); //打开终端看门狗设备ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);//读板SEO靠我卡信息,但不常用ioctl(fd,WDIOC_GETSUPPORT,&wi);printf("options is %d,identity is %s\n",wi.options,wi.identitSEO靠我y);//读看门狗溢出时间printf("put_usr return,if 0,success:%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i));printf("The olSEO靠我d reset time is: %d\n", i);//关闭i=WDIOS_DISABLECARD;//WDIOC_SETOPTIONS=0X0001printf("return ENOTTY,ifSEO靠我 -1,success:%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));//打开i=WDIOS_ENABLECARD;//WDIOS_ENABLECARD 0x0002priSEO靠我ntf("return ENOTTY,if -1,success:%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));i=atoi(argv[1]);printf("put_usSEO靠我er return,if 0,success:%d\n",ioctl(fd,WDIOC_SETTIMEOUT,&i));//读新的设置时间printf("put_usr return,if 0,sucSEO靠我cess:%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i));while(1){zsleep(100);if((c=Getch())!=27){//输入如果不是ESC,就喂狗,否SEO靠我则不喂狗,到时间后系统重启printf("keep alive \n");ioctl(fd,WDIOC_KEEPALIVE,NULL);//write(fd,NULL,1); //同样是喂狗}}cloSEO靠我se(fd); //关闭设备return 0; }

如果你和我一样是orangepi-4,这个代码你可以直接用,如果不是可能需要稍微修改下。

这段代码的意思是打开/dev/watchdogSEO靠我这个设备之后,必须在指定时间内向文件描述符中写入数据,如果超时没有写入,内核就重启系统。主要用途就是解决内核假死问题,由于是硬件定时器控制的,稳定性有保障,理论上除了硬件损坏以外,一定会执行。

2.软件SEO靠我看门狗

硬件看门狗需要在内核编译的时候打开,要不然操作系统是不支持的。换言之这是一个可以打开,也可以关闭的功能。接下来的软件层面的看门狗可以运行在没有硬件看门狗的操作系统里。

首先安装软件:

sudo apSEO靠我t update sudo apt install watchdog

接下来我们需要改2个配置文件,来实现我们需要监控的功能

/etc/default/watchdog oSEO靠我rangepi@orangepi4:~/wiringOP/examples$ cat /etc/default/watchdog # Start watchdog at boot tiSEO靠我me? 0 or 1 #是不是在系统启动的时候打开watchdog,1是打开,0是不打开 run_watchdog=1 # Start wd_keepaSEO靠我live after stopping watchdog? 0 or 1 #这个是保活机制,默认打开 run_wd_keepalive=1 # LoadSEO靠我 module before starting watchdog #维持原样 watchdog_module="none" # Specify addiSEO靠我tional watchdog options here (see manpage). #启动参数,具体参数下面贴出来 watchdog_options="-s -v SEO靠我-c /etc/watchdog.conf"

watchdog daemon options

watchdog [-F|--foreground] [-f|--force] [-c filename|--SEO靠我config-file filename][-v|--verbose] [-s|--sync] [-b|--softboot] [-q|--no-action]Available command liSEO靠我ne options are the following:-v, --verboseSet verbose mode. Only implemented if compiled with SYSLOGSEO靠我 feature. This mode willlog each several infos in LOG_DAEMON with priority LOG_INFO. This is useful SEO靠我if youwant to see exactly what happened until the watchdog rebooted the system. Currentlyit logs theSEO靠我 temperature (if available), the load average, the change date of thefiles it checks and how often iSEO靠我t went to sleep.-s, --syncTry to synchronize the filesystem every time the process is awake. Note thSEO靠我at thesystem is rebooted if for any reason the synchronizing lasts longer than a minute.-b, --softboSEO靠我otSoft-boot the system if an error occurs during the main loop, e.g. if a given fileis not accessiblSEO靠我e via the stat(2) call. Note that this does not apply to theopening of /dev/watchdog and /proc/loadaSEO靠我vg, which are opened before the main loopstarts.-F, --foregroundRun in foreground mode, useful for rSEO靠我unning under systemd (for example).-f, --forceForce the usage of the interval given or the maximal lSEO靠我oad average given in theconfig file. Without this option these values are sanity checked.-c config-fSEO靠我ile, --config-file config-fileUse config-file as the configuration file instead of the default/etc/wSEO靠我atchdog.conf.-q, --no-actionDo not reboot or halt the machine. This is for testing purposes. All cheSEO靠我cks areexecuted and the results are logged as usual, but no action is taken. Also yourhardware card SEO靠我or the kernel software watchdog driver is not enabled. Temperaturechecking is also disabled since thSEO靠我is triggers the hardware watchdog on some cards.

看不懂的可以用谷歌翻译翻一下,实在不懂可以留言问下我,程序员看英文的能力还是要有的。

/etc/watchSEO靠我dog.conf

这个才是我们今天的重头戏,我会详细说一说几个有用的配置 orangepi@orangepi4:~/wiringOP/examples$ cat /etc/watchdoSEO靠我g.conf #ping = 172.31.14.1 #ping = 172.26.1.255 interface = eth0 #fiSEO靠我le = /var/log/syslog #change = 1407# Uncomment to enable test. Setting one of these values tSEO靠我o 0 disables it. # These values will hopefully never reboot your machine during normal use SEO靠我 # (if your machine is really hung, the loadavg will go much higher than 25) #max-loadSEO靠我-1 = 24 #max-load-5 = 18 #max-load-15 = 12# Note that this is the number of pages! SEO靠我 # To get the real size, check how large the pagesize is on your machine. #min-memory SEO靠我= 1 #allocatable-memory = 1#repair-binary = /usr/sbin/repair #repair-timeout = 60 SEO靠我 #test-binary = #test-timeout = 60# The retry-timeout and repair limit are used to handSEO靠我le errors in a more robust # manner. Errors must persist for longer than retry-timeout to acSEO靠我tion a repair # or reboot, and if repair-maximum attempts are made without the test passing SEO靠我a # reboot is initiated anyway. #retry-timeout = 60 #repair-maximum = 1watchSEO靠我dog-device = /dev/watchdog# Defaults compiled into the binary temperature-sensor = /sys/clasSEO靠我s/thermal/thermal_zone0/temp max-temperature = 90# Defaults compiled into the binary SEO靠我 admin = root interval = 1 logtick = 1 log-dir = /var/log/watchdog# This greSEO靠我atly decreases the chance that watchdog wont be scheduled before # your machine is really loSEO靠我aded realtime = yes priority = 1# Check if rsyslogd is still running by enabling theSEO靠我 following line pidfile = /var/run/rsyslogd.pid pidfile = /var/run/inetd.pid SEO靠我 pidfile = /var/run/sshd.pid pidfile = /var/run/crond.pidwatchdog-timeout = 60# set heartbeaSEO靠我t setting # heartbeat-file = /var/log/watchdog/heartbeat.log # heartbeat-stamps = 30SEO靠我0

ping:可以配置多个选项,每隔一段时间会依次ping,如果有任何一个IP不通的话就会重启系统,这个过程会重试好多次,而不是一次。默认每4秒尝试一次。

interface:监视网卡数据吞吐,可以配置多SEO靠我个,依然是依次监视,如果任何一个网卡超时不吞吐数据就重启系统,这个过程会重试好多次,而不是一次。

file:设置监控文件模式发生改变,可以设置多个,依然是依次监视,如果任何一个文件模式发生改变就重启系统SEO靠我(比如rm文件一类的),这个过程会重试好多次,而不是一次。默认每20秒尝试一次。

change:配合file参数使用,单位是时间。

max-load-1:top命令就可以看到当前的数值,代表着1分钟的负载SEO靠我平均值,如果超过设置的值就重启系统,这个参数比较危险,和性能相关,不要胡乱设置。默认行注释或者数值写0都是禁用状态。

max-load-5:top命令就可以看到当前的数值,代表着5分钟的负载平均值,如果SEO靠我超过设置的值就重启系统,这个参数比较危险,和性能相关,不要胡乱设置。默认行注释或者数值写0都是禁用状态。

max-load-15:top命令就可以看到当前的数值,代表着15分钟的负载平均值,如果超过设置SEO靠我的值就重启系统,这个参数比较危险,和性能相关,不要胡乱设置。默认行注释或者数值写0都是禁用状态。

min-memory:允许的最小内存页数,这个地方不是指具体的MB或GB一类的,不懂的建议不要胡乱设置,SEO靠我以免引发意想不到的结果。

allocatable-memory:可供申请的内存页数,这个地方不是指具体的MB或GB一类的,不懂的建议不要胡乱设置,以免引发意想不到的结果。

watchdog-device:SEO靠我指定的硬件看门狗,这个地方行注释掉。

temperature-sensor:指定需要监控的温度传感器和max-temperature搭配使用,比如Ubuntu系统的CPU温度是 /sys/class/tSEO靠我hermal/thermal_zone0/temp,watchdog会间隔读取这个文件里面的数值和max-temperature的数值比较,如果大于max-temperature就重试多次,温度降不下SEO靠我来就重启。一般CPU硬件本身也有诸如温度墙和功耗墙的设置,这个选项更有点类似于在硬件之下的设置,可有可无吧,我是这么觉得的。

max-temperature:和temperature-sensor搭配使SEO靠我用,指定需要监控的设备。

admin:邮箱地址,如果/etc/watchdog.conf里面配置的任何一项触发了,再重启系统前,会给你发一封邮件通知你,属于比较温馨的设定吧。这个东西需要部署邮件服务,具SEO靠我体的自行研究。

interval :指定往硬件watchdog设备写入的时间间隔,这里用不到,请自行研究。

logtick:如果打开了日志记录功能,日志写入间隔,如果值太小会消耗操作系统资源,这里用不到,SEO靠我请自行研究。

log-dir:指定的日志记录目录,默认是/var/log/watchdog,程序会自动创建这个文件夹,不需要手动创建。这里我用不到,就不做过多的解释了,请自行研究。

realtime:waSEO靠我tchdog daemon后台程序保活用的,一般资源调度中,资源都会优先分配给前台程序,这个选项就是防止被系统杀死的。一定要打开。

priority:这个是设置程序优先级的,配合realtime使用的,SEO靠我默认是1就行了。

pidfile:这个很重要,如果你想监控sshd,crond,telnetd这一类的daemon运行状况,那么这个选项可以满足你,这个支持多设置。以sshd为例,如果sshd服务起来的SEO靠我话,/var/run/ 下面会有一个sshd.pid的文件,如果sshd服务不在了,这个文件就会消失,这个文件是sshd服务在配置里指定的,不一定所有的daemon都有这个文件,我知道的sshd、teSEO靠我lnetd(inetd.pid)、crond、rsyslogd是有的。总而言之是用来监视某一服务运行状态的,一旦服务死掉了,就重启系统。这个检测过程重试好几次,而不是一次。

watchdog-timeoSEO靠我ut:这个是和watchdog-device搭配使用的,我们没用到watchdog-device,这里就不赘述了,大家自行研究。

其他:太累了,一口气搞这么多,这些功能都是我一个个试的,其他几个我没用到SEO靠我,请大家自行研究吧。如果有一些描述不准确的地方欢迎大家评论里指出,让我们共同进步,多谢了!

启动服务

所有的配置都改好后,我们尝试启动watchdog服务,启动之前建议大家把daemon的自启动先关掉,要SEO靠我不然可能导致反复重启的问题。 sudo systemctl disable watchdog #禁止自启动 sudo systemctl start wSEO靠我atchdog #打开服务 systemctl status watchdog #实时查看状态 官方文献

/etc/watchdog.conf官方英文说SEO靠我

soft watchdog daemon官方英文说明

总结

1、配置过程不算太难,但还是需要研究下

2、特别需要注意的是,软件看门狗的稳定性有待挖掘,其本身会不会被内核杀死依然是一个谜,我觉得是有这种可能SEO靠我性的。遗憾的是我目前还没有模拟出来软件看门狗被杀死的情形,这个问题暂时留着。

3、建议软硬件搭配使用,防患于未然。
“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2