nagios-plugin(log)

來源:互聯網
上載者:User

標籤:python   nagios   

#!/usr/bin/env pythonimport datetimeimport re import sysREG_SYSLOG = re.compile(r‘(?P<logtime>\w+ \d+ [\d:]+) (?P<hostname>[\d\w.]+) (?P<progname>\w+)(\[\d*\])?: (?P<msg>.*)‘)DELTA = datetime.timedelta(minutes=3)MONTH_MAPPING = {"Jan":1,"Feb":2,"Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7,"Aug":8,"Sep":9,"Oct":10,"Nov":11,"Dec":12}PROG_COUNTER = {}KEYWORD_COUNTER = {}KEYWORD = [‘error‘,‘fatal‘]def parsertime(s):    month, day, t = s.split()    hour, minute, second = [int(i) for i in t.split(‘:‘)]    return datetime.datetime(2014,MONTH_MAPPING[month],int(day),hour,minute,second)def count(metric, counter):    if metric in counter:        counter[metric] += 1    else:        counter[metric] = 1def getMessages():    starttime = datetime.datetime.now() - DELTA    logfile = ‘/var/log/messages‘    with open(logfile) as fd:        for line in fd:            logmatch = REG_SYSLOG.match(line)            if logmatch:                logdict = logmatch.groupdict()                logtime = parsertime(logdict[‘logtime‘])                if logtime >= starttime:                    count(logdict[‘progname‘], PROG_COUNTER)                    for k in KEYWORD:                        if k in logdict[‘msg‘].lower():                            count(k, KEYWORD_COUNTER)def print_msgs(status,msgs):    mapping = {0:‘OK‘,1:‘Warning‘,2:‘Critcal‘,3:‘Unknown‘}    msg = ‘%s:%s‘ % (mapping[status],‘ | ‘.join(msgs))    print msg    sys.exit(status)def check_counter():    msgs = []    status = 0    if PROG_COUNTER:        for c in PROG_COUNTER:            if PROG_COUNTER[c] > 2:                msgs.append("prog %s has reached %s " % ( c, PROG_COUNTER[c]))                status = 2            elif PROG_COUNTER[c] <= 2:                msgs.append("prog %s has reached %s " % ( c, PROG_COUNTER[c]))                status = 1    if KEYWORD_COUNTER:        status = 2        msgs.append("keywords have reached %s" % ‘,‘.join([‘%s:%s‘%(k,KEYWORD_COUNTER[k]) for k in KEYWORD_COUNTER]))    print_msgs(status, msgs)if __name__ == ‘__main__‘:    getMessages()    check_counter()

本文出自 “muzinan的技術部落格” 部落格,謝絕轉載!

nagios-plugin(log)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.