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 ...
随机推荐
- ettercap局域网内DNS欺骗(隔壁的哥们轻一点 ...)
转自:http://www.cnblogs.com/hkleak/p/5043063.html 笔记一:ettercap是什么? 我们在对WEB安全检测的时候都会用到Cain和netfuke这两款工具 ...
- EventBus的使用,数据传递
通常情况下安卓下数据的传递有下面几种方法: 1.通过intent传递,包括显式意图和隐式意图,广播(Broadcast)和服务都能通过Intent传递 传递的数据类型包括8大基本数据类型 实现P ...
- KMP算法代码
以下是本人根据上一篇博客随笔http://www.cnblogs.com/jiayouwyhit/p/3251832.html,所写的KMP算法代码(暂未优化),个人认为在基于上一篇博客的基础上,代码 ...
- Angular JS 学习之简介
1.Angular JS是一个JavaScript框架,它是一个以JavaScript编写的库,它可以通过<script>标签添加到HTML页面: <script src=" ...
- js正则表达式练习
匹配完整的名字 function process_name() { var field = document.getElementById("name_field"); var n ...
- 20145223《Java程序程序设计》实验一实验报告
实验一 Java开发环境的熟悉(Windows + IDE) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDE 编辑.编译.运行.调试Java程序. 实验步骤 (一)命令行下Ja ...
- ol新属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SQL SERVER 和ACCESS的数据导入导出
//批量导入Access string filepath = Server.MapPath("student.mdb"); stri ...
- Django的cookie和session
http://www.cnblogs.com/lhj588/archive/2011/10/27/2226976.html
- 深度神经网络结构以及Pre-Training的理解
Logistic回归.传统多层神经网络 1.1 线性回归.线性神经网络.Logistic/Softmax回归 线性回归是用于数据拟合的常规手段,其任务是优化目标函数:$h(\theta )=\thet ...