PyServerMonitor.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: cp936 -*-
  2. import sys
  3. import time
  4. from socket import socket, SOCK_DGRAM, AF_INET
  5. from myproject.cplustools import Msg_Sender
  6. try:
  7. import psutil
  8. except ImportError:
  9. print 'please install psutil module. for example :' \
  10. 'pip install psutil'
  11. sys.exit(1)
  12. def sendmsg(phone,msg):
  13. msg_send_code = Msg_Sender.Msg_rpc().send_phone_msg(phone,msg)
  14. return msg_send_code
  15. class SystemInfo(object):
  16. def cpu_stat(self):
  17. interval=1
  18. cpu_useage = psutil.cpu_percent(interval)
  19. return cpu_useage
  20. def memory_stat(self):
  21. mem = {}
  22. f = open("/proc/meminfo")
  23. lines = f.readlines()
  24. f.close()
  25. for line in lines:
  26. if len(line) < 2: continue
  27. name = line.split(':')[0]
  28. var = line.split(':')[1].split()[0]
  29. mem[name] = long(var) * 1024.0
  30. mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached']
  31. return mem
  32. def disk_stat(self):
  33. import os
  34. hd={}
  35. disk = os.statvfs("/home")
  36. hd['available'] = (disk.f_bsize * disk.f_bavail) /1024
  37. hd['capacity'] = (disk.f_bsize * disk.f_blocks) / 1024
  38. hd['used'] = (disk.f_bsize * (disk.f_blocks - disk.f_bfree)) /1024
  39. return hd
  40. class Monitor(SystemInfo):
  41. def __init__(self):
  42. self.cpu_threshold = 70
  43. self.mem_threshold = 70
  44. self.hd_threshold = 70
  45. self.msg = ''
  46. s = socket(AF_INET, SOCK_DGRAM)
  47. s1 = socket(AF_INET, SOCK_DGRAM)
  48. s.connect(('192.168.0.1', 0))
  49. s1.connect(('114.114.114.114', 0))
  50. self.local_ip = s.getsockname()[0]
  51. self.int_ip = s1.getsockname()[0]
  52. def system_stat(self):
  53. cpu_useage = self.cpu_stat()
  54. if cpu_useage > self.cpu_threshold :
  55. self.msg += 'Warning: Host %s cpu used over %.2f%% !\n' % (self.int_ip,cpu_useage)
  56. meminfo = self.memory_stat()
  57. mem_used = meminfo['MemUsed']
  58. mem_total = meminfo['MemTotal']
  59. percent_memuseage = (float(mem_used) / float(mem_total)) * 100
  60. if percent_memuseage > self.mem_threshold:
  61. self.msg += 'Warning: Host %s mem used over %.2f%% !\n' % (self.int_ip,percent_memuseage)
  62. hdinfo = self.disk_stat()
  63. hd_used = hdinfo['used']
  64. hd_total= hdinfo['capacity']
  65. percent_hduseage = (float(hd_used) / float(hd_total)) * 100
  66. if percent_hduseage > self.hd_threshold:
  67. self.msg += 'Warning: Host %s disk space used over %.2f%% !\n' % (self.int_ip,percent_hduseage)
  68. if self.msg :
  69. return self.msg
  70. return
  71. if __name__ == '__main__':
  72. # phone_list = ['18086669830','15207125813']
  73. phone_list = ['15207125813','15002751813']
  74. warning_count = 0
  75. while 1:
  76. stats_msg = Monitor().system_stat()
  77. if stats_msg:
  78. warning_count += 1
  79. if warning_count <= 2:
  80. sleep_second = 600
  81. if warning_count > 2 and warning_count <= 6:
  82. sleep_second = 1800
  83. if warning_count > 6 and warning_count < 10:
  84. sleep_second = 3600
  85. if warning_count >= 10 :
  86. sleep_second = 10800
  87. warning_count = 0
  88. for phone in phone_list:
  89. # print stats_msg
  90. print sendmsg(phone,stats_msg)
  91. time.sleep(1)
  92. time.sleep(sleep_second)
  93. # sendmsg('18086669830','1321test')