存储-InfluxDB

1 TSDB

influxDB是一个time series时间序列数据库。

在监控系统的开发中,大体分为采集-存储-可视化三个大类。监控指标有很显著的时间特征数据,一般采用TSDB存储。

在TSDB中,一个指标(metrics)可以添加多个带索引的tag,然后对tag进行筛选,方便可视化的管理。

现在很多公司在用influxDB,包括一些大企。 它的优点是支持多维和多字段。缺点是开源的部分,不支持较大的并发,只能在对性能要求不大的场景中使用。

场景大的公司,有的会自研TSDB,以适应自身业务特征。

观念上,你可以认为measurement是一个SQL表,主索引永远是time。tags和fields这个表中有效的字段。tags是被索引的,而fileds则不。不同之处在于,在influxDB中,你可以有百万的measurement,you don’t have to define schemas up-front, and null values aren’t stored.

points使用Line协议写入influxDB,格式如下:

1
<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]

2 安装

wget https://dl.influxdata.com/influxdb/releases/influxdb-1.2.4.x86_64.rpm

yum localinstall influxdb-1.2.4.x86_64.rpm 

/usr/bin
influxd    influxdb服务器
influx      influxdb命令行客户端
influx_inspect  查看工具
influx_stress  压力测试工具
influx_tsm  数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)

/var/lib/influxdb
data  存放最终存储的数据,文件以.tsm结尾
meta  存放数据库元数据
wal  存放预写日志文件

/var/log/influxdb
influxd.log  日志文件

/etc/influxdb
influxdb.conf  配置文件

/var/run/influxdb
influxd.pid  PID文件

  

systemctl start influxdb

  

3 conf

全局配置

1
2
reporting-disabled = false  # 该选项用于上报influxdb的使用信息给InfluxData公司,默认值为false
bind-address = ":8088"  # 备份恢复时使用,默认值为8088

1、meta相关配置

1
2
3
4
[meta]
dir = "/var/lib/influxdb/meta"  # meta数据存放目录
retention-autocreate = true  # 用于控制默认存储策略,数据库创建时,会自动生成autogen的存储策略,默认值:true
logging-enabled = true  # 是否开启meta日志,默认值:true

2、data相关配置

1
2
3
4
5
6
7
8
9
10
[data]
dir = "/var/lib/influxdb/data"  # 最终数据(TSM文件)存储目录
wal-dir = "/var/lib/influxdb/wal"  # 预写日志存储目录
query-log-enabled = true  # 是否开启tsm引擎查询日志,默认值: true
cache-max-memory-size = 1048576000  # 用于限定shard最大值,大于该值时会拒绝写入,默认值:1000MB,单位:byte
cache-snapshot-memory-size = 26214400  # 用于设置快照大小,大于该值时数据会刷新到tsm文件,默认值:25MB,单位:byte
cache-snapshot-write-cold-duration = "10m"  # tsm引擎 snapshot写盘延迟,默认值:10Minute
compact-full-write-cold-duration = "4h"  # tsm文件在压缩前可以存储的最大时间,默认值:4Hour
max-series-per-database = 1000000  # 限制数据库的级数,该值为0时取消限制,默认值:1000000
max-values-per-tag = 100000  # 一个tag最大的value数,0取消限制,默认值:100000

3、coordinator查询管理的配置选项

1
2
3
4
5
6
7
8
[coordinator]
write-timeout = "10s"  # 写操作超时时间,默认值: 10s
max-concurrent-queries = 0  # 最大并发查询数,0无限制,默认值: 0
query-timeout = "0s  # 查询操作超时时间,0无限制,默认值:0s
log-queries-after = "0s"  # 慢查询超时时间,0无限制,默认值:0s
max-select-point = 0  # SELECT语句可以处理的最大点数(points),0无限制,默认值:0
max-select-series = 0  # SELECT语句可以处理的最大级数(series),0无限制,默认值:0
max-select-buckets = 0  # SELECT语句可以处理的最大"GROUP BY time()"的时间周期,0无限制,默认值:0

4、retention旧数据的保留策略

1
2
3
[retention]
enabled = true  # 是否启用该模块,默认值 : true
check-interval = "30m"  # 检查时间间隔,默认值 :"30m"

5、shard-precreation分区预创建

1
2
3
4
[shard-precreation]
enabled = true  # 是否启用该模块,默认值 : true
check-interval = "10m"  # 检查时间间隔,默认值 :"10m"
advance-period = "30m"  # 预创建分区的最大提前时间,默认值 :"30m"

6、monitor 控制InfluxDB自有的监控系统。 默认情况下,InfluxDB把这些数据写入_internal 数据库,如果这个库不存在则自动创建。 _internal 库默认的retention策略是7天,如果你想使用一个自己的retention策略,需要自己创建。

1
2
3
4
[monitor]
store-enabled = true  # 是否启用该模块,默认值 :true
store-database = "_internal"  # 默认数据库:"_internal"
store-interval = "10s  # 统计间隔,默认值:"10s"

7、admin web管理页面

1
2
3
4
5
[admin]
enabled = true  # 是否启用该模块,默认值 : false
bind-address = ":8083"  # 绑定地址,默认值 :":8083"
https-enabled = false  # 是否开启https ,默认值 :false
https-certificate = "/etc/ssl/influxdb.pem"  # https证书路径,默认值:"/etc/ssl/influxdb.pem"

8、http API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[http]
enabled = true  # 是否启用该模块,默认值 :true
bind-address = ":8086"  # 绑定地址,默认值:":8086"
auth-enabled = false  # 是否开启认证,默认值:false
realm = "InfluxDB"  # 配置JWT realm,默认值: "InfluxDB"
log-enabled = true  # 是否开启日志,默认值:true
write-tracing = false  # 是否开启写操作日志,如果置成true,每一次写操作都会打日志,默认值:false
pprof-enabled = true  # 是否开启pprof,默认值:true
https-enabled = false  # 是否开启https,默认值:false
https-certificate = "/etc/ssl/influxdb.pem"  # 设置https证书路径,默认值:"/etc/ssl/influxdb.pem"
https-private-key = ""  # 设置https私钥,无默认值
shared-secret = ""  # 用于JWT签名的共享密钥,无默认值
max-row-limit = 0  # 配置查询返回最大行数,0无限制,默认值:0
max-connection-limit = 0  # 配置最大连接数,0无限制,默认值:0
unix-socket-enabled = false  # 是否使用unix-socket,默认值:false
bind-socket = "/var/run/influxdb.sock"  # unix-socket路径,默认值:"/var/run/influxdb.sock"

9、subscriber 控制Kapacitor接受数据的配置

1
2
3
4
5
6
7
[subscriber]
enabled = true  # 是否启用该模块,默认值 :true
http-timeout = "30s"  # http超时时间,默认值:"30s"
insecure-skip-verify = false  # 是否允许不安全的证书
ca-certs = ""  # 设置CA证书
write-concurrency = 40  # 设置并发数目,默认值:40
write-buffer-size = 1000  # 设置buffer大小,默认值:1000

10、graphite 相关配置

1
2
3
4
5
6
7
8
9
10
11
12
[[graphite]]
enabled = false  # 是否启用该模块,默认值 :false
database = "graphite"  # 数据库名称,默认值:"graphite"
retention-policy = ""  # 存储策略,无默认值
bind-address = ":2003"  # 绑定地址,默认值:":2003"
protocol = "tcp"  # 协议,默认值:"tcp"
consistency-level = "one"  # 一致性级别,默认值:"one
batch-size = 5000  # 批量size,默认值:5000
batch-pending = 10  # 配置在内存中等待的batch数,默认值:10
batch-timeout = "1s"  # 超时时间,默认值:"1s"
udp-read-buffer = 0  # udp读取buffer的大小,0表示使用操作系统提供的值,如果超过操作系统的默认配置则会出错。 该配置的默认值:0
separator = "."  # 多个measurement间的连接符,默认值: "."

11、collectd

1
2
3
4
5
6
7
8
9
10
11
[[collectd]]
enabled = false  # 是否启用该模块,默认值 :false
bind-address = ":25826"  # 绑定地址,默认值: ":25826"
database = "collectd"  # 数据库名称,默认值:"collectd"
retention-policy = ""  # 存储策略,无默认值
typesdb = "/usr/local/share/collectd"  # 路径,默认值:"/usr/share/collectd/types.db"
auth-file = "/etc/collectd/auth_file"
batch-size = 5000
batch-pending = 10
batch-timeout = "10s"
read-buffer = 0  # udp读取buffer的大小,0表示使用操作系统提供的值,如果超过操作系统的默认配置则会出错。默认值:0

12、opentsdb

1
2
3
4
5
6
7
8
9
10
11
12
[[opentsdb]]
enabled = false  # 是否启用该模块,默认值:false
bind-address = ":4242"  # 绑定地址,默认值:":4242"
database = "opentsdb"  # 默认数据库:"opentsdb"
retention-policy = ""  # 存储策略,无默认值
consistency-level = "one"  # 一致性级别,默认值:"one"
tls-enabled = false  # 是否开启tls,默认值:false
certificate= "/etc/ssl/influxdb.pem"  # 证书路径,默认值:"/etc/ssl/influxdb.pem"
log-point-errors = true  # 出错时是否记录日志,默认值:true
batch-size = 1000
batch-pending = 5
batch-timeout = "1s"

13、udp

1
2
3
4
5
6
7
8
9
[[udp]]
enabled = false  # 是否启用该模块,默认值:false
bind-address = ":8089"  # 绑定地址,默认值:":8089"
database = "udp"  # 数据库名称,默认值:"udp"
retention-policy = ""  # 存储策略,无默认值
batch-size = 5000
batch-pending = 10
batch-timeout = "1s"
read-buffer = 0  # udp读取buffer的大小,0表示使用操作系统提供的值,如果超过操作系统的默认配置则会出错。 该配置的默认值:0 

14、continuous_queries

1
2
3
4
[continuous_queries]
enabled = true  # enabled 是否开启CQs,默认值:true
log-enabled = true  # 是否开启日志,默认值:true
run-interval = "1s"  # 时间间隔,默认值:"1s"

 

4 INSERT&SELECT

$ influx
Connected to http://localhost:8086 version 1.4.x
InfluxDB shell 1.4.x
>
#进入命令行工具

>CREATE DATABASE <db-name>
#全新安装的influxDB只有系统库_internal

> USE mydb
Using database mydb
#切换库

> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
>
#插入数据
#measurement为temperature
#有两个tag,machine和type
#两个field,external和internal

> select  * from cpu where time > '2018-11-16T07:26:30Z' and cpu = 'cpu-total'
name: cpu
time                cpu       host      region usage_guest usage_guest_nice usage_idle        usage_iowait        usage_irq usage_nice usage_softirq       usage_steal usage_system        usage_user          value
----                ---       ----      ------ ----------- ---------------- ----------        ------------        --------- ---------- -------------       ----------- ------------        ----------          -----
1542353200000000000 cpu-total webmaster        0           0                99.49647532728007 0                   0         0          0                   0           0.4028197381671498  0.10070493454184111
1542353210000000000 cpu-total webmaster        0           0                99.24433249371371 0.05037783375317808 0         0          0.05037783375315571 0           0.40302267002520986 0.25188916876574724
1542353220000000000 cpu-total webmaster        0           0                99.14400805638542 0.05035246727091133 0         0          0                   0           0.3021148036253249  0.5035246727088272 
1542353230000000000 cpu-total webmaster        0           0                99.19678714859504 0                   0         0          0                   0           0.4016064257028379  0.4016064257028379 
1542353240000000000 cpu-total webmaster        0           0                99.34343434344756 0                   0         0          0.05050505050505128 0           0.35353535353540383 0.2525252525253192 
1542353250000000000 cpu-total webmaster        0           0                99.44612286001723 0.050352467270849   0         0          0                   0           0.35246727089622926 0.15105740181261856
1542353260000000000 cpu-total webmaster        0           0                98.99345747357455 0                   0         0          0.05032712632108857 0           0.6542526421741425  0.3019627579265493 
1542353270000000000 cpu-total webmaster        0           0                99.24433249369552 0                   0         0          0                   0           0.4534005037783049  0.3022670025188699 
1542353280000000000 cpu-total webmaster        0           0                99.29577464789016 0                   0         0          0                   0           0.4024144869215736  0.3018108651911981 
1542353290000000000 cpu-total webmaster        0           0                99.39546599496731 0.05037783375317808 0         0          0.05037783375315123 0           0.3022670025189253  0.20151133501264074
1542353300000000000 cpu-total webmaster        0           0                98.42958459979086 0                   0         0          0.10131712259371246 0           1.0638297872339966  0.40526849037483187
1542353310000000000 cpu-total webmaster        0           0                98.68487607486902 0.10116337885683147 0         0          0.10116337885684942 0           0.4046535154273977  0.7081436519979639 
1542353320000000000 cpu-total webmaster        0           0                99.04282115867855 0                   0         0          0                   0           0.7052896725440059  0.25188916876570105
#从measurement cpu中读取所有tag、field、timestamp。
#筛选:时间戳大于某个值,cpu(tagk)=cpu-total(tagv)

> select  mean(usage_idle) from cpu where time > '2018-11-16T01:26:30Z'  group by time(10m) limit 10
name: cpu
time                mean
----                ----
1542331200000000000 97.81860093129568
1542331800000000000 97.94023021659679
1542332400000000000 98.15295604641332
1542333000000000000 98.00160525604912
1542333600000000000 98.25745141869217
1542334200000000000 98.34462917811965
1542334800000000000 93.87658077438817
1542335400000000000 98.25713867293715
1542336000000000000
1542336600000000000
#在cpu(measurement)中获取usage_idle(fieldk)的平均值(mean),间隔是10m。限制输出行为10

  

参考博文:http://www.cnblogs.com/MikeZhang/p/InfluxDBInstall20170206.html

原文地址:https://www.cnblogs.com/jabbok/p/9956442.html