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

69 lines
2.7 KiB
Python
Raw Normal View History

import time
from django.contrib.contenttypes.models import ContentType
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
from core.choices import JobStatusChoices
from core.models import Job
from extras.models import ReportModule
from extras.reports import run_report
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")
def handle(self, *args, **options):
for module in ReportModule.objects.all():
for report in module.reports.values():
if module.name in options['reports'] or report.full_name in options['reports']:
2017-09-21 16:32:05 -04:00
# Run the report and create a new Job
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
)
report_content_type = ContentType.objects.get(app_label='extras', model='report')
job = Job.enqueue_job(
run_report,
report.full_name,
report_content_type,
None,
job_timeout=report.job_timeout
)
# Wait on the job to finish
while job.status not in JobStatusChoices.TERMINAL_STATE_CHOICES:
time.sleep(1)
job = Job.objects.get(pk=job.pk)
2017-09-21 16:32:05 -04:00
# Report on success/failure
if job.status == JobStatusChoices.STATUS_FAILED:
status = self.style.ERROR('FAILED')
elif job == JobStatusChoices.STATUS_ERRORED:
status = self.style.ERROR('ERRORED')
else:
status = self.style.SUCCESS('SUCCESS')
for test_name, attrs in job.data.items():
2017-09-21 16:32:05 -04:00
self.stdout.write(
2017-09-28 14:57:54 -04:00
"\t{}: {} success, {} info, {} warning, {} failure".format(
test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failure']
2017-09-21 16:32:05 -04:00
)
)
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
)
self.stdout.write(
"[{:%H:%M:%S}] {}: Duration {}".format(timezone.now(), report.full_name, job.duration)
)
2017-09-21 16:32:05 -04:00
# Wrap things up
self.stdout.write(
"[{:%H:%M:%S}] Finished".format(timezone.now())
)