使用Python监控本机资源情况写入InfluxDB并使用Grafana监控

前面的文章介绍了如何安装influxdb以及grafana,如何使用python操作influxdb,本篇我们做一个小demo,使用python实时监控本机的资源占用情况,并写入influxdb数据库,通过grafana进行监控。

一、Grafana6.7.3安装及使用

二、InfluxDB1.1.0和1.8.0版本安装并开启web界面

三、使用Python操作InfluxDB时序数据库

-----------------------------------------------------------------------------------------------

Grafana官方网站:https://grafana.com/

InfluxDB官网:https://www.influxdata.com

安装python包

pip install influxdb==5.3.0
pip install psutil==5.6.3

编写脚本

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

启动influxdb

nohup $INFLUXDB18_HOME/usr/bin/influxd -config $INFLUXDB18_HOME/etc/influxdb/influxdb.conf &>$INFLUXDB18_HOME/log &

启动grafana

CentOS7:
systemctl restart grafana-server
systemctl enable grafana-server
 
CentOS6:
service grafana-server restart
chkconfig grafana-server on 

启动脚本

Grafana报表

其实grafana报表是以Json的方式存储的,我们也可以将它导出,附件中会提供本篇的代码和grafana报表的Json文件,按照上面的方式安装之后就能做出一样的效果。记得修改json文件中对应的grafana和influxdb的版本号,以及自定义的数据源名称。

报表及代码

导出报表:

点击share dashbord-->Export--SavetoFile

需要修改的部分:datasource,grafana版本号

配置自动刷新规则:

在报表设置中time picker可以设置此报表自动刷新可以选择的几种选项:

右上角的时间点击后可以选择显示的数据区间以及自动刷新时间间隔:

猜你喜欢

转载自blog.csdn.net/wsdc0521/article/details/106082411
今日推荐