python查看ambari报错

python脚本查询ambari错误信息

yum -y install python-psycopg2

#!/usr/bin/python

import sys
import psycopg2

# modify the following five options
psql_database = "ambari"
psql_user = "ambari"
psql_password = "bigdata"
psql_host = "127.0.0.1"
psql_port = "5432"

conn = psycopg2.connect(database=psql_database, user=psql_user, password=psql_password, host=psql_host, port=psql_port)
cur = conn.cursor()

dids=[]
cur.execute("select distinct(definition_id) from alert_current")
rows = cur.fetchall()
for row in rows:
  dids.append(row[0])


for i in dids:
    cur.execute("select alert_state,service_name,component_name,host_name,alert_label,alert_text from alert_history a where exists( select * from ( select host_name,max(alert_timestamp) as FTime from alert_history where alert_definition_id=" + str(i) + " group by host_name ) x where (x.host_name=a.host_name or (x.host_name is null and a.host_name is null)) and a.alert_timestamp=x.FTime and a.alert_definition_id=" + str(i) + " )")
    rows = cur.fetchall()
    for row in rows:
        if (row[0] == "CRITICAL" or row[0] == "WARNING"):
            line = "did=" + str(i) + " || alert_STATE=" + row[0] + " || " + str(row[1])+ " || " + str(row[2])+ " || " + str(row[3])+ " || " + str(row[4])+ " || " + str(row[5])
            print(line)

conn.close()

猜你喜欢

转载自www.cnblogs.com/nhs3217/p/9467240.html