前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ES相关操作

ES相关操作

作者头像
cuijianzhe
发布2024-03-07 09:14:21
560
发布2024-03-07 09:14:21
举报
文章被收录于专栏:cuijianzhecuijianzhe

查询到指定 index 日志,符合条件并报警

代码语言:javascript
复制
#!/bin/python3
#Author:cuijianzhe
from elasticsearch import Elasticsearch
from datetime import datetime, timedelta

#paradigm+
import requests
import json
import sys
import os


def search(es_object, index_name, search):
    res = es_object.search(index=index_name, body=search)
    for hit in res['hits']['hits']:
        # print(hit)
        timestamp = hit["_source"]["@timestamp"]
        local_time = str(datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ') + timedelta(hours=8))
        host = hit["_source"]["host"]
        message = hit["_source"]["message"]
        # log = {
        #     "message": hit["_source"]["message"],
        #     "host": hit["_source"]["host"],
        #     "timestamp": local_time
        # }
        # print(log)
        result = send_messag(local_time,host,message)  

def send_messag(timestamp,host,message):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    api_url = "https://www.qingzhouim.com/api/msg/groupmsgsend?access_token=d9929741d87d94a9bb0b2940a9a7f12"

    body = """
**IPMI登录提醒:**
*服务器IPMI:{}被登录!*
*登录时间: {}*
*设备IP: {}*
**message:** {}
    """.format(host,timestamp,host,message) 
    json_text = {
    "message":{
        "header":{
            "toid":[162531]
        },
        "body":[
            {
                "type": "MD",
                "content": body
            },
        ]
    }
}

    response = requests.post(api_url,json=json_text,headers=headers)
    print(response.content)



es = Elasticsearch(['http://172.27.128.219:9200'], http_auth=('elastic', 'GuxiqgSIWKffKCHHdP'))

# 获取1小时前的时间
time_one_hour_ago = datetime.now() - timedelta(hours=9)

search_object = {
    "query": {
        "bool": {
            "must": [
                { "match": { "message": "login" } },
                { "range": { "@timestamp": { "gte": time_one_hour_ago }}}
            ]
        }
    }
}
search(es, 'gpu-outband*', search_object)

效果:

删除 Elasticsearch 数据

代码语言:javascript
复制
curl -u 用户名:密码  -H'Content-Type:application/json' -d'{
    "query": {
        "range": {
            "@timestamp": {
                "lt": "now-7d",
                "format": "epoch_millis"
            }
        }
    }
}
' -XPOST "http://127.0.0.1:9200/*-*/_delete_by_query?pretty"

解释

-u 是格式为 userName:password,使用 Basic Auth 进行登录。如果 Elasticsearch 没有使用类似 x-pack 进行安全登录,则不需要加-u 参数

-H 是指定文档类型是 JSON 格式

-XPOST 是指定用 POST 方式请求

-d 是指定 body 内容

标题:ES相关操作

作者:cuijianzhe

地址:https://cloud.tencent.com/developer/article/2394434

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查询到指定 index 日志,符合条件并报警
  • 删除 Elasticsearch 数据
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档