Jenkins+ Basic Series 13: Extra Story--Add jar package startup monitoring log, Python script

1, due to:

I also used Jenkins in the company for half a year. I encountered a situation where the jar package project was successfully built and a DingTalk message was sent, but in fact, the project did not start successfully.

That is: the successful construction does not mean that the service startup is successful

2. Process:

The python programming language I use, in order to combine the following interface test scripts, needs to judge whether the service is started successfully. If the start is successful, the test script will be executed, otherwise it will not be executed.

3. Use tools:

Python3.x is easily integrated into the operation steps after the Jenkins job is built

4. The code is very simple, please point out where there is optimization, I can continue to optimize

# -*- coding:UTF-8 -*-
# /usr/bin/python3


import subprocess
import sys


def monitor_log(log_file):
    po_pen = subprocess.Popen('tail -f ' + log_file,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              shell=True,
                              encoding='UTF-8')
    pid = po_pen.pid
    print('po_pen.pid:' + str(pid))
    while True:
        line = po_pen.stdout.readline().strip()
        # 判断内容是否为空
        if line:
            print(line)
            if 'Started' in line and 'seconds (JVM running for' in line:
                print('启动成功')
                sys.exit(0)
            elif 'Exception' in line[:50]:
                print('启动失败,有报错')
                sys.exit(1)


if __name__ == '__main__':
    monitor_log(sys.argv[1])

5. Integration method

After the build operation, add the execution script directly in SSH Publishers. Note that the script is placed in the specified directory of the target server

python3 3.x operating environment

The middle is the absolute path of the py script

The last is the absolute path of the log that needs to be monitored

6. Principle process [important]

First of all, let's look at the code. Because it is monitoring the log, you cannot open the file with the normal open method. The log will definitely be particularly large.

Operations performed by the subprocess module used here

Then judge the field content of each line (the judgment string in the code needs to be optimized, especially the judgment of startup failure)

When integrating with Jenkins, a key issue is how to let Jenkins capture the success and failure in the py script. After consulting a lot of information, I finally know

(The food is the original sin · the landlord)

How Jenkins judges success or failure

Then look at the usual output of a piece of content when we execute a python program, code 0 code program exits normally

Finally, let's take a look at a certain function of the sys module. The most critical part is here. If I judge the startup is successful in the code, I will sys.exit(0) otherwise 1

Finally, practice on Jenkins

Successful status:

failed state

Cooperate with DingTalk group notifications

 

Finally, the perfect solution to boot log monitoring

sys.argv  大家可以研究下,卖个关子

Update on 1/15/2020: Added information capture for some startup exceptions (slightly clumsy)

# -*- coding:UTF-8 -*-
# /usr/bin/python3


import subprocess
import sys


def monitor_log(log_file):
    po_pen = subprocess.Popen('tail -f ' + log_file,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              shell=True,
                              encoding='UTF-8')
    pid = po_pen.pid
    print('po_pen.pid:' + str(pid))
    while True:
        line = po_pen.stdout.readline().strip()
        # 判断内容是否为空
        if line:
            print(line)
            if 'Started' in line and 'seconds (JVM running for' in line:
                print('启动成功')
                po_pen.kill()
                sys.exit(0)
            elif 'Application run failed' in line and 'ERROR' in line:
                print('启动失败,有报错')
                po_pen.kill()
                sys.exit(1)
            elif 'Exception' in line and 'Error' in line:
                print('启动失败,有报错')
                po_pen.kill()
                sys.exit(1)
            elif 'ERROR' in line and 'Exception' in line:
                print('启动失败,有报错')
                po_pen.kill()
                sys.exit(1)
            elif 'ERROR' in line and 'Migration of schema' in line and 'failed' in line:
                print('启动失败,有报错')
                po_pen.kill()
                sys.exit(1)
            else:
                continue


if __name__ == '__main__':
    monitor_log(sys.argv[1])

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=klkypps11wnj

Guess you like

Origin blog.csdn.net/yangj507/article/details/103101947