架构
grafana+influxdb在一台机器上
telegraf装到不同机器上
python也在不同机器上运行
Grafana8+influxDB2
influx升级到2后,跟原来1.x配套的 Telegraf,Grafana都要做对应升级
InfluxDB2的docker-compose
version: '3.5'
services:
influxdb2:
image: influxdb:latest
network_mode: "bridge"
container_name: influxdb2
ports:
- "8086:8086"
volumes:
- type: bind
source: /Users/linzhiji/Documents/docker/influxdb2/data
target: /var/lib/influxdb2
- type: bind
source: /Users/linzhiji/Documents/docker/influxdb2/config
target: /etc/influxdb2
environment:
- DOCKER_INFLUXDB_INIT_MODE=setup
- DOCKER_INFLUXDB_INIT_USERNAME=username
- DOCKER_INFLUXDB_INIT_PASSWORD=xxxxx!
- DOCKER_INFLUXDB_INIT_ORG=xxxx
- DOCKER_INFLUXDB_INIT_BUCKET=xxx
- DOCKER_INFLUXDB_INIT_RETENTION=1w
- DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=xxxx
restart: always
grafana8:
image: grafana/grafana:latest
network_mode: "bridge"
container_name: grafana8
volumes:
- /Users/linzhiji/Documents/docker/grafana:/var/lib/grafana
ports:
- "3000:3000"
restart: always
links:
- influxdb2
注意,grafana后面加了 links: - influxdb2,主要是为了docker之间互相访问
grafana配置数据源
Telegraf
安装,根据https://docs.influxdata.com/telegraf/v1.19/introduction/installation/官方说明
# 1添加ubuntu的数据源
wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
# 2安装telegraf
sudo apt-get update && sudo apt-get install telegraf
第三步,我们不根据官方文档流程走,而是用influxdb里的telegraf配置来,如下图,新建telegraf配置后,再编辑,把127.0.0.1换成我们influxdb的服务器地址
把框里的内容替换一下,然后在客户机上执行一下。这样所有的机器配置文件都集中管理了,不用每个机器都去编辑
telegraf+influxdb+grafana
在grafana中选个模板,https://grafana.com/grafana/dashboards/14126
Python
脚本
import _thread
import time
import socket
import psutil
from influxdb import InfluxDBClient
client=InfluxDBClient('localhost',8086,'u_wyk13195','p_wyk13195','my_monitor')
#获取本机IP
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 0))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
ip = get_ip()
print(ip)
#获取cpu信息
def get_cpu(sec):
while True:
time.sleep(sec)
info=psutil.cpu_percent(0)
text=[
{
"measurement":"cpu_info",
"tags":{
"host":ip
},
"fields":{
"percent":info
}
}
]
client.write_points(text)
def get_memory(sec):
while True:
time.sleep(sec)
info=psutil.virtual_memory()
text=[
{
"measurement":"memory_info",
"tags":{
"host":ip
},
"fields":{
"mem_percent":info.percent,
"mem_used":info.used,
"mem_free":info.free,
}
}
]
client.write_points(text)
def get_disk(sec):
while True:
time.sleep(sec)
info=psutil.disk_usage('/')
text=[
{
"measurement":"disk_info",
"tags":{
"host":ip
},
"fields":{
"disk_used":info.used,
"disk_free":info.free,
"disk_percent":info.percent,
}
}
]
client.write_points(text)
def get_network(sec):
while True:
time.sleep(sec)
#print(psutil.net_io_counters(pernic=True))
info = psutil.net_io_counters(pernic=True)['WLAN 3']
text=[
{
"measurement":"network_info",
"tags":{
"host":ip
},
"fields":{
"bytes_sent":info.bytes_sent,
"bytes_recv":info.bytes_recv,
}
}
]
client.write_points(text)
try:
_thread.start_new_thread( get_cpu,(10,))
except:
print("ERROR:cpu unable to start thread")
try:
_thread.start_new_thread( get_memory, (10,))
except:
print("ERROR:memory unable to start thread")
try:
_thread.start_new_thread( get_disk, (10,))
except:
print("ERROR:disk unable to start thread")
try:
_thread.start_new_thread( get_network,(10,))
except:
print("ERROR:net unable to start thread")
while 1:
pass
参考文档: