Python + Interface for O & M alarm, python
When it comes to O & M alarms, I think we can write a long history to explain in detail the past generation of alarms. For example, the earliest alarms were sent via emails, but the real-time emails were not high, for example, when you get home from work, you cannot always stare at your mailbox. Therefore, the alarm method like email is not suitable for reporting emergency faults. You can use it to monitor the daily disk utilization to report problems, if a website fails and cannot be accessed, it is obviously inappropriate to use it. In this case, the business with high requirements for business stability was developed into short messages, that is, the company bought a text message server, provide an http interface, and then the O & M personnel write scripts to write the collected abnormal data to the file. Then, the script detects in real time if the file is not empty, I called the SMS Server Interface to send the content in the file. This text message Alarm Method lasted for several years. Now, in the mobile age, with the advent of the new era, this text message method is gradually changing. Why? The simplest one is because this item has a cost. If it is not used for White, you will have to pay a text message fee every month. If you say the fee for this text message is enough, isn't one text message worth one, however, if the number of machines grows to tens of thousands of companies, each server deploys monitoring scripts based on three dimensions: underlying hardware monitoring, system layer monitoring, and application layer, each item is divided into N sub-items. We can imagine how terrible the number of text messages sent every day. Of course, some of the amazing data is invalid alarm, but you have to pay the same amount of money, when the monthly leader approves the money, he will be shocked by the number of alert text messages. Then, with a sigh, he replies with OK, so before there is a better solution, this method is cost-effective, but it must be invested in order to ensure business stability. But now, I am directly announcing the use of my platform to send free messages, because the terminal is still a mobile phone, the timeliness has not been reduced, and the cost is not, there is no unnecessary reason, so let's take a look at how to call interfaces to implement alarms for daily O & M messages.
First, you have to register a business number, address: https://qy.weixin.qq.com/cgi-bin/loginpage
If the registration process is for enterprise use, you need to select the enterprise, and then upload the qualification certificate of the enterprise. If the individual is registered, select the team and enter your ID card number to complete registration, other key steps have been clearly prompted during the registration process. For example, the enterprise account name cannot be modified.
After registration, go to step 2 and create an application in the application center, for example, O & M alarm. Then, click O & M alarm application on the left in settings-function settings-permission management, the CorpID and Secret will appear on the right side. Remember this. It will be used in the script. Select "Application permission" ">" O & M alarm ", and" "Address Book permission" ">" select readable, here, the enterprise number is set up on the platform, and then go to the Code step.
The platform has been set up. How can we send messages? In this case, we need to use python to call the message sending interface. To send messages to enterprise numbers, we must first obtain a token, which is provided by the platform, the message can be sent only after the token is obtained. Therefore, the script is divided into two parts: the first is to get the token, and the second is to send the message. The Code is as follows:
#! /usr/bin/env python import requestsimport 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()
The script uses the third-party module requests, which is more concise than the python standard module urllib and urllib2. You can directly use get (), post (), put (), delete (), head (), options (), method for url operations, the json module is to parse the returned json string and convert it to a data type that can be operated by python. This article writes about how to call message sending, you are welcome to leave a message.