frame告警_flask-caching

frame告警_flask-caching#发送告警邮件 cat check_backup.sh #!/bin/bash . ~/.bash_profile BASEDIR=`dirname $0` cd $BASEDIR parse_li…

基于flask的告警监控——for tidb backup

#发送告警邮件
cat  check_backup.sh
#!/bin/bash

. ~/.bash_profile
BASEDIR=`dirname $0`
cd $BASEDIR

parse_line(){
    EMAIL="xxxxxx@126.com;xxxxxx@126.com"
}
 parse_line
 python tidb_backup_check.py >check_backup_status.html  2>/dev/null
  mutt  -e "set content_type="text/html"" -e "set from="tidb@admin.com"" -e "set realname="TiDB_Admin"" -s "TiDB Backup Status Check" "xxxx@126.com";${EMAIL}"  <check_backup_status.html
  
  
 #获取具体的告警内容 
 cat tidb_backup_check.py 
#!/usr/bin/env python
import  psycopg2   as pg
from datetime import datetime, date, time
import json
import prettytable as pt
from prettytable import PrettyTable
import  pandas as pd
from flask import Flask
from flask import render_template

app = Flask("__name__)")
@app.route("/")
def getinfo(sql):
    try:
        pg_conn=pg.connect(database="db", user="tidb", password="123456", host="192.168.1.1", port="4000")
        pg_cur=pg_conn.cursor()
        pg_cur.execute(sql)
        rows = pg_cur.fetchall()
        pg_conn.commit()
        pg_conn.close()
        return rows
    except Exception as e:
        print(e)
        return 0


if __name__ == "__main__":
    html_string = """
    <html>
      <head><title>TiDB Backup Status Check</title>
      <style type="text/css">
       {styles}
      </style>
     <div class="container">
    <div class="page-header">
        <h1 class="tbsc">TiDB Backup Status Check</h1>
    </div>
</div>
           <div>
           <p>
            <h3 class="tbsc">说明:</h3>
                  <li class="tbsc">同一个实例通过tidb节点进行检查,包括相应节点的日志信息</li>
                  <li class="tbsc">以下备份信息主要是检查最近一天的备份,如果备份策略是周末全备需要单独查验</li>
          <li class="tbsc">如果同一个实体中所有tidb组件没有备份日期与大小,请检查备份job是否正常,zabbix agent 是否有安装</li>
              <li class="tbsc">如果备份数据库binglog 没有大小,请检查是否有开启归档,归档是否正常</li>
           </p>
           </div>

      <body >
        {table}
      </body>
    </html>
    """
    style="""
    h1.tbsc {
    font: bold 20pt Arial, Helvetica, Geneva, sans-serif;
    color: #336699;
    background-color: White;
    border-bottom: 1px solid #2e5c89;
    margin-top: 0pt;
    margin-bottom: 0pt;
    padding: 0px 0px 0px 0px;
    text-align: center;
}

h3.tbsc {
    font: bold 14pt Arial, Helvetica, Geneva, sans-serif;
    color: #336699;
    background-color: White;
    margin-top: 0pt;
    margin-bottom: 0pt;
    padding: 0px 0px 0px 0px;
    text-align: left;
}

li.tbsc {font: 12pt Arial,Helvetica,Geneva,sans-serif; color:E32636; background:White;}



Table {
  border: 1px solid #1C6EA4;
  background-color: #EEEEEE;
  width: 80%;
  text-align: center;
  border-collapse: collapse;
}
Table td, Table th {
  border: 1px solid #AAAAAA;
  padding: 3px 2px;
}
Table tbody td {
  font-size: 13px;
}
Table tr:nth-child(even) {
  background: #D0E4F5;
}
Table thead {
  background: #1C6EA4;
  background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
  background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
  background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
  border-bottom: 2px solid #444444;
}
Table thead th {
  font-size: 15px;
  font-weight: bold;
  color: #FFFFFF;
  text-align: center;
  border-left: 2px solid #D0E4F5;
}
Table thead th:first-child {
  border-left: none;
}

Table tfoot {
  font-size: 14px;
  font-weight: bold;
  color: #FFFFFF;
  background: #D0E4F5;
  background: -moz-linear-gradient(top, #dcebf7 0%, #d4e6f6 66%, #D0E4F5 100%);
  background: -webkit-linear-gradient(top, #dcebf7 0%, #d4e6f6 66%, #D0E4F5 100%);
  background: linear-gradient(to bottom, #dcebf7 0%, #d4e6f6 66%, #D0E4F5 100%);
  border-top: 2px solid #444444;
}
Table tfoot td {
  font-size: 14px;
}
tr:hover            { background-color: #ffff99;}
    """



    sql_getitem = """ select i.itemid --,i."name",i.key_,h.host,h.hostid,h."name" 
               from items i inner join public.hosts h on  i.hostid=h.hostid  
    where i."name"="tidb check backup status" and i.itemid!="3877749" """

    sql_getInstance = """
    select * from (
    SELECT  lower ("entityName") as instancename, "chineseDesc", "oprDba", case  environment
    when "DBE:PRD" then "生产"
    when "DBE:DRC" then "同城"
    else "远城"
     end env,  "instanceName", "hostIp1", "hostName1",
     substr("instanceName",length("entityName")+2,length("instanceName")) dbtype
    FROM tidb_info a where lower(a."databaseType")="tidb" and a.status="DBS:ON")  b
    where b.dbtype  like "D%"  order by b."instancename" ;"""
    """select i.itemid,i."name",i.key_,to_timestamp(hl.clock) ck,h.host,h.hostid,h."name" ,hl.value from items i 
         inner join hosts h on  i.hostid=h.hostid  
         inner join history_log hl on i.itemid=hl.itemid  where i."name"="tidb check backup status" 
          --and hl.value="/usr/bin/env: python3: No such file or directory"
         and  hl.clock >=extract(epoch from current_date)"""

    rows_item=getinfo(sql_getitem)
    rows_instance=getinfo(sql_getInstance)
    tidbInstance = []
    backrest = []
    if not (rows_instance == 0):
        # print(type(rows_instance))
        dt = pd.DataFrame(rows_instance)
        dt = dt.rename(
            columns={0: "instance", 1: "Chinese_name", 2: "DA", 3: "ENV", 4: "sub_instance", 5: "IP", 6: "hostname",
                     7: "组件"})
    if not (rows_item == 0):
        item = [x for i in rows_item for x in i]
        for i in item:
            sql_getbackup_status = """select value FROM history_log ht  where  itemid={}""".format(i) + 
                                   """and ht.value!="/usr/bin/env: python3: No such file or directory" group by value ,clock order by clock desc limit 1"""
            res = getinfo(sql_getbackup_status)
            if (len(res) > 0 and res != 0):
                h = [y for x in res for y in x]
                for x in h:
                    t = json.loads(x)
                    for m in t:
                        if not (m["instance"] == """[]""" or len(m["size"]) == 0):
                            backrest.append(m)
    # print(backrest)
    dt_backup = pd.DataFrame(backrest)
    #dt_backup=dt_backup.rename(columns={0:"检查日期",1:"备份开始时间",2:"备份结束时间",3:"大小",4:"binlog 大小"})
    # print(dt_backup)

    backinfo = pd.merge(dt, dt_backup, how="left", on=["hostname", "instance"])
    backinfo.rename(
        columns={"instance": "实体名", "Chinese_name": "实体中文名", "DA": "主管DA", "ENV": "环境", "sub_instance": "实例名",
                 "hostname": "主机名", "check day": "检查日期", "backup begin time": "备份开始时间", "backup end time": "备份结束时间",
                 "size": "备份大小", "binglog size": "日志大小"}, inplace=True)
    backinfo_html=backinfo.to_html()
    # print(backinfo)

    html_string = html_string.format(table=backinfo_html,styles=style)
    #html_string=html_string.format(styles=style)
    print(html_string)
    # with app.app_context():
    #     render_template("view.html", tables=[backinfo.to_html()] 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/6831.html

(0)
上一篇 2023-04-01
下一篇 2023-04-01

相关推荐

  • MySQL学习笔记(15):SQL Mode

    MySQL学习笔记(15):SQL Mode本文更新于2019-06-29,使用MySQL 5.7,操作系统为Deepin 15.4。 与其他数据库不同,MySQL可以运行在不同的SQL Mode(SQL模式)下。 可通过变量@@sql_mod

    2023-03-19
    146
  • Python实现温度转换

    Python实现温度转换温度转换是常见的工程计算之一,因为世界上各个国家和地区,对温度的表示方法不尽相同。其中最常用的两种是摄氏度和华氏度。本文将通过Python实现温度转换过程,以加深读者对温度转换的理解和应用。

    2024-09-16
    23
  • 40Mn18Cr3热锻环[通俗易懂]

    40Mn18Cr3热锻环[通俗易懂]无磁钢40Mn18Cr3和50Mn18Cr5经常作为电机配件进行热锻、模锻、温锻工艺的。 作为电机护环,对材料性能都有较高的要求,特别是护环的屈服。《135 85↘ 上海高高,十几年经营无磁钢下来。…

    2023-03-19
    151
  • windows下用navicat远程链接虚拟机Linux下MySQL数据库[通俗易懂]

    windows下用navicat远程链接虚拟机Linux下MySQL数据库[通俗易懂]
    今天想用navicat远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现原因是MySQL对远程用户登陆的授权问题。这里说一下我的解决方法…

    2023-04-05
    169
  • SQL Server 索引的含义和特点

    SQL Server 索引的含义和特点索引用于快速找出在某个列中某一特定值的行。不使索引,数据库必须从第一条记录开始读完整个表,直到找到相关行。如果表中查询的列有一个索引,数据库能快速到达一个位置去搜寻数据,而不必查看所有数据。 索引的含

    2023-03-16
    147
  • php操作mysql防止sql注入(合集)「建议收藏」

    php操作mysql防止sql注入(合集)「建议收藏」本文将从sql注入风险说起,并且比较addslashes、mysql_escape_string、mysql_real_escape_string、mysqli和pdo的预处理的区别。 当一个变量从…

    2022-12-23
    147
  • SQL 入门教程:聚集函数「建议收藏」

    SQL 入门教程:聚集函数「建议收藏」目录汇总:SQL 入门教程:面向萌新小白的零基础入门教程 我们经常需要汇总数据而不用把它们实际检索出来,为此 SQL 提供了专门的函数。使用这些函数,SQL 查询可用于检索数据,以便分析和报表生成。这

    2023-04-20
    135
  • Python Tkinter自定义滑块组件实现

    Python Tkinter自定义滑块组件实现Python Tkinter是Python标准库中一个GUI工具包,它提供了开发应用程序所需的各种GUI组件、窗口、框架等,灵活性强且易于使用。

    2024-04-05
    63

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注