2023年1月

基于mysubmail接口发送短信、电话语音通知。创建脚本后可以通过crontab -e每小时或半小时运行一次,例如:
/30 * python /etc/weed/checkRAID.py
可以在raid出现异常状态时快速获知及时处理,避免故障扩大。

# -*- coding: utf-8 -*-
#!/usr/bin/python
import os
import requests

node = '香港vps-nvmenode-1'
error = 0

def get_status(value):
    try:
        status = value.split(": ")
        return status[1].strip()
    except:
        return False
    

def send_warning():
    global node

    # 语音通知
    voice_url = 'http://api.mysubmail.com/voice/send.json'
    voice_params = { 'appid': '*****',
                      'to': '13200000000',
                      'content': '紧急事态:'+node+'硬盘状态异常,请立即检查',
                      'signature': '**************'
                   }
    #voice_res = requests.post(voice_url, data=voice_params)
    # print voice_res.text

    # 短信通知
    message_url = 'http://api.mysubmail.com/message/send.json'
    message_params = { 'appid': '*****',
                       'to': '13200000000',
                       'content': '【XXX】紧急事态:'+node+'硬盘状态异常,请立即检查',
                       'signature': '**************'
                     }
    message_res = requests.post(message_url, data=message_params)
    # print(message_res.text)


# 检查RAID状态,注意/dev/md10是变动参数,自行fdisk -l查看你的软列阵磁盘名称,如raid1则为/dev/md1
raidinfos = os.popen('mdadm -D /dev/md10').readlines()
for raidinfo in raidinfos:
    raidinfo = raidinfo.strip('\n')
    print(raidinfo)

    if "State : " in raidinfo:
        status = get_status(raidinfo)
        if status != 'active' and status != 'active, checking':
            error = 1

    if "Failed Devices : " in raidinfo:
        status = get_status(raidinfo)
        if status != '0':
            error = 1

    if "Active Devices : " in raidinfo:
        status = get_status(raidinfo)
        if status != '2':
            error = 1

# 发送通知
if error == 1:
    send_warning()