欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過shell+python實(shí)現(xiàn)企業(yè)微信預(yù)警

 更新時(shí)間:2019年03月07日 11:01:19   作者:自由早晚亂余生  
這篇文章主要介紹了通過shell+python實(shí)現(xiàn)企業(yè)微信預(yù)警,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一 注冊(cè)企業(yè)微信

本文所有內(nèi)容是基于2018年12月26日時(shí)的企業(yè)微信版本所做的教程。后面可能由于企業(yè)微信界面規(guī)則更改導(dǎo)致部分流程不一致。(大家看文章時(shí)請(qǐng)注意這一點(diǎn))

注冊(cè)企業(yè)微信必備條件

  • 微信號(hào)(實(shí)名認(rèn)證了)
  • 手機(jī)號(hào)

之前我有個(gè)誤區(qū),就是以為注冊(cè)企業(yè)微信就一定要有營業(yè)執(zhí)照之類的證件才可以注冊(cè),實(shí)際是不需要也可以的,因?yàn)槲覀冎苯幼?cè)后,即使不綁定企業(yè),我們也是可以正常使用的,未綁定實(shí)際企業(yè)的,有200人的數(shù)的上限(難道你們技術(shù)部有200人?不存在的,哈哈!)

注冊(cè)

注冊(cè)鏈接:企業(yè)微信


企業(yè)名稱可以隨便填,建議填真實(shí)的啊,其他的你就按照實(shí)際情況填了。

二 創(chuàng)建消息

創(chuàng)建部門

首先我們需要?jiǎng)?chuàng)建一個(gè)部門,然后將成員添加到一個(gè)部門里面

邀請(qǐng)成員加入

我們可以在首頁點(diǎn)擊進(jìn)行邀請(qǐng)。

也可以在選中要添加的部門后再選右邊的添加成員或者微信邀請(qǐng)。

創(chuàng)建應(yīng)用

創(chuàng)建的時(shí)候指定下可以接收的消息的部門

關(guān)注微工作平臺(tái)

在我的企業(yè) ---》 微工作平臺(tái) ---》邀請(qǐng)關(guān)注

三 實(shí)現(xiàn)預(yù)警

在完成的上面的所有準(zhǔn)備工作后,我們還需要獲取三個(gè)東西 :

1 企業(yè)ID

2 獲取應(yīng)用ID和Secret

獲取企業(yè)ID

獲取應(yīng)用ID和Secret

通過shell 腳本實(shí)現(xiàn)監(jiān)控預(yù)警

該shell 腳本實(shí)現(xiàn)的功能: 檢測端口是否在監(jiān)聽狀態(tài),不在則進(jìn)行微信預(yù)警。

#!/bin/bash
###############
#$Auth= djx
#$Function= monitoring service
#Date= 2018-12-26
###############
# 日志儲(chǔ)存文件
log_file='/var/log/ljf_status.log'
# 主機(jī)名
hostname=`hostname`
# 監(jiān)聽的端口數(shù)組
check_port=("3306" "3329" "4567")
# 數(shù)組長度
num=${#check_port[*]}
# 報(bào)警消息
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=""  #填入企業(yè)ID值
  Secret=""  #填入認(rèn)證密碼
  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", #要更改為我們的應(yīng)用ID
  "text":{
     "content":"
  故障:'$msg' "
       },
  "safe":0
  }'
  /usr/bin/curl --data-ascii "$body" $PURL >>$log_file 2>&1
fi

通過python 腳本實(shí)現(xiàn)監(jiān)控預(yù)警

由于centos7和centos6 默認(rèn)安裝的都是python2版本,所以下面的腳本是基于python2寫的,這樣我們就可以直接拿到我們的服務(wù)器上使用了。

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


# 企業(yè)號(hào)ID
wxid = ""
# 應(yīng)用ID
depid = ""
# 認(rèn)證密碼
secret = ""
# 獲取主機(jī)的名稱
hostname = os.popen("hostname").read()
# 日志儲(chǔ)存文件
log_file = '/var/log/ljf_status.log'
# 監(jiān)聽的端口列表
check_port = (
  "8500",
  "3306")
# 發(fā)送的消息
msg = ""
# 獲取當(dāng)前的時(shí)間
date_time = time.strftime("%Y-%m-%d %X")
# 檢查端口是否在監(jiān)聽
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"
# 預(yù)警判斷
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")

上面的腳本也可以應(yīng)用在zabbix或者是Open-falcon。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論