Python 脚本 监控数据库状态
2016-05-06 15:31
525 查看
打算用这个脚本通过zabbix 监控Mariadb的,无奈要等Mariadb完全上线才行,所以先写一个粗略大致功能的版本。
用pip安装MySQLdb库,需要会提示找不到Python.h 需要安装python-dev(ubuntu) pip install MySql-python
里面包含了许多基础知识,怕忘记 记录一下。
#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
里面包含了许多基础知识,怕忘记 记录一下。
相关文章推荐
- python的str,unicode对象的encode和decode方法
- 手把手教你用 Python 爬虫煎蛋妹纸海量图片
- python复制txt内容到另一个txt
- python SQLAlchemy 使用经验
- python新手必碰到的问题---encode与decode,中文乱码
- Sublime中配置python
- Python list dict iteration
- 练习008-009
- 你会用Python做出装逼的东西吗
- 【原】Spark之机器学习(Python版)(一)——聚类
- python: 知乎大规模(34k)用户爬虫
- 对Python的深度学习库Theano的介绍
- pythong中字符串strip的用法
- python中write没有写入是什么情况
- python 调用系统命令,并将输出重定向,输出其结果至文件或者字符串变量
- python 打印出执行函数及其所在路径以及行号
- python basestring( )
- Anaconda多环境多版本python配置指导
- Python中的正则表达式(re)
- 在线生成UUID、Python生成唯一UUID(Universally Unique IDentifier )