Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
蜕变的菜鸟
glances
提交
a27715b2
G
glances
项目概览
蜕变的菜鸟
/
glances
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glances
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a27715b2
编写于
5月 29, 2021
作者:
N
nicolargo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Generate specific tag from stats' key for InfluxDB and InfluxDB2 export #1871
上级
1f41d108
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
517 addition
and
1367 deletion
+517
-1367
conf/glances-grafana.json
conf/glances-grafana.json
+414
-1310
conf/glances.conf
conf/glances.conf
+10
-12
docs/gw/influxdb.rst
docs/gw/influxdb.rst
+33
-15
glances/exports/glances_influxdb.py
glances/exports/glances_influxdb.py
+9
-2
glances/exports/glances_influxdb2.py
glances/exports/glances_influxdb2.py
+51
-28
未找到文件。
conf/glances-grafana.json
浏览文件 @
a27715b2
此差异已折叠。
点击以展开。
conf/glances.conf
浏览文件 @
a27715b2
...
...
@@ -449,12 +449,11 @@ db=glances
# => foo.cpu
# => foo.mem
# You can also use dynamic values
#prefix=`hostname`
prefix
=
localhost
# Tags will be added for all measurements
#tags=foo:bar,spam:eggs
# You can also use dynamic values
#tags=system:`uname -s`
#prefix=foo
# Followings tags will be added for all measurements
# You can also use dynamic values.
# Note: hostname is always added as a tag
#tags=foo:bar,spam:eggs,domain:`domainname`
[
influxdb2
]
# Configuration for the --export influxdb2 option
...
...
@@ -470,12 +469,11 @@ token=EjFUTWe8U-MIseEAkaVIgVnej_TrnbdvEcRkaB1imstW7gapSqy6_6-8XD-yd51V0zUUpDy-kA
# => foo.cpu
# => foo.mem
# You can also use dynamic values
#prefix=`hostname`
prefix
=
localhost
# Tags will be added for all measurements
#tags=foo:bar,spam:eggs
# You can also use dynamic values
#tags=system:`uname -s`
#prefix=foo
# Followings tags will be added for all measurements
# You can also use dynamic values.
# Note: hostname is always added as a tag
#tags=foo:bar,spam:eggs,domain:`domainname`
[
cassandra
]
# Configuration for the --export cassandra option
...
...
docs/gw/influxdb.rst
浏览文件 @
a27715b2
...
...
@@ -5,9 +5,29 @@ InfluxDB
You can export statistics to an ``InfluxDB`` server (time series server).
In Glances version 3.1.8 and higher, the value of the key field will be
converted as a tag. For example, in the Docker plugin, the key = name,
so the container name will be tag and not a field.
In Glances version 3.1.8 and higher, the way Glances exports stats to
InfluxDB changes. The following fields will be added as tags:
- key stats (for example *interface_name* for network, container *name* for docker...)
- hostname (shortname)
- tags
Glances InfluxDB data model:
+---------------+-----------------------+-----------------------+
| Measurement | Fields | Tags |
+===============+=======================+=======================+
| cpu | user | hostname |
| | system | |
| | iowait... | |
+---------------+-----------------------+-----------------------+
| network | rx | |
| | tx | |
| | time_since_update... | hostname |
| | | interface_name |
+---------------+-----------------------+-----------------------+
| docker | cpu_percent | hostname |
| | memory_usage... | name |
+---------------+-----------------------+-----------------------+
InfluxDB (up to version 1.7.x)
------------------------------
...
...
@@ -29,12 +49,11 @@ following:
# => foo.cpu
# => foo.mem
# You can also use dynamic values
#prefix=`hostname`
prefix=localhost
# Tags will be added for all measurements
#tags=foo:bar,spam:eggs
# You can also use dynamic values
#tags=system:`uname -s`
#prefix=foo
# Followings tags will be added for all measurements
# You can also use dynamic values.
# Note: hostname is always added as a tag
#tags=foo:bar,spam:eggs,domain:`domainname`
and run Glances with:
...
...
@@ -72,12 +91,11 @@ following:
# => foo.cpu
# => foo.mem
# You can also use dynamic values
#prefix=`hostname`
prefix=localhost
# Tags will be added for all measurements
#tags=foo:bar,spam:eggs
# You can also use dynamic values
#tags=system:`uname -s`
#prefix=foo
# Followings tags will be added for all measurements
# You can also use dynamic values.
# Note: hostname is always added as a tag
#tags=foo:bar,spam:eggs,domain:`domainname`
and run Glances with:
...
...
glances/exports/glances_influxdb.py
浏览文件 @
a27715b2
...
...
@@ -20,6 +20,7 @@
"""InfluxDB (up to InfluxDB 1.7.x) interface class."""
import
sys
from
platform
import
node
from
glances.logger
import
logger
from
glances.exports.glances_export
import
GlancesExport
...
...
@@ -56,6 +57,9 @@ class Export(GlancesExport):
if
not
self
.
export_enable
:
sys
.
exit
(
2
)
# The hostname is always add as a tag
self
.
hostname
=
node
().
split
(
'.'
)[
0
]
# Init the InfluxDB client
self
.
client
=
self
.
init
()
...
...
@@ -115,7 +119,7 @@ class Export(GlancesExport):
else
:
fields
=
data_dict
# Transform to InfluxDB datamodel
# https://docs.influxdata.com/influxdb/v1.
5
/write_protocols/line_protocol_reference/
# https://docs.influxdata.com/influxdb/v1.
8
/write_protocols/line_protocol_reference/
for
k
in
fields
:
# Do not export empty (None) value
if
fields
[
k
]
is
None
:
...
...
@@ -137,6 +141,8 @@ class Export(GlancesExport):
tags
[
fields
[
'key'
]]
=
str
(
fields
[
fields
[
'key'
]])
# Remove it from the field list (can not be a field and a tag)
fields
.
pop
(
fields
[
'key'
])
# Add the hostname as a tag
tags
[
'hostname'
]
=
self
.
hostname
# Add the measurement to the list
ret
.
append
({
'measurement'
:
name
,
'tags'
:
tags
,
...
...
@@ -153,7 +159,8 @@ class Export(GlancesExport):
logger
.
debug
(
"Cannot export empty {} stats to InfluxDB"
.
format
(
name
))
else
:
try
:
self
.
client
.
write_points
(
self
.
_normalize
(
name
,
columns
,
points
),
time_precision
=
"s"
)
self
.
client
.
write_points
(
self
.
_normalize
(
name
,
columns
,
points
),
time_precision
=
"s"
)
except
Exception
as
e
:
# Log level set to debug instead of error (see: issue #1561)
logger
.
debug
(
"Cannot export {} stats to InfluxDB ({})"
.
format
(
name
,
e
))
...
...
glances/exports/glances_influxdb2.py
浏览文件 @
a27715b2
...
...
@@ -2,7 +2,7 @@
#
# This file is part of Glances.
#
# Copyright (C) 202
0
Nicolargo <nicolas@nicolargo.com>
# Copyright (C) 202
1
Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
...
...
@@ -67,7 +67,7 @@ class Export(GlancesExport):
try
:
client
=
InfluxDBClient
(
url
=
url
,
enable_gzip
=
False
,
org
=
self
.
org
,
org
=
self
.
org
,
token
=
self
.
token
)
except
Exception
as
e
:
logger
.
critical
(
"Cannot connect to InfluxDB server '%s' (%s)"
%
(
url
,
e
))
...
...
@@ -76,7 +76,6 @@ class Export(GlancesExport):
logger
.
info
(
"Connected to InfluxDB server version {} ({})"
.
format
(
client
.
health
().
version
,
client
.
health
().
message
))
# Create the write client
write_client
=
client
.
write_api
(
write_options
=
WriteOptions
(
batch_size
=
500
,
flush_interval
=
10000
,
...
...
@@ -89,31 +88,55 @@ class Export(GlancesExport):
def
_normalize
(
self
,
name
,
columns
,
points
):
"""Normalize data for the InfluxDB's data model."""
for
i
,
_
in
enumerate
(
points
):
# Supported type:
# https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/
if
points
[
i
]
is
None
:
# Ignore points with None value
del
(
points
[
i
])
del
(
columns
[
i
])
continue
try
:
points
[
i
]
=
float
(
points
[
i
])
except
(
TypeError
,
ValueError
):
pass
ret
=
[]
# Build initial dict by crossing columns and point
data_dict
=
dict
(
zip
(
columns
,
points
))
# issue1871 - Check if a key exist. If a key exist, the value of
# the key should be used as a tag to identify the measurement.
keys_list
=
[
k
.
split
(
'.'
)[
0
]
for
k
in
columns
if
k
.
endswith
(
'.key'
)]
if
len
(
keys_list
)
==
0
:
keys_list
=
[
None
]
for
measurement
in
keys_list
:
# Manage field
if
measurement
is
not
None
:
fields
=
{
k
.
replace
(
'{}.'
.
format
(
measurement
),
''
):
data_dict
[
k
]
for
k
in
data_dict
if
k
.
startswith
(
'{}.'
.
format
(
measurement
))}
else
:
continue
try
:
points
[
i
]
=
str
(
points
[
i
])
except
(
TypeError
,
ValueError
):
pass
else
:
continue
return
[{
'measurement'
:
name
,
'tags'
:
self
.
parse_tags
(
self
.
tags
),
'fields'
:
dict
(
zip
(
columns
,
points
))}]
fields
=
data_dict
# Transform to InfluxDB datamodel
# https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/
for
k
in
fields
:
# Do not export empty (None) value
if
fields
[
k
]
is
None
:
fields
.
pop
(
k
)
# Convert numerical to float
try
:
fields
[
k
]
=
float
(
fields
[
k
])
except
(
TypeError
,
ValueError
):
# Convert others to string
try
:
fields
[
k
]
=
str
(
fields
[
k
])
except
(
TypeError
,
ValueError
):
pass
# Manage tags
tags
=
self
.
parse_tags
(
self
.
tags
)
if
'key'
in
fields
and
fields
[
'key'
]
in
fields
:
# Create a tag from the key
# Tag should be an string (see InfluxDB data model)
tags
[
fields
[
'key'
]]
=
str
(
fields
[
fields
[
'key'
]])
# Remove it from the field list (can not be a field and a tag)
fields
.
pop
(
fields
[
'key'
])
# Add the hostname as a tag
tags
[
'hostname'
]
=
self
.
hostname
# Add the measurement to the list
ret
.
append
({
'measurement'
:
name
,
'tags'
:
tags
,
'fields'
:
fields
})
return
ret
def
export
(
self
,
name
,
columns
,
points
):
"""Write the points to the InfluxDB server."""
...
...
@@ -127,7 +150,7 @@ class Export(GlancesExport):
try
:
self
.
client
.
write
(
self
.
bucket
,
self
.
org
,
self
.
_normalize
(
name
,
columns
,
points
),
self
.
_normalize
(
name
,
columns
,
points
),
time_precision
=
"s"
)
except
Exception
as
e
:
# Log level set to debug instead of error (see: issue #1561)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录