为什么5小时内查不到插入influxdb的数据
why I can not find out the data which insert in influxdb within 5 hours
我有一个测试我的 influxdb 的脚本:
from influxdb import InfluxDBClient
import time
now_time = "2016-03-21 15:03:46"
#now_time = time.strftime("%Y-%m-%dT%XZ",time.localtime())
json_body = [
{
"measurement": "info_cpu_load3",
"tags":{
"host": "1",
"cpu": "cpu0"
},
"time": "%s"%time.strftime("%Y-%m-%d %X",time.localtime()),
"fields": {
"user":12,
"iowait":15
}
}
]
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'monitor')
client.create_database('monitor')
client.write_points(json_body)
当我注释行 "now_time = time.strftime("%Y-%m-%dT%XZ",time.localtime())" 时它会正常工作,但取消注释这一行,我不能从数据库中找到任何东西,为什么?
我发现它是时区的原因,在搜索了 influxdb 文档后,我了解到 influxdb 只支持 utc 时间,所以我通过编写一个将本地时间转换为 utc 时间的函数来解决这个问题。
import pytz
def utctime(self,times):
'''
transform local time to utc time
:param times:
:return:
'''
try:
utc = pytz.utc
cmd = "cat /etc/sysconfig/clock|cut -d = -f 2"
out,err,stat = exec_shell(cmd)
now_utc = out.read().strip()
fmt = '%Y-%m-%d %H:%M:%S'
amsterdam = pytz.timezone(now_utc)
dt = datetime.datetime.strptime(times, fmt)
am_dt = amsterdam.localize(dt)
utc_time = am_dt.astimezone(utc).strftime(fmt)
except Exception,err:
utc_time = 1
dlog("utctime err:%s"%err)
return utc_time
我有一个测试我的 influxdb 的脚本:
from influxdb import InfluxDBClient
import time
now_time = "2016-03-21 15:03:46"
#now_time = time.strftime("%Y-%m-%dT%XZ",time.localtime())
json_body = [
{
"measurement": "info_cpu_load3",
"tags":{
"host": "1",
"cpu": "cpu0"
},
"time": "%s"%time.strftime("%Y-%m-%d %X",time.localtime()),
"fields": {
"user":12,
"iowait":15
}
}
]
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'monitor')
client.create_database('monitor')
client.write_points(json_body)
当我注释行 "now_time = time.strftime("%Y-%m-%dT%XZ",time.localtime())" 时它会正常工作,但取消注释这一行,我不能从数据库中找到任何东西,为什么?
我发现它是时区的原因,在搜索了 influxdb 文档后,我了解到 influxdb 只支持 utc 时间,所以我通过编写一个将本地时间转换为 utc 时间的函数来解决这个问题。
import pytz
def utctime(self,times):
'''
transform local time to utc time
:param times:
:return:
'''
try:
utc = pytz.utc
cmd = "cat /etc/sysconfig/clock|cut -d = -f 2"
out,err,stat = exec_shell(cmd)
now_utc = out.read().strip()
fmt = '%Y-%m-%d %H:%M:%S'
amsterdam = pytz.timezone(now_utc)
dt = datetime.datetime.strptime(times, fmt)
am_dt = amsterdam.localize(dt)
utc_time = am_dt.astimezone(utc).strftime(fmt)
except Exception,err:
utc_time = 1
dlog("utctime err:%s"%err)
return utc_time