Весь код ниже.
Сама функция
def mail(node, count): import smtplib from email.mime.text import MIMEText me = 'email' you = 'email' smtp_server = 'mailhost.lol.ru' msg = MIMEText( node , count) msg['Subject'] = 'Alarm Node' msg['From'] = me msg['To'] = you s = smtplib.SMTP(smtp_server) s.sendmail(me, [you], msg.as_string()) s.quit()
Весь код
#!/usr/bin/python # -*- coding: utf-8 -*- import sys #import subprocess import datetime import os import socket def mail(node, count): import smtplib from email.mime.text import MIMEText me = 'email' you = 'email' smtp_server = 'mailhost.lol.ru' msg = MIMEText( node , count) msg['Subject'] = 'Alarm Node' msg['From'] = me msg['To'] = you s = smtplib.SMTP(smtp_server) s.sendmail(me, [you], msg.as_string()) s.quit() CountNodeOK = 0 CountWarning = 0 nodeOK = '' nodeWarning = '' CountCritical = 0 nodeCritical = '' index = 0 StrCut = '' end = 0 CountOverloaded = 0 nodeOverloaded = '' Time = datetime.datetime.now() HostName = socket.gethostname() FileName = HostName[:12]+("_")+Time.strftime("%d.%m.%Y_%H:%M") testpython = open(FileName, 'a') for x in open(FileName, 'r').readlines(): #Откры if x.find('OK') > 0: CountNodeOK += 1 index = x.find(":") + 1 StrCut = x[index:] index = StrCut.find(":") + 1 StrCut = StrCut[index:] index = StrCut.find(":") + 1 end = StrCut.rfind(",") StrCut = StrCut[index:end] nodeOK = nodeOK + StrCut + "\n" if x.find('Warning') > 0: CountnodeWarning += 1 index = x.find(":") + 1 StrCut = x[index:] index = StrCut.find(":") + 1 StrCut = StrCut[index:] index = StrCut.find(":") + 1 StrCut = StrCut[index:] end = StrCut.rfind(",") StrCut = StrCut[index:end] nodeWarning = nodeWarning + StrCut + "\n" if x.find('Critical') > 0: CountCritical += 1 index = x.find(":") + 1 StrCut = x[index:] index = StrCut.find(":") + 1 StrCut = StrCut[index:] index = StrCut.find(":") + 1 StrCut = StrCut[index:] end = StrCut.rfind(",") StrCut = StrCut[index:end] nodeCritical = nodeCritical + StrCut + "\n" if x.find('Overloaded') > 0: CountOverloaded += 1 index = x.find(":") + 1 StrCut = x[index:] index = StrCut.find(":") + 1 StrCut = StrCut[index:] index = StrCut.find(":") + 1 end = StrCut.rfind(",") StrCut = StrCut[index:end] nodeOverloaded = nodeOverloaded + StrCut + "\n" print >> testpython, 'Скрипт был вызван в:' , Time.strftime("%d. %m. %Y %H: %M %p" + "\n") print >> testpython, 'Ноды в ок:' , nodeOK, 'всего' , CountNodeOK print >> testpython, 'Ноды в варнинге:' , nodeWarning , 'всего' , CountWarning print >> testpython, 'Ноды в критикл:' , nodeCritical, ' всего' , CountCritical print >> testpython, 'Ноды в оверлоад:', nodeOverloaded, 'всего' , CountOverloaded #if CountnWarning > 3: #p = subprocess.Popen(['./test.sh', '1'], stdout=subprocess.PIPE) #line = p.stdout.readline() #if CountCritical > 1: # p = subprocess.Popen(['./test.sh', '1'], stdout=subprocess.PIPE) # line = p.stdout.readline() #if CountOverloaded > 1: #p = subprocess.Popen(['./test.sh', '1'], stdout=subprocess.PIPE) #line = p.stdout.readline() #else: #os.remove(FileName) mail(str(CountNodeOK),nodeOK)