Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: zenoss/ZenPacks.chudler.xmppBot
base: master
...
head fork: alienth/ZenPacks.chudler.xmppBot
compare: dev
Checking mergeability… Don't worry, you can still create the pull request.
  • 11 commits
  • 12 files changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 09, 2012
@alienth alienth fix broken indent 16c564e
@alienth alienth call the reactor every cycletime, rather than 60 seconds 3af29af
Commits on Feb 10, 2012
@alienth alienth Remove the resource from the sender in findUser, as it tries to match…
… sender with JabberId.
c27e6e1
Commits on Feb 11, 2012
@alienth alienth Fixed missing parameter for acknowledge(). df001cd
@alienth alienth Move instantiation of ZenAdapter out of Plugin calls, and into Plugin…
… setup.

Previously, each Plugin.call() that was made was firing a new ZenAdapter.
This somehow resulted in hanging references to the Zen dmd, causing
dangling file descriptors and sockets.

This commit works around this by only creating a ZenAdapter at Plugin init,
and reusing it in each plugin.
1d1a5af
@alienth alienth custom version bump 8780cf4
Commits on Apr 17, 2012
@alienth alienth Return after sending messages to all receipients, instead of just one…
… recipient.
36c48cd
@alienth alienth custom version bump bb8cffd
Commits on May 25, 2012
@alienth alienth add device flag. don't ack commits below minseverity. 77f0d78
@alienth alienth some cleanup / syntax fixes a04660a
Commits on Jun 20, 2012
@alienth alienth actually send the message b3437a8
View
13 ZenPacks/chudler/xmppBot/Jabber/Plugins.py
@@ -1,5 +1,9 @@
import sys, logging
+from Products.ZenUtils.ZCmdBase import ZCmdBase
+
+from Jabber.ZenAdapter import ZenAdapter
+
"""Find and load plugins for the jabber bot"""
class Plugin(object):
@@ -8,8 +12,13 @@ class Plugin(object):
threadsafe = False
private = True
- def __init__(self, jabberClient, **kw):
- self.jabberClient = jabberClient
+ def __init__(self, jabberClient):
+ self.jabberClient=jabberClient
+
+ dmd = ZCmdBase(noopts = True).dmd
+ self.adapter = ZenAdapter(dmd)
+
+
def __repr__(self):
return '<%s %r>' % (
View
12 ZenPacks/chudler/xmppBot/Jabber/ZenAdapter.py
@@ -4,19 +4,13 @@
log = logging.getLogger('zen.xmppBot')
-from Products.ZenUtils.ZCmdBase import ZCmdBase
from Products.AdvancedQuery import MatchGlob, Eq, Or
class ZenAdapter:
-"""Thin wrapper for accessing and controlling parts of zenoss"""
+ """Thin wrapper for accessing and controlling parts of zenoss"""
- def __init__(self):
- """I don't know a suitable way to get dmd, so
- ask ZCmdBase for it.
- """
- # FIXME 09JUN09: This can have side-effects?
- cmd = ZCmdBase(noopts = True)
- self.dmd = cmd.dmd
+ def __init__(self, dmd):
+ self.dmd = dmd
self.evManager = self.dmd.ZenEventManager
self.stateMap = self.eventStates()
View
5 ZenPacks/chudler/xmppBot/Jabber/plugins/aazenossadmin.py
@@ -2,7 +2,6 @@
incomming message."""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
class AAZenossAdminPlugin(Plugin):
@@ -18,8 +17,6 @@ def call(self, sender, log, **kw):
if '/' in sender:
sender = sender.split('/')[0]
- adapter = ZenAdapter()
-
if sender == None:
log.warn('Failed to extract sender from %s. Will not authorize communications.' % sender)
return False
@@ -27,7 +24,7 @@ def call(self, sender, log, **kw):
log.debug('Got a message from %s. Going to look for a Zenoss user to map it to.' % sender)
# look through all zenoss users until we find one with the sender's JabberID
- for user in adapter.userSettings():
+ for user in self.adapter.userSettings():
try:
jabberProperty = user.getProperty('JabberId').lower()
except:
View
65 ZenPacks/chudler/xmppBot/Jabber/plugins/ack.py
@@ -1,7 +1,6 @@
"""Acknowledge Zenoss events by their EventID"""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
from Jabber.Options import Options
from optparse import OptionError
import transaction
@@ -10,13 +9,13 @@ class Ack(Plugin):
name = 'ack'
capabilities = ['ack', 'acknowledge', 'help']
+ minseverity = 4
private = False
def call(self, args, log, client, sender, messageType, **kw):
log.debug('Alert Ack plugin running with arguments: %s' % args)
- adapter = ZenAdapter()
opts = self.options()
@@ -28,39 +27,63 @@ def call(self, args, log, client, sender, messageType, **kw):
client.sendMessage(str(message), sender, messageType)
return False
- if options.eventIds is None and not options.all:
- message = 'NO. -e or --eventids is required if -a is not specified.'
+ if options.eventIds is None and options.device is None and not options.all and not arguments:
+ message = 'must specify --all, -d, or event id list'
client.sendMessage(message, sender, messageType)
return False
+ if arguments and options.eventIds is None and options.device is None and not options.all:
+ idsToAck = arguments
+
+ if options.eventIds:
+ idsToAck = options.eventIds.lower().split(',')
+
+
# we will build this list of matching eventids, then ack them using acknowledge()
acking = []
if options.all:
log.debug('User has requested to ack all events.')
- for event in adapter.events():
+ for event in self.adapter.events():
+ if event.severity < self.minseverity:
+ continue
acking.append(event.evid)
log.debug('Queuing %s event to ack.' % event.evid)
- return self.acknowledge(client, adapter, options.test, options.verbose, acking, sender, log)
-
- idsToAck = options.eventIds.lower().split(',')
- for event in adapter.events():
+ return self.acknowledge(client, options.test, options.verbose, acking, sender, messageType, log)
+
+ if options.device:
+ log.debug('User has requested to ack all events for device %s.' % options.device)
+ for event in self.adapter.events():
+ if event.severity < self.minseverity:
+ continue
+ if event.device == options.device:
+ acking.append(event.evid)
+ log.debug('Queuing %s event to ack.' % event.evid)
+ return self.acknowledge(client, options.test, options.verbose, acking, sender, messageType, log)
+
+ for event in self.adapter.events():
# python 2.5 will accept tuple instead of this.
for idToAck in idsToAck:
+ if event.severity < self.minseverity:
+ continue
eventid = event.evid
log.debug('Checking if eventid %s is one to ack (%s)' % (eventid, idToAck))
if eventid.lower().startswith(idToAck) or eventid.lower().endswith(idToAck):
log.debug('We should ack this event: %s. It will be queued' % eventid)
acking.append(eventid)
- if len(acking) > 0:
- return self.acknowledge(client, adapter, options.test, options.verbose, acking, sender, log)
+ if len(acking) > 1:
+ message = 'ambiguous results; please be more specific'
+ client.sendMessage(str(message), sender, messageType)
+ return False
+ elif len(acking) == 1:
+ return self.acknowledge(client, options.test, options.verbose, acking, sender, messageType, log)
else:
message = 'Sorry. Found no events to acknowledge.'
client.sendMessage(str(message), sender, messageType)
return False
- def acknowledge(self, client, adapter, dryrun, verbose, events, sender, log):
+ def acknowledge(self, client, dryrun, verbose, events, sender, messageType, log):
if dryrun:
log.debug('Test mode is activated, so events will not be acknowledged.')
message = 'Test mode: %d WOULD have been acknowledged.' % len(events)
@@ -68,8 +91,8 @@ def acknowledge(self, client, adapter, dryrun, verbose, events, sender, log):
else:
log.debug('Acking all queued events.')
- zenUser = self.findUser(sender, adapter)
- adapter.ackEvents(zenUser, events)
+ zenUser = self.findUser(sender)
+ self.adapter.ackEvents(zenUser, events)
log.debug('Done Acking all queued events.')
transaction.commit()
message = 'Acknowledged %d' % len(events)
@@ -80,8 +103,14 @@ def acknowledge(self, client, adapter, dryrun, verbose, events, sender, log):
message += ', '.join(events)
client.sendMessage(message, sender, messageType)
- def findUser(self, sender, adapter):
- for user in adapter.userSettings():
+ def findUser(self, sender):
+
+ # remove the resource from the sender
+ if '/' in sender:
+ sender = sender.split('/')[0]
+
+
+ for user in self.adapter.userSettings():
try:
jabberProperty = user.getProperty('JabberId').lower()
if jabberProperty == sender:
@@ -94,8 +123,8 @@ def findUser(self, sender, adapter):
def options(self):
parser = Options(description = 'Acknowledge events by eventid', prog = 'ack')
parser.add_option('-e', '--eventids', dest='eventIds', help='Complete or partial eventids to ack. Ids can be sepratated by commas. Partial ids can match either the beginning or end of the eventid.')
- parser.add_option('-a', '--all', dest='all', action='store_true', default=False, help='Acknowledge all events. If -e is also specified, it will still acknowledge every event.')
- parser.add_option('-d', '--device', dest='device', help='Only ack events that exist on this device. NOT IMPLEMENTED.')
+ parser.add_option('--all', dest='all', action='store_true', default=False, help='Acknowledge all events. If -e is also specified, it will still acknowledge every event.')
+ parser.add_option('-d', '--device', dest='device', help='Only ack events that exist on this device.')
parser.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False, help='Send list of all acknowledged events. Can be noisy. USE WITH CAUTION.')
parser.add_option('-t', '--test', dest='test', action='store_true', default=False, help='Do not acknowledge events, but show what would be done. Works with -v.')
return parser
View
4 ZenPacks/chudler/xmppBot/Jabber/plugins/data.py
@@ -1,7 +1,6 @@
"""Extract data from Zenoss RRD files."""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
from Jabber.Options import Options
from optparse import OptionError
@@ -17,7 +16,6 @@ def call(self, args, log, client, sender, messageType, **kw):
opts = self.options()
- adapter = ZenAdapter()
try:
(options, arguments) = opts.parse_args(args)
log.debug('Done parsing arguments. Options are "%s", arguments expanded to %s' % (options, arguments))
@@ -30,7 +28,7 @@ def call(self, args, log, client, sender, messageType, **kw):
client.sendMessage(message, sender, messageType)
return False
- devices = adapter.devices(options.deviceName)
+ devices = self.adapter.devices(options.deviceName)
if len(devices) == 0:
message = 'Cannot find a device, ip or mac for "%s"' % options.deviceName
client.sendMessage(message, sender, messageType)
View
9 ZenPacks/chudler/xmppBot/Jabber/plugins/events.py
@@ -1,7 +1,6 @@
"""Show events from Zenoss. Needs work!"""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
from Jabber.Options import Options
from optparse import OptionError
@@ -28,17 +27,15 @@ def call(self, args, log, client, sender, messageType, **kw):
client.sendMessage('Unknown option, use -h for help', sender, messageType)
return False
- adapter = ZenAdapter()
-
if options.acked:
log.debug('Looking for Acknowledged events')
message = 'Acknowledged Events\n'
- events = adapter.acknowledgedEvents()
+ events = self.adapter.acknowledgedEvents()
else:
message = 'Current Events\n'
- events = adapter.newEvents()
+ events = self.adapter.newEvents()
if options.device:
- devices = adapter.devices(options.device)
+ devices = self.adapter.devices(options.device)
if len(devices) == 0:
message = 'Cannot find a device, ip or mac named "%s"' % options.device
client.sendMessage(message, sender, messageType)
View
4 ZenPacks/chudler/xmppBot/Jabber/plugins/model.py
@@ -1,7 +1,6 @@
"""Model a device in Zenoss."""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
from Jabber.Options import Options
from optparse import OptionError
@@ -15,7 +14,6 @@ def call(self, args, log, client, sender, messageType, **kw):
log.debug('Modeler plugin running with arguments: %s' % args)
- adapter = ZenAdapter()
opts = self.options()
try:
@@ -34,7 +32,7 @@ def call(self, args, log, client, sender, messageType, **kw):
message = 'Please wait while the device is loaded.'
client.sendMessage(message, sender, messageType)
- adapter.loadDevice(**options.__dict__)
+ self.adapter.loadDevice(**options.__dict__)
message = 'Done discovering %s.' % options.deviceName
client.sendMessage(message, sender, messageType)
View
5 ZenPacks/chudler/xmppBot/Jabber/plugins/setjid.py
@@ -1,7 +1,6 @@
"""Check if the sender is a valid zenoss admin. For access control"""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
from Jabber.Options import Options
from optparse import OptionError
import transaction
@@ -27,12 +26,10 @@ def call(self, args, client, sender, messageType, log, **kw):
client.sendMessage('No. -u and -j are both required', sender, messageType)
return False
- adapter = ZenAdapter()
-
jabberId = options.jabberId.lower()
haveUser = False
- for user in adapter.userSettings():
+ for user in self.adapter.userSettings():
if user.id.lower() == options.zenUser.lower():
haveUser = True
try:
View
4 ZenPacks/chudler/xmppBot/Jabber/plugins/users.py
@@ -1,7 +1,6 @@
"""Check if the sender is a valid zenoss admin. For access control"""
from Jabber.Plugins import Plugin
-from Jabber.ZenAdapter import ZenAdapter
from Jabber.Options import Options
from optparse import OptionError
@@ -14,12 +13,11 @@ class Users(Plugin):
def call(self, args, log, client, sender, messageType, **kw):
log.debug('Users plugin running with arguments %s' % args)
# TODO switch this to optparse and make sure help works.
- adapter = ZenAdapter()
users = []
message = 'No Users'
haveUser = False
- for user in adapter.userSettings():
+ for user in self.adapter.userSettings():
haveUser = True
try:
jabberId = user.getProperty('JabberId') or 'No JabberId'
View
0  ZenPacks/chudler/xmppBot/daemons/xmppBot 100644 → 100755
File mode changed
View
4 ZenPacks/chudler/xmppBot/xmppBot.py
@@ -142,7 +142,7 @@ def runCycle(self):
self.log.info('processed %s rules in %.2f secs', len(self.actions), time.time()-start)
except:
self.log.exception('unexpected exception')
- reactor.callLater(60, self.runCycle)
+ reactor.callLater(self.options.cycletime, self.runCycle)
# ripped from zenactions.py
def loadActionRules(self):
@@ -178,7 +178,7 @@ def sendXmpp(self, action, data, clear = None):
else:
messageType = 'chat'
self.client.sendMessage(message, recipient, messageType)
- return True
+ return True
def getAddress(self, action):
if action.targetAddr:
View
2  setup.py
@@ -3,7 +3,7 @@
# or saved. Do not modify them directly here.
# NB: PACKAGES is deprecated
NAME = "ZenPacks.chudler.xmppBot"
-VERSION = "2.0"
+VERSION = "2.0~alienth2"
AUTHOR = "Colin Hudler"
LICENSE = ""
NAMESPACE_PACKAGES = ['ZenPacks', 'ZenPacks.chudler']

No commit comments for this range

Something went wrong with that request. Please try again.