XenAPIでライセンスの期限を調べて、期限が近づいてたらメールで通知する。
コマンドラインだと、このコマンド。
xe host-license-view
メールは、普段ならPostfixを入れるけど、XenServerのホストマシンには殆ど何も入れられないから、元々入ってるPythonで対応。
これを作ってからsSMTPというものを知った。
どうやら/etc/ssmtp/ssmtp.confでSMTPサーバを指定できたらしい…。
check_license_expiry.py
とりあえず、ライセンス期限の60日前になったらメールを送るようにする。
#!/usr/bin/env python # set encoding=utf-8 import time, datetime import XenAPI, Mail def getExpiry(): # login session = XenAPI.getSession() # get host host = hosts = session.xenapi.host.get_all()[0] # get expiry record = session.xenapi.host.get_record(host) hostname = record.get('hostname'); license = record.get('license_params'); expiry = license.get('expiry') # validy term t_expiry = time.strptime(expiry, '%Y%m%dT%H:%M:%SZ'); d_expiry = datetime.date(t_expiry[0], t_expiry[1], t_expiry[2]) d_today = datetime.date.today() limit = (d_expiry - d_today).days # logout session.xenapi.session.logout() return (hostname, d_expiry, limit) ### application start if __name__ == "__main__": (hostname, limit_date, limit_count) = getExpiry() if limit_count<=60: sub = 'XenServer license expires on the %s' % limit_date body = 'ライセンスが切れるまであと%d日。\n\nライセンス終了: %s\nホスト名: %s' % (limit_count, limit_date, hostname) Mail.send(sub, body)
Mail.py
LAN内のSMTPサーバにリレーさせる場合
自宅サーバでは、OP25Bの制限があるため、LAN内のSMTPサーバ(Postfix)を経由させる。
ログ用にしか使わないだろうから、FromもToも固定にしといた。
#!/usr/bin/env python import smtplib from email.MIMEText import MIMEText from email.Header import Header from email.Utils import formatdate # mail config smtp_server = '192.168.xxx.xxx:25' from_addr = 'xxx@xxx' to_addr = 'xxx@xxx' # send def send(subject, body): msg = MIMEText(body) msg['Subject'] = Header(subject, 'UTF-8') msg['From'] = from_addr msg['To'] = to_addr msg['Date'] = formatdate() s = smtplib.SMTP(smtp_server) s.sendmail(from_addr, [to_addr], msg.as_string()) s.close()
GmailのSMTPサーバを使って送信する場合
from_addrに、ログイン用のメールアドレス。
passwordに、from_addrのパスワード。
Fromを書き換える事は出来なかった。
#!/usr/bin/env python import smtplib from email.MIMEText import MIMEText from email.Header import Header from email.Utils import formatdate # config smtp_server = 'smtp.gmail.com:587' from_addr = 'xxx@xxx' to_addr = 'xxx@xxx' password = 'xxx' # send def send(subject, body): msg = MIMEText(body) msg['Subject'] = Header(subject, 'UTF-8') msg['From'] = from_addr msg['To'] = to_addr msg['Date'] = formatdate() s = smtplib.SMTP(smtp_server) s.ehlo() s.starttls() s.ehlo() s.login(from_addr, password) s.sendmail(from_addr, [to_addr], msg.as_string()) s.close()
XenAPI.py
Python2.4でも使えるように修正。
修正前。
xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, verbose, allow_none)
修正後。
xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, verbose)
いちいち認証部分を書くのが面倒だから、getSession()を追加する。
def getSession(host='http://127.0.0.1/', username='xxx', password='xxx'): session = Session(host) session.xenapi.login_with_password(username, password) return session
/etc/cron.d/check_license_expiry
/root/cronに置いてる前提。
01 04 * * * root /root/cron/check_license_expiry.py
参考URL
ウノウラボ Unoh Labs: Pythonでメールを送信したい人のためのサンプル集
http://labs.unoh.net/2007/06/python_2.html