标签:hal .com ber method roo upd grep row localhost
#!/usr/bin/pythonimport 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()
标签:hal .com ber method roo upd grep row localhost
原文地址:http://blog.51cto.com/11346335/2150146