最新消息:

监控dmesg的报错(Zabbix+Python)

Linux ipcpu 4022浏览

概述

dmesg是Linux中的输出系统错误的命令,比如OOM或者Kernel Panic都会显示在这里,因此监控dmesg的报错也极为重要。

使用Python编写检测脚本

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Debug in Python2.7&3.5
# writen by ipcpu

import os,sys,re

kernel_problems = {}
kernel_problems["general protection fault"] = re.compile("^.*general protection fault.*$")
kernel_problems["bug"] = re.compile(".*(kernel BUG at|double fault:|Badness at|[Uu]nable to handle kernel|" +
                                    "sysctl table check failed|------------[ cut here ]------------|Oops:).*$")
kernel_problems["oom"] = re.compile(".*Out of memory: Kill process.*")
kernel_problems["userland_kill"] = re.compile(".*killed by.*")
kernel_problems["ioerror"] = re.compile(r".*(end_request: I/O error, dev ..*, sector ..*|error on device ..*, logical block ..*).*")


dmesg = os.popen('dmesg')
matched = []

for line in dmesg:    
    #print line.strip()
     for ident, regex in kernel_problems.items():
            if regex.match(line):
                #sys.stderr.write("%s : %s\n" % (ident, line.strip()))
                matched.append("%s" % (ident))



#python2和3的兼容输出
from distutils.log import warn as printf
printf(len(matched))
#输出结果为dmesg中包含错误信息的行数
#原理比较简单,就是获取dmesg的输出,然后逐行比对是否存在报错信息

设置Zabbix监控项和触发器

由于脚本输出的是报错信息的行数,每当有报错时,输出结果就会加1,因此触发器是这么写的:

{TPL-OS-DMESG:checkoom.abschange()}>0

参考资料

报错信息的匹配是从下面的开源程序中获取的:
https://github.com/scoopex/zabbix-agent-extensions

转载请注明:IPCPU-网络之路 » 监控dmesg的报错(Zabbix+Python)