Remember to follow me and keep sharing your notes on programming development, data analysis, machine learning and so on.

Learn about cloud server development with my boss (if you are reading this series for the first time, it is highly recommended to learn the following articles) :

Pilot: Have a server, I unexpectedly so cool?

Alternative project: write a resume page in 10 lines of code!

How to apply for a free SSL certificate for a domain name

A pagoda in Linux, a real pagoda! Detailed tutorial

Finally, a website that everyone can visit

How to send alarm notifications to spikes using Python?

One, foreword

Not long ago, I saw mingo write how to use Python to send warning notifications to enterprise wechat, remember I wrote before using Pytho to send specified format data to the nail service, this article reconstructs the previous code, into a: use Python to monitor server data, and then send exceptions through the nail to the user.

The project outline is as follows, the project has been open source to GitHub, you can directly click to read the original text or browser visit: github.com/XksA-me/Din… Download the project.

Project Environment Description:

  • Python 3.6.8 (Theory 3.6 and above definitely ok)
  • Third-party dependency libraries:
    • Requests send POST requests for data
    • Psutil Obtains operating system running data
    • The apScheduler sets scheduled tasks

Since there are fewer dependencies, you can either install it locally or create a virtual environment (Python virtual environments are recommended to be managed using PipenV).

Once in the environment, enter the following PIP instruction to install:

pip3 install requests psutil apscheduler
Copy the code

Two, start using your head

2.1 Create a stitching robot

Dingdingbot personal version is only for group chat, so we need to create a group first, open Dingdingbot, then create a group chat, randomly pull two people, once created, you can remove these two friends from the group chat (a little damage? !). Click the Smart Group Assistant in Group Settings.

Enter the robot management page and click the three-point button after adding the robot to enter the robot selection page.

Let’s scroll down and select custom robot.

Give the robot a name, then set the security Settings, select checkmark mode (data transmission is required as a parameter, to avoid security problems), copy the contents of the inside. Click Finish to complete the creation.

Copy the Webhook link. Later we will send a POST request to this URL for data transmission through Python. You can click on the Settings to view the robot’s related functions and configuration methods.

If you forget the signed key or Webhook address set earlier, you can view or modify it by group management -> Intelligent Group Assistant -> click the three-point button of the corresponding robot.

Now that we’ve created the pegging robot, all we need to do is code Python.

2.2 Write a simple pin messaging assistant

2.2.1 Calculate the number signature to prepare for automatic message sending

In front of us, we set the security protection method of signing, so before we carry out data transmission, we have to calculate the digital signature content of the nail robot first. There are very detailed instructions on the nail document, and the calculation method of various languages, we can directly take debugging.

The official documentation address: developers.dingtalk.com/document/ro…

You don’t need to understand the calculation process (the code below), just change secret to your own.

import hmac
import hashlib
import base64
import urllib.parse
from time import time


"" Digital signature calculation for stapling robot" "
def get_digest() :
    Round (x, n) is the result of x to n decimal places
    timestamp = str(round(time() * 1000))
    secret = 'Your own signature'
    secret_enc = secret.encode('utf-8')  # utf-8 encoding
    string_to_sign = '{}\n{}'.format(timestamp, secret)  # string formatting concatenation
    string_to_sign_enc = string_to_sign.encode('utf-8')  # utf-8 encoding
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()  # HmacSHA256 computes the signature
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))  UrlEncode after Base64 encoding
    # return the timestamp and the calculated encoded concatenation string, then concatenate directly to the Webhook
    return f"&timestamp={timestamp}&sign={sign}"
Copy the code

2.2.2 Post requests to send data, realizing automatic message sending to nail

We can simply send a POST request to our own Webhook address to transfer data. Here I use the Markdown data type, and there are many other data formats to choose from, such as text, jump cards, information flow cards, etc., very rich.

We are interested in to see: developers.dingtalk.com/document/ro…

Note that if you want the bot to return the @designated person when sending a message, you will need to add the @designated person’s pin number to the content (@xxxxxx in the code below) as well.

Simply send a MarkDown message
def warning_bot() :
    data = {
        "msgtype": "markdown"."markdown": {
            "title": "[Simple Python] Welfare of the Day"."text": Hello, I am my old cousin, and I learned Python, cloud server development and other programming knowledge. \n >! [](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/db15766f66e54a228b4c637c19163ecf~tplv-k3u1fbpfcp-zoom-1.image) * * [class blog, has launched] (https://python-brief.com/) * * \ n "" "
        },
        "at": {
          "atMobiles": [
              "xxxxxx"  # request @object's mobile phone number].}}Send a POST request to send instructions to the pin robot
    webhook_url = 'your Webhookurl'
    Use requests to send post requests
    req = requests.post(webhook_url+get_digest(), json=data)
    
Copy the code

It looks like this (above is the chat message bar, showing the title we specified; Below is a group chat, showing the render effect of Markdown), nice, remember to like (surprise, by this time, there are 1000 words ~ like ~ forward ~.

2.3 Write functions for the basic data of the statistical system

In Linux, we usually use the top command to check CPU usage, mainly looking at the following data: process CPU usage, load, virtual/physical memory usage, so in this section we will use Python to get relevant data.

Here we take advantage of psutil, Process and System Utilities, a cross-platform library for retrieving information about the processes and System usage (CPU, memory, disk, network, sensor) that the System is running. With just a few lines of code, you can get data about your local system

import psutil as psu
import os


Cloud Server Basic data Server run time, load status, CPU usage, running memory usage, physical memory usage
def get_server_info() :
    Get basic data of the system
    Server run time = the difference between the current time and server start time
    run_times = str(timedelta(seconds=int(time())-int(psu.boot_time())))
    System load status (last 1, 5, 15 minutes)
    loadavg = [round(i, 2) for i in os.getloadavg()]
    The CPU usage test interval is 0.3 seconds
    cpu_in_use = psu.cpu_percent(interval=0.3)
    System running memory usage
    Memory usage greater than 80% triggers an alarm
    vm_in_use = psu.virtual_memory().percent
    vm_available = round(psu.virtual_memory().available/(1024**3), 2)
    System physical storage usage
    disk_in_use = psu.disk_usage('/').percent
    disk_free = round(psu.disk_usage('/').free/(1024**3), 2)
    
    You can also add information about processes, threads, etc
    
    base_info = F """> Your cloud server is running -{run_times}, the load of the machine is (the last 1, 5 and 15 minutes) :{loadavg}- Current CPU usage:{cpu_in_use}%, - The system running memory usage is:{vm_in_use}%, - the remaining free running memory is:{vm_available}GiB: - The system storage memory usage is:{disk_in_use}%, - The remaining available memory is:{disk_free}GiB
<br>**{'Machine CPU usage is normal' if cpu_in_use<=80 else 'Machine CPU usage is too high, warning may be triggered'}"* *" "
    return base_info, loadavg, cpu_in_use, vm_in_use, disk_in_use
Copy the code

We called some built-in methods of psutil, and we got some basic data of the operating system. In addition, we used the built-in method getLoadavg of OS module to get the load of the system. Finally, we used F-string to get the data. Click “like” to break 50 out of an article to introduce to you, here mainly to give you extra said GB and GiB difference:

1GB = 1000MB = 1000*1000 KB = 1000*1000*1000 B
1GiB = 1024MiB = 1024*1024 KiB = 1024*1024*1024 B
I have the opportunity to chat with you in detail
Copy the code

2.4 Write a function to monitor data

Next, we need to write a monitoring function. There are only two simple monitoring functions (the project is open source, I will also use some time later). One is to monitor the load, and the other is to monitor the CPU usage, setting two thresholds: CPU usage 70% and load (1 minute and 15 minutes) 70%* NUMBER of cpus, when the threshold is exceeded, the corresponding alarm content will be returned, and no problem will be returned OK.

Screenshot @Ruan Yifeng teacher’s blog

"Server Alert Settings" This article is simple, only set load and CPU usage alert ""
def get_warning() :
    base_info, loadavg, cpu_in_use, vm_in_use, disk_in_use = get_server_info()
    Determine the server load first
    # If you only look at the last minute and last 15 minutes, it should <= 0.7*CPU number
    loadavg_max = psu.cpu_count() * 0.7
    loadavg_max = 0.01   # Test use, official environment please comment out
    if loadavg[0] >= loadavg_max and loadavg[2] >= loadavg_max:
        warning1 = F '⚠️<font color="#d30c0c"> [Warning] </font> The current load rate of your cloud server is (last 1, 5, 15 minutes)-{loadavg}<font color="#d30c0c">{round(loadavg[2]/loadavg_max, 2) *100}%</font>, please check whether there is a problem in the system in time, you can also @ me, send: basic information, view the cloud server basic information. '
        return warning1
        
    if cpu_in_use >= 80:
        warning2 = F '⚠️<font color="#d30c0c"> [Warning] </font> Your cloud server is currently CPU usage <font color="#d30c0c">{cpu_in_use}%</font>, please check whether there is a problem in the system in time, you can also @ me, send: basic information, view the cloud server basic information. '
        return warning2
    return 'ok'
    
Copy the code

Originally also wanted to write a function, is the previous said always view data (@ robot, give corresponding instructions on the line), but to find this function needs to build a new enterprise robot, next time to share it with you ~

2.5 Writing a Scheduled Task

On Linux, we can directly use the pagoda panel to set up the scheduled task. You can see the pagoda in Linux, the real pagoda! Detailed tutorial, here we write the scheduled task, we use the third party library apscheduler, about this library and can write a long introduction, here will not expand to say, this article likes if break 100, I will arrange next week (old watch online low ask praise! .

from apscheduler.schedulers.blocking import BlockingScheduler


1. Send the server information to the nail group at 9:00 am every day.
def every_day_nine() :
    message = get_server_info()[0]
    title = 'Server Basics'
    warning_bot(message, title)


2. Constant warning (check every 30 seconds)
def every_seconds_30() :
    warning = get_warning()
    ifwarning ! ='ok':
        title = '[⚠️ Warning] Server failure'
        warning_bot(warning, title)

"3, @ robot, automatic question and answer setup next arrangement, need to create another enterprise robot and group chat robot process is different ~"
    
    
# Select BlockingScheduler
sched = BlockingScheduler(timezone='Asia/Shanghai')

# job_every_nine runs a daily send every day at 9am
sched.add_job(every_day_nine, 'cron', hour=21, minute=55)

# every_SECONds_30 Perform data monitoring every 30s
sched.add_job(every_seconds_30, 'interval', seconds=3)

# Start a scheduled task
sched.start()

Copy the code

Add_job Interval of a scheduled task is triggered periodically, for example, every minute. Cron is the most powerful trigger in apScheduler and can trigger at a specific time of the month, such as 9:00 a.m.

2.6 Run, to see the effect

At present, all belong to automatic trigger, and then send messages, the first is the daily timer 9:00 am send server basic situation.

The second is every 30s for a server data detection (CPU usage and load), when the data exceeds the threshold, trigger an alarm, send a message to remind.

2.7 Creating a daemon for an Application

After we finished the feature development, we found that once we shut down the application, the alert monitoring service would also stop, so we needed to create a daemon to protect our process.

In my case, after logging into the pagoda panel, go to /etc/systemd/system and create a new ding_bot.service file and write the following:

[Unit]
Description=Dingding Bot service

[Service]
Type=forking
ExecStart=/usr/bin/python3 /root/Project/Little_project/DingdingBot/scheduler.py
KillMode=process
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target
Copy the code

Type=forking means that when the program is started, it will run in the background. ExecStart service execution instructions (execute scheduler.py file); KillMode=process Indicates that the main process of the program is killed when the service is stopped. Restart=on-failure Indicates that the system automatically restarts when the program exits due to an accident.

After saving the file, we can directly execute the following command in the terminal to start the daemon process. After running, it will enter the daemon state. We can press CTRL + C to exit without affecting the daemon process:

systemctl start ding_bot
Copy the code

After the code is modified, the daemon process needs to be restarted for the modification to take effect. The restart command is as follows:

systemctl restart ding_bot
Copy the code

If you do not want to set up the daemon, you can stop the service (and the program will also stop) by executing the stop command:

systemctl stop ding_bot
Copy the code

Other commands and operations related to the daemon system can be searched by themselves, but also can be exchanged in the message area

Iii. Notice of the next period

The overall effect feels good, hey, hey, hey, I will continue to optimize later, if you have any additional ideas and needs, you can also leave a message.

Another post is expected this week: In addition, psutil and apscheduler libraries and daemons mentioned in the article are introduced in detail. Please share with us when you have time and scenarios, or like this tweet to 50+50+50 ~

Learn about cloud server development with my boss (if you are reading this series for the first time, it is highly recommended to learn the following articles) :

Pilot: Have a server, I unexpectedly so cool?

Alternative project: write a resume page in 10 lines of code!

How to apply for a free SSL certificate for a domain name

A pagoda in Linux, a real pagoda! Detailed tutorial

Finally, a website that everyone can visit

In fact, the message is more important, because the message can be more intuitive to let me see everyone’s support, interaction and communication with everyone, I think it can also trigger a lot of my creative inspiration.

Ok, see you next time, I love the cat love technology, more love si si’s old cousin Da Mian ଘ(˙꒳˙)ଓ Di Di