diagnose.py 2.03 KB
import shlex
import subprocess
import re
import time
import os
import threading
import commands

#config
check_mediaserver_period_seconds = 900
if_failed_next_check_period_seconds = 120


mediaserver_last_status = 1
def check_mediaserver():
    res = False
    global mediaserver_last_status
    rtspserver_cmd_str = './rtsp_decode_encode_test_app --url0=./jiaotong4.264 --serverport=18554'
    rtspserver_cmd = shlex.split(rtspserver_cmd_str)
    play_cmd_str = 'python try_play.py'

    p1 = subprocess.Popen(rtspserver_cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    time.sleep(5)

    (status, output) = commands.getstatusoutput(play_cmd_str)
    if(status == 0):
        res = True
    else:
        res = False

    os.system('kill -9 `ps aux|grep rtsp_decode_encode_test_app| grep -v grep | awk \'{print $2}\'`')
    p1.wait()
    # os.system('kill -9 {}'.format(p1.pid))
    return res


if __name__ == '__main__':
    last_check_media = time.time();
    try_count = 0;
    checkperiod = check_mediaserver_period_seconds

    while True:
        now = time.time();
        if(now-last_check_media> checkperiod):
            last_check_media = now;
            if(check_mediaserver()):
                print('mediaserver is working');
                checkperiod = check_mediaserver_period_seconds
                try_count =0
                mediaserver_last_status = 1
            else:
                try_count=try_count+1
                checkperiod = if_failed_next_check_period_seconds
            if(try_count >= 3):
                print('maybe mediaserver has some problem, restart!')
                os.system('kill -9 `ps aux|grep MediaServer| grep -v grep | awk \'{print $2}\'`')
                checkperiod = check_mediaserver_period_seconds
                try_count =0
                mediaserver_last_status = 0
        else:
            print('dianose working tick, now-last_check_media:{}, try_count:{}, mediaserver_last_status:{}'.format(now-last_check_media, try_count, mediaserver_last_status))
            time.sleep(1)