runreport.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 reports to be run
  13. reports = get_reports()
  14. # Run reports
  15. for module_name, report in reports:
  16. for report_name, report_cls in report:
  17. report_name_full = '{}.{}'.format(module_name, report_name)
  18. if module_name in options['reports'] or report_name_full in options['reports']:
  19. # Run the report
  20. self.stdout.write(
  21. "[{:%H:%M:%S}] Running {}.{}...".format(timezone.now(), module_name, report_name)
  22. )
  23. report = report_cls()
  24. results = report.run()
  25. # Report on success/failure
  26. status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS')
  27. for test_name, attrs in results.items():
  28. self.stdout.write(
  29. "\t{}: {} success, {} info, {} warning, {} failed".format(
  30. test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failed']
  31. )
  32. )
  33. self.stdout.write(
  34. "[{:%H:%M:%S}] {}.{}: {}".format(timezone.now(), module_name, report_name, status)
  35. )
  36. # Wrap things up
  37. self.stdout.write(
  38. "[{:%H:%M:%S}] Finished".format(timezone.now())
  39. )