#!/usr/bin/python
#coding=utf-8

import time
import smtplib
import urllib2

monitor_url="http://192.168.71.45:50090/"

monitor_time=time.ctime()
message = """From: monitor <monitor@taomee.com>
To: related persons
MIME-Version: 1.0
Content-type: text/html
Subject: monitor secondary namenode

<h1>monitor mail for hadoop secondary namenode</h1>
<br/>
cannot get info
<br/>
monitor url: %s
<br/>
monitor time: %s
""" %(monitor_url, monitor_time)

sender = 'itl_alarm@taomee.com'
receivers = ['lc@taomee.com']
def sendaltermail():
    try:
        smtpObj = smtplib.SMTP('mail.taomee.com')
        smtpObj.sendmail(sender, receivers, message)
        print "Successfully sent email"
    except SMTPException:
        print "Error: unable to send email"

#-------------- main logic --------------
try:
    cc = urllib2.urlopen(monitor_url)
    line = cc.readlines()
    if len(line) <= 0:
        sendaltermail()
        print "[%s] error occur: %s" %(monitor_time, message)
    else:
        print "[%s] all ok" %(monitor_time)
except urllib2.URLError,e:
    sendaltermail()
    print "error: %r" %e
