目的
- 理解influxDB的数据收集原理和方法
- 为使用grafana分析数据及展示结作好准备
介绍
- [收集数据] Telegraf 是一个用 Go 编写的代理程序,可收集系统和服务的统计数据,并写入到 InfluxDB 数据库。Telegraf 具有内存占用小的特点,通过插件系统开发人员可轻松添加支持其他服务的扩展。
- [存储数据] InfluxDB 是 Go 语言开发的一个开源分布式时序数据库,非常适合存储指标、事件、分析等数据
- [展示数据] Grafana 是纯 Javascript 开发的前端工具,用于访问InfluxDB,自定义报表、显示图表等。
telegraf安装
- 下载
- 安装
- yum localinstall telegraf-0.11.1-1.x86_64.rpm -y
- 启动服务、添加开机启动
- systemctl start telegraf.service
- service telegraf status
- systemctl enable telegraf.service
- 查看版本
- telegraf --version
- Telegraf - Version 0.11.1
- 配置
- 路径:/etc/telegraf/telegraf.conf
- 示例:安装完成后会有一个示列配置文件,请根据所需仔细阅读文件。
influxDB安装
-
安装部署,添加yum 源
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo [influxdb] name = InfluxDB Repository - RHEL \$releasever baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable enabled = 1 gpgcheck = 1 gpgkey = https://repos.influxdata.com/influxdb.key EOF
-
安装: yum install influxdb -y
-
启动服务、添加开机启动
service influxdb start systemctl enable influxdb service influxdb status
-
服务默认使用端口:
Networking By default, InfluxDB uses the following network ports: TCP port 8083 is used for InfluxDB’s Admin panel TCP port 8086 is used for client-server communication over InfluxDB’s HTTP API TCP ports 8088 and 8091 are required for clustered InfluxDB instances
-
服务验证 -输入 influx 进入数据库
[root@ctn-7-12 ~]# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.11.0
InfluxDB shell 0.11.0
- 创建一个查询用户
CREATE USER "ptquery" WITH PASSWORD 'ptquery'
> show users;
user admin
ptquery false
ptdb1 fals
7.也可以在页面创建查询用户 CREATE USER "ptquery" WITH PASSWORD 'ptquery'
- 查看服务端口
[root@ctn-7-12 ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8083 :::* LISTEN 22124/influxd
tcp6 0 0 :::8086 :::* LISTEN 22124/influxd
- 浏览器访问数据库管理平台:
- 参考信息:
grafana安装
- 手动安装:
wget https://grafanarel.s3.amazonaws.com/builds/grafana-latest-1.x86_64.rpm
yum install grafana-latest-1.x86_64.rpm
- 安装包详情
二进制文件 /usr/sbin/grafana-server
启动脚本 /etc/init.d/grafana-server
环境变量 /etc/sysconfig/grafana-server
配置文件 /etc/grafana/grafana.ini
systemd服务 grafana-server.service
日志 /var/log/grafana/grafana.log
- 服务详情
启动用户 grafana
服务名称 grafana-server
默认端口 3000
账号 admin
密码 admin
- 启动服务、添加开机启动
systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
systemctl enable grafana-server.service
- 访问
- http://server IP :3000
将数据写入influxDB
influxDB line写入协议介绍
- 格式: [measurements],[tag]=[tagvalue]空格 [field1]=[field1value],[field1]=[field1value]
- 说明:
- 命名规则:measurements、tag、field的命名不能含空间,逗号,如必须有,需有\转义;
- 行格式有严格的要求,“=”左右及","分隔不能有空格
- measurements,从统计学角度上看,这叫做样本集。相当于关系数据库的Table
- tag,样本集中个体的标识符,相当于关系数据库的primary key,
- filed,个体属性的度量值,可以为整型,浮点型,字符型,布尔型 ,相当于关系数据库的一般字段
- 根据上面的写入协议,编写sh脚本,然后通过telegraf调度执行,就可以把数据写入到influxDB,具体步骤如下:
- 编写sh脚本,例子如下(bash shell脚本也可以调用python脚本,只要满足line写入协议输出即可):
#! /bin/env bash echo 'employee,empname=kk age=20,salary=3000'
- 修改telegraf的配置文件/etc/telegraf/telegraf.conf,具体如下:
[[outputs.influxdb]] urls = ["http://192.168.18.118:8086"] #infulxdb地址 database = "telegraf" #数据库 precision = "s" timeout = "5s" username = "admin" #帐号 password = "admin" #密码 [[inputs.exec]] # Shell/commands array commands = ["/tmp/qq.sh"] # Data format to consume. This can be "json", "influx" or "graphite" (line-protocol) # NOTE json only reads numerical measurements, strings and booleans are ignored. data_format = "influx" interval = "60s" #调度间隔 timeout = "15s" #超时控制
- 检验数据,登录到http://192.168.18.118:8086 数据已经被写入到influxDB中
- 编写sh脚本,例子如下(bash shell脚本也可以调用python脚本,只要满足line写入协议输出即可):