NOTE: In all the examples below, indentation is three spaces per level. Python is indentation-sensitive, so be sure to match the indentation exactly as it is in the examples.
In the code below, code in green is optional. It just provides visual feedback to explain what the program is doing.
Any of the instructions below can be converted to "turn on monitoring" by changing
interface.monitor = False
to
interface.monitor = True
SSH into your Zenoss system as user zenoss
At the command prompt enter:
[zenoss@zenoss ~]$ zendmd
Now you will be at the zendmd prompt. Enter the following:
>>> device = dmd.Devices.findDevice( '[name of your device]' )
>>> for interface in device.os.interfaces():
... if interface.isLockedFromUpdates():
... print interface.interfaceName + ' is locked from updates, skipping'
... else:
... interface.monitor = False
... print interface.interfaceName + ' monitoring set to False'
...
[ code will execute here...]
>>> commit()
(^d to exit)
SSH into your Zenoss system as user zenoss
At the command prompt enter:
[zenoss@zenoss ~]$ zendmd
Now you will be at the zendmd prompt. Enter the following:
>>> for device in dmd.Devices.[device class path].devices():
... print device.getId() + ":"
... for interface in device.os.interfaces():
... if interface.isLockedFromUpdates():
... print interface.interfaceName + ' is locked from updates, skipping'
... else:
... interface.monitor = False
... print interface.interfaceName + ' monitoring set to False'
...
[ code will execute here...]
>>> commit()
(^d to exit)
Example:
If you wanted to turn off monitoring on all the interfaces on all the devices in the /Devices/Network/Switch device class, the first line of code would be:
>>> for device in dmd.Devices.Network.Switch.devices():
In this case, we define a python function and then execute the function with a specific set of parameters
SSH into your Zenoss system as user zenoss
At the command prompt enter:
[zenoss@zenoss ~]$ zendmd
Now you will be at the zendmd prompt. Enter the following:
>>> def setMonitor(deviceClass, path):
... print path
... for device in deviceClass.devices():
... print ' ' + device.getId() + ':'
... for interface in device.os.interfaces():
... if interface.isLockedFromUpdates():
... print interface.interfaceName + ' is locked from updates, skipping'
... else:
... interface.monitor = False
... print interface.interfaceName + ' monitoring set to False'
... for item in deviceClass.__dict__:
... if type(deviceClass.__dict__[item]).__name__ == \
... 'DeviceClass' and item != '__primary_parent__':
... setMonitor(deviceClass.__dict__[item], path + '/' + item)
...
>>> setMonitor(dmd.Devices.[device class path], '/Devices/[path name]')
[ code will execute here...]
>>> commit()
(^d to exit)
Example:
If you wanted to turn off monitoring on all the interfaces on all the devices and sub-devices in the /Devices/Network/Switch device class, the last line of code would be:
>>> setMonitor(dmd.Devices.Network.Switch, '/Devices/Network/Switch')
Although it takes a bit more work, and is a bit complicated, this method is much more convenient to use. With this method, you will have user commands available from the drop down menu whenever you want. For convenience, I've attached the python script below.
Start by creating a python script in the $ZENHOME/bin directory. I named the file user.InterfaceMonitor.py. Enter the following into the file using your favorite text editor:
#!/usr/bin/env python
# This script is designed to be executed from the command line
# with the following syntax:
# user.InterfaceMonitor.py '(device name)' (ON | OFF)
# Example:
# user.InterfaceMonitor.py 'Cisco Switch 1' OFF
import Globals
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit
import sys
def showHelp():
print '\nSyntax:'
print ' user.InterfaceMonitor.py \'(device name)\' (ON | OFF)\n'
print 'Example:'
print ' user.InterfaceMonitor.py 'Cisco Switch 1' OFF\n'
sys.exit()
if len(sys.argv) != 3:
print '\n*** Invalid Syntax ***'
showHelp()
dmd = ZenScriptBase(connect=True).dmd
deviceName = sys.argv[1]
device = dmd.Devices.findDevice(deviceName)
if device is None:
print '\nDevice not found in dmd.\n'
sys.exit()
commandState = sys.argv[2].upper()
if commandState == 'ON':
monitorState = True
elif commandState == 'OFF':
monitorState = False
else:
print '\n*** Invalid Syntax ***'
showHelp()
for interface in device.os.interfaces():
if interface.isLockedFromUpdates():
print interface.interfaceName + ' is locked from updates, skipping'
else:
interface.monitor = monitorState
print interface.interfaceName + ' monitoring set to ' \
+ commandState
commit()
Make the file executable using the command:
[zenoss@zenoss bin] chmod u+x user.InterfaceMonitor.py
Once the file is in place, you must create the user commands. You must decide which device class to put them in. I put mine in /Devices/Network but there is no reason you can't put them in /Devices. The commands will be available for any device or sub-device under the device class where you put them. For instance, because I put mine in /Devices/Network, the commands will be available when I am viewing the device /Devices/Network/Switch/Cisco Switch 1. They will not be available from /Devices/Power.
To turn on/off monitoring the interfaces on just one device:
To turn on/off monitoring the interfaces on every device in a device class:
There are no comments on this document