In Linux, set the scheduled task to monitor the service process. If the trigger meets the conditions, the robot notification interface of the nail will be called to send messages

Apply for the nail robot

This function belongs to the custom robot function of Dingding. There are specific access descriptions in the custom robot access – Dingding open platform. The following are some key points.

Robot safety Settings

In security Settings, user-defined keywords indicate that the sent message content must contain the predefined keywords; otherwise, the message cannot be sent. An IP address segment indicates that only requests from this IP address are received. This article uses the Linux curl command to send messages directly, so there is no need to check the checkmark option.

After clicking “Finish”, you can get the Webhook address of the robot, and then use this address to send messages to the nail.

View the Webhook address of the robot

If the address is lost, you can view it again in the following ways:

Try sending a message to a robot in Linux

Replace the request address in the following with the Webhook address of the newly created robot, and then send a message. If the message is successfully received in the pin, the API is available.

curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx' \ 
	-H 'Content-Type: application/json' \ 
	-d '{" msgtype ", "text", "text" : {" content ":" monitoring alarm: I am who I am, is not the same fireworks "}}'
Copy the code

The diagram below:

Linux Scripting

My Linux comparison dishes, here is just to provide an idea.

Write a generic notification sending script notify_dd.sh

Keyword is the keyword that is set when the robot is created, while writing content that is received from parameters passed in externally.

#! /bin/bash

function send_notify {
  keyword="Monitoring alarm"

  curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx' \
     -H 'Content-Type: application/json' \
     -d '{ "msgtype": "text", "text": { "content": "'$keyword':'The $1'"}}'
}
Copy the code

After the script is written, run chmod u+x notify_dd.sh to add execution permission to the script.

Write the process to view the monitor_monkey.sh script

Use. /opt/scripts/notify-dd.sh to import the notification sending script and wrap send_notify as output_notify. Note that the absolute path is used because the relative path does not take effect when a scheduled task is added later.

Through the ps – ef | grep -c StartMonkey output to determine the number of service process exists, if does not exist, call the output_notify function to send a notification, and restart the service.

Echo “$(date) “in output_notify: $result” >> /opt/scripts/notify-monkey.log Redirects the returned content to the specified log file.

#! /bin/bash

. /opt/scripts/notify-dd.sh

function output_notify {
  text=The $1
  result=$(send_notify $text)
  echo "$(date) : $result" >> /opt/scripts/notify-monkey.log
}

function restart_monkey {
  cd /opt/monkey/server1
  nohup ./start.sh >/dev/null 2>&1 &
}

count=$(ps -ef|grep -c StartMonkey)

if [ $count != 2 ]
then
  $(output_notify "Code engine outage detected, attempting to restart.")

  $(restart_monkey)
  sleep 3

  if [ $(ps -ef|grep -c StartMonkey) != 2 ]
  then
    $(output_notify "Code engine restart failed. Request human intervention.")
  else
    $(output_notify "Code engine restarted successfully, daemon will continue to monitor.")
  fi
else
  echo "$(date): The code engine is working fine." >> /opt/scripts/notify-monkey.log
fi
Copy the code

After compiling the script, run chmod u+x Monitor_monkey. sh to add execution permission to the script.

Crontab Creates a scheduled task

Crontab is a scheduled task management tool built with Linux. For details, see Cron – Wikipedia. The implementation method also refers to crontab configuration guide – CSDN

Viewing Service Status

Run the /sbin/service crond status command to view the crontab service status. If the service is not started, run /sbin/service crond start to start the service.

Adding a Scheduled Task

You can run the crontab -e command to add a scheduled task for the current user. The effect is as follows: * The operation mode is similar to that of the Vim editor. Press I to enter editing mode and enter :wq to save the configuration and exit

Note that: After saving the configuration and exiting, you need to restart the crontab service to make the new task take effect. Run the /sbin/service crond restart command to restart the crontab service

The effect of the first task in the figure above is that the /opt/scripts/ monitor-monk. sh script is executed at the 0th minute of every two hours. The effect of the second task is that the specified script is executed at the 15th minute of every two hours.

The configuration rules are as follows: * User name Optional

View CRON logs

To check whether the crontab task takes effect, you can run the crontab -l command to check the existing scheduled task and search for cron logs in /var/log/. The result is as follows:

However, if there is an exception in the script to be executed, it is not shown here. For example, after the monitor-monkey.sh script is added to a scheduled task, it fails to execute successfully. Finally, it is found that other scripts introduced in the script use relative paths.

Final execution effect