前言
既然已经搭了es和kibana, 那肯定要做一下kibana的图表, 看日志超级方便的, 首先写个脚本生成一点测试数据
生成测试数据
"""
@author xiaofei
@date 2019-05-20
@desc 百度云es添加数据
"""
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import random
import json
host = 'IP:9200'
es_conn = Elasticsearch(hosts=host, maxsize=25)
# 操作索引 请不要在意我的命名...
index_name = "pics_a"
type_name = "pic"
online_alias = "pics"
# mapping
doc_mapping = {
"properties": {
"code": {"type": "long"},
"timeout": {"type": "long"},
"timeline": {"type": "float"},
"url": {"type": "keyword"},
"ctime": {"type": "date"}
}
}
# # 删除索引
# res = es_conn.indices.delete(index=index_name)
# print(res)
# # 创建库
# es_conn.indices.create(index=index_name, ignore=400)
# es_conn.indices.put_mapping(index=index_name, doc_type=type_name, body=doc_mapping)
# es_conn.indices.put_alias(index_name, online_alias)
code = [200, 404, 302, 500]
timeline = [1.1, 0.4, 0.1, 0.02, 0.29, 3, 0.5, 1.2]
url = ["Add", "list", "update", "del"]
date = ["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"]
data = []
for x in range(1000):
dat = {
"code": random.choice(code),
"timeline": random.choice(timeline),
"url": random.choice(url),
"ctime": random.choice(date)
}
dat["timeout"] = 1 if dat['timeline'] > 1 else 0
action = {"_index": index_name, "_type": type_name, "_source": dat}
data.append(action)
helpers.bulk(es_conn, data)
print("插入成功")
开始图表
应该都能看出来我的测试数据是什么, 就是api请求统计, 首先来画一个每天请求状态码分布图
首先添加一个图表
选择折线图
选择需要画图的索引
x轴用ctime添加日期维度, 点击红圈生成图像
再次添加x轴聚合统计x轴
ok, 这就是成品了
点击保存写上标题就成功了
要是感觉不够细致, 还可以在加一层, 每个路由的状态码统计
成品图
颜色什么的自己都可以随便改变, 这是随机的测试数据, 正常的肯定不能是这种花里胡哨的曲线