• Zabbix监控nginx-rtmp status(json版)


    与前面的文章 zabbix监控nginx-rtmp status(html版)区别只在于取值的页面不一样

    http://127.0.0.1:81/control/get/all_streams

    stat状态值详解

    name:application名

    static_pulls:从编码器拉流模式

    pushes:编码器推流模式

    {

      name:流名字

      audio_bitrate:音频码流

      video_bitrate:视频码流

      active:1(流up)0(流down)

    }

    stream.py  # 先判断active是否up,up则取值视音频码流,down则返回0

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    # date 2016/10/27 author pdd
    
    '''
        curl的直播流状态值写入本地CACHE文件 后续具体的直播频道流值从该文件获取 CACHE文件CACHETTL秒更新一次(基于文件属性ctime)
        http://127.0.0.1:81/control/get/all_streams
    '''
    
    import os
    import sys
    import time
    import json
    import urllib2
    
    class Live(object):
    
        def __init__(self,url,CACHETTL,CACHE,STREAM):
            self.url = url
            self.CACHETTL = CACHETTL
            self.CACHE = CACHE
            self.STREAM = STREAM
    
        def __request(self):
            try:
                r = urllib2.urlopen(self.url,timeout=1)
            except urllib2.URLError, e:
                print 0  # 给zabbix触发直播流down报警的值
                exit(1)  # 异常退出
            else:
                stream = json.loads(r.read())
                channel = stream["servers"][0]["applications"][0]["pushes"]  # 选择推流模式数据
                clear = {}
                for i in channel:
                    clear.update({i["name"]:{ "active":i["active"],"bitrate":(i["audio_bitrate"]+i["video_bitrate"])}})
                with open(self.CACHE,'w') as f:
                    f.write(json.dumps(clear))  # 把clear(dict类型)序列化后写入CACHE文件
    
        def __generate_cache(self):
            TIMENOW = int(time.time())
            if os.path.isfile(self.CACHE) and os.path.getsize(self.CACHE) > 0:
                TIMECACHE = int(os.path.getctime(self.CACHE))
            else:
                TIMECACHE = 0
            if (TIMENOW - TIMECACHE) > self.CACHETTL:
                self.__request()
    
        def get_status(self):
            self.__generate_cache()
            with open(self.CACHE,'r') as f:
                r = json.load(f)  # 从CACHE文件中读取字符串并反序列化
                if r[self.STREAM]["active"] == 1:
                    print r[self.STREAM]["bitrate"]
                else:
                    print 0
    
    if __name__ == "__main__":
        url = "http://127.0.0.1:81/control/get/all_streams"  # 直播频道流状态值url
        CACHETTL = 60  # 本地CACHE文件更新时间间隔
        CACHE = "/tmp/pyrtmp-status.cache"  # 本地CACHE文件绝对路径
        STREAM = sys.argv[1]  # 某个具体直播频道
        status = Live(url,CACHETTL,CACHE,STREAM)
        status.get_status()
  • 相关阅读:
    poj 1511Invitation Cards
    hust 1608Dating With Girls
    sdibt 2128Problem A:Convolution Codes
    hdu 1325Is It A Tree?
    poj 2240Arbitrage
    hdu 2818Building Block
    poj 1789Truck History
    poj 1125Stockbroker Grapevine
    展望未来
    告别过去
  • 原文地址:https://www.cnblogs.com/metasequoia/p/6015682.html
Copyright © 2020-2023  润新知