随笔 - 321  文章 - 0  评论 - 6  阅读 - 34万

metricbeat用于old gc time的监控小程序

复制代码
import datetime
import elasticsearch
import pytz
import requests
import time

today_tmp = datetime.datetime.today()
today_final = str(today_tmp).replace("-", ".").split(" ")[0]
utc_tz = pytz.timezone('UTC')
now_tmp = datetime.datetime.now(tz=utc_tz)
min_tmp = now_tmp - datetime.timedelta(minutes=40)
now_tmp1 = str(now_tmp).split(" ")[0] + "T"
now_tmp2 = str(now_tmp).split(" ")[1].split("+")[0] + "z"
now_final = now_tmp1 + now_tmp2
min_tmp1 = str(min_tmp).split(" ")[0] + "T"
min_tmp2 = str(min_tmp).split(" ")[1].split("+")[0] + "z"
min_final = min_tmp1 + min_tmp2


search_index = "metricbeat-6.8.15-" + today_final
search_body = {
  "query":{
    "bool":{
      "filter":[
        {
          "exists": {
          "field": "elasticsearch.node.stats.jvm.gc.collectors.old.collection.ms"
          }
        },
        {
          "range": {
            "@timestamp": {
            "gte": min_final,
            "lte":  now_final
            }
          }
        }
      ]
    }
  },
  "sort": { "@timestamp": { "order": "desc" }}
}


es = elasticsearch.Elasticsearch("xxxxx:xxxxx")
while True:
    res = es.search(index=search_index, body=search_body)
    hits_num=len(res['hits']['hits'])
    if hits_num <= 0:
     #处理未收到metricbeat收集的信息时的情况else:
        recent_hits_node = res['hits']['hits'][0]["_source"]['elasticsearch']['node']
        node_name = recent_hits_node['name']
        recent_oldgc_time = recent_hits_node['stats']['jvm']['gc']['collectors']['old']['collection']['ms']
        if recent_oldgc_time >= 5000:
            #处理超过阈值时的情况

    time.sleep(60)
复制代码

 metricbeat的安装和使用参考这里:https://www.cnblogs.com/to-here/p/14653636.html

posted on   该用户很懒  阅读(69)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示