runreport.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import unicode_literals
  2. from django.core.management.base import BaseCommand
  3. from django.utils import timezone
  4. from extras.models import ReportResult
  5. from extras.reports import get_reports
  6. class Command(BaseCommand):
  7. help = "Run a report to validate data in NetBox"
  8. def add_arguments(self, parser):
  9. parser.add_argument('reports', nargs='+', help="Report(s) to run")
  10. # parser.add_argument('--verbose', action='store_true', default=False, help="Print all logs")
  11. def handle(self, *args, **options):
  12. # Gather all available reports
  13. reports = get_reports()
  14. # Run reports
  15. for module_name, report_list in reports:
  16. for report in report_list:
  17. if module_name in options['reports'] or report.full_namel in options['reports']:
  18. # Run the report and create a new ReportResult
  19. self.stdout.write(
  20. "[{:%H:%M:%S}] Running {}...".format(timezone.now(), report.full_name)
  21. )
  22. report.run()
  23. # Report on success/failure
  24. status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS')
  25. for test_name, attrs in report.result.data.items():
  26. self.stdout.write(
  27. "\t{}: {} success, {} info, {} warning, {} failure".format(
  28. test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failure']
  29. )
  30. )
  31. self.stdout.write(
  32. "[{:%H:%M:%S}] {}: {}".format(timezone.now(), report.full_name, status)
  33. )
  34. # Wrap things up
  35. self.stdout.write(
  36. "[{:%H:%M:%S}] Finished".format(timezone.now())
  37. )