python报错killed,监控dmesg的报错(Zabbix+Python)

概述

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

设置完毕,这样对系统中出现的错误就能通过zabbix及时报警出来了,简单实用,如果还没有对系统做此监控的小伙伴可以试试哟~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值