You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  1. import sys
  2. import time
  3. from django.core.management.base import BaseCommand
  4. from django.utils import timezone
  5. from hc.api.models import Check
  6. from hc.lib.emails import send_status_notification
  7. def _log(message):
  8. sys.stdout.write(message)
  9. sys.stdout.flush()
  10. class Command(BaseCommand):
  11. help = 'Ensures triggers exist in database'
  12. def handle(self, *args, **options):
  13. while True:
  14. # Gone down?
  15. query = Check.objects
  16. query = query.filter(alert_after__lt=timezone.now())
  17. query = query.filter(enabled=True, status="up")
  18. for check in query:
  19. check.status = "down"
  20. check.save()
  21. _log("\nSending email about going down for %s\n" % check.code)
  22. send_status_notification(check)
  23. # Gone up?
  24. query = Check.objects
  25. query = query.filter(alert_after__gt=timezone.now())
  26. query = query.filter(enabled=True, status="down")
  27. for check in query:
  28. check.status = "up"
  29. check.save()
  30. _log("\nSending email about going up for %s\n" % check.code)
  31. send_status_notification(check)
  32. time.sleep(1)
  33. _log(".")