up_checker.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/python3
  2. """
  3. smells like updog in here
  4. generates an email for sendmail to notify me if all my websites are up,
  5. or if one or more of them is down. "up" means http response is 200.
  6. ./up_checker.py | sendmail user@email.com
  7. authored:
  8. Jun 15 2022; spokane valley, wa
  9. jordyn
  10. """
  11. from collections import namedtuple
  12. import time
  13. import requests
  14. Status = namedtuple('Status', ['website', 'status'])
  15. websites = [
  16. "https://queerscompute.gay/",
  17. "https://jordandashel.com/",
  18. "https://acrossthecountryin90days.com/",
  19. "https://4-letters.net/"
  20. ]
  21. def website_is_up(website):
  22. return website.status == 200
  23. def website_status(website):
  24. try:
  25. response = requests.head(website)
  26. status = response.status_code
  27. return Status(website, status)
  28. except:
  29. return Status(website, None)
  30. statuses = [website_status(website) for website in websites]
  31. all_up = all([website_is_up(website) for website in statuses])
  32. report = ""
  33. for status in statuses:
  34. website_name = str.split(status.website, "://")[1]
  35. website_name = website_name[:-1]
  36. report += f'{website_name:.<40} {"up" if website_is_up(status) else "DOWN"}\n'
  37. email = f"""Subject: websites are {"up" if all_up else "DOWN"}
  38. From: robot.dashel@gmail.com
  39. Content-Type: text/html; charset="utf8"
  40. <html>
  41. <body>
  42. <pre>
  43. all up? {all_up}.
  44. reporting at {time.strftime("%A %B %d %Y, %X %Z")}
  45. from squidward at bikinib:
  46. {report}
  47. </pre>
  48. </body>
  49. </html>"""
  50. print(email)