get_cpuusage.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #pip3 install psutil
  4. #pip3 install gpiozero
  5. import psutil
  6. import time
  7. import os
  8. import datetime
  9. import requests
  10. import urllib
  11. import time
  12. from gpiozero import CPUTemperature
  13. path ="http://www.aisky.com.tw/field/reboot.php"
  14. nr = "GTW009002014"
  15. def get_cpuusage():
  16. cpu_usage_core = psutil.cpu_count()
  17. cpu_usage = psutil.cpu_percent(interval=0.3)
  18. print('cpu count: {}'.format(cpu_usage_core))
  19. print('cpu percent: {}'.format(cpu_usage))
  20. cpu = CPUTemperature()
  21. cpu_temp = cpu.temperature
  22. print('temperature is :{}'.format(cpu_temp))
  23. if cpu_temp >= 70:
  24. data = {'nr': nr, 'info': 'overheat', 'data': str(cpu_temp)}
  25. data = urllib.parse.urlencode(data)
  26. data = data.encode('utf-8')
  27. req = urllib.request.Request(path, data)
  28. req.add_header('User-Agent', 'Magic Browser')
  29. resp = urllib.request.urlopen(req)
  30. respData = resp.read()
  31. print(data)
  32. time.sleep(1)
  33. os.system('sudo reboot')
  34. elif cpu_usage > 60:
  35. data = {'nr': nr, 'info': 'overload', 'data': str(cpu_usage)}
  36. data = urllib.parse.urlencode(data)
  37. data = data.encode('utf-8')
  38. req = urllib.request.Request(path, data)
  39. req.add_header('User-Agent', 'Magic Browser')
  40. resp = urllib.request.urlopen(req)
  41. respData = resp.read()
  42. print(data)
  43. time.sleep(1)
  44. os.system('sudo reboot')
  45. while True:
  46. get_cpuusage()
  47. time.sleep(3)