"Raspberry Pi" gets the current temperature, CPU, memory, and the state of the hard disk

Source: Internet
Author: User
Tags memory usage readline split cpu usage disk usage in python

Go to a script written in Python that outputs the various states and data of the Raspberry Pi, including CPU temperature, CPU usage, memory usage, and hard disk usage.

Article from: Raspberry Pi Lab

First, create a script:

sudo nano get.py


Second, paste the following code: Bash

Import OS # Return CPU temperature as a character string Def getcputemperature (): res = Os.popen (' Vcgencmd measure_temp '). ReadLine () Return (Res.replace ("temp=", ""). Replace ("' c\n '," ")) # return RA                                                              
M information (unit=kb) in a list # Index 0:total RAM # Index 1:used RAM # Ind
    Ex 2:free RAM def getraminfo (): p = os.popen (' free ') i = 0 while 1:i = i + 1 line = P.readline () if I==2:return (Line.split () [1:4] ) # return% of CPU used by user as a character string Def getcpuuse (): Return (str (OS . Popen ("top-n1 | awk '/cpu\ (s\):/           {print $} '). ReadLine (). Strip ()) # Return information about disk space as a list (unit included)         
# index 0:total disk space # index 1:used disk space                                                    
# Index 2:remaining disk space
    # Index 3:percentage of disk used Def getdiskspace ():
            p = Os.popen ("df-h/") i = 0 while 1:i = i +1 line = P.readline () if i==2: Return (Line.split () [1:5]) # CPU Informatiom cpu_temp = getcputemperature () Cpu_usage = Getcpuuse () # RAM Informati  On # Output are in kilobytes, here I-convert it in Mb for readability ram_stats = Getraminfo () ram_total = round (int (ram_stats[0))
/1000,1) ram_used = round (int (ram_stats[1])/1000,1) Ram_free = round (int (ram_stats[2])/1000,1) # Disk Information Disk_stats = Getdiskspace () disk_total = disk_stats[0] disk_used = disk_stats[1] Disk_perc = disk_stats[3] if __name__ = = ' __main__ ': print (Print (' CPU temperature = ' +cpu_temp) print (' CPU use = ' +cpu_usage) print (") print (' RAM total = ' +str ( 
    ram_total) + ' MB ') print (' Ram used = ' +str (ram_used) + ' MB ') print (' Ram free = ' +str (ram_free) + ' MB ') print (') Print (' disk total Space = ' +str (disk_total) + ' B ') print (' disk used space = ' +str (disk_used) + ' B ') print (' Disk U sed Percentage = ' +str (DISK_PERC))


Third, execute script: Bash

chmod +x get.py  #赋予可执行权限
python get.py    #执行脚本


The expected output results are as follows: Bash

CPU temperature = 53.0
CPU use = 13.5
 
ram total = 497.0 MB
ram used = 116.0 MB
RAM free = 381.0 MB
 
DIS K Total Space = 3.6GB
disk used Space = 1.8GB
disk used Percentage = 53%

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.