圆月山庄资源网 Design By www.vgjia.com
实时读取logstash日志,有异常错误keywork即触发报警。
# /usr/bin/env python3 # -*- coding: utf-8 -*- # __author__ = caozhi # create_time 2018-11-12,update_time 2018-11-15 # version = 1.0 # 录像高可用报警 # 1 读取日志 使用游标移动 # 2 线上业务日志文件会切割,切割后,读取上一个切割的日志 import os import sys import json import requests import time import re cini = conf.ini' log_file = logstash.log' def readconf(): try: with open(cini, 'r+') as f: CONF = json.load(f) except: CONF = {"seek": 0, "inode": 922817, "last_file": logstash.log"} writeconf(CONF=CONF) print('conf.ini 配置文件缺失,自动创建一个新的配置文件') return CONF def writeconf(CONF): with open(cini, 'w+') as e: json.dump(CONF, e) def read_log(log_file, seek): try: f = open(log_file, 'r') except FileNotFoundError: f = open(logstash.log', 'r') seek = 0 print('上一个文件读取失败了,请检查切割的日志文件') except: print('日志文件打开错误,退出程序') sys.exit() f.seek(seek) line = f.readline() new_seek = f.tell() if new_seek == seek: print('没有追加日志,退出程序') sys.exit() while line: try: logstash = json.loads(line) except: CONF = {"seek": 0, "inode": 922817, "last_file": "/data/logs/lmrs/logstash.log"} writeconf(CONF=CONF) print('json数据加载错误,重新创建一个新的配置文件') sys.exit() #if '''re.search(time.strftime("%Y:%H:%M", time.localtime()), logstash.get('log_time')) and '''logstash.get('rtype') == 6 and logstash.get('uri') == '/publish' and logstash.get('event') == 0: if logstash.get('rtype') == 6 and logstash.get('uri') == '/publish' and logstash.get('event') == 0: value = 1 stream = logstash.get('name') print('{} {}'.format(value, stream)) record(value=value, stream=stream) else: value = 0 stream = 0 line = f.readline() seek = f.tell() f.close return value, stream, seek def record(value, stream): data = [] record = {} record['metric'] = 'recording_high_availability_monitor' record['endpoint'] = os.uname()[1] record['timestamp'] = int(time.time()) record['step'] = 60 record['value'] = value record['counterType'] = 'GAUGE' record['Tags'] = '{}={}'.format(int(time.time()), stream) data.append(record) if data: print('这是data的json数据') print(data) falcon_request = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(data)) #falcon_request = requests.post("http://127.0.0.1:1988/v1/push", json=data) print('json参数请求返回状态码为:' + str(falcon_request.status_code)) print('json参数请求返回为:' + str(falcon_request.text)) if __name__ == '__main__': print() print('***************************************') print('本次执行脚本时间:{}'.format(time.strftime("%Y%m%d_%H%M", time.localtime()))) CONF = readconf() print('first_CONF :{}'.format(CONF)) print('NO1.log_file',log_file) last_inode = CONF['inode'] inode = os.stat(log_file).st_ino print('last_inode: {} inode: {}'.format(last_inode, inode)) if inode == last_inode: seek = CONF['seek'] next_file = 0 else: log_file = CONF['last_file'] + time.strftime("-%Y%m%d_", time.localtime()) + str(time.strftime("%H%M", time.localtime()))[:-1] + '0' next_file = 1 seek = CONF['seek'] print('NO2.log_file',log_file) value, stream, seek = read_log(log_file=log_file,seek=seek) if next_file: CONF['seek'] = 0 else: CONF['seek'] = seek CONF['inode'] = os.stat(logstash.log').st_ino writeconf(CONF=CONF) print('last_CONF :{}'.format(CONF))
补充知识:logstash 调用exec
我就废话不多说了,还是直接看代码吧!
[elk@Vsftp logstash]$ cat t3.conf input { stdin { } } filter { grok { match => [ "message","("] } date { match => ["time", "yyyy-MM-dd HH:mm:ss,SSS"] } mutate { add_field =>["type","tailong"] add_field =>["messager","%{type}-%{message}"] remove_field =>["message"] } } output { if ([Level] == "ERROR" or [messager] =~ "Exception" ) and [messager] !~ "温金服务未连接" and [messager] !~ "调用温金代理系统接口错误" and [messager] !~ "BusinessException" { exec { command => "/bin/smail.pl \"%{messager}\" \"%{type}\" " } } stdout { codec =>rubydebug } } Vsftp:/root# cat /bin/smail.pl #!/usr/bin/perl use Net::SMTP; use HTTP::Date qw(time2iso str2time time2iso time2isoz); use Data::Dumper; use Getopt::Std; use vars qw($opt_d ); getopts('d:'); # mail_user should be your_mail@163.com $message= "@ARGV"; $env="$opt_d"; sub send_mail{ my $CurrTime = time2iso(time()); my $to_address = shift; my $mail_user = 'zhao.yangjian@163.com'; my $mail_pwd = 'xx'; my $mail_server = 'smtp.163.com'; my $from = "From: $mail_user\n"; my $subject = "Subject: zjcap info\n"; my $info = "$CurrTime--$message"; my $message = <<CONTENT; $info CONTENT my $smtp = Net::SMTP->new($mail_server); $smtp->auth($mail_user, $mail_pwd) || die "Auth Error! $!"; $smtp->mail($mail_user); $smtp->to($to_address); $smtp->data(); # begin the data $smtp->datasend($from); # set user $smtp->datasend($subject); # set subject $smtp->datasend("\n\n"); $smtp->datasend("$message\n"); # set content $smtp->dataend(); $smtp->quit(); }; send_mail ('zhao.yangjian@163.com'); 2017-01-12 10:19:19,888 jjjjj Exception { "@version" => "1", "@timestamp" => "2017-01-12T02:19:19.888Z", "host" => "Vsftp", "time" => "2017-01-12 10:19:19,888", "Level" => "jjjjj", "type" => "tailong", "messager" => "tailong-2017-01-12 10:19:19,888 jjjjj Exception" }
以上这篇python实时监控logstash日志代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月02日
2024年11月02日
- 群星.2003-存为爱2CD【环球】【WAV+CUE】
- 韩磊《试音天碟》高清音频[WAV+CUE]
- 邓涛《寂寞蒲公英(黑胶CD)》[WAV]
- 江志丰.2011-爱你的理由【豪记】【WAV+CUE
- 群星《传承-太平洋影音45周年纪念版 (CD2)》[320K/MP3][140.01MB]
- 群星《传承-太平洋影音45周年纪念版 (CD2)》[FLAC/分轨][293.29MB]
- 首首经典《滚石红人堂I 一人一首成名曲 4CD》[WAV+CUE][2.5G]
- s14上单t0梯度怎么排名 s14世界赛上单t0梯度排行榜
- tes目前进了几次s赛 LPL队伍tes参加全球总决赛次数总览
- 英雄联盟巅峰礼赠什么时候开始 2024巅峰礼赠活动时间介绍
- 冯骥发文谈睡觉重要性 网友打趣:求求你先做DLC
- 博主惊叹《少女前线2》万圣节大雷皮肤:这真能过审吗?
- 《生化危机8》夫人比基尼Mod再引骂战:夸张身材有错吗?
- 江蕙.1994-悲情歌声【点将】【WAV+CUE】
- 戴娆.2006-绽放【易柏文化】【WAV+CUE】