标签:
#!/usr/bin/env python
# coding:utf-8
import os
import datetime,time
import json,urllib2
#zaabix api and user\pwd
url=‘http://10.1.180.166/zabbix/api_jsonrpc.php‘
username=‘Admin‘
password=‘zabbix‘
‘‘‘定义日期,和记录日期文件路径‘‘‘
#windows
# interval_file_path=r‘C:\\Users\\SammyTan.SAMMY-PC\\Desktop\\‘
# log_file=r‘C:\\Users\\SammyTan.SAMMY-PC\\Desktop\\host_delete.log‘
#linux
interval_file_path=r‘/tmp/‘
log_file=r‘/tmp/host_delete.log‘
#set expire time
day =5
def authenticate(url, username, password):
values ={‘jsonrpc‘:‘2.0‘,
‘method‘:‘user.login‘,
‘params‘:{
‘user‘: username,
‘password‘: password
},
‘id‘:‘0‘
}
data = json.dumps(values)
req = urllib2.Request(url, data,{‘Content-Type‘:‘application/json-rpc‘})
response = urllib2.urlopen(req, data)
output = json.loads(response.read())
try:
message = output[‘result‘]
except:
message = output[‘error‘][‘data‘]
print message
quit()
return output[‘result‘]
def invalid_host_query(auth):
‘‘‘API Check unreachable host‘‘‘
values ={
"jsonrpc":"2.0",
"method":"host.get",
"params":{
"output":[
"hostid",
"name",
"available"
],
"filter":{
"available":"2"
}
},
"id":1,
"auth": auth
}
data = json.dumps(values)
req = urllib2.Request(url, data,{‘Content-Type‘:‘application/json-rpc‘})
response = urllib2.urlopen(req, data)
output = json.loads(response.read())
try:
message = output[‘result‘]
except:
message = output[‘error‘][‘data‘]
print message
quit()
return output[‘result‘]
def delete_host(hostid,auth):
‘‘‘API delete unreachable host ‘‘‘
values ={
"jsonrpc":"2.0",
"method":"host.delete",
"params":[
hostid
],
"auth": auth,
"id":1
}
data = json.dumps(values)
req = urllib2.Request(url, data,{‘Content-Type‘:‘application/json-rpc‘})
response = urllib2.urlopen(req, data)
output = json.loads(response.read())
try:
message = output[‘result‘]
except:
message = output[‘error‘][‘data‘]
print message
quit()
return output[‘result‘]
def host_period_file(hostid,hostname):
‘‘‘check and create unreachable host date log file‘‘‘
nowtime = datetime.datetime.now()
hostid = str(hostid)
hostname = str(hostname)
if os.path.exists(interval_file_path+hostid+‘.log‘):
file = open(interval_file_path+hostid+‘.log‘,‘r‘)
content = file.readline()[0:19]
file.close
return content
else:
file = open(interval_file_path+hostid+‘.log‘,‘w‘)
file.write(str(nowtime))
file.close
return str(nowtime)[0:19]
def main():
auth = authenticate(url,username,password)
hostlist = invalid_host_query(auth)
for host in hostlist:
hostid = int(host[‘hostid‘])
hostname = str(host[‘name‘])
oldtime = host_period_file(hostid,hostname)
if oldtime !=‘None‘:
ago_day = datetime.datetime.strptime(oldtime,‘%Y-%m-%d %H:%M:%S‘)
now_day = datetime.datetime.now()
result = int((now_day - ago_day).days)
if result ==0:
print‘Pass‘
elif result == day:
delete_host(hostid,auth)
#remove the host log file
os.system(‘rm -f %s‘%(interval_file_path+hostid+‘.log‘))
print‘Delete host‘,hostname
elif result > day:
#reset the host log file
os.system(‘:> %s‘%(interval_file_path+hostid+‘.log‘))
else:
print‘The host not log file , create file ‘
if __name__ ==‘__main__‘:
main()
标签:
原文地址:http://www.cnblogs.com/gyming/p/5781395.html