DyDanMuProcessDeamon.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from datetime import datetime
  2. from myproject.cplustools.component_controler import *
  3. from PyServerMonitor import *
  4. def deamon(MsgRepeaters):
  5. for MsgRepeater in MsgRepeaters:
  6. component = Action(MsgRepeater)
  7. if not component.isalived():
  8. component.start()
  9. content = '%s was down, now get starting.' % MsgRepeater
  10. writelog(content)
  11. def stopMsgRepeater(MsgRepeaters):
  12. for MsgRepeater in MsgRepeaters:
  13. component = Action(MsgRepeater)
  14. component.stop()
  15. content = '%s was shutdown by deamon process.' % MsgRepeater
  16. writelog(content)
  17. def checkmemused():
  18. meminfo = SystemInfo().memory_stat()
  19. mem_used = meminfo['MemUsed']
  20. mem_total = meminfo['MemTotal']
  21. percent_memuseage = (float(mem_used) / float(mem_total)) * 100
  22. if int(percent_memuseage) > 97:
  23. content = 'memory used over %.2f%% !\n' % percent_memuseage
  24. writelog(content)
  25. return True
  26. return False
  27. def writelog(content, logfile = 'Process_deamon.log'):
  28. f = open(logfile,'a')
  29. f.write('%s %s\n' % (datetime.now(),content))
  30. f.close()
  31. if __name__ == '__main__':
  32. MsgRepeaters = ['MsgRepeater1','MsgRepeater2','MsgRepeater3','MsgRepeater4']
  33. while 1:
  34. if checkmemused():
  35. stopMsgRepeater(MsgRepeaters)
  36. deamon(MsgRepeaters)
  37. time.sleep(10)