PMUtils.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. '''
  2. Created on 13-04-2013
  3. @author: citan
  4. '''
  5. import os
  6. from pimonitor.PM import PM
  7. class PMUtils(object):
  8. '''
  9. classdocs
  10. '''
  11. # Return CPU temperature as a character string
  12. @classmethod
  13. def get_cpu_temperature(cls):
  14. res = os.popen('vcgencmd measure_temp').readline()
  15. return(res.replace("temp=", "").replace("'C\n", ""))
  16. # Return RAM information (unit=kb) in a list
  17. # Index 0: total RAM
  18. # Index 1: used RAM
  19. # Index 2: free RAM
  20. @classmethod
  21. def get_ram_info(cls):
  22. p = os.popen('free')
  23. i = 0
  24. while 1:
  25. i = i + 1
  26. line = p.readline()
  27. if i == 2:
  28. return(line.split()[1:4])
  29. # Return % of CPU used by user as a character string
  30. @classmethod
  31. def get_cpu_use(cls):
  32. return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
  33. # Return information about disk space as a list (unit included)
  34. # Index 0: total disk space
  35. # Index 1: used disk space
  36. # Index 2: remaining disk space
  37. # Index 3: percentage of disk used
  38. @classmethod
  39. def get_disk_space(cls):
  40. p = os.popen("df -h /")
  41. i = 0
  42. while 1:
  43. i = i + 1
  44. line = p.readline()
  45. if i == 2:
  46. return(line.split()[1:5])
  47. @classmethod
  48. def log_os_stats(cls):
  49. try:
  50. cpu_temp = PMUtils.get_cpu_temperature()
  51. if len(cpu_temp) > 0:
  52. PM.log("CPU temp: " + cpu_temp)
  53. ram_stats = PMUtils.get_ram_info()
  54. if len(ram_stats) == 3:
  55. ram_free = round(int(ram_stats[2]) / 1000,1)
  56. PM.log("RAM free: " + str(ram_free))
  57. cpu_usage = PMUtils.get_cpu_use()
  58. if len(cpu_usage) > 0:
  59. PM.log("CPU usage: " + str(cpu_usage))
  60. except IOError:
  61. pass