检测mysql备份是否成功触发openfalcon 报警

7/1/2018 openfalconmysql

# openfalcon 监控mysql备份报警

思路:mysql备份输出日志 检测 errr 等相关字段 判断成功与否,将1或者0 传入openfalcon

# python 检测脚本

执行python脚本的服务器 要有日志、已经安装并运行的openfalcon 客户端

#!-*- coding:utf8 -*-
# author: liuwenqi 
# date: 2018-07-11
import os
import requests
import time
import json
import  socket

## 环境变量

log_dir ='/Users/kame/code/scripts/test_tmp'  ## 日志地址
scp_success='scp success!'  ## 判断成功与否字符串
scp_fail = 'scp failure!'
backup_success = 'innobackupex full backup complete !'
backup_fail = 'innobackupex full backup failure!'
hostname = socket.gethostname()   ## 获取 hostname


def scp_replace(log_dir):

    os.environ['log_dir']=str(log_dir)
    log_dir_os = os.popen("cat $log_dir | grep `date +%Y%m%d`")
    file_tmp = log_dir_os.read()
    ## 判断scp是否成功
    if scp_success in file_tmp:
        scp_staus = 0
    else :
        scp_staus = 1
    return scp_staus

def back_replace(log_dir):

    os.environ['log_dir']=str(log_dir)
    log_dir_os = os.popen("cat $log_dir | grep `date +%Y%m%d`")
    file_tmp = log_dir_os.read()
    ## 判断backup
    if backup_success in file_tmp:
        backup_staus = 0

        #print(backup_staus)
    else:
        backup_staus = 1
        #print(backup_staus)
    return backup_staus



open_env_scp_status = scp_replace(log_dir)
open_env_back_status = back_replace(log_dir)

print(open_env_scp_status)
print(open_env_back_status)

ts = int(time.time())
payload = [
    {
        "endpoint": hostname,
        "metric": "scp_status",
        "timestamp": ts,
        "step": 60,
        "value": open_env_scp_status,
        "counterType": "GAUGE",
        "tags": "",
    },
    {
        "endpoint": hostname,
        "metric": "backup_status",
        "timestamp": ts,
        "step": 60,
        "value": open_env_back_status,
        "counterType": "GAUGE",
        "tags": "",
    },


]

r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(payload))

print r.text

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

# openfalcon 报警设置

设置 报警触发 ,然后设置 hostGroups 绑定templates