This article mainly introduces information about how to implement O & M alarms using Python + interfaces. if you need such information, refer to O & M alarms, I think we can write a long history to explain in detail the past life of the alarm. for example, the earliest alarm was sent via email, but the real-time mail was not very real-time. for example, when I got home from work, I could never keep staring at my mailbox, therefore, the alarm method like email is not suitable for reporting emergency faults. it can be used to monitor the daily disk utilization to report the fault, and the website cannot be accessed due to downtime, 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 an SMS server and provided an http interface, then, the O & M personnel write a script to write the collected abnormal data to the file, and then the script detects in real time that if the file is not empty, it calls the SMS interface to send the content in the file, this text message alarm method has lasted for several years. now, in the mobile age, with the advent of the new era, this method of text message 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.
For more articles about how to implement O & M alarms using Python + interfaces, please follow the PHP Chinese network!