Last active
August 29, 2015 14:13
-
-
Save cluther/190a028458b94a26d449 to your computer and use it in GitHub Desktop.
change-datasource-interval
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
#!/usr/bin/env python | |
# | |
# This script allows a single datasource's cycletime (interval) to be | |
# changed. | |
# | |
# Example usage: | |
# change-cpu-interval --device=web-server-a --template=Device --datasource=cpu --interval=10 | |
import sys | |
import Globals | |
from ZODB.transact import transact | |
from Products.ZenUtils.ZenScriptBase import ZenScriptBase | |
class IntervalChanger(ZenScriptBase): | |
def __init__(self): | |
super(IntervalChanger, self).__init__(connect=True) | |
def buildOptions(self): | |
super(IntervalChanger, self).buildOptions() | |
self.parser.add_option( | |
'-d', '--device', | |
dest='device', | |
help='Device ID') | |
self.parser.add_option( | |
'-t', '--template', | |
dest='template', | |
help='Monitoring template name') | |
self.parser.add_option( | |
'--datasource', | |
dest='datasource', | |
help='Datasource name') | |
self.parser.add_option( | |
'-i', '--interval', | |
type='int', | |
dest='interval', | |
help='New interval (in seconds)') | |
def validate_options(self): | |
if not self.options.device: | |
sys.exit("You must specify the --device option.") | |
if not self.options.template: | |
sys.exit("You must specify the --template option.") | |
if not self.options.datasource: | |
sys.exit("You must specify the --datasource option.") | |
if not self.options.interval: | |
sys.exit("You must specify the --interval option.") | |
@transact | |
def run(self): | |
self.validate_options() | |
device = self.dmd.Devices.findDevice(self.options.device) | |
if not device: | |
sys.exit("No {!r} device found".format(self.options.device)) | |
template = device.getRRDTemplateByName(self.options.template) | |
if not template: | |
sys.exit("No {!r} template found.".format(self.options.template)) | |
# Must make a local copy of the template if it isn't already local. | |
if not device.isLocalName(self.options.template): | |
device.makeLocalRRDTemplate(self.options.template) | |
template = device.getRRDTemplateByName(self.options.template) | |
datasource = template.datasources._getOb(self.options.datasource, None) | |
if not datasource: | |
sys.exit("No {!r} datasource found.".format(self.options.datasource)) | |
# Make sure we match the type of the existing cycletime property. | |
dstype = type(datasource.cycletime) | |
interval = dstype(self.options.interval) | |
if datasource.cycletime != interval: | |
datasource.cycletime = interval | |
if __name__ == '__main__': | |
changer = IntervalChanger() | |
changer.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment