2017-09-19 17:47:42 -04:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-09-19 17:47:42 -04:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
from extras.models import ReportResult
|
|
|
|
from extras.reports import get_reports
|
2017-09-19 17:47:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Run a report to validate data in NetBox"
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('reports', nargs='+', help="Report(s) to run")
|
|
|
|
# parser.add_argument('--verbose', action='store_true', default=False, help="Print all logs")
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
# Gather all reports to be run
|
2017-09-21 16:32:05 -04:00
|
|
|
reports = get_reports()
|
2017-09-19 17:47:42 -04:00
|
|
|
|
|
|
|
# Run reports
|
2017-09-21 16:32:05 -04:00
|
|
|
for module_name, report in reports:
|
|
|
|
for report_name, report_cls in report:
|
|
|
|
report_name_full = '{}.{}'.format(module_name, report_name)
|
|
|
|
if module_name in options['reports'] or report_name_full in options['reports']:
|
|
|
|
|
|
|
|
# Run the report
|
|
|
|
self.stdout.write(
|
|
|
|
"[{:%H:%M:%S}] Running {}.{}...".format(timezone.now(), module_name, report_name)
|
|
|
|
)
|
|
|
|
report = report_cls()
|
2017-09-25 17:27:58 -04:00
|
|
|
result = report.run()
|
2017-09-21 16:32:05 -04:00
|
|
|
|
2017-09-22 12:11:10 -04:00
|
|
|
# Record the results
|
|
|
|
ReportResult.objects.filter(report=report_name_full).delete()
|
2017-09-25 17:27:58 -04:00
|
|
|
ReportResult(report=report_name_full, failed=report.failed, data=result).save()
|
2017-09-22 12:11:10 -04:00
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
# Report on success/failure
|
|
|
|
status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS')
|
2017-09-25 17:27:58 -04:00
|
|
|
for test_name, attrs in result.items():
|
2017-09-21 16:32:05 -04:00
|
|
|
self.stdout.write(
|
|
|
|
"\t{}: {} success, {} info, {} warning, {} failed".format(
|
|
|
|
test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failed']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.stdout.write(
|
|
|
|
"[{:%H:%M:%S}] {}.{}: {}".format(timezone.now(), module_name, report_name, status)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Wrap things up
|
|
|
|
self.stdout.write(
|
|
|
|
"[{:%H:%M:%S}] Finished".format(timezone.now())
|
|
|
|
)
|