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": '[email protected]',
"monitor_email": '[email protected]',
"monitor_password": 'password',
# Leave as it is to use gmail as the server
"monitor_server": '[email protected]',
"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?
-
1What do you mean with combin ? Cant you gust copy and paste the code?buhtz– buhtz2018年04月22日 07:38:15 +00:00Commented Apr 22, 2018 at 7:38
-
This question already has an answer at stackoverflow.com/a/16604453caylee– caylee2018年04月22日 07:39:51 +00:00Commented Apr 22, 2018 at 7:39
-
when i put recipient = [email protected] its not working , I want to pass values directly.Maximus– Maximus2018年04月22日 07:44:38 +00:00Commented Apr 22, 2018 at 7:44
2 Answers 2
You need to copy the entire file since "recipient_email": '[email protected]' is not valid variable definition.
Replace from conf import settings, sites with the file contents, or better, define the variables as you want them.
For example, rather than
sender = settings["monitor_email"]
recipient = settings["recipient_email"]
subject = settings["email_subject"]
Do
sender = "[email protected]"
recipient = "[email protected]"
subject = 'Server Monitor Alert'
# TODO: Define other values
when i put recipient = [email protected] its not working
You need quotes around string variables...
5 Comments
settings. In fact, delete settings variable entirely.TODO... You must define the other values, or in-line them. smtplib.SMTP('[email protected]', ...python ping.pyCopy and paste the complete content of conf.py into ping.py just after the
import datetime
line. Then remove the line
from conf import settings, sites
from ping.py.
Note that doing so is usually quite the opposite of what you should be doing in terms of good coding style. Generally, you want to modularize your code, while this means taking (more or less) modular code and turning it into a big and unwieldy clump.