Elasticsearch 常用任务管理命令及实战应用

常用任务管理命令

  • 列出所有任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?detailed=true&pretty" -H 'Content-Type: application/json'
  • 获取特定类型的任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?actions=<action_type>" -H 'Content-Type: application/json'
  • 列出所有查询任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?detailed=true&actions=*search" -H 'Content-Type: application/json'
  • 取消所有查询任务
    如果 es 查询因大任务而卡住,可以临时采取此措施
shell 复制代码
curl -X POST "http://<es_host>:<es_port>/_tasks/_cancel?actions=*search" -H 'Content-Type: application/json'
  • 获取特定任务的详细信息
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks/<task_id>" -H 'Content-Type: application/json'
  • 取消特定任务
shell 复制代码
curl -X POST "http://<es_host>:<es_port>/_tasks/_cancel?task_id=<task_id>" -H 'Content-Type: application/json'
  • 获取特定节点上的任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?nodes=<node_id>" -H 'Content-Type: application/json'

实战

定时检测 Elasticsearch 后台运行的查询任务,如果任务运行时间超过 59 秒,则进行企业微信群告警通知

python 复制代码
import requests
import time

# Elasticsearch节点的URL
es_url = "http://<es_user>:<es_pwd>@<es_host>:<es_port>/_tasks?detailed=true"

# 获取任务信息
response = requests.get(es_url)
tasks_data = response.json()

# 遍历节点和任务
for node_id, node_info in tasks_data.get('nodes', {}).items():
    for task_id, task_info in node_info.get('tasks', {}).items():
        running_time_seconds = task_info.get('running_time_in_nanos', 0) / 1e9
        description = task_info.get('description', '')
        
        if running_time_seconds > 59 and description:
            running_time_formatted = f"{running_time_seconds:.2f}"
            # 准备单个任务的Markdown内容
            content = (
                f"# 有大任务在 Elasticsearch 上运行\n"
                f"- **任务 ID**: {task_id}\n"
                f"  **查询语句**: {description}\n"
                f"  **运行时间**: {running_time_formatted} seconds\n"
            )

            # 发送到Webhook
            QYWX_BODY = {
                "msgtype": "markdown",
                "markdown": {
                    "content": content
                }
            }

            BOT_KEY = "xxxxxxxxxxxxxxxxx"  # 企业微信群 bot key

            webhook_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={BOT_KEY}"
            headers = {'Content-Type': 'application/json; charset=utf-8'}

            response = requests.post(webhook_url, json=QYWX_BODY, headers=headers)

            # 检查响应状态
            if response.status_code == 200:
                print(f"Notification for Task ID {task_id} sent successfully.")
            else:
                print(f"Failed to send notification for Task ID {task_id}. Status code: {response.status_code}, Response: {response.text}")
            # 等待 2 秒
            time.sleep(2)

print("Processing completed.")
相关推荐
JAVA学习通8 小时前
OJ竞赛平台----C端题目列表
java·开发语言·jvm·vue.js·elasticsearch
Elasticsearch10 小时前
根据用户行为数据中的判断列表在 Elasticsearch 中训练 LTR 模型
elasticsearch
失散1312 小时前
分布式专题——45 ElasticSearch基础数据管理详解
java·分布式·elasticsearch·架构
没有bug.的程序员12 小时前
分布式监控体系:从指标采集到智能告警的完整之道
java·分布式·告警·监控体系·指标采集
今天头发还在吗12 小时前
解决 Git 推送冲突:使用 Rebase 整合远程更改
大数据·git·elasticsearch
橘子在努力1 天前
【橘子ES】如何本地调试ES源码
elasticsearch·搜索引擎
柳贯一(逆流河版)1 天前
ElasticSearch 实战:全文检索与数据聚合分析的完整指南
大数据·elasticsearch·全文检索
Jammingpro1 天前
【Git版本控制】Git初识、安装、仓库初始化与仓库配置(含git init、git config与配置无法取消问题)
java·git·elasticsearch
失散131 天前
分布式专题——44 ElasticSearch安装
java·分布式·elasticsearch·架构
arvin_xiaoting1 天前
#zsh# #Ubuntu# 一键安装zsh、oh-my-zsh、常用插件
linux·ubuntu·elasticsearch