raspberry pi 运行状态提醒

1.获取运行状态

获取树莓派运行状态用到的命令如下表:

命令 说明
vcgencmd measure_temp 获取CPU温度
top 获取CPU使用率
free 获取RAM使用率
df 获取DISK使用率
ifconfig 获取IP地址
  • 1.1 获取CPU温度:
    1
    2
    3
    $ vcgencmd measure_temp
    temp=40.8C
    # 直接返回温度,只要把 temp= 去掉就行
  • 1.2 获取CPU使用率

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    $ 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使用率

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ 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使用率

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $ 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 地址

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ 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. 完整的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import time
import requests

def now():
return '[' + time.strftime('%Y-%m-%d %H:%M:%S') + ']'

# run command
def run_cmd(cmd):
p = os.popen(cmd).readline().strip().split()
return p

cpu_temp = run_cmd(" vcgencmd measure_temp ")
cpu_stat = run_cmd(" top -bn1 | awk '/Cpu\(s\)/' ")
cpu_used = 1 - float(cpu_stat[7])/100.0

ram_stat = run_cmd(" free -m | awk '/Mem/' ")
ram_used = (int(ram_stat[1]) - int(ram_stat[6])) / float(ram_stat[1])

dsk_stat = run_cmd(" df -h | awk '/root/' ")
ipconfig = run_cmd("sudo ifconfig wlan0 | awk '/inet/'")
print(cpu_temp, cpu_stat, cpu_used, ram_stat, ram_used, dsk_stat, ipconfig, sep='\n')

###
status = now() + '\n\n'
status += 'Raspi Status: \n'
status += 'CPU Used = {:.2%} \n'.format(cpu_used)
status += 'CPU Temp = {} \n'.format(cpu_temp[0].replace('temp=', ''))
status += 'RAM Total = {}M \n'.format(ram_stat[1])
status += 'RAM Used = {:.2%} \n'.format(ram_used)
status += 'DISK Total = {} \n'.format(dsk_stat[1])
status += 'DISK Used = {} \n'.format(dsk_stat[2])
status += 'DISK Perc = {} \n'.format(dsk_stat[4])
status += 'IP Address = {} \n'.format(ipconfig[1])

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)

# Send to wechat
import wechat_push as wp

access_token, _ = wp.get_access_token(wp.corp_id, wp.corp_secret)
wp.wechat_push_text(agent_id=wp.agent_id, access_token=access_token, message=status)