Thanks MAX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import glob import datetime import shutil import os.path import subprocess import optparse import sys import smtplib class splitData: def checkTest(self): data = {} file = '/usr/local/nagios/var/status.dat' with open(file) as f: for line in f: if "host_name" in line: host_name = line.split('=')[1].strip() if "last_check" in line: last_check = line.split('=')[1].strip() data[host_name] = int(last_check) now = datetime.datetime.now() for k in data: timestamp = data[k] dt = datetime.datetime.fromtimestamp(timestamp) if now - dt > datetime.timedelta(minutes = 10): print "Host: {0}. Last check: {1}, which is {2} minutes ago".format(k, dt, (now - dt).total_seconds() / 60) To: To Person <[email protected]> Subject: 'Nagios Alert, Last Check Alert' """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email" splitData().checkTest() |