检查resin的gc相关log的脚本(nagios插件版),resinnagios,输入:log_path-


输入:log_path-日志位置;间隔监控时间(单位为分钟);full_gc_count-fullgc次数报警值;gc_interval-gc间隔时间报警值;gc_count-gc次数报警值

输出:当被检查log中有任一项报警内容大于报警值的时候,脚本退出状态为2,将所需报警内容打印在stdout中。

稍作修改也可以写在crontab里,作为定时任务执行。

脚本检测方法:

./check_gc.py -p/home/gc.log -f5 -g50 -i3 -t5

使用./ check_gc.py可以看到关于输入简要的帮助文档

#! /usr/local/bin/python3import datetimeimport reimport sysimport optparsedef gen_time(minutedelta):    format='%Y-%m-%dT%H:%M:%S'    return (datetime.datetime.today()-    datetime.timedelta(minutes=minutedelta)).strftime(format)def sub_times(time1,time2):    format='%Y-%m-%dT%H:%M:%S'    translate=datetime.datetime.strptime    return (translate(time1,format)-    translate(time2,format)).total_seconds()def change_to_data(time1):    format='%Y-%m-%dT%H:%M:%S'    return datetime.datetime.strptime(time1,format)def check_log(log_path,examine_time):    report_dir={}    full_gc_re=re.compile('\\[Full GC')    full_gc_count=0    gc_count=0    last_line_timestamp=0    min_gc_interval=999999    with open(log_path) as file:        for line in file:            line=line.strip()            time_line=line.split('.')[0]            #这里加入转换日期错误的捕获,保证之后所比较的都是期望的时间格式            try:                change_to_data(time_line)            except ValueError:                pass            else:                if time_line >=examine_time:                    if last_line_timestamp != 0:                        try:                            gc_interval=sub_times(time_line,last_line_timestamp)                            if gc_interval < min_gc_interval and gc_interval != 0:                                min_gc_interval=gc_interval                        except ValueError:                            pass                        else:                            last_line_timestamp=time_line                    else:                        last_line_timestamp=time_line                    gc_count+=1                    if full_gc_re.search(line):                        full_gc_count+=1    report_dir['full_gc']=full_gc_count    report_dir['min_gc_interval']=min_gc_interval    report_dir['gc_count']=gc_count    return report_dirdef check_result(report,full_gc,min_gc_interval,gc_count):    #这里只是简要的报警规则,报警规则可以更加的复杂    if full_gc <= report['full_gc']:        display(report)        sys.exit(2)    if min_gc_interval >= report['min_gc_interval']:        display(report)        sys.exit(2)    if gc_count <= report['gc_count']:        display(report)        sys.exit(2)def display(display_data):    display_lines=''    for key in sorted(display_data):        display_lines+=(key+':'+str(display_data[key])+';')    print(display_lines)def exec_check(begin_time,log_path,full_gc,min_gc_interval,gc_count):    time_begin=gen_time(begin_time)    result_dir=check_log(options.log_path,time_begin)    check_result(result_dir,full_gc,min_gc_interval,gc_count)def generate_arguments():#建议使用argparser,这里为了兼容旧版使用了optparse        parser=optparse.OptionParser()    parser.add_option('-p',action='store',type='string',    dest='log_path',help='gc_log_path')    parser.add_option('-f',action='store',type='int',    dest='full_gc',help='full_gc apper times')    parser.add_option('-g',action='store',type='int',    dest='gc_count',help='gc apper times')    parser.add_option('-i',action='store',type='int',    dest='min_gc_interval',help='the interval of gc')    parser.add_option('-t',action='store',type='int',    dest='begin_time',help='check log from this value to now')    (options,others)=parser.parse_args()    return optionsif __name__ == '__main__':    options=generate_arguments()    exec_check(options.begin_time,options.log_path,    options.full_gc,options.min_gc_interval,options.gc_count)#该片段来自于http://byrx.net

评论关闭