centos7上
下载influxdb:
#wget https://dl.influxdata.com/influxdb/releases/influxdb-0.13.0.x86_64.rpm
安装:
#sudo yum localinstall influxdb-0.13.0.x86_64.rpm
运行:
#systemctl start influxdb.service
进入终端
#influx
> create database cadvisor ## 创建数据库cadvisor
> show databases
name: databases name
---- _internal cadvisor
> CREATE USER testuser WITH PASSWORD 'testpwd' ## 创建用户和设置密码
> GRANT ALL PRIVILEGES ON cadvisor TO testuser ## 授权数据库给指定用户
> CREATE RETENTION POLICY "cadvisor_retention" ON "cadvisor" DURATION 30d REPLICATION 1 DEFAULT## 创建默认的数据保留策略,设置保存时间30天,副本为1
-----------------------------------------------
import psutil
import os
frominfluxdb import InfluxDBClient
import time,math,random
pidp1=psutil.Process(os.getpid()) #获取当前运行的
from influxdb import InfluxDBClient
import time,math,random
while True:
a = psutil.virtual_memory().percent #内存占用率
b = psutil.cpu_percent(interval=1.0) #cpu占用率
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, 'testuser', 'testpwd', 'xxyyxx')
client.create_database('xxyyxx')
client.write_points(json_body)
#result = client.query('select value from cpu_load_short;')
#print("Result: {0}".format(result))
time.sleep(2)