shell nail robot operation to achieve a reminder alarm

We know that before the operation and maintenance of multi-alarm notification by mail and other means to appropriate personnel, is difficult to achieve anytime, anywhere viewing. With the development of the phone APP, many IM software to send an alarm start up. Now commonly used is to send a letter to the micro and nails, today we will focus on the nail. Group robot is an advanced extension nail group, the group robot can be aggregated into third-party services group chat, automated synchronization of information. With nails robot, API provided by the official, can easily post data to the appropriate recipient. Group robot custom protocol support access Webhook support more possibilities, for example: You can customize the operation and maintenance alarm robot by polymerization to achieve staple Group alerts.

Linux Shell scripting language is an important tool for system administration and automation must achieve. Shell expertly written language can enhance the efficiency of operation and maintenance personnel, adapt to the complex working environment.

In this article, we focus alarm information is sent by means of nails robot achieved through shell scripting language.

1: For more information base of the robot, you can sign the official website to learn

https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

2. Give your own robot from a good name

As follows:

 3. Use the command line tool curl

curl is a command-line tool to upload or download data by specifying a URL, the data displayed. The curl crepresent client, and URL, that URL.

3.1 for use with curl agreement, most or HTTP

In each HTTP request has a corresponding method, commonly used methods are: GET, POST, HEADand PUT. If you do not specify a particular method in a curl command, the default is to use the GETmethod. For other methods, curl can be specified in the command:

method option
POST -dor-F
HEAD -I
PUT -T

POSTIt is a method for submitting data to the HTTP server. In the browser, after filling out the data may be in the form, the browser will use the default data to fill the key=valueform of string transformation. In the curl, we can use -dor the --dataoption to specify specific data.

3.2 nailed two main parameters associated with the curl tool

Parameters (short form) Parameter (long form) Explanation specific description
-d --data  HTTP POST data (H) Transmitting the specified data to the server.
-H --header Custom header to pass to server (H)

When POSTsubmitting data method, the data submitted for the following four main forms:

  • application/x-www-form-urlencoded: The default form, that key1=value1&key2=value2form;
  • multipart/form-data: Use this form when using the form to upload files;
  • application/json: Submit data in JSON format;
  • text/xml: Submit data in XML format.

Content-Type Header is a, if not specified, the default application/x-www-form-urlencodeddata is sent, when it is desired to use another form of data transmission, the need to specify Header.

3.3 call format (Case)

curl ' https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx ' \
    -H ' the Content-the Type: file application / JSON ' \
    -d ' { "MsgType": "text", 
        " text " : {
              " Content " : " I am me, it is not the same fireworks " 
        } 
      } '

During the test, set the above commands directly copied to the command line, and then xxxxxxxx replaced with real access_token.

 

4 different types markdown

Official website, in addition to markdown type, test examples of other types of messages (script) can demonstrate directly in the shell

Official website markdown type of test cases

{ 
     "MsgType": "Markdown", 
     "Markdown": { 
         "title": "Hangzhou weather" 
         + "#### Hangzhou weather 156xxxx8827 @ \ the n-": "text" 
                 "> 9 degrees, northwest wind level 1, air good 89% relative humidity 73 is \ n-\ n-"+ 
                 ">! [Screenshot] (https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \ n-"+ 
                 "> ### Post ### 10:20 [weather] (http://www.thinkpage.cn/) \ n-" 
     }, 
    " AT ": { 
        " atMobiles ": [ 
            " 156xxxx8827 ", 
            " 189xxxx8325 " 
        ], 
        " isAtAll " : false 
    } 
 }

 In the shell execution error message, the error message is:

{ " The errcode " : 40035 , " ErrMsg " : " missing argument JSON " }

 The reason is given "text" value corresponding to the value in the column "+" operator causes the intermediate

" +
"

delete.

It can execute code updated to:

{
      " MsgType " : " Markdown " ,
      " Markdown " : {
          " title " : " Hangzhou weather " ,
          " text " : " #### Hangzhou Weather @ 156xxxx8827 \ n> 9 degrees, northwest wind Class 1, good air 89 , relative humidity 73% \ n \ n>! [screenshot] (https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \ n> ###### 10 point 20 is stars Date [ Weather] (http://www.thinkpage.cn/) \ n- " 
     }, 
    " AT " : {
         " atMobiles " :
            [
            "156xxxx8827", 
            "189xxxx8325"
        ], 
        "isAtAll": false
    }
 }

Execution OK, the error disappears.

Implementation of the following:

 Successfully received the information, content shots are as follows:

 

5. nails robot code into files, making it easy to call

For example: Create a monitor when the VIP QQOrder_ERP cluster drift, calls for sending alarms nails executable file ddalarm.sh.

The main code is as follows:

#!/bin/bash

webhook='https://oapi.dingtalk.com/robot/send?access_token=34XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
cluster='QQOrder_ERP'
host=`hostname -I | awk '{print $1}'`
vip=$1

function SendMsgToDingding() {
    curl $webhook -H 'Content-Type: application/json' -d "
    {
        'msgtype': 'text',
         ' Text ' : {
             ' Content ' : ' Cluster Name: $ cluster \ n alarm information: Virtual IP <$ vip> has drifted to the node <$ host>, please note that \ the n- ' 
        }, 
        ' AT ' : {
             ' isAtAll ' : to true 
        } 
    } "
 } 


SendMsgToDingding

This test file, need to pass a parameter (VIP) when called.

Trigger execution case are as follows:

 Phone nail received alarm information

 

References:

1. nailing group robot

https://ding-doc.dingtalk.com/doc#/serverapi2/krgddi 

2.curl Guide

https://www.jianshu.com/p/fc0eb6c60816

Guess you like

Origin www.cnblogs.com/xuliuzai/p/11469039.html
Recommended