#!/usr/local/bin/python
#
# Script to connect to the EDB and get the alerts data
#

import psycopg2

try:
     conn = psycopg2.connect("postgresql://wfs:Wf$1234@DBSERVER:5444/ttv")
except:
    print "I am unable to connect to the database"

cur = conn.cursor()
try:
    cur.execute("""select a.identifier, a.alert_name, a.thresh_count, a.thresh_secs, a.freq_count, a.freq_secs, aa.action_type, aa.detail, an.pattern from alerts a, alert_actions aa, alert_names an where a.identifier = aa.identifier and a.alert_name = an.alert_name""")
except:
    print "I could not select the data"

rows = cur.fetchall()

print "---"
for row in rows:
    print "- alert:"
    print "    name: ", row[0]
    print "    pattern: ", row[8]
    print "    frequency:"
    print "        count: ", row[4]
    print "        time: ", row[5]
    print "    threshold:"
    print "        count: ", row[2]
    print "        time: ", row[3]
    if row[6] == 1:
        print "    trap: ", row[7]
    else:
        print "    email: ", row[7]

