SoFunction
Updated on 2024-11-18

Python+WeChat interface to realize operation and maintenance alarms

Speaking of operation and maintenance alarm, I think we can write a long history to explain in detail the past life of the alarm, such as the earliest alarms are using e-mail, but e-mail real-time is not high, such as home from work can not people have been staring at the mailbox it, so the mail this alarm is not suitable for reporting urgent failures, daily monitoring of disk utilization and what can be used to report no problem, the site is down can not access this kind of failure, with the It is obviously not suitable, that the stability of this business requires a relatively high level of business, and later developed into a text message, that is, the company to buy a short message machine, provide a http interface, and then operations and maintenance personnel to write scripts to collect the abnormal data written into the file, and then script real-time detection if the file is not empty, call the SMS machine interface to the content of the file to be sent out, the SMS alarm way! lasted a few years, and now turned to the mobile era, with the emergence of WeChat, this SMS way is now also slowly transformed, why? The simplest 1, because this thing has a cost, with not for nothing, you have to pay a monthly SMS fee, if you say that the cost of SMS can be how much, 1 is not 1 gross, but you think if the size of the machine developed to tens of thousands of companies, each server in accordance with the bottom from the bottom of the hardware monitoring, monitoring the system layer, the application layer of the three dimensions to deploy the monitoring script, where each item is divided into a N small items, it can be imagined that every day to send a text message to the police, the police, the police, the police, the police, the police, the police, the police, the police, the police, the police, the police, the police and the police. You can imagine how horrible the number of text messages sent every day, of course, this amazing data there is a part of the invalid alarm, but the same have to pay ah, the monthly leadership of the approval of the money will be alarmed by the number of text messages shocked, and then exclaimed on the reply to the OK, so there is no better way before this way there is a cost, but in order to stabilize the business must be invested, but now WeChat came, directly announced the use of my! This platform to send messages free of charge, because the terminal is still a cell phone, timeliness and no reduction in cost and no, there is no reason not to use it, so this we will look at how to tune the WeChat interface to achieve the daily operation and maintenance of the message alarm.

First, you have to go and register a WeChat enterprise number at :/cgi-bin/loginpage

Registration process if it is for the enterprise to use to select the enterprise, and then upload the enterprise's qualification certificate, if it is an individual registration, select the team, and then enter their ID number can complete the registration, other key steps in the registration process has been prompted very clearly, such as the name of the enterprise number can not be modified, and so on.

After registration into the second step, in the application center to create a new application, for example: operation and maintenance alarm, and then in the settings - function settings - rights management click on the left side of the operation and maintenance alarm application, on the right side of the CorpID and Secret will appear, this should be remembered, will be used in the script, and then" Application Permissions" "choose the operation and maintenance alarm this application," "Address Book Permissions" "choose readable, to the enterprise here on the platform settings are complete, the next step into the code.

platform are set up, how can we send information, it is necessary to use python to call the interface to send a message, to send a message to WeChat Enterprise, first of all, we must obtain a token, this is the platform provided to, after obtaining the token to send a message, so our script is divided into 2 parts, the first to get a token, the second is to send a message, the code is as follows:

#! /usr/bin/env python
 
import requests
import json
 
def get_token():
 
  url='/cgi-bin/gettoken'
  values = {'corpid' : 'your corpid' ,
      'corpsecret':'your corpsecret',
       }
  req = (url, params=values)  
  data = ()
  return data["access_token"]
 
def send_msg():
  url="/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 = (values) 
  req = (url, values)  
 
if __name__ == '__main__':
  send_msg()

script uses a third-party module requests, this module than the standard python module urllib, urllib2, more concise, you can directly use get(), post(), put(), delete(), head() ,options(), method of operation on the url, the json module is to parse the return of the json string, into python operable data types, on how to call WeChat send a message this article is written here, welcome to leave a message to exchange.