前言:php
作运维的很重要的基础工做就是监控,以前都是统计数据入库,而后前端js图表插件出图,费时费力,可定制性差html
前几天接触到了ELK(logstash, elasticsearch, kibana)这套日志收集展现工具集,的确很方便,可是手头没有那么大的存储啊前端
也不是全部的日志数据都须要,而后就发现了grafana + influxdb的解决方案python
简介:mysql
先给出这两个工具的官网nginx
http://grafana.org/web
http://influxdb.com/sql
建议各位看官先大致浏览下这两个工具的介绍和文档shell
grafana是前端展现界面,要放到apache或nginx下,不须要php环境奥数据库
influxdb是一个时间序列的数据库,你插入的每条数据会自动附加上两个字段,一个时间,一个序列号(用来做为主键的)
ps: influxdb的0.8版本不支持centos 5,只能是centos6以上,因此centos5的仍是用0.7的版本
安装:
influxdb就一个rpm包,没有其余依赖,是用go语言写的,go发展很迅猛啊
influxdb会监听4个端口
tcp 0 0 0.0.0.0:8099 0.0.0.0:* LISTEN 29458/influxdb
tcp 0 0 0.0.0.0:8083 0.0.0.0:* LISTEN 29458/influxdb
tcp 0 0 0.0.0.0:8086 0.0.0.0:* LISTEN 29458/influxdb
tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 29458/influxdb
其中单机使用只须要用到两个,另外两个是分布式部署时采用的
8083 web管理端 http://ip:8083 用户名和密码都是 root
8086 api接口调用端
配置
grafana配置
重命名配置文件
mv config-sample.js config.js
// InfluxDB example setup (the InfluxDB databases specified need to exist) datasources: { influxdb: { type: 'influxdb', url: "http://10.75.25.103:8086/db/directmessage", username: 'root', password: 'root', }, grafana: { type: 'influxdb', url: "http://10.75.25.103:8086/db/grafana", username: 'root', password: 'root', grafanaDB: true }, },
这里配置了两个数据源,第一个是你要存储的监控数据,第二个是grafana用的
固然了你须要提早在influxdb的界面里创建好这两个库(这里就不演示influxdb web界面的登录和建库操做了)
如今浏览器打开grafana
这个界面是我配置好的,你看到的样子是黑色的官网上那个样子,界面自带黑白两个主题
为了便于理解grafana的配置方法
这里要说明一下收集数据的过程,我用的python收集
从日志中收集了两个字段 status和type
python脚本以下
#!/usr/bin/python # push log to influxdb on 10.75.25.103 import time from influxdb import client as influxdb host = '10.75.25.103' port = 8086 username = 'root' password = 'root' database = 'directmessage' db = influxdb.InfluxDBClient(host, port, username, password, database) log_fn = '/data1/multimedia/logs/scribe.log' f = open(log_fn) f.seek(0,2) while True: line = f.readline() try: lines = line.split('\t') post_data=[("name","multimedia"),("columns" ,["status", "type"]),("points",[[lines[5], lines[6]]])] data = [ {"name":"multimedia", "columns" : ["status", "type"], "points" : [[lines[5], lines[6]]] } ] db.write_points(data) except: f.close() f = open(log_fn) f.seek(0,2) time.sleep(5)
influxdb提供了python的模块,用pip安装便可,若是不以为麻烦也能够用shell的curl
下面是grafana最主要的配置项,其他本身摸索下就能够了
若是grafana的config.js配置没问题,点击输入框会自动提示字段的
这里就至关于你要展现那些字段,相似sql语法
第一个multimedia是python脚本中的name,至关于mysql中的表
select后面的 count(type) 是你要在图表中展现那些数据
后面的漏斗至关于 sql的where status = ‘succ'
group by time 60s 至关于你用cron一分钟收集一次数据
后面的type 就是 group by type
好了其他都是页面展现的微调。
收工。