用企業微信實現預警(shell + python)

一 註冊企業微信

本文全部內容是基於2018年12月26日時的企業微信版本所作的教程。後面可能因爲企業微信界面規則更改致使部分流程不一致。(你們看文章時請注意這一點)shell

註冊企業微信必備條件

  1. 微信號(實名認證了)
  2. 手機號

以前我有個誤區,就是覺得註冊企業微信就必定要有營業執照之類的證件才能夠註冊,實際是不須要也能夠的,由於咱們直接註冊後,即便不綁定企業,咱們也是能夠正常使用的,未綁定實際企業的,有200人的數的上限(難道大家技術部有200人?不存在的,哈哈!)json

註冊

註冊連接:企業微信
image
企業名稱能夠隨便填,建議填真實的啊,其餘的你就按照實際狀況填了。centos

二 建立消息

建立部門

首先咱們須要建立一個部門,而後將成員添加到一個部門裏面
imageapi

邀請成員加入

咱們能夠在首頁點擊進行邀請。
image數組

也能夠在選中要添加的部門後再選右邊的添加成員或者微信邀請。
imagebash

建立應用

image

建立的時候指定下能夠接收的消息的部門
image服務器

關注微工做平臺

在個人企業 ---》 微工做平臺 ---》邀請關注
image微信

三 實現預警

在完成的上面的全部準備工做後,咱們還須要獲取三個東西 :curl

1 企業ID

2 獲取應用ID和Secret

獲取企業ID

image

獲取應用ID和Secret

image

經過shell 腳本實現監控預警

該shell 腳本實現的功能: 檢測端口是否在監聽狀態,不在則進行微信預警。

#!/bin/bash
###############
#$Auth= djx
#$Function= monitoring service
#Date= 2018-12-26
###############
# 日誌儲存文件
log_file='/var/log/ljf_status.log'
# 主機名
hostname=`hostname`
# 監聽的端口數組
check_port=("3306" "3329" "4567")
# 數組長度
num=${#check_port[*]}
#  報警消息
msg=""
echo "--------------------------$(date +%F_%T)-----------------" >>$log_file

for i in  `seq 0 $num`
do
    netstat -tnlp|grep "${check_port[${i}]}"  >>/dev/null
    if [ $? -ne 0 ]
    then
        msg="\\n${hostname}:The Port ${check_port[${i}]} is down \\n"${msg}
        echo "$(date +%F_%T)  ${hostname}:The Port ${check_port[${i}]} is down">>$log_file
    fi
done
if [ "${msg}" !=  "" ]
then
    CropID=""   #填入企業ID值
    Secret=""   #填入認證密碼
    GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"  
    # 獲取token
    Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')  
    PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" 
    body='{
    "touser": "@all",
    "msgtype": "text",
    "agentid": "1000002", #要更改成咱們的應用ID
    "text":{
          "content":"
    故障:'$msg' "
             },
    "safe":0
    }'
    /usr/bin/curl --data-ascii "$body" $PURL >>$log_file 2>&1
fi

經過python 腳本實現監控預警

因爲centos7和centos6 默認安裝的都是python2版本,因此下面的腳本是基於python2寫的,這樣咱們就能夠直接拿到咱們的服務器上使用了。

# -*- coding: utf-8 -*-
# @Time    : 2018/12/27 0021 11:58
# @Author  : djx
# @Email   : 1120236774@qq.com
# @File    : 微信預警腳本.py
# @Software: PyCharm
import os
import time
import urllib2
import json


# 企業號ID
wxid = ""
# 應用ID
depid = ""
# 認證密碼
secret = ""
# 獲取主機的名稱
hostname = os.popen("hostname").read()
# 日誌儲存文件
log_file = '/var/log/ljf_status.log'
# 監聽的端口列表
check_port = (
    "8500",
    "3306")
# 發送的消息
msg = ""
# 獲取當前的時間
date_time = time.strftime("%Y-%m-%d %X")
# 檢查端口是否在監聽
for i in check_port:
    shell = "netstat -nutlp |grep \"" + i + "\""
    recv = os.popen(shell).read()
    if recv == "":
        msg = msg  + hostname + ": The Port " + i + "is down \n"
# 預警判斷
if msg != "":
    url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + \
        wxid + "&corpsecret=" + secret
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    recv_info = response.read()
    recv_info = eval(recv_info) 
    wx_token = recv_info['access_token']
    msg_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + wx_token
    send_msg = {
        "touser": "@all",
        "msgtype": "text",
        "agentid": depid,
        "text": {"content": msg},
        "safe": 0
    }
    send_msg_json = json.dumps(send_msg)
    request_post = urllib2.urlopen(msg_url,send_msg_json)
    recv_msg = request_post.read()
    with open(log_file,mode='a') as f:
        f.write(date_time)
        f.write("\n")
        f.write(msg)
        f.write(recv_msg)
        f.write("\n")

上面的腳本也能夠應用在zabbix或者是Open-falcon。

最後送上一句以爲不錯的話 : 立志要如山,行道要如水,如山能堅決,如水能曲達

相關文章
相關標籤/搜索