群晖-python运行状态提醒

1.获取运行状态

获取群晖运行状态用到的命令如下表:

命令 说明
top 获取CPU使用率
cat /sys/class/hwmon/hwmon0/temp1_input 获取CPU温度
free 获取RAM使用率
df 获取DISK使用率
ifconfig 获取IP地址
  • 1.1 获取CPU温度:
    1
    2
    3
    $ cat /sys/class/hwmon/hwmon0/temp1_input
    36000
    # 实际温度需要除以1000
  • 1.2 获取CPU使用率

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    $ top -bn1
    top - 19:31:32 up 1 day, 15:30, 1 user, load average: 0.19, 0.05, 0.01 [IO: 0.00, 0.00, 0.00 CPU: 0.19, 0.05, 0.01]
    Tasks: 372 total, 1 running, 371 sleeping, 0 stopped, 0 zombie
    %Cpu(s): 22.2 us, 36.1 sy, 0.0 ni, 41.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
    GiB Mem : 5.651 total, 0.201 free, 0.660 used, 4.789 buff/cache
    GiB Swap: 5.391 total, 5.391 free, 0.000 used. 4.586 avail Mem

    PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
    16145 root 20 0 467.5m 82.6m 94.12 1.427 77:03.73 S /var/packages/DownloadStation/target/sbin/transmissiond +
    ...
    ...
    # -bn1 参数: n1指定只运行一次,b输出结果为batch-mode,避免出现乱码
    # 可以看到CPU的信息在第3行,只要输出CPU这一行

    $ top -bn1 | awk '/Cpu\(s\)/'
    %Cpu(s): 5.4 us, 8.1 sy, 0.0 ni, 86.5 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: 5786 674 208 126 4904 4698
    Swap: 5519 0 5519
    # -m 参数是指定输出结果以 MB 为单位
    # 修改一下命令,只要输出 Mem 这一行

    $ free -m | awk '/Mem/'
    Mem: 5786 674 208 126 4904 4698
    # 计算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/md0 2.3G 1.3G 909M 59% /
    devtmpfs 2.9G 0 2.9G 0% /dev
    tmpfs 2.9G 184K 2.9G 1% /dev/shm
    tmpfs 2.9G 18M 2.9G 1% /run
    tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup
    tmpfs 2.9G 2.3M 2.9G 1% /tmp
    /dev/vg1/volume_2 889G 389G 501G 44% /volume2
    /dev/vg1000/lv 890G 680G 211G 77% /volume1
    # -h 参数是指定输出结果人性化,自动选择单位
    # 修改一下命令,只要输出 /dev/root 这一行

    $ df -h | awk '/vg1000/'
    /dev/vg1000/lv 890G 680G 211G 77% /volume1
  • 1.5 获取 IP 地址

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ ifconfig eth0
    eth0 Link encap:Ethernet HWaddr 00:11:32:B9:D1:64
    inet addr:192.168.31.xxx Bcast:192.168.31.xxx Mask:255.255.255.0
    ...
    # ifconfig 命令要用管理员运行
    # wlan0 参数是指wifi网络;如果是有线网络,改为: eth0
    # 修改一下命令,只要输出 inet 这一行

    $ ifconfig eth0 | awk '/inet/'
    inet addr:192.168.31.xxx Bcast:192.168.31.xxx Mask:255.255.255.0

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
51
52
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 ")
temp1_in = run_cmd(" cat /sys/class/hwmon/hwmon0/temp1_input ")
cpu_temp = float(temp1_in[0])/1000.0
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])

disk_st0 = run_cmd(" df -h | awk '/vg1/' ")
disk_st1 = run_cmd(" df -h | awk '/vg2/' ")
ipconfig = run_cmd(" ifconfig eth0 | awk '/inet/' ")
print(temp1_in, cpu_temp, cpu_stat, cpu_used, sep='\n')
print(ram_stat, ram_used, disk_st0, disk_st1, ipconfig, sep='\n')

###
status = now() + '\n\n'
status += 'Synology Status: \n'
status += 'CPU Used = {:.2%} \n'.format(cpu_used)
status += 'CPU Temp = {:.2f}°C \n'.format(cpu_temp)
status += 'RAM Total = {}M \n'.format(ram_stat[1])
status += 'RAM Used = {:.2%} \n'.format(ram_used)
status += 'DISK Total = {}, {} \n'.format(disk_st0[1], disk_st1[1])
status += 'DISK Used = {}, {} \n'.format(disk_st0[2], disk_st1[2])
status += 'DISK Perc = {}, {} \n'.format(disk_st0[4], disk_st1[4])
status += 'IP Address = {} \n'.format(ipconfig[1].replace('addr:', ''))
print(status)

# Send to ftqq
key = 'SCUxxxxxxxxxxxxxxx'
api = 'https://sc.ftqq.com/{}.send'.format(key)
send_data = {'text': 'Synology_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)