Auto delete down host

  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import os
  4. import datetime,time
  5. import json,urllib2
  6. #zaabix api and user\pwd
  7. url='http://10.1.180.166/zabbix/api_jsonrpc.php'
  8. username='Admin'
  9. password='zabbix'
  10. '''定义日期,和记录日期文件路径'''
  11. #windows
  12. # interval_file_path=r'C:\\Users\\SammyTan.SAMMY-PC\\Desktop\\'
  13. # log_file=r'C:\\Users\\SammyTan.SAMMY-PC\\Desktop\\host_delete.log'
  14. #linux
  15. interval_file_path=r'/tmp/'
  16. log_file=r'/tmp/host_delete.log'
  17. #set expire time
  18. day =5
  19. def authenticate(url, username, password):
  20. values ={'jsonrpc':'2.0',
  21. 'method':'user.login',
  22. 'params':{
  23. 'user': username,
  24. 'password': password
  25. },
  26. 'id':'0'
  27. }
  28. data = json.dumps(values)
  29. req = urllib2.Request(url, data,{'Content-Type':'application/json-rpc'})
  30. response = urllib2.urlopen(req, data)
  31. output = json.loads(response.read())
  32. try:
  33. message = output['result']
  34. except:
  35. message = output['error']['data']
  36. print message
  37. quit()
  38. return output['result']
  39. def invalid_host_query(auth):
  40. '''API Check unreachable host'''
  41. values ={
  42. "jsonrpc":"2.0",
  43. "method":"host.get",
  44. "params":{
  45. "output":[
  46. "hostid",
  47. "name",
  48. "available"
  49. ],
  50. "filter":{
  51. "available":"2"
  52. }
  53. },
  54. "id":1,
  55. "auth": auth
  56. }
  57. data = json.dumps(values)
  58. req = urllib2.Request(url, data,{'Content-Type':'application/json-rpc'})
  59. response = urllib2.urlopen(req, data)
  60. output = json.loads(response.read())
  61. try:
  62. message = output['result']
  63. except:
  64. message = output['error']['data']
  65. print message
  66. quit()
  67. return output['result']
  68. def delete_host(hostid,auth):
  69. '''API delete unreachable host '''
  70. values ={
  71. "jsonrpc":"2.0",
  72. "method":"host.delete",
  73. "params":[
  74. hostid
  75. ],
  76. "auth": auth,
  77. "id":1
  78. }
  79. data = json.dumps(values)
  80. req = urllib2.Request(url, data,{'Content-Type':'application/json-rpc'})
  81. response = urllib2.urlopen(req, data)
  82. output = json.loads(response.read())
  83. try:
  84. message = output['result']
  85. except:
  86. message = output['error']['data']
  87. print message
  88. quit()
  89. return output['result']
  90. def host_period_file(hostid,hostname):
  91. '''check and create unreachable host date log file'''
  92. nowtime = datetime.datetime.now()
  93. hostid = str(hostid)
  94. hostname = str(hostname)
  95. if os.path.exists(interval_file_path+hostid+'.log'):
  96. file = open(interval_file_path+hostid+'.log','r')
  97. content = file.readline()[0:19]
  98. file.close
  99. return content
  100. else:
  101. file = open(interval_file_path+hostid+'.log','w')
  102. file.write(str(nowtime))
  103. file.close
  104. return str(nowtime)[0:19]
  105. def main():
  106. auth = authenticate(url,username,password)
  107. hostlist = invalid_host_query(auth)
  108. for host in hostlist:
  109. hostid = int(host['hostid'])
  110. hostname = str(host['name'])
  111. oldtime = host_period_file(hostid,hostname)
  112. if oldtime !='None':
  113. ago_day = datetime.datetime.strptime(oldtime,'%Y-%m-%d %H:%M:%S')
  114. now_day = datetime.datetime.now()
  115. result = int((now_day - ago_day).days)
  116. if result ==0:
  117. print'Pass'
  118. elif result == day:
  119. delete_host(hostid,auth)
  120. #remove the host log file
  121. os.system('rm -f %s'%(interval_file_path+hostid+'.log'))
  122. print'Delete host',hostname
  123. elif result > day:
  124. #reset the host log file
  125. os.system(':> %s'%(interval_file_path+hostid+'.log'))
  126. else:
  127. print'The host not log file , create file '
  128. if __name__ =='__main__':
  129. main()
 





posted @ 2016-08-17 19:08  betterman.com  阅读(227)  评论(0编辑  收藏  举报