-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collect Juniper dom threshold values #2513
Draft
stveit
wants to merge
24
commits into
Uninett:master
Choose a base branch
from
stveit:feature.juniper-dom-threshold-values
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
22aa26d
Add metric path for thresholds
stveit 2324494
Add function for getting thresholds in juniper mib
stveit e2e869b
Collect threshold values
stveit ebc385d
Give thresholds generic nameing
stveit 2d6f307
Add threshold properties
stveit b4f2cb8
Add related sensor for threshold sensors
stveit 867376e
Update SQL tables
stveit ed62f06
Revert "Collect threshold values"
stveit fd078b6
Change field to refer to oid of other sensor
stveit 9c8a55f
Add property for accessing related sensor
stveit 5eba3e0
Collect threshold values as sensors
stveit d21aeb9
Access items correctly
stveit 11a5613
Do not access items
stveit ebcec4f
fixup! Change field to refer to oid of other sensor
stveit 7a93cea
fixup! Add property for accessing related sensor
stveit f6088cd
Revert "Add metric path for thresholds"
stveit f768bdf
Add property for getting relevant thresholds
stveit 2a2ce7e
Add test file for juniper dom mib
stveit 1ec98ce
Add tests for creating sensor dicts
stveit a58dd65
Allow field to be null
stveit 0d5f2b6
Add tests for sensor model changes
stveit 4fd5cf5
fixup! Add tests for sensor model changes
stveit ab3d427
Make test work with existing stuff in database
stveit a9a6080
Reference sensor for oid
stveit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,16 @@ | |
from nav.ipdevpoll import db | ||
from nav.ipdevpoll.db import run_in_thread | ||
from nav.metrics.carbon import send_metrics | ||
from nav.metrics.templates import metric_path_for_sensor | ||
from nav.metrics.templates import metric_path_for_sensor, metric_path_for_threshold | ||
from nav.models.manage import Sensor | ||
from nav.mibs.juniper_dom_mib import JuniperDomMib | ||
from nav.enterprise.ids import VENDOR_ID_JUNIPER_NETWORKS_INC | ||
|
||
# Ask for no more than this number of values in a single SNMP GET operation | ||
MAX_SENSORS_PER_REQUEST = 5 | ||
|
||
THRESHOLD_MIBS = {VENDOR_ID_JUNIPER_NETWORKS_INC: [JuniperDomMib]} | ||
|
||
|
||
class StatSensors(Plugin): | ||
"""Collects measurement values from registered sensors and pushes to | ||
|
@@ -59,6 +63,11 @@ def handle(self): | |
defer.returnValue(None) | ||
netboxes = yield db.run_in_thread(self._get_netbox_list) | ||
sensors = yield run_in_thread(self._get_sensors) | ||
yield self._handle_sensor_stats(sensors, netboxes) | ||
yield self._handle_thresholds(sensors, netboxes) | ||
|
||
@defer.inlineCallbacks | ||
def _handle_sensor_stats(self, sensors, netboxes): | ||
self._logger.debug("retrieving data from %d sensors", len(sensors)) | ||
oids = list(sensors.keys()) | ||
requests = [ | ||
|
@@ -73,6 +82,8 @@ def handle(self): | |
|
||
def _get_sensors(self): | ||
sensors = Sensor.objects.filter(netbox=self.netbox.id).values() | ||
for sensor in sensors: | ||
self._logger.info(f"Internal names: {sensor.get('internal_name', None)}") | ||
return dict((row['oid'], row) for row in sensors) | ||
|
||
def _response_to_metrics(self, result, sensors, netboxes): | ||
|
@@ -98,6 +109,52 @@ def _response_to_metrics(self, result, sensors, netboxes): | |
send_metrics(metrics) | ||
return metrics | ||
|
||
@defer.inlineCallbacks | ||
def _handle_thresholds(self, sensors, netboxes): | ||
metrics = yield self._collect_thresholds(netboxes, sensors) | ||
self._logger.info(f"Metrics: {metrics}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about debug logging and argument expansion applies here. |
||
if metrics: | ||
send_metrics(metrics) | ||
|
||
@defer.inlineCallbacks | ||
def _collect_thresholds(self, netboxes, sensors): | ||
for mib in self._mibs_for_me(THRESHOLD_MIBS): | ||
try: | ||
metrics = yield self._collect_thresholds_from_mib( | ||
mib, netboxes, sensors | ||
) | ||
except (TimeoutError, defer.TimeoutError): | ||
self._logger.debug( | ||
"collect_thresholds: ignoring timeout in %s", mib.mib['moduleName'] | ||
) | ||
else: | ||
if metrics: | ||
defer.returnValue(metrics) | ||
defer.returnValue([]) | ||
|
||
@defer.inlineCallbacks | ||
def _collect_thresholds_from_mib(self, mib, netboxes, sensors): | ||
metrics = [] | ||
timestamp = time.time() | ||
thresholds = yield mib.get_all_thresholds() | ||
self._logger.debug(f"Threshold: {thresholds}") | ||
for threshold in thresholds: | ||
sensor = sensors[threshold['sensor_oid']] | ||
value = self.convert_to_precision(threshold['value'], sensor) | ||
for netbox in netboxes: | ||
path = metric_path_for_threshold( | ||
netbox, sensor['internal_name'], threshold['name'] | ||
) | ||
# thresholds will have the same precision etc. as its related sensor | ||
metrics.append((path, (timestamp, value))) | ||
return metrics | ||
|
||
def _mibs_for_me(self, mib_class_dict): | ||
vendor = self.netbox.type.get_enterprise_id() if self.netbox.type else None | ||
mib_classes = mib_class_dict.get(vendor, None) or mib_class_dict.get(None, []) | ||
for mib_class in mib_classes: | ||
yield mib_class(self.agent) | ||
|
||
|
||
def convert_to_precision(value, sensor): | ||
"""Moves the decimal point of a value according to the precision defined | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None
when using.get()
on a dict -None
is already the default fallback value.