from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from smtplib import SMTP

import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("skyline.analyzer.alerts")


"""
Create any alerter you want here. The function will be invoked from trigger_alert.
Two arguments will be passed, both of them tuples: alert and metric.

metric: the anomalous metric
datapoint: the anomalous datapoint (timestamp, value)
kwargs: alert specific settings (e.g. notification recipient)
settings: general alert settings (e.g. authentication information)
"""


# This specifies the mailserver to connect to.
# If user or password are blank no authentication is used.
def alert_smtp(metric, datapoint, ensemble, args, settings):
    # Connect to the mail server
    conn = SMTP(settings["host"])
    user = settings.get("user")
    password = settings.get("password")
    if user and password:
        conn.login(user, password)

    sender = settings['sender']
    recipients = kwargs['recipients']
    for recipient in recipients:
        msg = MIMEMultipart('alternative')
        msg['Subject'] = '[Skyline] {0}'.format(metric)
        msg['From'] = sender
        msg['To'] = recipient
        link = ""  # settings.GRAPH_URL.format(metric)
        body = 'Anomalous metric: {0} (datapoint: {1})<br><a href="{2}"><img src="{2}"/></a>'.format(metric, datapoint, link)
        msg.attach(MIMEText(body, 'html'))
        conn.sendmail(sender, recipients, msg.as_string())
    conn.quit()


def alert_pagerduty(metric, datapoint, ensemble, args, settings):
    import pygerduty
    pager = pygerduty.PagerDuty(settings['subdomain'], settings['auth_token'])
    pager.trigger_incident(settings['key'], "Anomalous metric: {0} (datapoint: {1})".format(metric, datapoint))


def alert_hipchat(metric, datapoint, ensemble, args, settings):
    import hipchat
    hipster = hipchat.HipChat(token=settings['auth_token'])
    rooms = kwargs['rooms']
    link = ""  # settings.GRAPH_URL.format(metric)
    for room in rooms:
        hipster.method('rooms/message', method='POST', parameters={'room_id': room,
                                                                   'from': 'Skyline',
                                                                   'color': 'red',
                                                                   'message': 'Anomalous metric: {0} (datapoint: {1})<br><a href="{2}"><img src="{2}"/></a>'.format(metric, datapoint, link)})


def alert_stdout(metric, datapoint, ensemble, args, settings):
    logger.warning("metric={0} datapoint={1}".format(metric, datapoint))


def alert_syslog(metric, datapoint, ensemble, args, settings):
    import syslog
    syslog.openlog("skyline", syslog.LOG_PID, syslog.LOG_LOCAL4)
    syslog.syslog(syslog.LOG_LOCAL4, str("metric={0} datapoint={1}".format(metric, datapoint)))

@@ -491,18 +491,57 @@
  value)%0A
-kw
+ensemble: the ensemble result dictionary%0A
 args: al
@@ -1078,26 +1078,24 @@
 ecipients = 
-kw
 args%5B'recipi
@@ -1354,16 +1354,18 @@
 %0A       
+ #
  body = 
@@ -1475,16 +1475,98 @@
 , link)%0A
+        body = 'Anomalous metric: %7B0%7D (datapoint: %7B1%7D)'.format(metric, datapoint)%0A
         
@@ -2116,10 +2116,8 @@
 s = 
-kw
 args
@@ -2173,24 +2173,226 @@
 mat(metric)%0A
+    # body = 'Anomalous metric: %7B0%7D (datapoint: %7B1%7D)%3Cbr%3E%3Ca href=%22%7B2%7D%22%3E%3Cimg src=%22%7B2%7D%22/%3E%3C/a%3E'.format(metric, datapoint, link)%0A    body = 'Anomalous metric: %7B0%7D (datapoint: %7B1%7D)'.format(metric, datapoint)%0A
     for room
@@ -2651,14 +2651,37 @@
 or':
+ settings.get('color',
  'red'
+)
 ,%0A  
@@ -2760,118 +2760,12 @@
 e': 
-'Anomalous metric: %7B0%7D (datapoint: %7B1%7D)%3Cbr%3E%3Ca href=%22%7B2%7D%22%3E%3Cimg src=%22%7B2%7D%22/%3E%3C/a%3E'.format(metric, datapoint, link)
+body
 %7D)%0A%0A

use correct alert arguments