1.获取运行状态
获取树莓派运行状态用到的命令如下表:
命令 | 说明 |
---|---|
vcgencmd measure_temp |
获取CPU温度 |
top |
获取CPU使用率 |
free |
获取RAM使用率 |
df |
获取DISK使用率 |
ifconfig |
获取IP地址 |
- 1.1 获取CPU温度:
$ vcgencmd measure_temp
temp=40.8C
# 直接返回温度,只要把 temp= 去掉就行
- 1.2 获取CPU使用率
$ top -bn1
top - 16:27:28 up 1 day, 18:49, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 105 total, 1 running, 104 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.4 us, 2.7 sy, 0.0 ni, 95.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 926.1 total, 697.2 free, 51.3 used, 177.6 buff/cache
MiB Swap: 100.0 total, 100.0 free, 0.0 used. 797.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
411 pi 20 0 10188 2804 2500 R 5.9 0.3 0:00.04 top
...
...
# -bn1 参数: n1指定只运行一次,b输出结果为batch-mode,避免出现乱码
# 可以看到CPU的信息在第3行,只要输出CPU这一行
$ top -bn1 | awk '/Cpu\(s\)/'
%Cpu(s): 1.4 us, 2.7 sy, 0.0 ni, 95.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
# 缩写分别是user, system, nice, idle, wait, ...
# 计算CPU使用率等于 1 - idle%
- 1.3 获取RAM使用率
$ free -m
total used free shared buff/cache available
Mem: 926 52 696 23 177 796
Swap: 99 0 99
# -m 参数是指定输出结果以 MB 为单位
# 修改一下命令,只要输出 Mem 这一行
$ free -m | awk '/Mem/'
Mem: 926 52 696 23 177 796
# 计算RAM使用率等于 (total - available) / total; (仅供参考)
- 1.4 获取DISK使用率
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 1.7G 27G 6% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 464M 0 464M 0% /dev/shm
tmpfs 464M 24M 440M 6% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 464M 0 464M 0% /sys/fs/cgroup
/dev/mmcblk0p1 253M 53M 200M 21% /boot
tmpfs 93M 0 93M 0% /run/user/1000
# -h 参数是指定输出结果人性化,自动选择单位
# 修改一下命令,只要输出 /dev/root 这一行
$ df -h | awk '/root/'
/dev/root 29G 1.7G 27G 6% /
- 1.5 获取 IP 地址
$ sudo ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255
...
...
# ifconfig 命令要用管理员运行
# wlan0 参数是指wifi网络;如果是有线网络,改为: eth0
# 修改一下命令,只要输出 inet 这一行
$ sudo ifconfig wlan0 | awk '/inet/'
inet 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255
2. 完整的代码
import os
import requests
# run command
def run_cmd(cmd):
p = os.popen(cmd).readline().strip().split()
return p
cpu_temp = run_cmd('vcgencmd measure_temp')
cpu = run_cmd("top -bn1 | awk '/Cpu\(s\)/'")
cpu_usage = 1 - float(cpu[7])/100.0
ram = run_cmd("free -m | awk '/Mem/'")
ram_usage = (int(ram[1]) - int(ram[6])) / float(ram[1])
disk = run_cmd("df -h | awk '/root/'")
ip = run_cmd("sudo ifconfig wlan0 | awk '/inet/'")
print(cpu_temp, cpu, cpu_usage, ram, ram_usage, disk, ip, sep='\n')
###
status = 'Raspi Status: \n'
status += '```\n'
status += 'CPU Temp = {} \n'.format(cpu_temp[0].replace("temp=", ""))
status += 'CPU Used = {:.2%} \n'.format(cpu_usage)
status += 'RAM Total = {}M \n'.format(ram[1])
status += 'RAM Used = {:.2%} \n'.format(ram_usage)
status += 'DISK Total = {} \n'.format(disk[1])
status += 'DISK Used = {} \n'.format(disk[2])
status += 'DISK Perc = {} \n'.format(disk[4])
status += 'IP Address = {} \n'.format(ip[1])
status += '```'
print(status)
# Send to ftqq
key = 'SCUxxxxxxxxxxxxxxx'
api = 'https://sc.ftqq.com/{}.send'.format(key)
send_data = {'text': 'Raspi_Status', 'desp': status}
res = requests.post(api, data=send_data)
print(res.content)
网友评论