iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >python个人记录3 mysql监控
  • 477
分享到

python个人记录3 mysql监控

pythonmysql 2023-01-31 06:01:45 477人浏览 薄情痞子
摘要

#!/usr/bin/pythonimport Mysqldbimport pycurldef master_work(ip,port):    print ip,port    conn=mysqldb.connect(host=str(

#!/usr/bin/python

import Mysqldb

import pycurl



def master_work(ip,port):

    print ip,port

    conn=mysqldb.connect(host=str(ip),user='***',passwd='****',port=int(port))

    cur=conn.cursor(cursorclass=Mysqldb.cursors.DictCursor)

    cur.execute('show status;')

    result=cur.fetchall()

    #conn1=result[-5]['Value']

    for result_master in result:

        if result_master['Variable_name'] == 'Threads_running':

            conn1=result_master['Value']

    print str(ip),str(port),str(conn1)

    cur.execute('show global variables like "%conn%";')

    result_2=cur.fetchall()

    for results_master in result_2:

        if results_master['Variable_name'] == 'max_user_connections':

            max_conn=results_master['Value']

    print conn1

    if int(conn1) >= 700:

        print 'master_'+str(ip)+str(port)+str(conn1)

        master_monitor('master_'+str(ip)+'_'+str(port)+'_'+'threading:'+str(conn1)+'_max_:'+str(max_conn))

    cur.close()

    conn.close()

    



def slave_work(ip,port,role):

    print role

    conn=MySQLdb.connect(host=ip,user='***',passwd='****',port=int(port))

    cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

    cur.execute('show slave status;')

    result=cur.fetchone()

    Slave_IO_Running=result['Slave_IO_Running']  

    Slave_SQL_Running=result['Slave_SQL_Running']

    Seconds_Behind_Master=result['Seconds_Behind_Master']

    if Slave_IO_Running != 'Yes' or Slave_SQL_Running != 'Yes':

        print str(ip),str(port)+'_error'

        slave_monitor(str(ip)+'_'+str(port)+'_mysql_slave_down')

    if Seconds_Behind_Master >= 86400 and role == 'backup':

        if str(ip) == '***' and str(port) == '***':

            return None 

        elif str(ip) == '****' and str(port) == '***':

            return None

        else:

            print str(ip),str(port)+'_Seconds_Behind_Master:'+str(Seconds_Behind_Master)

            slave_monitor(str(ip)+'_'+str(port)+'_Seconds_Behind_Master_'+str(Seconds_Behind_Master))

    elif Seconds_Behind_Master > 100 and role == 'slave':

        if str(ip) == '****' and str(port) == '****':

            return None 

        elif str(ip) == '****' and str(port) == '***':

            return None

        else:

            print str(ip),str(role),str(port)+'_Seconds_Behind_Master:'+str(Seconds_Behind_Master)

            slave_monitor(str(ip)+'_'+str(port)+'_Seconds_Behind_Master_'+str(Seconds_Behind_Master))

    cur.execute('show status;')

    result_1=cur.fetchall()

    for results in result_1:

        if results['Variable_name'] == 'Threads_running':

            conn1=results['Value']

    cur.execute('show global variables like "%conn%";')

    result_2=cur.fetchall()

    for result_i in result_2:

        if result_i['Variable_name'] == 'max_user_connections':

            max_conn=result_i['Value']

    print str(ip),str(port),str(max_conn)

    if int(conn1) >= 700:

        print str(ip),str(port),str(conn1)+'_error'

        slave_monitor('slave_'+str(ip)+'_'+str(port)+'_threading:'+str(conn1)+'_max_conn:'+str(max_conn))

    cur.close()

    conn.close()



def slave_monitor(content):

    c=pycurl.Curl()

    c.setopt(c.URL,'Http:/new/?service=mysql_slave&checkpoint=mysql_slave_error&title=%s&content=%s&cluster=public&grade=2'%(content,content)) 

    c.perfORM()

    

def master_monitor(content):

    c=pycurl.Curl()

    c.setopt(c.URL,'http:/new/?service=mysql_master&checkpoint=mysql_master_error&title=%s&content=%s&cluster=public&grade=2'%(content,content))

    c.perform()


conn=MySQLdb.connect(host='****',user='***',passwd='****',db='****',port=***)

cur=conn.cursor()

cur.execute('select distinct port from node order by port;')

res=cur.fetchall()

list_1=[]

dict_1={}

dict_2={}

for i in res:

    cur.execute('select distinct host,port,type from node where port=%s'%str(i[0]))

    res1=cur.fetchall()

    list_1.append(res1)

for ii in list_1:

    for iii in ii:

        ip_1=iii[0]

        port_1=iii[1]

        dict_2[port_1]=ip_1

        if iii[2] == 'master':

            master_ip=iii[0]

            port=iii[1]

            dict_1[port]=master_ip

            master_work(master_ip,port)

cur.close()

conn.close()

for aa in list_1:

    for aaa in aa:

        port_2=aaa[1]

        

        if aaa[0] == dict_1[port_2]:

            pass

        else:

            slave_work(aaa[0],aaa[1],aaa[2])


您可能感兴趣的文档:

--结束END--

本文标题: python个人记录3 mysql监控

本文链接: https://www.lsjlt.com/news/189894.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python个人记录3 mysql监控
    #!/usr/bin/pythonimport MySQLdbimport pycurldef master_work(ip,port):    print ip,port    conn=MySQLdb.connect(host=str(...
    99+
    2023-01-31
    python mysql
  • python 点滴记录3:python中
    for i in range(8):    print i显示结果为:0,1,2,3,4,5,6,7for i in range(1,8):    print i显示结果为:1,2,3,4,5,6,7...
    99+
    2023-01-31
    点滴 python
  • 你知道怎么用Python监控聊天记录吗
    目录关于 pynput实例键盘监控鼠标监控监控并记录到日志文件 关于 pynput     pynput 可以监控我们的键盘和鼠标。目前具有此类功能的库有很多...
    99+
    2024-04-02
  • MySQL8.0.22安装过程记录(个人笔记)
    点击下载MySQL 2.解压到本地磁盘(注意路径中不要有中文) 3.在解压目录创建my.ini文件 文件内容为 [mysql]# 设置mysql客户端默认字符集default-character-set=utf8 [mysqld]# 设...
    99+
    2023-08-30
    笔记 mysql
  • python 监控mysql脚本
    #!/usr/bin/env python #-*- coding: UTF-8 -*- from __future__ import print_function from mysql import connector import lo...
    99+
    2023-01-31
    脚本 python mysql
  • PHP 日志记录和监控的配置
    php 日志记录和监控的配置对于应用程序稳定性至关重要。使用 monolog 记录事件,sentry 分析错误,prometheus 监控度量数据,可以让开发人员快速诊断问题,提高应用程...
    99+
    2024-05-01
    php 日志 linux git composer
  • python 键盘监听+记录 [ p
    今天想做一个键盘监听的小程序,去网上搜了一下,发现用Python写键盘监听主要有pyhook和pynput两种……… 这里用的是pynput,logging。 本来想用Jupyter Notebook写的,但是发现运行不了,可...
    99+
    2023-01-31
    键盘 python
  • python功能笔记——远程监控
    python 监控远程主机(根据慕课网手打)1.获取要监控的计算机的信息os.system('command') 直接输出结果os.popen('command') 返回一个文件open('/proc/file') ...
    99+
    2023-01-31
    远程监控 功能 笔记
  • python全栈学习记录--3(2018
    一、Linux操作系统1、unix是目前世界上最稳定,安全的系统。是分时操作系统,多个联机终端和采用多道技术。2、开源代表代表公开,所有人可以编辑查看,但不代表免费。3、服务器要的是性能,稳定,效率,不间断运行。故LINUX更能胜任,WIN...
    99+
    2023-01-31
    python
  • Python 3 学习笔记:流程控制
    程序结构 计算机在解决某个具体问题时,主要有三种形式,分别是顺序控制执行所有语句、选择执行部分语句和循环执行部分语句。根据以上三种解决问题的形式,程序设计过程中,也有三种基本结构,分别为顺序结构、选择结构和循环结构。 顺序控制 顺序结构很好...
    99+
    2023-01-31
    学习笔记 流程 Python
  • PHP 框架中日志记录和监控策略
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-24
  • 如何使用vb 监控电脑活动记录
    目录主要函数实现开启自启动运行最近看到了一个日文版的监控电脑活动记录的软件,又在win 32 APi中看到了GetForegroundWindow函数,于是决定动动小手用vb写个监控...
    99+
    2024-04-02
  • python笔记3:依次输入3个数排序打
    #方法1:if语句,练习逻辑能力num1 = int(input('请输入第一个数:'))num2 = int(input('请输入第二个数:'))num3 = int(input('请输入第三个数:'))if num1 >= num...
    99+
    2023-01-31
    个数 笔记 python
  • Python 3 学习笔记:目录&文件处
    路径 路径,用于定位目录或文件的字符串。 相对路径 相对路径依赖于当前工作目录(即当前文件所在的目录),可以使用如下函数获取当前工作目录, 1os.getcwd()复制在当前工作目录中,可以使用相对路径访问这个目录中的所有子目录和其中的文件...
    99+
    2023-01-31
    学习笔记 文件 目录
  • 用python实现监控视频人数统计
    目录一、图示二、准备三、一个简单服务器应用四、向服务器发送图片五、最终关键yolov5调用代码:一、图示 客户端请求输入一段视频或者一个视频流,输出人数或其他目标数量,上报给上层...
    99+
    2024-04-02
  • python学习笔记3—流程控制if、f
    流程控制ifif 语句if expression:    statement(s)elseelse语句:if 语句,else语句if expression:    statement(s)else:    statement(s)elif语...
    99+
    2023-01-31
    学习笔记 流程 python
  • Nagios如何处理监控事件的历史记录
    Nagios存储监控事件的历史记录是通过将事件信息写入日志文件或者数据库来实现的。Nagios可以配置为将监控事件的历史记录写入到指...
    99+
    2024-03-12
    Nagios
  • 使用vb怎么监控电脑的活动记录
    这期内容当中小编将会给大家带来有关使用vb怎么监控电脑的活动记录,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。主要函数函数名参数返回值GetForegroundWindow(void)无当前窗口的句柄Ge...
    99+
    2023-06-14
  • Zabbix怎么实现监控数据的历史记录
    Zabbix是一个开源的网络监控系统,可以实现监控数据的历史记录。要实现监控数据的历史记录,可以按照以下步骤进行操作: 配置Za...
    99+
    2024-04-02
  • 如何查看阿里云服务器监控记录
    阿里云服务器提供了强大的监控功能,可以帮助用户及时发现和解决服务器问题。本文将详细介绍如何查看阿里云服务器的监控记录,以便更好地管理和维护服务器。 一、查看阿里云服务器监控记录的步骤登录阿里云控制台首先,需要在浏览器中打开阿里云的官方网站,...
    99+
    2023-10-30
    阿里 服务器
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作