Skip to content

Commit

Permalink
Closes #18362: Create a system job for census reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jan 9, 2025
1 parent 4090afb commit dd61902
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
48 changes: 46 additions & 2 deletions netbox/core/jobs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import requests
import sys

from netbox.jobs import JobRunner
from django.conf import settings
from netbox.jobs import JobRunner, system_job
from netbox.search.backends import search_backend
from .choices import DataSourceStatusChoices
from .choices import DataSourceStatusChoices, JobIntervalChoices
from .exceptions import SyncError
from .models import DataSource

Expand Down Expand Up @@ -31,3 +34,44 @@ def run(self, *args, **kwargs):
if type(e) is SyncError:
logging.error(e)
raise e


@system_job(interval=JobIntervalChoices.INTERVAL_DAILY)
class SystemHousekeepingJob(JobRunner):
"""
Perform daily system housekeeping functions.
"""
class Meta:
name = "System Housekeeping"

def run(self, *args, **kwargs):
# Skip if running in development or test mode
if settings.DEBUG or 'test' in sys.argv:
return

# TODO: Migrate other housekeeping functions from the `housekeeping` management command.
self.send_census_report()

@staticmethod
def send_census_report():
"""
Send a census report (if enabled).
"""
# Skip if census reporting is disabled
if settings.ISOLATED_DEPLOYMENT or not settings.CENSUS_REPORTING_ENABLED:
return

census_data = {
'version': settings.RELEASE.full_version,
'python_version': sys.version.split()[0],
'deployment_id': settings.DEPLOYMENT_ID,
}
try:
requests.get(
url=settings.CENSUS_URL,
params=census_data,
timeout=3,
proxies=settings.HTTP_PROXIES
)
except requests.exceptions.RequestException:
pass
13 changes: 0 additions & 13 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import platform
import sys
import warnings
from urllib.parse import urlencode

import requests
from django.contrib.messages import constants as messages
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator
Expand Down Expand Up @@ -583,17 +581,6 @@ def _setting(name, default=None):
# Calculate a unique deployment ID from the secret key
DEPLOYMENT_ID = hashlib.sha256(SECRET_KEY.encode('utf-8')).hexdigest()[:16]
CENSUS_URL = 'https://census.netbox.oss.netboxlabs.com/api/v1/'
CENSUS_PARAMS = {
'version': RELEASE.full_version,
'python_version': sys.version.split()[0],
'deployment_id': DEPLOYMENT_ID,
}
if CENSUS_REPORTING_ENABLED and not ISOLATED_DEPLOYMENT and not DEBUG and 'test' not in sys.argv:
try:
# Report anonymous census data
requests.get(f'{CENSUS_URL}?{urlencode(CENSUS_PARAMS)}', timeout=3, proxies=HTTP_PROXIES)
except requests.exceptions.RequestException:
pass


#
Expand Down

0 comments on commit dd61902

Please sign in to comment.