Python+WeChat interface implements operation and maintenance alarms

高洛峰
Release: 2017-01-17 09:45:58
Original
1758 people have browsed it

Speaking of operation and maintenance alarms, I think you can write a long history to explain in detail the past and future lives of alarms. For example, the earliest alarms were made by email, but the real-time nature of emails is not high. For example, when you come home from get off work, you can’t be watched all the time. Keep your mailbox, so the alarm method of email is not suitable for reporting urgent faults. You can use it to report daily disk utilization monitoring and other problems without any problem. If the website is down and cannot be accessed, it is obviously inappropriate to use it. , businesses that have relatively high requirements for business stability later developed into using SMS. That is, the company buys an SMS machine and provides an http interface, and then the operation and maintenance personnel write scripts to write the collected abnormal data into files. Then the script detects in real time that if the file is not empty, it calls the SMS interface to send the contents of the file. This SMS alarm method has lasted for several years, and now in the mobile era, with the emergence of WeChat, This method of text messaging is slowly changing now. Why? The simplest one, because this thing has a cost, and it is not in vain. You have to pay the SMS fee every month. If you say how much the SMS fee can be, isn't it only 1 cent per SMS? But think about it. For companies with tens of thousands of machines, each server deploys monitoring scripts from three dimensions: underlying hardware monitoring, system layer monitoring, and application layer. Each item here is divided into N small items. As you can imagine, You know how terrifying the number of text messages sent every day is. Of course, part of this astonishing data is invalid alarms, but you still have to pay. Every month when the leader approves the money, he will be shocked by the number of alarm text messages, and then sigh. I replied OK, so until there is no better way, this method has costs, but it must be invested in order to stabilize the business. But now WeChat is here and directly announced that using my platform to send messages is free, because the terminal is still a mobile phone, and it is timely. The performance has not been reduced, the cost has not been reduced, and there is no reason not to use it, so in this article we will take a look at how to adjust the WeChat interface to realize alarms for daily operation and maintenance messages.

First of all, you need to register a WeChat enterprise account. The address is: https://qy.weixin.qq.com/cgi-bin/loginpage

If the registration process is for enterprise use You need to select a company and then upload the company’s qualification certificate. If it is an individual registration, select a team and enter your ID number to complete the registration. Other key steps have been clearly indicated during the registration process, such as the name of the company number. Cannot be modified etc.

After registration, enter the second step. Create a new application in the application center, for example: operation and maintenance alarm, and then click the operation and maintenance alarm application on the left in Settings – Function Settings – Permission Management. CorpID and CorpID will appear on the right. Secret, remember this, it will be used in the script, then select the operation and maintenance alarm application in "Application Permission", and select Readable in "Address Book Permission". At this point, the setting of the enterprise account on the platform is completed, and then Come down and enter the code step.

The platform has been set up. How can I send a message? This requires using python to call the message sending interface. To send a message to the WeChat enterprise account, you must first obtain a token. , this is provided by the platform. You can only send messages after obtaining this token, so our script is divided into two parts. The first is to obtain the token, and the second is to send the message. The code is as follows:

#! /usr/bin/env python
  
import requests
import json
  
def get_token():
  
  url='https://qyapi.weixin.qq.com/cgi-bin/gettoken'
  values = {'corpid' : 'your corpid' ,
      'corpsecret':'your corpsecret',
       }
  req = requests.post(url, params=values)  
  data = json.loads(req.text)
  return data["access_token"]
  
def send_msg():
  url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+get_token()
  values = """{"touser" : "1" ,
      "toparty":"1",
      "msgtype":"text",
      "agentid":"1",
      "text":{
        "content": "%s"
      },
      "safe":"0"
      }""" %(str("10.1.1.8 is down"))
   
  data = json.loads(values) 
  req = requests.post(url, values)  
  
if __name__ == '__main__':
  send_msg()
Copy after login

The script is used Third-party module requests, This module is more concise than python standard modules urllib and urllib2. You can directly use get(), post(), put(), delete(), head(), options() to modify url. Operation, the json module is used to parse the returned json string and convert it into a python operable data type. This article about how to call WeChat to send messages is written here. Everyone is welcome to leave a message and communicate.

More Python+WeChat interfaces. For articles related to implementing operation and maintenance alarms, please pay attention to the PHP Chinese website

!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!