1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

49 lines
1.8 KiB
Python
Raw Normal View History

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):
2017-09-28 12:50:52 -04:00
# Gather all available reports
2017-09-21 16:32:05 -04:00
reports = get_reports()
2017-09-19 17:47:42 -04:00
# Run reports
2017-09-28 12:50:52 -04:00
for module_name, report_list in reports:
for report in report_list:
if module_name in options['reports'] or report.full_namel in options['reports']:
2017-09-21 16:32:05 -04:00
2017-09-28 12:50:52 -04:00
# Run the report and create a new ReportResult
2017-09-21 16:32:05 -04:00
self.stdout.write(
2017-09-28 12:50:52 -04:00
"[{:%H:%M:%S}] Running {}...".format(timezone.now(), report.full_name)
2017-09-21 16:32:05 -04:00
)
2017-09-28 12:50:52 -04:00
report.run()
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-28 12:50:52 -04:00
for test_name, attrs in report.result.data.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(
2017-09-28 12:50:52 -04:00
"[{:%H:%M:%S}] {}: {}".format(timezone.now(), report.full_name, status)
2017-09-21 16:32:05 -04:00
)
# Wrap things up
self.stdout.write(
"[{:%H:%M:%S}] Finished".format(timezone.now())
)