Home  >  Article  >  Operation and Maintenance  >  A must-read for operation and maintenance monitoring: A must-know InfluxDB usage guide that can be used at critical moments

A must-read for operation and maintenance monitoring: A must-know InfluxDB usage guide that can be used at critical moments

WBOY
WBOYforward
2023-06-09 13:40:491578browse

​What is InfluxDB

InfluxDB is an open source sequential database developed by InfluxData. It is written in Go and focuses on high-performance query and storage of time series data. InfluxDB is widely used in scenarios such as monitoring data of storage systems and real-time data in the IoT industry. Technical features include:

  • InfluxDB fully utilizes the characteristics of the Go language in terms of technical implementation and can be deployed independently without any external dependencies [5].
  • InfluxDB provides a query language similar to SQL and a series of built-in functions to facilitate users to query data.
  • The data stored in InfluxDB is logically composed of Measurement, tag group, field group and a timestamp:

Measurement: A string represents the meaning of the record. . For example, it can be monitoring data cpu_load​, or measurement data average_temperature

tag group: It consists of a set of key-value pairs, which represents a series of attribute information of the record. The same measurement data does not necessarily have the same tag group, and it is Schema-free. Tag information is indexed by default.

Field group: It is also composed of a set of key-value pairs, which represents the specific value information (with a name) of the record. Definable value types in the field group include: 64-bit integer, 64-bit floating point, string and Boolean. Field information cannot be indexed.

Time stamp: It is the time attribute of the record. If the timestamp is not explicitly specified when inserting data, the timestamp stored in the database by default will be the entry time of the record.

InfluxDB supports HTTP-based data insertion and query. It also accepts connections directly based on TCP or UDP protocols.

InfluxDB allows users to define data retention policies (Retention Policies) to delete or downsample data stored for more than a specified period of time.

Add, delete, modify and query operations

Enter influxDB command line

influx -precision rfc3339

InfluxDB database operation

  • Display database
show databases
  • New database
create database shhnwangjian
  • Delete database
drop database shhnwangjian
  • Use the specified database
use shhnwangjian

InfluxDB data table Operation

In InfluxDB, there is no concept of table (table), instead it is MEASUREMENTS. The functions of MEASUREMENTS are consistent with the tables in traditional databases, so we can also call MEASUREMENTS a table in InfluxDB.

• Display all tables

SHOW MEASUREMENTS

• Create a new table

There is no explicit statement to create a new table in InfluxDB. New tables can only be created by inserting data.

insert disk_free,hostname=server01 value=442221834240i
insert cpu_virtual_used_num,host=1 value=41556593150

where disk_free​ is the table name, hostname​ is the index (tag), value=xx is the record value (field), there can be multiple record values, and the system comes with additional timestamps

Or when adding data, write the timestamp yourself

insert disk_free,hostname=server01 value=442221834240i 1435362189575692182

• Delete table

drop measurement disk_free

Data retention policies (Retention Policies)

influxDB does not provide direct deletion of data records method, but provides a data storage strategy, which is mainly used to specify the data retention time. If the specified time is exceeded, this part of the data will be deleted.

  • View current database Retention Policies
show retention policies on "db_name"
show retention policies on cdhnm
  • Create new Retention Policies
create retention policy "rp_name" on "db_name" duration 3w replication 1 default
create retention policy test on cdhnm duration 1h replication 1 default

rp_name: Policy name;

db_name: specific database name;

3w: Save for 3 weeks, data before 3 weeks will be deleted. influxdb has various event parameters, such as: h (hour), d (day), w (week); replication 1: number of copies, usually 1 is enough;

default: set as the default policy

  • Modify Retention Policies
alter retention policy “rp_name” on “db_name” duration 30d default
alter retention policy autogen on cdhnm duration 1h default
  • Modify data policy
alter retention policy autogen on cdhnm duration 0h replication 1 default
  • Delete Retention Policies
drop retention policy “rp_name” on “db_name"
drop retention policy test on cdhnm

Query data

select * fromcpu_virtual_used_num

Insert data

Insert data and create a table at the same time

insert disk_free,hostname=server01 value=442221834240i
insert cpu_virtual_used_num,host=470b14f0-e869-43ed-a8e6-fd634258271f,hostname=server01 value=0.3 1557023160

Delete data

influxDB does not provide a method to directly delete data records, but it provides a data preservation strategy, which is mainly used to specify the data retention time, exceeding the specified time , delete this part of the data. Create a new database expiration policy for at least one hour

Retention Policies
create retention policy "rp_name" on "db_name" duration 3w replication 1 default
retention policy duration must be at least 1h0m0s influxdb

Query table field

Query tag: show tag keys from cluster_metric

Query field: show field keys from cluster_metric

Time zone problem

When using InfluxDB, I found that because InfluxDB uses UTC time, time zone problems are often encountered when querying

1. Time format

InfluxDB In addition to supporting epoch_time, it also supports rfc3339_date_time_string and rfc3339_like_date_time_string.

epoch_time

Some theoretical explanations are the time that has passed since Coordinated Universal Time (Thursday, 1 January 1970). For example, what we get by using System.currentTimeMillis() in a java program is this time. Generally, it is millisecond level (ms) precision, that is, 13-bit Long type. In InfluxDB, the accuracy of timestamps can reach nanosecond level (ns), which is the 19-bit Long type.

rfc3339_date_time_string

rfc3339时间格式是ietf协会定义的一种时间格式,这个名字是因为它被定义在rfc3339中。感兴趣的同学可以自己查看上面的连接。InfluxDB中rfc3339的时间格式是这样的:

‘YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ’

其中nnnnnnnnn是可选的,如果不写则会被设置为000000000。注意,如果使用这种时间格式,需要使用单括号(’)将时间括起来。

rfc3339_like_date_time_string

因为rfc3339_date_time_string的格式确实比较反人类,所以InfluxDB也支持这种人类阅读更友好的格式:

‘YYYY-MM-DD HH:MM:SS.nnnnnnnnn’

其中HH:MM:SS.nnnnnnnnn是可选的,如果不填写会被设置为00:00:00.000000000。所以查询时可以设置到天、小时、分钟、秒等不同精度。这种时间格式同样要求被单括号括起来。

2、调整时间戳精度

InfluxDB默认东时间是纳秒(ns),即19位时间戳。但是一般情况下时间精度不会这么高。所以如果使用秒级精度查询:

select * from cpu_virtual_used_num where time >= 1435333209s and time <= 1542964713s

如果使用毫秒级精度查询:

select * from cpu_virtual_used_num where time >= 1435333209000ms and time <= 1542964714000ms

3、调整时区

如果需要使用北京时间(东八区),可以在SQL中使用tc关键字:

select * from cpu_virtual_used_num where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai')

4、UTC时间与Beijing时间转换

Timestamp时间列

既然是时间序列数据库,influxdb 的数据都有一列名为 time 的列,里面存储 UTC 时间戳。

Influxdb 时间转成北京时间:UTC time + 8 hours = Beijing time

sql语句

influx -precision rfc3339 
show retention policies on cdhnm
alter retention policy autogen on cdhnm duration 1h default
create retention policy test on cdhnm duration 1h replication 1 default
drop retention policy test on cdhnm 
insert cpu_virtual_used_num,host=470b14f0-e869-43ed-a8e6-fd634258271f,hostname=server01 value=0.9 1557045292000000000
select * from cpu_virtual_used_num where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai')
delete from cpu_virtual_used_num

启动服务

切换到root用户
命令:su
输入密码:123456

启动:

sudo service influxdb start

重启:

service influxdb restart

切换到普通用户:

命令:exit

The above is the detailed content of A must-read for operation and maintenance monitoring: A must-know InfluxDB usage guide that can be used at critical moments. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete