forked from injetlee/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCpuToInfluxdb.py
53 lines (40 loc) · 1.19 KB
/
CpuToInfluxdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import psutil
import os
from influxdb import InfluxDBClient
import time,math,random
#获取当前运行的pid
p1=psutil.Process(os.getpid())
#打印本机的内存信息
#print ('直接打印内存占用: '+(str)(psutil.virtual_memory))
#打印内存的占用率
from influxdb import InfluxDBClient
import time,math,random
while True:
# for i in range(360):
# sin = round(random.random()*1000,2)
# print (sin)
a = psutil.virtual_memory().percent
#本机cpu的总占用率
b = psutil.cpu_percent(interval=1.0)
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')
print('aaaaaa')
#client.create_database('xxyyxx',if_not_exists=False)
print('bbbbb')
client.write_points(json_body)
#result = client.query('select value from cpu_load_short;')
#print("Result: {0}".format(result))
time.sleep(2)