#!/usr/bin/env python3
# -*- coding:utf8 -*-
# Author: light.wang
# Create Date: 2021-10-08 10:21:17
# Last Modified: 2021-10-09 17:52:17
# Description: 腾讯云mariadb指标采集,适用于centos7
import json
import pymysql
class TencentMariadb:
def __init__(self, params):
self.params = json.loads(json.dumps(params))
# 数据库连接
def mariadb_conn(self, sql):
conn = pymysql.connect(
host=self.params.get('parameters').get('ip'),
user=self.params.get('parameters').get('user'),
password=self.params.get('parameters').get('passwd'),
port=self.params.get('parameters').get('port')
)
cursor = conn.cursor()
cursor.execute(sql)
cursor.close()
conn.close()
return cursor
# 数据库名称
def mariadb_name(self):
sql = 'show databases;'
return [i[0] for i in self.mariadb_conn(sql).fetchall() if i[0] not in ["information_schema", "mysql", "performance_schema", "sysdb"]]
# 数据库版本
def mariadb_version(self):
sql = 'select version() from dual;'
return self.mariadb_conn(sql).fetchone()[0]
# 数据库HOME目录
def basedir(self):
sql = "show global variables like '%basedir%';"
basedir_value = self.mariadb_conn(sql).fetchone()[1] if self.mariadb_conn(sql).fetchone() else ''
return basedir_value
# 查询日志
def general_log_file(self):
sql = "show global variables like '%general_log_file%';"
general_log_file_value = self.mariadb_conn(sql).fetchone()[1] if self.mariadb_conn(sql).fetchone() else ''
return general_log_file_value
# 慢查询日志
def slow_query_log_file(self):
sql = "show variables like 'slow_query_log_file';"
return self.mariadb_conn(sql).fetchone()[1]
# 二进制日志
def log_bin_basename(self):
sql
mariadb监控指标采集
于 2021-10-23 16:33:43 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)