Package ZenModel :: Module BatchDeviceLoader
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.BatchDeviceLoader

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2007, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 as published by 
 8  # the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13  __doc__="""BatchDeviceLoader.py 
14   
15  BatchDeviceLoader.py loads a list of devices read from a file. 
16  The file can be formatted like this: 
17   
18  device0 
19  /Path/To/Device 
20  device1 
21  device2 
22  /Path/To/Other 
23      device3 
24      device4""" 
25   
26  import Globals 
27  import transaction 
28  import re 
29  import sys 
30   
31  from Products.ZenUtils.ZCmdBase import ZCmdBase 
32  from Products.ZenUtils.CmdBase import CmdBase 
33   
34   
35 -class BatchDeviceLoader(ZCmdBase):
36
37 - def __init__(self):
38 ZCmdBase.__init__(self) 39 #self.dmd = ZCmdBase().dmd 40 self.loader = self.dmd.DeviceLoader
41
42 - def loadDevices(self):
43 data = open(self.options.infile,'r').read() 44 device_list = self.parseDevices(data) 45 while device_list: 46 device = device_list.pop() 47 if device['deviceName']: 48 self.log.info("Attempting to load %s" % device['deviceName']) 49 self.loader.loadDevice(**device)
50
51 - def buildOptions(self):
52 ZCmdBase.buildOptions(self) 53 self.parser.add_option('-i', '--infile', 54 dest="infile", default="", 55 help="input file for import (default stdin)")
56
57 - def parseDevices(self, rawDevices):
58 _slash = re.compile(r'\s*/', re.M) 59 _sp = re.compile(r'\s+', re.M) 60 _r = re.compile(r'\s+/', re.M) 61 if not _slash.match(rawDevices): 62 rawDevices = "/Discovered\n" + rawDevices 63 sections = [re.split(_sp, x) for x in re.split(_r, rawDevices)] 64 finalList = [] 65 for s in sections: 66 path, devs = s[0], s[1:] 67 if not path.startswith('/'): path = '/' + path 68 finalList += [{'deviceName':x,'devicePath':path} for x in devs] 69 return finalList
70 71 if __name__=='__main__': 72 b = BatchDeviceLoader() 73 b.loadDevices() 74