python access pin group alarm

Background


1 environment Python 3.7

2. Mail control service (because the company uses zabbix (server infrastructure), elk (business alarm), promethues alertManager (mainly used on k8s), grafana (some data collected by promethues are also reported here, so it is easier to set the alarm here, but the disadvantage is that when there are variables in the graph, the alarm cannot be carried out, to be optimized in the later version), alarm Without control, there will be nearly 3k seals generated in a day. If it is set separately, the workload will be increased. A filter (also python) will be written directly. If there is a demand, an alarm will be sent to the nail)

3 mail passing nail alarm

Official link to pin alert

https://ding-doc.dingtalk.com/doc#/serverapi3/iydd5h Nail custom robot development platform

Be careful:

1. Each robot can send up to 20 pieces per minute. If more than 20, the current will be limited for 10 minutes

1, Robot for opening nails

1 click group settings in the upper right corner, intelligent group assistant

           

2 Click to add robot

3 Click the button+

4 Click to customize

5 click Add.

6 input the robot name, and set the robot head image for the robot.

7 complete the security settings (select at least one). For security configuration details, see Robot development Security configuration node for.

Check that I have read and agree to the custom robot service and disclaimer and click finish.

8 after the robot is created successfully, you can click Copy to copy its webhook address, or click the setting instructions to view the robot setting, debugging and other instructions.

9 node output

The successful output data is the data returned by the pin API, as shown in the following example. For specific pin API return data, see Nail development document.

 {
    "errmsg": "ok",
    "errcode": 0
}

 

Most of the code can be made completely through the nail instruction documents, but for the convenience of recording.

#_*_coding:utf-8_*_
# Author: yehuai
# Creation time: 2020/1/15 14:40
import base64
import hashlib
import hmac
import time
import urllib

import requests
import json

TOKEN = "By nailing the swarm, add the swarm robot, and then in the"
SECRET = "It can be obtained from the nail robot"
headers = {'Content-Type': 'application/json;charset=utf-8'}
atmobile_list = [
                "#Mobile number, every email will be sent to this person ",
                ""
            ],

def get_url():
    # Encrypt according to the official requirements, and generate the completed link. The official is py2, here is py3
    # According to the official requirements, the requested url must carry three parameters, access_token, timestamp and sign (the signature is encrypted by secret)
    timestamp = round(time.time() * 1000)
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, SECRET)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    
    # Complete url
    api_url = "https://oapi.dingtalk.com/robot/send?access_token={}&timestamp={}&sign={}".format(TOKEN, timestamp, sign)
    return api_url


def ddmsgsend(text):
    api_url = get_url()
    # Transfer method is text
    json_text = {
        "msgtype": "text",
        # The AIT way
        "at": {
            # AIT people press the cell phone AIT
            "atMobiles": atmobile_list, 
            "isAtAll": False
        },
        # Send text
        "text": {
            "content": text
        }
    }
    # Request url
    return requests.post(api_url, json.dumps(json_text), headers=headers).content.decode("utf8")






if __name__ == '__main__':
    text = "Microservice alarm(test) \n"  \
            "Alarm time: 2020-01-15 14:00:55 \n" \
            "Alarm service: data synchronization service microservice;\n" \
            "replicas: 5 One;\n" \
            "Current surviving node: 1;\n" \
            "Survival node id:['data-sync-service-10-244-88-18-20200'] \n" \
            "Event level: highest;\n" \
            "Alarm information: data-sync-service replicas Inconsistent with the surviving node, please handle in time"
    res = ddmsgsend(text)
    print(res)

 

Published 40 original articles, won praise 14, visited 80000+
Private letter follow

Tags: JSON Python Zabbix Mobile

Posted on Wed, 12 Feb 2020 06:43:16 -0500 by Superian