1. import os
    2. # Return CPU temperature as a character string
    3. def getCPUtemperature():
    4. res = os.popen('vcgencmd measure_temp').readline()
    5. return(res.replace("temp=","").replace("'C\n",""))
    6. # Return RAM information (unit=kb) in a list
    7. # Index 0: total RAM
    8. # Index 1: used RAM
    9. # Index 2: free RAM
    10. def getRAMinfo():
    11. p = os.popen('free')
    12. i = 0
    13. while 1:
    14. i = i + 1
    15. line = p.readline()
    16. if i==2:
    17. return(line.split()[1:4])
    18. # Return % of CPU used by user as a character string
    19. def getCPUuse():
    20. return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
    21. # Return information about disk space as a list (unit included)
    22. # Index 0: total disk space
    23. # Index 1: used disk space
    24. # Index 2: remaining disk space
    25. # Index 3: percentage of disk used
    26. def getDiskSpace():
    27. p = os.popen("df -h /")
    28. i = 0
    29. while 1:
    30. i = i +1
    31. line = p.readline()
    32. if i==2:
    33. return(line.split()[1:5])
    34. # CPU informatiom
    35. CPU_temp = getCPUtemperature()
    36. CPU_usage = getCPUuse()
    37. # RAM information
    38. # Output is in kb, here I convert it in Mb for readability
    39. RAM_stats = getRAMinfo()
    40. RAM_total = round(int(RAM_stats[0]) / 1000,1)
    41. RAM_used = round(int(RAM_stats[1]) / 1000,1)
    42. RAM_free = round(int(RAM_stats[2]) / 1000,1)
    43. # Disk information
    44. DISK_stats = getDiskSpace()
    45. DISK_total = DISK_stats[0]
    46. DISK_used = DISK_stats[1]
    47. DISK_perc = DISK_stats[3]
    48. if __name__ == '__main__':
    49. print('')
    50. print('CPU Temperature = '+CPU_temp)
    51. print('CPU Use = '+CPU_usage)
    52. print('')
    53. print('RAM Total = '+str(RAM_total)+' MB')
    54. print('RAM Used = '+str(RAM_used)+' MB')
    55. print('RAM Free = '+str(RAM_free)+' MB')
    56. print('')
    57. print('DISK Total Space = '+str(DISK_total)+'B')
    58. print('DISK Used Space = '+str(DISK_used)+'B')
    59. print('DISK Used Percentage = '+str(DISK_perc))