打算用这个脚本通过zabbix 监控Mariadb的,无奈要等Mariadb完全上线才行,所以先写一个粗略大致功能的版本。
#coding:utf-8
#author:shiyiwen
#version:1.1
#display:monitor mariadb galera cluster
import os
import sys
import MySQLdb
import subprocess
import time
import re
def mysqlprocessstatus():
runcmd = 'service mysql start'
p = subprocess.Popen('netstat -ntlp|grep mysql',shell=True,stdout=subprocess.PIPE) #将stdout缓存进pipe
#p1 = subprocess.Popen('grep java',shell=True,stdin=p.stdout,stdout=subprocess.PIPE) #筛选出mysql进程并存入stdout
status = p.stdout.readlines()
if len(status) > 0:
return 'Sucess'
else:
sys.stderr.write('%s process down, running [%s] \n' % ('Mysql',runcmd))
subprocess.Popen(runcmd,shell=True)
if len(status) == 0:
return 'Mysql Down'
def initconn():
try:
conn = MySQLdb.connect(host='172.16.38.175', user='root', passwd='anjubao.com', port=3306)
return conn
except MySQLdb.Connect().Error,e:
print "Mysql connet Error"
def clusterwsrepstatus(conn,item):
query = 'show status like "wsrep%"'
cur = conn.cursor()
cur.execute(query) #返回code 这个code 为 数据条数,也可以通过fetchmany来接受数据,但是要指定数据条数,对没错就是这个code!
result = cur.fetchall() #直接接受所有数据,返回多个tuple
conn.commit() #真正commit DDL DML
cur.close()
for t in result:
for i in t:
if i == item:
return t[1]
def analysis():
pass
def execerror():
pass
def insertdb():
pass
if __name__ == '__main__':
#mysqlprocessstatus()
wsrep_conn=clusterwsrepstatus(initconn(),'wsrep_connected')
wsrep_uuid=clusterwsrepstatus(initconn(),'wsrep_cluster_state_uuid')
wsrep_size=clusterwsrepstatus(initconn(),'wsrep_cluster_size')
if wsrep_conn=='on' and wsrep_size=='3' and wsrep_uuid=='BBBB-BBBB':
print "mengmengda"
else:
print "Error"
用pip安装MySQLdb库,需要会提示找不到Python.h 需要安装python-dev(ubuntu) pip install MySql-python
里面包含了许多基础知识,怕忘记 记录一下。