使用python巡检ELK的index数据量(elk2csv.py)

34 阅读1分钟

一、逻辑

  1. 取值逻辑

将devtoos 查询出来的结果,去除mb、kb、k大小的index,只保留gb的index。

input.csv 中的内容由 以下命令得出:(只要日志量超过1gb的所有index

get _cat/indices?v&s=store.size:desc

  1. 依赖模块

需要安装 pandas模块




二、python

import pandas as pd

# 读取CSV文件
input_file = 'input.csv'  # 假设输入文件名为 input.csv
output_file = 'new.csv'   # 输出文件名为 new.csv

# 读取CSV文件
df = pd.read_csv(input_file, sep='\s+', engine='python')

# 定义一个函数将存储大小转换为GB
def convert_to_gb(size_str):
    size, unit = size_str[:-2], size_str[-2:]
    size = float(size)
    if unit.lower() == 'tb':
        return size * 1024
    elif unit.lower() == 'gb':
        return size
    else:
        raise ValueError(f"Unknown unit: {unit}")

# 应用转换函数
df['store.size_gb'] = df['store.size'].apply(convert_to_gb)

# 提取 topic 名字(删除日期后缀)
df['topic'] = df['index'].str.rsplit('-', n=1).str[0]

# 按 topic 汇总求和
summary = df.groupby('topic')['store.size_gb'].sum().reset_index()

# 按汇总后的存储大小降序排序
summary = summary.sort_values(by='store.size_gb', ascending=False)

# 保存到新的CSV文件
summary.to_csv(output_file, index=False)

# 打印结果
print(summary)



三、执行

  1. 执行

  1. 输出csv结果

  1. 将new.csv 的内容贴到飞书文档的表格中,然后“数据分列”格式化,存档

[250210 日志量巡检] lue

图片.png