Created
June 25, 2013 17:15
-
-
Save cluther/5860350 to your computer and use it in GitHub Desktop.
Add datapoint_count attribute to devices.
This file contains hidden or 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
| 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