活用时序数据库,玩转载动化监控
<div style="color: black; text-align: left; margin-bottom: 10px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">InfluxDB是一款用Go语言编写的开源分布式时序、事件和指标数据库,无需<span style="color: black;">外边</span>依赖。该数据库<span style="color: black;">此刻</span><span style="color: black;">重点</span>用于存储<span style="color: black;">触及</span><span style="color: black;">海量</span>的时间戳数据,如DevOps监控数据,APP metrics, loT传感器数据和实时分析数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">InfluxDB特征</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>无结构(无模式):<span style="color: black;">能够</span>是任意数量的列(tags)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong><span style="color: black;">能够</span>设置metric的<span style="color: black;">保留</span>时间。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>支持与时间<span style="color: black;">相关</span>的<span style="color: black;">关联</span>函数(如min、max、sum、count、mean、median等),方便统计。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>支持存储策略:<span style="color: black;">能够</span>用于数据的删改(influxDB<span style="color: black;">无</span><span style="color: black;">供给</span>数据的删除与修改<span style="color: black;">办法</span>)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>支持连续<span style="color: black;">查找</span>:是数据库中自动<span style="color: black;">按时</span><span style="color: black;">起步</span>的一组语句,和存储策略搭配<span style="color: black;">能够</span>降低InfluxDB的系统占用量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>支持类似SQL语法。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>支持设置数据在集群中的副本数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>支持<span style="color: black;">定时</span>采样数据,写入<span style="color: black;">另一</span>的measurement,方便分粒度存储数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>自带web管理界面,方便<span style="color: black;">运用</span>(登入方式:http://< InfluxDB-IP >:8083)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">✔ </strong>最关键的一点,<span style="color: black;">亦</span>是我采用的<span style="color: black;">原由</span>,支持Grafana画图展示。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了InfluxDB+Grafana后,你就<span style="color: black;">能够</span>用python写<span style="color: black;">有些</span>简单的程序了,<span style="color: black;">能够</span>只负责写后端<span style="color: black;">规律</span>部分,数据都<span style="color: black;">能够</span>存入InfluxDB,<span style="color: black;">而后</span><span style="color: black;">经过</span>Grafana展示出来,<span style="color: black;">而后</span>做一系列的监控工作,后面会进行<span style="color: black;">仔细</span>介绍</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">influxdb安装</strong></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">二进制安装,<span style="color: black;">这儿</span>以centos6.5为例进行安装。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">wget https://dl.influxdata.com/influxdb/releases/influxdb-1.1.0.x86_64.rpm</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">yum localinstall influxdb-1.1.0.x86_64.rpm</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">安装后,在/usr/bin下面有如下文件。</p>influxd influxdb服务器
influx influxdb命令行客户端
influx_inspect 查看工具
influx_stress 压力测试工具
influx_tsm 数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)
在/var/lib/influxdb/下面会有如下文件夹data 存放<span style="color: black;">最后</span>存储的数据,文件以.tsm结尾
meta 存放数据库元数据
wal 存放预写日志文件
InfluxDB基本概念<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">详细</span>的讲解influxdb的<span style="color: black;">关联</span>操作之前先说说influxdb的<span style="color: black;">有些</span>专有名词,这些名词<span style="color: black;">表率</span>什么。先看下面一段Influxdb中的表信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">> select * from disk;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">name: disk</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">time count device free hostname used used_percent</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">---- ----- ------ ---- -------- ---- ------------</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1502089306183159978 1 /data 80 server01 100 60.33</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1502089342879496791 1 /data 90 server02 110 68.33</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">InfluxDB是时序数据库,<span style="color: black;">因此</span>怎么都绕不开时间,<span style="color: black;">第1</span>纵列time存储着时间戳,而时间戳是与数据进行<span style="color: black;">相关</span>,<span style="color: black;">这般</span><span style="color: black;">才可</span>将时间和数据进行展示。</p>influxDB中的名词
database 数据库名,在InfluxDB中<span style="color: black;">能够</span>创建多个数据库,<span style="color: black;">区别</span>数据库中的数据文件是隔离存放的,存放在磁盘上的<span style="color: black;">区别</span>目录
measurement <span style="color: black;">测绘</span>指标名,相当于传统数据库表名
tag 标签,在InfluxDB中,tag是一个非常重要的部分,表名+tag<span style="color: black;">一块</span><span style="color: black;">做为</span>数据库的索引,是”key-value”的形式
tag key <span style="color: black;">各样</span>索引的key
tag value <span style="color: black;">各样</span>索引的value
tag set 由一系列的tag组成。tags 在 InfluxDB 中会<span style="color: black;">根据</span>字典序排序,不管是 tagk 还是 tagv,只要不一致就分别属于两个 key,例如 host=server01,region=us-west 和 host=server02,region=us-west <span style="color: black;">便是</span>两个<span style="color: black;">区别</span>的 tag set
field 数据,field<span style="color: black;">重点</span>是用来存放数据的部分,<span style="color: black;">亦</span>是”key-value”的形式
field key <span style="color: black;">各样</span>记录key
field value field对应的value<span style="color: black;">便是</span>fieldName,InfluxDB中支持一条数据中<span style="color: black;">插进</span>多个fieldName,这其实是一个语法上的优化,在<span style="color: black;">实质</span>的底层存储中,是当作多条数据来存储
field set 由一系列的key=value<span style="color: black;">构成</span>key set
timestamp 时间戳,<span style="color: black;">做为</span>时序型数据库,时间戳是InfluxDB中最重要的部分,在<span style="color: black;">插进</span>数据时<span style="color: black;">能够</span>自己指定<span style="color: black;">亦</span>可留空让系统指定,在<span style="color: black;">插进</span>新数据时,tag、field和timestamp之间用空格分隔。在 TSM 存储引擎中会特殊对待,为了优化后续的<span style="color: black;">查找</span>操作
series 序列,所有在数据库中的数据,都<span style="color: black;">必须</span><span style="color: black;">经过</span>图表来展示,而这个series<span style="color: black;">暗示</span>这个表里面的数据,<span style="color: black;">能够</span>在图表上画成几条线,<span style="color: black;">经过</span>tags<span style="color: black;">摆列</span>组合算出来
Retention policy 数据<span style="color: black;">保存</span>策略,<span style="color: black;">能够</span>定义数据<span style="color: black;">保存</span>的时长,<span style="color: black;">每一个</span>数据库<span style="color: black;">能够</span>有多个数据<span style="color: black;">保存</span>策略,但只能有一个默认策略,InfluxDB 会<span style="color: black;">定时</span>清除过期的数据<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">InfluxDB基本操作</strong></h1>#创建数据库create database "db_name"
#<span style="color: black;">表示</span>所有的数据库show databases
#删除数据库drop database "db_name"
#<span style="color: black;">运用</span>数据库;use db_name
#<span style="color: black;">表示</span>该数据库中所有的表;show measurements
#创建表,直接在<span style="color: black;">插进</span>数据的时候指定表名(key-value)insert disk,hostname=server02,device=/data free=90,used=110,used_percent=98.33,count=1
#删除表;drop measurement "measurement_name"
python操作influxdb数据库<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">安装influxdb-python</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pip install influxdb</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>python操作influxdb,数据库操作完整示例如下</p>#! /usr/bin/env python
#-*- coding:utf-8 -*-
from influxdb import InfluxDBClient
client = InfluxDBClient(‘localhost‘, 8086, ‘root‘, ‘‘, ‘‘) # 初始化
print client.get_list_database() # <span style="color: black;">表示</span>所有数据库名<span style="color: black;">叫作</span>
client.create_database(‘testdb‘) # 创建数据库
print client.get_list_database() # <span style="color: black;">表示</span>所有数据库名<span style="color: black;">叫作</span>
client.drop_database(‘testdb‘) # 删除数据库
print client.get_list_database() # <span style="color: black;">表示</span>所有数据库名<span style="color: black;">叫作</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">创建新表并添加数据</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">InfluxDB<span style="color: black;">无</span><span style="color: black;">供给</span>单独的建表语句,<span style="color: black;">能够</span><span style="color: black;">经过</span>并添加数据的方式建表,示例如下</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> json_body = [</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "measurement": "students",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"tags": {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "stuid": "s123"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> },</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> #"time": "2017-03-12T22:00:00Z",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "fields": {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "score": 89</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">client = InfluxDBClient(‘localhost‘, 8086, ‘root‘, ‘‘, ‘testdb‘) # 初始化(指定要操作的数据库)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> client.write_points(json_body) # 写入数据,<span style="color: black;">同期</span>创建表</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">利用时序数据库进行自动化监控</strong></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上讲解了influxdb的基本<span style="color: black;">运用</span>,后面谈一下influxdb在监控中的应用,<span style="color: black;">运用</span>python脚本将监控数据存入influnxdb,<span style="color: black;">而后</span><span style="color: black;">运用</span>grafana进行图表<span style="color: black;">表示</span>。下面案例是监控<span style="color: black;">照片</span>上传到七牛云存储的上传和下载时间,以及ping七牛域名的响应时间,在工作中<span style="color: black;">咱们</span>经常上传云存储比较慢的现象,<span style="color: black;">咱们</span>将采集到的数据存入到influnx</p>##
脚本放到linux的crontab脚本中,1分钟执行一次,每次执行会将数据打印到influnxdb进行存储# -*- coding: utf-8 -*-
import os,sys,commands
import datetime
from influxdb import InfluxDBClient
import sys, time, subprocess,os
logFile = os.getcwd()+"/vrping.log"
#打印日志函数
def logger(logContent,logFile):
with open(logFile,a) as f:
f.write(logContent+\n)
#获取时期函数
def get_today_date():
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=0)
yes_time_nyr = yes_time.strftime(%Y-%m-%d)
result = str(yes_time_nyr)
return result
def annotate(time_loss,time_delay,server_name,Average_Speed,Average_Time,used_time):
"""Send the data across to InfluxDB as an annotation"""
# client = InfluxDBClient("10.99.69.40", 8086, testdb)
#连接influnxdb
client = InfluxDBClient(10.99.69.40, 8086, root, , testdb)
#构造数据
data = [
{
# The table
"measurement": "vr_ping",
# Metadata
"tags": {
"stuid": "sdds"
},
# Data
"fields": {
"time_loss": time_loss,
"time_delay": time_delay,
"server_name":server_name,
"Average_Speed":Average_Speed,
"Average_Time":Average_Time,
"used_time":used_time
}
}
]
try:
client.write_points(data)
except Exception as e:
logger("写入数据失败",logFile)
print e
#定义函数,制作数据写入influnxdb
def vr_ping(server_name):
file = /tmp/zabbix-vr.jpg
command = wget --timeout=3 http://oe9anrc75.bkt.clouddn.com/zabbix-vr.jpg?attname= -O %s % file
cmd1="ping -W 0.5 -i 0.05 -c 1 {0} -w 1|grep -oP (?<=time\=)+||echo 1000.1".format(server_name)
cmd2="ping -W 0.5 -i 0.05 -c200" +" " + server_name +" "+ " -w 15|grep loss|awk {print (200-$4)/200*100}"
cmd3 = "/root/qshell fput panop2 zabbix-vr.jpg /root/zabbix-vr.jpg true|grep Average Speed:|awk {print $(NF-1)}"
cmd4 = "/root/qshell fput panop2 zabbix-vr.jpg /root/zabbix-vr.jpg true|grep Average Speed:|awk {print $3}"
(status, output) = commands.getstatusoutput(cmd1)
if status == 0:
time_delay=output
else:
logger("time_delay 执行失败",logFile)
(status, output) = commands.getstatusoutput(cmd2)
if status == 0:
time_loss=output
else:
logger("time_loss 执行失败", logFile)
(status, output) = commands.getstatusoutput(cmd3)
Average_Speed = output
(status, output) = commands.getstatusoutput(cmd4)
Average_Time = output
# command = wget --timeout=3 http://oanphm9zg.bkt.clouddn.com/0cf863.m3u8?attname= -O %s % file
#print command
start_time = time.time()
# print start_time
sp = subprocess.Popen(command, bufsize=1024, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True)
out, err = sp.communicate()
end_time = time.time()
# print end_time
if err:
if 200 OK not in err:
used_time = 5
print used_time
sys.exit(1)
used_time = (end_time - start_time).seconds
used_time = round(used_time,2)
os.remove(file)
annotate(time_loss, time_delay, server_name,Average_Speed,Average_Time,used_time)
vr_ping(up.qiniu.com)<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">结合grafana<span style="color: black;">运用</span></strong></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">grafana 是一款界面美观、强大的可视化监控指标展示工具,采用 go 语言编写的开源应用,<span style="color: black;">重点</span>用于大规模指标数据的可视化展现,基于<span style="color: black;">商场</span>友好的 Apache License 2.0 开源协议。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">grafana安装</p>$ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.2.0-1.x86_64.rpm
$ sudo yum localinstall grafana-4.2.0-1.x86_64.rpm
grafana<span style="color: black;">起步</span>service grafana-server start
grafana后台页面<span style="color: black;">拜访</span>http://IP:3000<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Grafana添加IfluxDB为数据源</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/740a948eab61466aabb3565955e5d8ce~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723899321&x-signature=L5CCrOxUORwPK5liXfVUxYmKzUQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">利用grafana进行画图</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9c552a3e616a43569aa0d6fdbead052b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723899321&x-signature=q%2FZJJNfZY5KhsC1E0XsaLqovnZE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">总结</strong></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">咱们</span>平时工作中,会遇到各式各样的监控,<span style="color: black;">运用</span>python +influnxdb+grafana进行数据改造、数据入库、数据图形化<span style="color: black;">表示</span>是一种比较灵活<span style="color: black;">平常</span>的方式,在工作中会遇到<span style="color: black;">非常多</span>,不管是业务数据还是<span style="color: black;">设备</span><span style="color: black;">基本</span>监控数据,都<span style="color: black;">能够</span>将数据打点,最后展示出比较友好的图形化界面。</p>
</div>
真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。 你的话深深触动了我,仿佛说出了我心里的声音。 楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/ “板凳”(第三个回帖的人) 软文发布平台 http://www.fok120.com/ 这篇文章真的让我受益匪浅,外链发布感谢分享!
页:
[1]