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 ...
随机推荐
- 【微信Java开发 --2】接入微信公众平台开发,配置自己的服务器,验证过程
接入微信公众平台开发,开发者需要按照如下步骤完成: 1.填写服务器配置 2.验证服务器地址的有效性 3.依据接口文档实现业务逻辑好我们就开始:1.填写好我们的URL和Token[此处是已经通过验证的] ...
- ViewPager的广告条轮播
首先布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...
- Java和数据库时间格式化格式
JAVA: yyyy-MM-dd HH-mm-ss(大小HH表示24小时制); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd&q ...
- iphone 数字字段颜色兼容问题
在iphone手机中,有些字段的颜色会自动发生变化,在head中添加如下meta即可.<meta name = "format-detection" content=&quo ...
- (转)ACM next_permutation函数
转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 (1) int 类型的next_permuta ...
- DSP using MATLAB 示例 Example3.11
用到的性质 上代码: n = -5:10; x = rand(1,length(n)); k = -100:100; w = (pi/100)*k; % freqency between -pi an ...
- hdu 1078 FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 非传统题【A002】
[A002]非传统题[难度A]————————————————————————————————————————————————————————————————————————————————————— ...
- AFNetworking 之于 https 认证
写在开头: 本来这篇内容准备写在AFNetworking到底做了什么?(三)中的,但是因为我想在三中完结这个系列,碍于篇幅所限.并且这一块内容独立性比较强,所以单独拎出来,写成一篇. 本文从源码的角度 ...
- GridView中超链接设置
<%# Eval("id") %>Bind方式 <%# Bind("id","~/info.aspx?id={0}" ...