码迷,mamicode.com
首页 > 数据库 > 详细

mysql

时间:2018-07-25 19:04:26      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:hal   .com   ber   method   roo   upd   grep   row   localhost   

#!/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

@classmethod
def 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

@classmethod
def 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

@classmethod
def 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

@classmethod
def 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

@classmethod
def 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()

mysql

标签:hal   .com   ber   method   roo   upd   grep   row   localhost   

原文地址:http://blog.51cto.com/11346335/2150146

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!