乐趣区

关于python: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()
退出移动版