Guest User

Untitled

a guest
Jun 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """Custom ActionRule/ZenAction processor for sending xmpp messages"""
  3.  
  4. import Globals
  5. import sys
  6. from Products.ZenUtils.Utils import monkeypatch
  7. from Products.ZenEvents.zenactions import *
  8.  
  9. target = getattr(sys.modules['Products.ZenEvents.zenactions'], 'ZenActions')
  10.  
  11. @monkeypatch(target)
  12. def mainbody(self):
  13. """main loop to run actions.
  14. """
  15. from twisted.internet.process import reapAllProcesses
  16. reapAllProcesses()
  17. zem = self.dmd.ZenEventManager
  18. self.loadActionRules()
  19. self.processRules(zem)
  20.  
  21. @monkeypatch(target)
  22. def runCycle(self):
  23. try:
  24. start = time.time()
  25. self.syncdb()
  26. self.mainbody()
  27. self.log.info("processed %s rules in %.2f secs", len(self.actions), time.time()-start)
  28. self.sendHeartbeat()
  29. except:
  30. self.log.exception("unexpected exception")
  31. reactor.callLater(60, self.runCycle)
  32.  
  33. @monkeypatch(target)
  34. def run(self):
  35. self.schedule.start()
  36. self.runCycle()
  37. reactor.run()
  38.  
  39. @monkeypatch(target)
  40. def sendXmpp(self, action, data, clear = None):
  41. fmt, body = self.format(action, data, clear)
  42. recipients = action.getAddresses()
  43. if not recipients:
  44. self.log.warning('failed to send message %s on rule %s: %s', action.getUser().id, action.id, 'Unspecified recipient.')
  45. return True
  46. for recipient in recipients:
  47. self.log.info('Sending message to %s: %s', recipient, fmt)
  48. return True
  49.  
  50. if __name__=='__main__':
  51. za = ZenActions()
  52. # filter out actions that we are not interested in
  53. za.actions = filter(lambda ar: ar.action.title() != 'Xmpp', za.actions)
  54. import logging
  55. logging.getLogger('zen.Events').setLevel(90)
  56. za.run()
Add Comment
Please, Sign In to add comment