博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql
阅读量:6046 次
发布时间:2019-06-20

本文共 2665 字,大约阅读时间需要 8 分钟。

#!/usr/bin/python

-- coding: utf-8 --

import sys

import subprocess
import MySQLdb

class MySQL(object):

def init(self):
pass

def sql_init(self):    conn = MySQLdb.connect("localhost", "root", "", "test", charset='utf8' )    cur = conn.cursor()    return conn, cur@classmethoddef query_sql(cls, sql):    res = cls().sql_init()    conn = res[0]    cur = res[1]    try:        cur.execute(sql)        data = cur.fetchall()    except Exception:        data = None        print 'SQL ERROR!!'    conn.close()    return data@classmethoddef insert_sql(cls, sql):    res = cls().sql_init()    conn = res[0]    cur = res[1]    try:        cur.execute(sql)        conn.commit()        result = True    except:        conn.rollback()        result = False    conn.close()    return result@classmethoddef delete_sql(cls, sql):    res = cls().sql_init()    conn = res[0]    cur = res[1]    try:        cur.execute(sql)        conn.commit()        result = True    except:        conn.rollback()        result = False    conn.close()    return result@classmethoddef update_sql(cls, sql):    res = cls().sql_init()    conn = res[0]    cur = res[1]    try:        cur.execute(sql)        conn.commit()        result = True    except:        conn.rollback()        result = False    conn.close()    return result@classmethoddef do_mysql(cls, sql):    """        其他任意的SQL语句,如创建表    """    res = cls().sql_init()    conn = res[0]    cur = res[1]    try:        cur.execute(sql)        result = True    except:        result = False    conn.close()    return result

def getUnknownPods():

s=subprocess.Popen('kubectl get pods --all-namespaces|grep Unknown', shell=True, stdout=subprocess.PIPE)
out = s.communicate()[0]

if s != '':    #print out    result = out.split('\n')    #print result    lines = result[0:-1]    print lines    for line in lines:        rows = line.split()        #print rows        sql = 'insert into alerts (type,name,number,error,time) values("pod","%s",1,"Unknown Pod",now()) '%(rows[1])        #print sql        MySQL.insert_sql(sql)

def getRestartPods():

s=subprocess.Popen('kubectl get pods --all-namespaces', shell=True, stdout=subprocess.PIPE)
out = s.communicate()[0]

if s != '':   #print out   result = out.split('\n')   #print result   lines = result[1:-1]   print lines   for line in lines:       rows = line.split()       #print rows       if rows[4] > 3:           sql = 'insert into alerts (type,name,number,error,time) values("pod","%s",%s,"Unknown Pod",now()) '%(rows[1],rows[4])           #print sql           MySQL.insert_sql(sql)

def main():

#getUnknownPods()
getRestartPods()

if name == 'main':

main()

转载于:https://blog.51cto.com/11346335/2150146

你可能感兴趣的文章
Android 中间人攻击
查看>>
HTTP服务端JSON服务端
查看>>
PhpSpreadsheet生成Excel时实现单元格自动换行
查看>>
创建版本库【转】
查看>>
Atitit java jsp 新的tag技术
查看>>
Elasticsearch的CRUD:REST与Java API
查看>>
图像处理基础(5):双边滤波器
查看>>
scrapy xpath、正则表达式、css选择器
查看>>
[Angular] *ngIf syntx
查看>>
【Spark】Sparkstreaming-性能调优
查看>>
Android 通过onTouchEvent判断是否为双击事件
查看>>
Codeforces Round #144 (Div. 1) A. Cycles
查看>>
读书笔记《集体智慧编程》Chapter 11 : Evolving Intellignece
查看>>
jQuery的.bind()、.live()和.delegate()之间区别 [转]
查看>>
搭建代码审查系统Gerrit
查看>>
FineUI(专业版)高清大图赏析!(第二波)
查看>>
NGUI和UGUI动画不能设置alpha值的问题
查看>>
我也谈“the difference between Factory, Service, and Provider in Angular”
查看>>
1.浅谈CLR
查看>>
获取设备IMEI ,手机名称,系统SDK版本号,系统版本号
查看>>