Auto delete down host
#!/usr/bin/env python# coding:utf-8import osimport datetime,timeimport json,urllib2#zaabix api and user\pwdurl='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'#linuxinterval_file_path=r'/tmp/'log_file=r'/tmp/host_delete.log'#set expire timeday =5def 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 messagequit()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 messagequit()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 messagequit()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.closereturn contentelse:file = open(interval_file_path+hostid+'.log','w')file.write(str(nowtime))file.closereturn 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 fileos.system('rm -f %s'%(interval_file_path+hostid+'.log'))print'Delete host',hostnameelif result > day:#reset the host log fileos.system(':> %s'%(interval_file_path+hostid+'.log'))else:print'The host not log file , create file 'if __name__ =='__main__':main()
Growing old is mandatory, growing up is optional .

浙公网安备 33010602011771号