前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python小工具-监控leveldb并使用zabbix报警通知

python小工具-监控leveldb并使用zabbix报警通知

作者头像
用户8483969
发布2021-04-09 11:34:51
4650
发布2021-04-09 11:34:51
举报
文章被收录于专栏:bgmonkeybgmonkey

监控需求 日志里面包含了许多需要注意的信息,返回码、响应时间、请求ip等等。网上有不少监控的工具,比如elk。但结合已有的监控,我决定用python把数据分析出来,存放到redis里面,用zabbix做可视化。监控到每台机器上leveldb的平均每分钟响应时间和超时次数。

监控脚本 pip install redis datetime

代码语言:javascript
复制
#!/usr/bin/python
from datetime import datetime,timedelta
import time
import os,sys
import redis
import mylog
count = 0
alltime = 0
avetime = 0
count1 = 0

###
利用生成器,倒叙读日志
###

def read_reverse(filename):
    f = open(filename)
    f.seek(0, 2)
    last_position = f.tell()

    while True:
        line = f.readline()
        current_position = f.tell()
        i = 1
        while current_position == last_position:
            if len(line) == current_position:
                yield line
                return
            i += 0.5
            f.seek(max(int(-72 * i), -current_position), 1)
            line = f.readline()
            current_position = f.tell()

        while current_position != last_position:
            line = f.readline()
            current_position = f.tell()

        yield line
        last_position = last_position - len(line)
        f.seek(max(-72, -last_position) - len(line), 1)

###
格式化时间,为了防止遗漏数据,统计上一分钟的nginx日志
###
time0 = datetime.now()
time1 = datetime.now() - timedelta(minutes = 1)
time2 = datetime.now() - timedelta(minutes = 2)
format = '%Y-%m-%d:%H:%M'
Time1 = time1.strftime(format)
Time2 = time2.strftime(format)

ms = "ms"
try:    
    f = read_reverse('/path/access.log')
    for line in f:
                c = ':'.join(':'.join(line.split(" ")[1:3]).split(".")[0].split(":")[0:3])        
                b = line.split(":")[-1]                
                if (c == Time1):                     
                      if(b[-3:-1] == ms ):
                          count = count + 1
                          alltime = float(b[0:5]) + alltime
                          avetime = alltime / float(count)
                          avetime = round(avetime,3)
                          if(float(b[0:3]) > float(20)):
                             count1 = count1 + 1
                          else:
                             pass
                elif (c == Time2):
                      break
except e:
    mylog.logging.error('%s %s' %e %Time1 )

###
写到redis中,zabbix-agent直接从redis中捞数据即可
###    
try:
    r = redis.Redis(host = ip,port = port,password = pwd)
    r.set('level03_count', count1) 
    r.set('level03_avetime',avetime)
except e:
    mylog.logging.error('%s %s' %e %Time1 ) 
sys.exit()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档