监控思路
采集(使用Python脚本进行采集)
入库(Python脚本采集完插入Influxdb数据库)
展现(Grafana读取Influxdb的数据进行展现)

1.Python操作Influxdb数据库得先安装扩展

yum install epel-release -y #安装epel yum源

grafana 监控GPU grafana监控cpu,内存_python

yum install python2-pip -y #安装pip工具

grafana 监控GPU grafana监控cpu,内存_db数据库_02

pip install influxdb -i http://pypi.douban.com/simple --trusted-host pypi.douban.com #安装Influxdb扩展,有镜像的话直接使用pip install influxdb进行安装。

grafana 监控GPU grafana监控cpu,内存_python_03

测试一下是否安装成功

import成功,说明扩展安装成功了!使用influxdb这个扩展操作influxdb数据库。

grafana 监控GPU grafana监控cpu,内存_python_04

在python脚本中安装psutil,psutil可用来采集cpu,内存的一些数据。yum install python-devel gcc -y

pip install psutil -i http://pypi.douban.com/simple --trusted-host pypi.douban.com #安装psutil模块采集硬件信息

如果下载失败,可以试试pip install psutil==5.6.7

grafana 监控GPU grafana监控cpu,内存_linux_05

2.python操作influxdb(/data/influxdb/test.py)

python需要使用influxdb扩展去操作influxdb数据库
Client需要指定influxdb的ip、端口、用户名、密码、数据库名称

from influxdb import InfluxDBClient
client = InfluxDBClient('127.0.0.1', 8086, 'tuoyuxin', '123456', 'tuoyuxin')
data_list = [{'measurement': 'mytest', 'tags': {'item':'host_ip_item'}, 'fields': {'value': 100}}]
client.write_points(data_list)

验证Python操作Influxdb是否成功

grafana 监控GPU grafana监控cpu,内存_python_06

influx -username ‘tuoyuxin’ -password ‘123456’ -database tuoyuxin

select * from mytest

grafana 监控GPU grafana监控cpu,内存_grafana 监控GPU_07

3.实战采集Cpu load并绘图(/data/influxdb/loadavg.py

grafana 监控GPU grafana监控cpu,内存_linux_08

from influxdb import InfluxDBClient
import commands
client = InfluxDBClient('127.0.0.1', 8086, 'tuoyuxin', '123456', 'tuoyuxin')

status,loadavg1 = commands.getstatusoutput(" cat /proc/loadavg |awk '{print $1}' ")
status,loadavg5 = commands.getstatusoutput(" cat /proc/loadavg |awk '{print $2}' ")
status,loadavg15 = commands.getstatusoutput(" cat /proc/loadavg |awk '{print $3}' ")

data_list = [{'measurement': 'loadavg', 'tags': {'item': 'tuoyuxin_x.x.x.x_loadavg1'}, 'fields': {'value': float(loadavg1)}}]
client.write_points(data_list)
data_list = [{'measurement': 'loadavg', 'tags': {'item': 'tuoyuxin_x.x.x.x_loadavg5'}, 'fields': {'value': float(loadavg5)}}]
client.write_points(data_list)
data_list = [{'measurement': 'loadavg', 'tags': {'item': 'tuoyuxin_x.x.x.x_loadavg15'}, 'fields': {'value': float(loadavg15)}}]
client.write_points(data_list)

编辑/data/influxdb/loadavg.py文件,将上面的代码添加到该文件中,然后运行该文件。

grafana 监控GPU grafana监控cpu,内存_grafana 监控GPU_09

添加到linux的crontab任务,然后图表展示观察(/etc/crontab)

将*****root python /data/influxdb/loadavg.py >/dev/null 2>/dev/null添加到/etc/crontab文件中。

grafana 监控GPU grafana监控cpu,内存_linux_10

4.python监控cpu时间并入influxdb(/data/influxdb/cpu_times_percent.py)

创建一个新的dashboard,起名为cpu监控。

grafana 监控GPU grafana监控cpu,内存_db数据库_11


grafana 监控GPU grafana监控cpu,内存_linux_12

grafana 监控GPU grafana监控cpu,内存_db数据库_13


grafana 监控GPU grafana监控cpu,内存_linux_14

from influxdb import InfluxDBClient
import psutil

cpu_times_percent = psutil.cpu_times_percent(interval=1)
client = InfluxDBClient('127.0.0.1', 8086, 'shijiange', '123456', 'shijiange')

data_list = [{'measurement': 'cpu_times_percent', 'tags': {'item': 'shijiange_x.x.x.x_user'}, 'fields': {'value': float(cpu_times_percent.user)}}]
client.write_points(data_list)
data_list = [{'measurement': 'cpu_times_percent', 'tags': {'item': 'shijiange_x.x.x.x_system'}, 'fields': {'value': float(cpu_times_percent.system)}}]
client.write_points(data_list)
data_list = [{'measurement': 'cpu_times_percent', 'tags': {'item': 'shijiange_x.x.x.x_iowait'}, 'fields': {'value': float(cpu_times_percent.iowait)}}]
client.write_points(data_list)
data_list = [{'measurement': 'cpu_times_percent', 'tags': {'item': 'shijiange_x.x.x.x_idle'}, 'fields': {'value': float(cpu_times_percent.idle)}}]
client.write_points(data_list)

将上面的代码添加到/data/influxdb/cpu_times_percent.py文件中,然后运行,如下图所示。

grafana 监控GPU grafana监控cpu,内存_linux_15

grafana 监控GPU grafana监控cpu,内存_linux_16

5.添加到linux的crontab任务,然后图表展示观察

将*****root python /data/influxdb/cpu_times_percent.py >/dev/null 2>/dev/null 添加到/etc/crontab文件中。

grafana 监控GPU grafana监控cpu,内存_linux_17

下面将运行一个比较耗cpu的命令,来观察该图表的变化

grafana 监控GPU grafana监控cpu,内存_grafana 监控GPU_18


grafana 监控GPU grafana监控cpu,内存_Python_19

grafana 监控GPU grafana监控cpu,内存_linux_20


grafana 监控GPU grafana监控cpu,内存_grafana 监控GPU_21


grafana 监控GPU grafana监控cpu,内存_db数据库_22