Last active
February 2, 2017 21:27
-
-
Save albertogaona/ed2c4573ce74abdc9fc8c8b415946550 to your computer and use it in GitHub Desktop.
Provides a set of effective tools to query Zenoss database ( ZeoDB ) using an active socket.
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
""" Zen.py | |
Provides a set of effective tools to query Zenoss | |
database ( ZeoDB ) using an active socket. | |
Through this class all device features can be | |
retreived | |
""" | |
#import pdb #-- Se utiliza para habilitar el python debuger en modo command line | |
import os | |
import sys | |
class Zen: | |
""" Keep it Simple """ | |
connection = None | |
device = None | |
debug = False | |
# Obtener el home de zenoss | |
ZENHOME = os.environ.get('ZENHOME', '/opt/zenoss') | |
if ZENHOME.endswith(os.sep): | |
ZENHOME = os.path.dirname(ZENHOME) | |
if debug: | |
print 'ZENHOME: %s' % os.environ.get('ZENHOME', 'Not set') | |
def getZenHome(self): | |
return self.ZENHOME | |
def getDataSources(self, device): | |
"""Returns a list of paths where data sources can be found, None if nothing available""" | |
if self.debug: | |
print "getDataSources( self, device ):" | |
print "getDataSources: device =", device | |
try: | |
import os | |
if self.debug: | |
print "getDataSources: importing os ..." | |
except IOError: | |
print "getDataSources: exception importing os !" | |
return None | |
list = os.listdir(device) | |
sources = [] | |
for any in list: | |
if self.debug: | |
print "detected: ", any | |
if os.path.isfile(device + "/" + any): | |
sources.append(device + "/" + any) | |
if self.debug: | |
print "appended: " + device + "/" + any | |
return sources | |
def getInferfaces(self, path): | |
"""Returns a list of existing interfaces for the specified device( path )""" | |
if self.debug: | |
print "getInterfaces( self, path ):" | |
print "getInterfaces: path =", path | |
if path: | |
pass | |
else: | |
if self.debug: | |
print "getInterfaces: path =", path | |
print "getInterfaces: first define any path to search for interfaces !" | |
return None | |
target = path + "/os/interfaces" #before looking up | |
if self.debug: | |
print "getInterfaces: reconsidering target =", target | |
try: | |
import os | |
if self.debug: | |
print "getInterfaces: importing os...." | |
except: | |
print "getInterfaces: exception importing os !" | |
return None | |
temp = os.listdir(target) | |
interfaces = [] | |
for any in temp: | |
interfaces.append(target + "/" + any) | |
if self.debug: | |
print "getInterfaces: getting ", any | |
return interfaces | |
def getDevices(self): | |
if self.debug: | |
print "zenGetDevices( self ):" | |
if self.connection: | |
if self.debug: | |
print "zenGetDevices: zen connection detected ... getting device list !" | |
return self.connection.dmd.Devices | |
if self.debug: | |
print "zenGetDevices: zen connection DOWN... trying to connect" | |
self.zenConnect() | |
if self.connection: | |
if self.debug: | |
print "zenGetDevices: zen connection achieved ..." | |
print "zenGetDevices: getting device list ...." | |
return self.connection.dmd.Devices | |
if self.debug: | |
print "zenGetDevices: zen connection COULD NOT be settled" | |
print "zenGetDevices: 2 tries have been made ... exiting " | |
print "zenGetDevices: check connection to ZeoDB first !" | |
self.connection = False | |
return None | |
def zenGetDevices(self): | |
"""Depending on the connection state returns the list of current Zenoss devices """ | |
if self.debug: | |
print "zenGetDevices( self ):" | |
if self.connection: | |
if self.debug: | |
print "zenGetDevices: zen connection detected ... getting device list !" | |
return self.connection.dmd.Devices.getSubDevices() | |
if self.debug: | |
print "zenGetDevices: zen connection DOWN... trying to connect" | |
self.zenConnect() | |
if self.connection: | |
if self.debug: | |
print "zenGetDevices: zen connection achieved ..." | |
print "zenGetDevices: getting device list ...." | |
return self.connection.dmd.Devices.getSubDevices() | |
if self.debug: | |
print "zenGetDevices: zen connection COULD NOT be settled" | |
print "zenGetDevices: 2 tries have been made ... exiting " | |
print "zenGetDevices: check connection to ZeoDB first !" | |
self.connection = False | |
return None | |
def zenGetFilteredDevices(self): | |
"""Returns a list of current devices as zenGetDevices but filteded""" | |
if self.debug: | |
print "zenGetFilteredDevices( self ):" | |
tempList = self.zenGetDevices() | |
finalList = [] | |
for any in tempList: | |
temp = str.replace(any.__str__(), "<Device at ", "") | |
finalList.append(str.replace(temp, ">", "")) | |
if self.debug: | |
print "zenGetFilteredDevices: appending %s" \ | |
% str.replace(temp, ">", "") | |
return finalList | |
def zenFind(self, thisDevice): | |
"""Finds and returns thisDevices if existent in db """ | |
if self.debug: | |
print "zenFind( self, thisDevice = %s)" % thisDevice | |
if self.connection: | |
self.device = self.connection.dmd.Devices.findDevice(thisDevice) | |
if self.device: | |
if self.debug: | |
print "zenFind: device %s found !" % self.device | |
return self.device | |
return None | |
def zenFindByPath(self, thisPath): | |
"""Returns an existing Zenoss Object regarding the given specified path otherwise None""" | |
if self.debug: | |
print "zenFindByPath(self, thisPath):" | |
print "zenFindByPath: thisPath = %s" % thisPath | |
if self.connection: | |
if self.debug: | |
print "zenFindByPath: connection to ZEO detected !" | |
print "zenFindByPath: TRYING to find object ......." | |
try: | |
target = self.connection.dmd.Devices.getObjByPath(thisPath) | |
if target: | |
if self.debug: | |
print "zenFindByPath: found object = ", target | |
return target | |
except KeyError: | |
print "zenFindByPath: exception trying to find object =", thisPath | |
print "zenFindByPath: possible path error " | |
return None | |
if self.debug: | |
print "zenFindByPath: No connection to ZEO detected ....... connecting !" | |
self.zenConnect() | |
if self.connection: | |
if self.debug: | |
print "zenFindByPath: connection reached !" | |
try: | |
target = self.connection.dmd.Devices.getObjByPath(thisPath) | |
if target: | |
if self.debug: | |
print "zenFindByPath: found object =", target | |
return target | |
except KeyError: | |
print "zenFindByPath: exception trying to find object =", thisPath | |
return None | |
if self.debug: | |
print "zenFindByPath: no Zeo connection reached..... " | |
print "zenFindByPath: check Zeo connection first " | |
return None | |
def zenConnect(self): | |
""" tries to connect to the current Zenoss database """ | |
#pdb.set_trace() | |
if self.debug: | |
print "zenConnect( self ):" | |
print "zenConnect: connection =", self.connection | |
if self.connection: | |
if self.debug: | |
print "zenConnect: self.connection exists, wont' be created again !" | |
return None | |
try: | |
import sys | |
if self.debug: | |
print "zenConnect: importing sys" | |
except IOError: | |
print "zenConnect: IO Exception trying to import sys" | |
return None | |
## Agregar al path la ruta de las bibliotecas de zenoss y zope | |
#sys.path.append('/opt/zenoss/lib/python') | |
#sys.path.append('/opt/zenoss/Products/ZenUtils') | |
## Agregar al path la ruta de las bibliotecas de zenoss y zope | |
#sys.path.append(os.path.join(self.getZenHome(), 'lib/python')) | |
#sys.path.append(os.path.join(self.getZenHome(), 'Products/ZenUtils')) | |
try: | |
import Globals | |
if self.debug: | |
print "ZenConnect: importing Globals" | |
except IOError: | |
print "zenConnect: IO Exception trying to import Globals" | |
return None | |
try: | |
import ZCmdBase | |
if self.debug: | |
print "zenConnect: importing ZCmdBase to form the connection" | |
except IOError: | |
print "zenConnect: IO Exception trying to import ZCmdBase" | |
return None | |
self.connection = ZCmdBase.ZCmdBase() | |
if self.debug: | |
print "zenConnect: zen = ZCmdBase() connection created !" | |
print "zenConnect: zen connection returned " | |
return self.connection | |
if __name__ == '__main__': | |
zen = Zen() | |
print 'Iniciando conexion...' | |
zen.zenConnect() | |
if not zen.connection: | |
print 'No hay conexion!!!' | |
print 'Estamos conectados!!!' | |
# list = zen.getDevices(); | |
# for dev in list: | |
# print dev | |
print 'Interfaces:' | |
list = zen.getInferfaces(path) | |
for any in list: | |
print any | |
print 'DataSources:' | |
list = zen.getDataSources(path) | |
for any in list: | |
print any | |
device = zen.zenFind('some') | |
if device: | |
print 'Si existe!' | |
else: | |
print 'No existe' | |
device = zen.zenFindByPath("Balanceadores/Radware/LinkProof/devices/LPMain"); | |
print 'Balanceadores/Radware/LinkProof/devices/LPMain' | |
if device: | |
print 'Si existe!' | |
else: | |
print 'No existe' | |
if device: | |
graphDefs = device.getDefaultGraphDefs() | |
for gd in graphDefs: | |
print gd['url'] | |
print 'Testing Finalizado' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Provides a set of effective tools to query Zenoss
database ( ZeoDB ) using an active socket.