Skip to content

Instantly share code, notes, and snippets.

@cluther
Created June 25, 2013 17:15
Show Gist options
  • Select an option

  • Save cluther/5860350 to your computer and use it in GitHub Desktop.

Select an option

Save cluther/5860350 to your computer and use it in GitHub Desktop.
Add datapoint_count attribute to devices.
from Products.ZenUtils.Utils import monkeypatch
from ZODB.transact import transact
def get_datapoint_count(device):
'''
Return the total number of monitored datapoints on device.
'''
if not device.monitorDevice():
return 0
total = 0
for template in device.getRRDTemplates():
for datasource in template.datasources():
if datasource.enabled:
total += datasource.datapoints.countObjects()
for component in device.getMonitoredComponents():
for template in device.getRRDTemplates():
for datasource in template.datasources():
if datasource.enabled:
total += datasource.datapoints.countObjects()
return total
@transact
def update_datapoint_count(device, datapoint_count):
'''
Update device.datapoint_count in a retrying transaction.
'''
device.datapoint_count = datapoint_count
@monkeypatch('Products.ZenModel.Device.Device')
def setLastChange(self, value=None):
'''
Set the changed datetime for this device.
Patch: Update device's datapoint_count if necessary.
@param value: secs since the epoch, default is now
@type value: float
@permission: ZEN_CHANGE_DEVICE
'''
# original is injected by the @monkeypatch decorator.
original(self, value=value)
# This will only occur one the patched setSnmpLastCollection has run
# at least once, and when the device's model changes in some way.
if hasattr(self, 'datapoint_count'):
datapoint_count = get_datapoint_count(self)
if self.datapoint_count != datapoint_count:
update_datapoint_count(self, datapoint_count)
@monkeypatch('Products.ZenModel.Device.Device')
def setSnmpLastCollection(self, value=None):
'''
Set the last time snmp collection occurred.
Patch: Initialize and set device's datapoint_count if necessary.
@param value: secs since the epoch, default is now
@type value: float
@permission: ZEN_CHANGE_DEVICE
'''
# original is injected by the @monkeypatch decorator.
original(self, value=value)
# This should only occur once for devices that were added to Zenoss
# after this monkeypatch was installed.
if not hasattr(self, 'datapoint_count'):
update_datapoint_count(self, get_datapoint_count(self))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment