I found these codes from various websites: ping.py
and conf.py
. It's working fine. I need to combine these files into a single file.
ping.py
:
#!/usr/bin/env python
import smtplib
import pyping
from conf import settings, sites
import time
import datetime
"""Sends an e-mail to the specified recipient."""
sender = settings["monitor_email"]
recipient = settings["recipient_email"]
subject = settings["email_subject"]
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(settings["monitor_server"],
settings["monitor_server_port"])
session.ehlo()
session.login(settings["monitor_email"], settings["monitor_password"])
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
for site in sites:
checker = pyping.ping(site)
# The site status changed from it's last value, so send an email
if checker.ret_code == 0:
# The site is UP
body = "%s This Server is up %s" % (site, st)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
else:
# The site is Down
body = "%s This Server is down %s" % (site, st)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
conf.py
:
sites = (
"192.168.1.1",
"192.168.2.1",
"192.168.3.1",
)
settings = {
"recipient_email": 'tomail@domain.com',
"monitor_email": 'frommail@domain.com',
"monitor_password": 'password',
# Leave as it is to use gmail as the server
"monitor_server": 'frommail@domain.com',
"monitor_server_port": 587,
# Optional Settings
"email_subject": 'Server Monitor Alert'
}
How to integrate conf.py
into ping.py
so I can get output from running a single file?