Open-Falcon 使用 python 监控发布中未分发的命令数

桑鸿志
2023-12-01

性能计数器监控参考 :SqlServer 监控发布中未分发的命令数

但 open-falcon 不能监控 windows 性能计数器,因此只能自定义监控推送监控信息。open-falcon 使用 python 可以推送数据,因此需安装python(脚本使用python2.7)。

自定义push数据到open-falcon 参考:自定义push数据到open-falcon

 

 

#coding=gbk

#python2.7.13

import requests
import time
import json
import pymssql

# 推送默认参数
# https://book.open-falcon.org/zh/usage/data-push.html

endpoint = "KK-PC"
metric = "repl."
timestamp = int(time.time())
step = 300
value = 0
counterType = "GAUGE"
tags = "db="

# 存储过程参数声明(保存到json中),用于执行sp_replmonitorsubscriptionpendingcmds

"""
EXEC distribution.sys.sp_replmonitorsubscriptionpendingcmds 
 @publisher = N'publisher'
,@publisher_db = N'publisher_db'
,@publication = N'publication_name'
,@subscriber = N'subscriber'
,@subscriber_db = N'subscriber_db'
,@subscription_type =0
"""
publicationlist = [
    {
        "publisher": "KK-PC",
        "publisherdb": "DB01",
        "publication": "repl_test_DB01",
        "subscriber": "AA-PC",
        "subscriberdb": "DB01",
    },
    {
        "publisher": "KK-PC",
        "publisherdb": "DB02",
        "publication": "repl_test_DB02",
        "subscriber": "AA-PC",
        "subscriberdb": "DB02",
    },
    {
        "publisher": "KK-PC",
        "publisherdb": "DB03",
        "publication": "repl_test_DB03",
        "subscriber": "AA-PC",
        "subscriberdb": "DB03",
    },
    {
        "publisher": "KK-PC",
        "publisherdb": "DB04",
        "publication": "repl_test_DB04",
        "subscriber": "AA-PC",
        "subscriberdb": "DB04",
    },
]


def main():

    #连接数据库执行存储过程
    #dbdate 保存存储过程结果;pbdate 保存名称

    dbdate = []
    pbdate = []
    conn=pymssql.connect(host='.',database='distribution')
    cur=conn.cursor()

    for i in publicationlist: 
        sql = """
        EXEC distribution.sys.sp_replmonitorsubscriptionpendingcmds
         @publisher = N'"""+i["publisher"]+"""'
        ,@publisher_db = N'"""+i["publisherdb"]+"""'
        ,@publication = N'"""+i["publication"]+"""'
        ,@subscriber = N'"""+i["subscriber"]+"""'
        ,@subscriber_db = N'"""+i["subscriberdb"]+"""'
        ,@subscription_type =0"""
        cur.execute(sql)
        dbdate.append(cur.fetchall())
        pbdate.append(i["publisher"]+"."+i["publisherdb"])

    cur.close()
    conn.close()

    #配置推送的json格式,都添加到 payload 中

    payload = []
    data = {"endpoint":endpoint,"metric":metric,"timestamp":timestamp,"step":step,"value":value,"counterType":counterType,"tags":tags}

    for item,repl in zip(dbdate,pbdate):
        data = data.copy() 
        data["endpoint"] = endpoint
        data["metric"] = metric + "pendingcmdcount"
        data["timestamp"] = timestamp
        data["step"] = step
        data["value"] = item[0][0]
        data["counterType"] = counterType
        data["tags"] = tags + repl
        payload.append(data)
        data = data.copy() 
        data["endpoint"] = endpoint
        data["metric"] = metric + "estimatedprocesstime"
        data["timestamp"] = timestamp
        data["step"] = step
        data["value"] = item[0][1]
        data["counterType"] = counterType
        data["tags"] = tags + repl
        payload.append(data)

    r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(payload))
    print r.text


if __name__ == "__main__":
    main()


创建定时任务,每个5分钟执行一次。

 

 

 

 类似资料: