component_alived_check.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # coding: utf-8
  2. import MySQLdb
  3. import time
  4. import json
  5. class CheckDB(object):
  6. def initdbconfig(self,dbhost,dbport,dbuser,dbpasswd):
  7. self.conn=MySQLdb.connect(user=dbuser, passwd=dbpasswd, host=dbhost , port=dbport)
  8. self.cursor = self.conn.cursor()
  9. def check_component_stats(self,delay_seconds):
  10. delay_unix_time = int(time.time()) - delay_seconds
  11. sql = self.cursor.execute("use stt_online")
  12. self.cursor.execute('SELECT * FROM stt_online.web_online_server where avtive_time < %d and server_id <> 8123 and server_id <> 28002' % delay_unix_time )
  13. self.result = self.cursor.fetchall()
  14. def data2json(self):
  15. if self.result:
  16. d = {}
  17. for each in self.result:
  18. server_name = each[3]
  19. server_id = int(each[0])
  20. server_ip = each[4]
  21. server_active_time = int(each[7])
  22. strftime = self.timestamp_datetime(server_active_time)
  23. d[server_name] = [server_ip,server_id,strftime]
  24. jsondata = json.dumps(d)
  25. return jsondata
  26. else:
  27. return
  28. def timestamp_datetime(self,value):
  29. format = '%Y-%m-%d %H:%M:%S'
  30. value = time.localtime(value)
  31. dt = time.strftime(format, value)
  32. return dt
  33. if __name__ == '__main__':
  34. ckeckdb = CheckDB()
  35. ckeckdb.initdbconfig('192.168.1.1',3306,'root','HVAGKKgRTT3SbpDmVzF5')
  36. ckeckdb.check_component_stats(90)
  37. result = ckeckdb.data2json()
  38. if result:
  39. print result
  40. else:
  41. print 1