1
2
3
4
5
6
7
8
9
10
11
12
13
14 import logging
15 log = logging.getLogger('zen.CommandConfig')
16
17 from ZODB.POSException import POSError
18
19 from PerformanceConfig import PerformanceConfig
20 from Products.ZenRRD.zencommand import Cmd, DeviceConfig, DataPointConfig
21 from Products.ZenHub.PBDaemon import translateError
22 from Products.DataCollector.Plugins import getParserLoader
23 from Products.ZenEvents.ZenEventClasses import Warning
26 """Return list of command definitions.
27 """
28 perfServer = comp.device().getPerformanceServer()
29 for templ in comp.getRRDTemplates():
30 basepath = comp.rrdPath()
31 for ds in templ.getRRDDataSources('COMMAND'):
32 if not ds.enabled: continue
33 parserName = getattr(ds, "parser", "Auto")
34 ploader = getParserLoader(dmd, parserName)
35 if ploader is None:
36 log.error("Could not load %s plugin", parserName)
37 continue
38 component_name = ds.getComponent(comp)
39 parser = ploader.create()
40 points = []
41 for dp in ds.getRRDDataPoints():
42 dpc = DataPointConfig()
43 dpc.id = dp.id
44 dpc.component = component_name
45 dpc.rrdPath = "/".join((basepath, dp.name()))
46 dpc.rrdType = dp.rrdtype
47 dpc.rrdCreateCommand = dp.getRRDCreateCommand(perfServer)
48 dpc.rrdMin = dp.rrdmin
49 dpc.rrdMax = dp.rrdmax
50 dpc.data = parser.dataForParser(comp, dp)
51 points.append(dpc)
52 cmd = Cmd()
53 cmd.useSsh = getattr(ds, 'usessh', False)
54 try:
55 cmd.cycleTime = int(ds.cycletime)
56 except ValueError:
57 message = "Unable to convert the cycle time '%s' to an " \
58 "integer for %s/%s on %s" \
59 " -- setting to 300 seconds" % (
60 ds.cycletime, templ.id, ds.id, comp.device().id)
61 log.error(message)
62 component = ds.getPrimaryUrlPath()
63 dedupid = "Unable to convert cycletime for %s" % component
64 dmd.ZenEventManager.sendEvent(dict(
65 device=comp.device().id, component=component,
66 eventClass='/Cmd', severity=Warning, summary=message,
67 dedupid=dedupid,
68 ))
69 cmd.cycleTime = 300
70 cmd.component = component_name
71 cmd.eventClass = ds.eventClass
72 cmd.eventKey = ds.eventKey or ds.id
73 cmd.severity = ds.severity
74 cmd.parser = ploader
75 cmd.command = ds.getCommand(comp)
76 cmd = commandCache.setdefault(cmd.commandKey(), cmd)
77 cmd.points.extend(points)
78 commandSet.add(cmd)
79 return comp.getThresholdInstances('COMMAND')
80
83 if not dev.monitorDevice():
84 return None
85 cache = {}
86 cmds = set()
87 try:
88 threshs = getComponentCommands(dev, cache, cmds, dev.getDmd())
89 except (SystemExit, KeyboardInterrupt), ex:
90 log.exception("Unable to process device commands for %s -- skipping",
91 dev.id)
92 for o in dev.getMonitoredComponents(collector="zencommand"):
93 try:
94 threshs.extend(getComponentCommands(o, cache, cmds, dev.getDmd()))
95 except (SystemExit, KeyboardInterrupt), ex:
96 log.exception("Unable to process component commands for %s %s -- skipping",
97 dev.id, o.id)
98 if cmds:
99 d = DeviceConfig()
100 d.lastChange = dev.getLastChange()
101 d.device = dev.id
102 d.ipAddress = dev.getManageIp()
103 d.port = dev.getProperty('zCommandPort')
104 d.username = dev.getProperty('zCommandUsername')
105 d.password = dev.getProperty('zCommandPassword')
106 d.loginTimeout = dev.getProperty('zCommandLoginTimeout')
107 d.commandTimeout = dev.getProperty('zCommandCommandTimeout')
108 d.keyPath = dev.getProperty('zKeyPath')
109 d.maxOids = dev.getProperty('zMaxOIDPerRequest')
110 d.concurrentSessions = dev.getProperty('zSshConcurrentSessions')
111 d.commands = list(cmds)
112 d.thresholds = threshs
113 return d
114 return None
115
118
119 @translateError
122
123
126
127
130
131
133 '''Get the command configuration for all devices.
134 '''
135 result = []
136 for dev in self.config.devices():
137 if devices and dev.id not in devices: continue
138 dev = dev.primaryAq()
139 try:
140 cmdinfo = getDeviceCommands(dev)
141 if not cmdinfo: continue
142 result.append(cmdinfo)
143 except POSError: raise
144 except:
145 self.log.exception("getDeviceCommands() exception for device %s",
146 dev.id)
147 return result
148
156