Python script query Ambari error message
Yum-y Install PYTHON-PSYCOPG2
#!/usr/bin/pythonImportSYSImportPSYCOPG2#Modify the following five optionsPsql_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 () forRowinchrows:dids.append (row[0]) forIinchDids: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 was null and a.host_name is null)) and a.alert_t Imestamp=x.ftime and A.alert_definition_id="+ STR (i) +" )") Rows=Cur.fetchall () forRowinchrows:if(Row[0] = ="CRITICAL" orRow[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 ()
Python View Ambari Error