Python/CpuToInfluxdb.py

37 lines
952 B
Python
Raw Normal View History

2016-06-23 23:27:21 +08:00
import psutil
import os
from influxdb import InfluxDBClient
import time,math,random
#获取当前运行的pid
p1=psutil.Process(os.getpid())
from influxdb import InfluxDBClient
import time,math,random
while True:
2016-06-24 09:36:57 +08:00
a = psutil.virtual_memory().percent #内存占用率
2016-06-23 23:27:21 +08:00
2016-06-24 09:36:57 +08:00
b = psutil.cpu_percent(interval=1.0) #cpu占用率
2016-06-23 23:27:21 +08:00
json_body = [
{
"measurement": "cpu_load_short",
"tags": {
"host": "server01",
"region": "us-west"
},
#"time": "2009-11-10T23:00:00Z",
"fields": {
"cpu": b,
"mem": a
}
}
]
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'xxyyxx')
2016-06-24 09:36:57 +08:00
client.create_database('xxyyxx',if_not_exists=False)
2016-06-23 23:27:21 +08:00
client.write_points(json_body)
#result = client.query('select value from cpu_load_short;')
#print("Result: {0}".format(result))
time.sleep(2)