Influxdb常用操作

Influxdb常用操作

进入influxdb客户端

root@dev1:~# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.13.0
InfluxDB shell version: 0.13.0
>

查看所有库

> show databases
name: databases
 -------------
name
_internal
test

进入某个库

> use test
Using database test

查看库中的所有表

> show measurements
name: measurements
 ------------------
name
ceph_cluster

查看某个tag

> show tag values from ceph_cluster with key=type_instance
name: ceph_cluster
------------------
key     value
type_instance   cluster.free
type_instance   cluster.health
type_instance   cluster.iops

查看多个tag

> show tag values from ceph_cluster with key in (type, type_instance)
name: ceph_cluster
 ------------------
key     value
type        ceph_cluster
type_instance   cluster.free
type_instance   cluster.health
type        ceph_mon

根据条件匹配tag

> show tag values from ceph_cluster with key in (type, type_instance) where host='dev2'
>

查看所有tag序列

> show series from ceph_cluster
key
ceph_cluster,host=dev1,type=ceph_cluster,type_instance=cluster.free,unit=MB
ceph_cluster,host=dev1,type=ceph_cluster,type_instance=cluster.total,unit=MB
ceph_cluster,host=dev1,type=ceph_cluster,type_instance=cluster.used,unit=MB
ceph_cluster,host=dev1,type=ceph_mon,type_instance=cluster.mon.quorum_num
ceph_cluster,host=dev1,type=ceph_mon,type_instance=cluster.mon.total_num

查看表中数据

  • 按时间顺序查询数据
> select * from ceph_cluster limit 5
name: ceph_cluster
 ------------------
time            host    type        type_instance   unit    value
1499212809488769000 dev1    ceph_cluster    cluster.used    MB  58266.52734375
1499212809488819000 dev1    ceph_cluster    cluster.total   MB  245633.90625
1499212809488885000 dev1    ceph_cluster    cluster.free    MB  187367.37890625
1499212809491293000 dev1    ceph_cluster    cluster.health      0
1499212809491349000 dev1    ceph_cluster    cluster.iops        0
  • 设置时间显示模式
> precision rfc3339
> select * from ceph_cluster limit 5
name: ceph_cluster
 ------------------
time                host    type        type_instance   unit    value
2017-07-05T00:00:09.488769Z dev1    ceph_cluster    cluster.used    MB  58266.52734375
2017-07-05T00:00:09.488819Z dev1    ceph_cluster    cluster.total   MB  245633.90625
2017-07-05T00:00:09.488885Z dev1    ceph_cluster    cluster.free    MB  187367.37890625
2017-07-05T00:00:09.491293Z dev1    ceph_cluster    cluster.health      0
2017-07-05T00:00:09.491349Z dev1    ceph_cluster    cluster.iops        0
  • 按时间逆序查询数据
> select * from ceph_cluster order by desc limit 5
name: ceph_cluster
 ------------------
time                host    type        type_instance       unit    value
2017-08-04T01:48:49.521545Z dev1    ceph_pg     cluster.pg.active+clean     1024
2017-08-04T01:48:49.521501Z dev1    ceph_pg     cluster.pg.nums         1024
2017-08-04T01:48:49.507881Z dev1    ceph_osd    cluster.osd.down        0
2017-08-04T01:48:49.507831Z dev1    ceph_osd    cluster.osd.up          6
2017-08-04T01:48:49.507803Z dev1    ceph_osd    cluster.osd.out         0
  • 匹配tag字段查询数据
> select * from ceph_cluster where type='ceph_pg' order by desc limit 5
name: ceph_cluster
 ------------------
time                host    type    type_instance       unit    value
2017-08-04T01:49:19.517004Z dev1    ceph_pg cluster.pg.active+clean     1024
2017-08-04T01:49:19.516957Z dev1    ceph_pg cluster.pg.nums         1024
2017-08-04T01:49:09.509483Z dev1    ceph_pg cluster.pg.active+clean     1024
2017-08-04T01:49:09.509424Z dev1    ceph_pg cluster.pg.nums         1024
2017-08-04T01:48:59.518189Z dev1    ceph_pg cluster.pg.active+clean     1024
  • 查询某个时间范围内的数据
> select * from ceph_cluster where type_instance='cluster.pg.active+clean' and time > now() - 1h and time < now() - 10m order by desc limit 5
name: ceph_cluster
 ------------------
time                host    type    type_instance       unit    value
2017-08-04T02:16:59.501857Z dev1    ceph_pg cluster.pg.active+clean     1024
2017-08-04T02:16:49.505655Z dev1    ceph_pg cluster.pg.active+clean     1024
2017-08-04T02:16:39.524362Z dev1    ceph_pg cluster.pg.active+clean     1024
2017-08-04T02:16:29.512281Z dev1    ceph_pg cluster.pg.active+clean     1024
2017-08-04T02:16:19.517638Z dev1    ceph_pg cluster.pg.active+clean     1024

注:
时间可以是具体的时间戳,也可以是上面那种时间偏移。

  • 查询一个时间段内的平均值
> select mean(value) from ceph_cluster where type_instance='cluster.pg.active+clean' and time > now() - 1h and time < now() - 10m order by desc limit 5
name: ceph_cluster
 ------------------
time                mean
2017-08-04T01:30:08.159324342Z  1024
  • 查询一个时间段内的最大值
> select max(value) from ceph_cluster where type_instance='cluster.pg.active+clean' and time > now() - 1h and time < now() - 10m order by desc limit 5
name: ceph_cluster
 ------------------
time                max
2017-08-04T02:20:49.513249Z 1024
  • 查询一个时间段内的最后值
> select last(value) from ceph_cluster where type_instance='cluster.pg.active+clean' and time > now() - 1h and time < now() - 10m order by desc limit 5
name: ceph_cluster
 ------------------
time                last
2017-08-04T02:22:19.509789Z 1024
  • 按时间聚合查询一个时间段内的平均值
> select mean(value) from ceph_cluster where type_instance='cluster.pg.active+clean' and time > now() - 1h and time < now() - 10m group by time(10m)
name: ceph_cluster
 ------------------
time            mean
2017-08-04T01:30:00Z    1024
2017-08-04T01:40:00Z    1024
2017-08-04T01:50:00Z    1024
2017-08-04T02:00:00Z    1024
2017-08-04T02:10:00Z    1024
2017-08-04T02:20:00Z    1024
  • 按某个tag聚合查询一个时间段内的平均值
> select mean(value) from ceph_cluster where type='ceph_pg' and time > now() - 1h and time < now() - 10m group by type_instance
name: ceph_cluster
tags: type_instance=cluster.pg.active+clean
time                mean
 ----               ----
2017-08-04T01:35:59.515665887Z  1024
 --
name: ceph_cluster
tags: type_instance=cluster.pg.nums
time                mean
 ----               ----
2017-08-04T01:35:59.515665887Z  1024
  • 查看一个时间段内的数据量
> select count(value) from ceph_cluster where type='ceph_pg' and time > now() - 1h and time < now() - 10m
name: ceph_cluster
 ------------------
time                count
2017-08-04T01:40:00.016998982Z  600

使用正则匹配

> select * from ceph_cluster where type_instance =~ /cluster.osd/ order by desc limit 5
name: ceph_cluster
 ------------------
time                host    type        type_instance       unit    value
2017-08-04T02:42:59.503222Z dev1    ceph_osd    cluster.osd.down        0
2017-08-04T02:42:59.503201Z dev1    ceph_osd    cluster.osd.up          6
2017-08-04T02:42:59.50318Z  dev1    ceph_osd    cluster.osd.out         0
2017-08-04T02:42:59.503159Z dev1    ceph_osd    cluster.osd.in          6
2017-08-04T02:42:59.503137Z dev1    ceph_osd    cluster.osd.total       6

> select * from ceph_cluster where type_instance !~ /cluster.osd/ order by desc limit 5
name: ceph_cluster
 ------------------
time                host    type        type_instance       unit    value
2017-08-04T02:43:39.53797Z  dev1    ceph_pg     cluster.pg.active+clean     1024
2017-08-04T02:43:39.537936Z dev1    ceph_pg     cluster.pg.nums         1024
2017-08-04T02:43:39.494511Z dev1    ceph_mon    cluster.mon.quorum_num      3
2017-08-04T02:43:39.494489Z dev1    ceph_mon    cluster.mon.total_num       3
2017-08-04T02:43:39.494454Z dev1    ceph_cluster    cluster.read.bandwidth  MB/s    0

匹配删除数据

> delete from system_disk where instance='dm-0' or instance='dm-1' or instance='rbd0' or instance='rbd1'
>
> delete from system_cpu where type_instance='cpu.used' and time < now() - 1h
>

删除库

> drop database test
> 
> show databases
name: databases
---------------
name
_internal

查看库订阅信息

> show subscriptions
name: collectd
--------------
retention_policy    name                        mode    destinations
default         kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:41148]
common          kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:55940]
longterm        kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:35278]

name: _internal
---------------
retention_policy    name                        mode    destinations
monitor         kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:56977]

name: physics_monitor
---------------------
retention_policy    name                        mode    destinations
default         kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:33502]

name: docker_monitor
--------------------
retention_policy    name                        mode    destinations
default         kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:51330]

name: k8s
---------
retention_policy    name                        mode    destinations
default         kapacitor-24697dad-aa7d-4ed7-b634-d6c00ff7b2de  ANY [udp://localhost:52220]

查看库的保存策略

> show retention policies on collectd
name        duration    shardGroupDuration  replicaN    default
default     168h0m0s    24h0m0s         1       true
common      1h0m0s      1h0m0s          1       false
longterm    2h0m0s      1h0m0s          1       false

创建库的保存策略

> create retention policy test on test duration 7d replication 1 default
>
> show retention policies on test
name    duration    shardGroupDuration  replicaN    default
default 0       168h0m0s        1       false
test    168h0m0s    24h0m0s         1       true

修改库的保存策略

> alter retention policy test on test duration 1d default
>
> show retention policies on test
name    duration    shardGroupDuration  replicaN    default
default 0       168h0m0s        1       false
test    24h0m0s     1h0m0s          1       true

注:

  • 以上所有SQL语句都可以多条同时查询,每个语句后加上 ‘;’ 即可。
  • 以上所有命令都可以通过influxdb的HTTP接口访问查询。

链接

  • Influxdb官方文档

你可能感兴趣的:(监控)