需求
今天写了个exporter监控下刚投入使用的mysql主从的主机。主要用到了python的flask和pymysql模块。
程序
引入模块。没有的需要自己pip安装
# coding=utf-8
#author:OrangeLoveMlian
#!/bin/python
#coding=utf-8
import pymysql
import pymysql.cursors
import sys
import prometheus_client
from prometheus_client.core import CollectorRegistry
from prometheus_client import Gauge
from flask import Response,Flask
登陆mysql,写了一个探测函数,如果不能连接到mysql,返回0.说明mysql可能出现问题了。
###参数字典
config = {
'host':'ip',
'port':port,
'user':'user',
'password':'pwd',
}
###获取连接状态
def connection_status():
try:
connection = pymysql.connect(**config)
connection_status = 1
connection.close()