python3 zabbix discovery cronjob

Posted 芒果牛奶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 zabbix discovery cronjob相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3.4
# -*- coding:utf8 -*-
import re
import json
from croniter import croniter

def getCronTab():
    cron = []
    with open("xxx", \'r\') as f:
        for i in f.readlines():
            if filterCron(i):
                cron.append(i.strip())
    return cron

def filterCron(cron):
    if re.search(r\'passive_check.sh\', cron) and not re.match(\'#\', cron):
        return 1
    return 0


def getInterval(expr):
    iter = croniter(expr)
#    interval = 0 - (iter.get_next()-iter.get_next())
    interval_next = 0 - (iter.get_next()-iter.get_next())
    interval_prev = iter.get_next()-iter.get_prev()
    if interval_next >= interval_prev:
        interval = interval_next
    else:
        interval = interval_prev
    return interval

def main():
    CronLists = getCronTab()
    data =[]
    for i in CronLists:
        v = i.split()
        item = {
            "{#CRONITEM}": v[-3],
            "{#INTERVAL}": int(getInterval(\' \'.join(v[0:5]))) + 1000
        }
        data.append(item)
    items = json.dumps({"data": data})
    print(items)

if __name__ == "__main__":
    main()

以上是关于python3 zabbix discovery cronjob的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix SNMP OID discovery,自动发现

zabbix系列zabbix3.0.4 探索主机Discovery自动发现agent主机和zabbix-agent自动注册详细图文教程

Zabbix监控Low level discovery实时监控网站URL状态

Zabbix分布式监控之Discovery自动发现配置

Low Level Discovery发现实现实时监控

Zabbix监控qemuKVM进程-low_level_discovery