Grafana 8 - InfluxDB 2 - Telegraf - python系统监控

架构

 

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

参考文档:

Infludb 2.0官方文档

Telegraf官方文档

Grafana 8 - InfluxDB 2 - Telegraf - 2021 monitoring stack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值