Python 脚本 监控数据库状态
打算用这个脚本通过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
里面包含了许多基础知识,怕忘记 记录一下。
Python 脚本 监控数据库状态的更多相关文章
- shell脚本监控网站状态
		
shell脚本监控网站状态 #!/bin/sh date=`date +"%Y%m%d-%H%M"` title="status" contentFail=&q ...
 - Redis之使用python脚本监控队列长度
		
编辑python脚本redis_conn.py #!/usr/bin/env python #ending:utf-8 import redis def redis_conn(): pool = re ...
 - zabbix 调用python脚本监控 磁盘剩余空间(创建模版,创建监控项,创建触发器)
		
主要 记录一下 使用zabbix 自己创建模版.监控项.触发器,并调用python脚本. 需求: 监控备份机磁盘剩余空间(windows系统) 一.安装zabbix_agent 比较简单 修改配置文 ...
 - python脚本监控股票价格钉钉推送
		
关注股市,发家致富 问题:一天天盯着股市多累,尤其上班,还不能暴露,股票软件,红红绿绿,这么明显的列表页面,一看就知道在摸鱼.被领导发现饭碗就没了 解决:搞个脚本监听一下自己关注的股票,一到价格就发个 ...
 - 在nagios中使用python脚本监控linux主机
		
在被监控端192.168.5.1101.先把getload.py放到/usr/local/nagios/libexec内[root@nhserver1 ~]# vim /usr/local/nagio ...
 - 写一个python脚本监控在linux中的进程
		
在虚拟机中安装Linux中的CentOS7系统 https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for= ...
 - 使用python脚本监控weblogic
		
1.python的脚本如下: ############################################################################### #crea ...
 - python脚本监控获取当前Linux操作系统[内存]/[cpu]/[硬盘]/[登录用户]
		
此脚本应用在linux, 前提是需要有python和python的psutil模块 脚本 #!/usr/bin/env python # coding=utf-8 import sys import ...
 - 使用python脚本从数据库导出数据到excel
		
python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...
 
随机推荐
- HDU 5652 India and China Origins(经典并查集)
			
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
 - DLL中传递STL参数(如Vector或者list等)会遇到的问题[转载]
			
最近的一个项目中遇到了调用别人的sdk接口(dll库)而传给我的是一个vector指针,用完之后还要我来删除的情况.这个过程中首先就是在我的exe中将其vector指针转为相应指针再获取vector中 ...
 - SU Demos-06Selecting Traces
			
不足之处,欢迎批评指正 共3个脚本,先看readme 第1个脚本 运行结果 第2个脚本 运行结果 第3个脚本 运行结果
 - HTTP基础11--web(3)
			
邮件首部注入攻击 指 Web 应用中的邮件发送功能,攻击者通过向邮件首部 To 或 Subject 内任意添加非法内容发起的攻击.利用存在安全漏洞的 Web 网站,可对任意邮件地址发送广告邮件或病毒邮 ...
 - git 基础使用
			
1: 安装客户端 2: 注册使用github 3: 具体操作 3-1: 右键打开:git bash here 执行 ssh-keygen -t rsa -C "youremail@examp ...
 - PHP如何实现文件上传
			
PHP如何实现文件上传 1.表单部分 允许用户上传文件,在HTML表单的声明中要加上一个上传的属性: enctype = 'multipart/form-data' 表单的method必须是PO ...
 - 用触发器来实现Oracle的自增长列
			
1, 添加id列 -- ############################################### -- add ID column for XXXXXXTABLE -- #### ...
 - C#调用本机摄像头
			
这段时间一个小项目中需要调用本机的摄像头进行拍照,网上搜集了一些资料以及解决的一些小问题,在此记录以便后续使用. 硬件环境:联想C360一体机,自带摄像头 编写环境:vs2010 语言:C# WPF ...
 - WPF之依赖属性
			
Introduction When you begin to develop appliations with WPF, you will soon stumble across Dependency ...
 - CF# 334 Moodular Arithmetic
			
B. Moodular Arithmetic time limit per test 1 second memory limit per test 256 megabytes input standa ...