eventlisten.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Use this script to dump the event data out to the terminal. It needs to know
  4. what the sock_dir is.
  5. This script is a generic tool to test event output
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function
  9. import optparse
  10. import pprint
  11. import time
  12. import os
  13. # Import Salt libs
  14. import salt.utils.event
  15. # Import 3rd-party libs
  16. from salt.ext import six
  17. def parse():
  18. '''
  19. Parse the script command line inputs
  20. '''
  21. parser = optparse.OptionParser()
  22. parser.add_option(
  23. '-s',
  24. '--sock-dir',
  25. dest='sock_dir',
  26. default='/var/run/salt',
  27. help=('Statically define the directory holding the salt unix '
  28. 'sockets for communication')
  29. )
  30. parser.add_option(
  31. '-n',
  32. '--node',
  33. dest='node',
  34. default='master',
  35. help=('State if this listener will attach to a master or a '
  36. 'minion daemon, pass "master" or "minion"')
  37. )
  38. parser.add_option(
  39. '-f',
  40. '--func_count',
  41. default='',
  42. help=('Return a count of the number of minions which have '
  43. 'replied to a job with a given func.')
  44. )
  45. parser.add_option(
  46. '-i',
  47. '--id',
  48. default='',
  49. help=('If connecting to a live master or minion, pass in the id')
  50. )
  51. parser.add_option(
  52. '-t',
  53. '--transport',
  54. default='zeromq',
  55. help=('Transport to use. (Default: \'zeromq\'')
  56. )
  57. options, args = parser.parse_args()
  58. opts = {}
  59. for k, v in six.iteritems(options.__dict__):
  60. if v is not None:
  61. opts[k] = v
  62. opts['sock_dir'] = os.path.join(opts['sock_dir'], opts['node'])
  63. if 'minion' in options.node:
  64. if args:
  65. opts['id'] = args[0]
  66. return opts
  67. if options.id:
  68. opts['id'] = options.id
  69. else:
  70. opts['id'] = options.node
  71. return opts
  72. def check_access_and_print_warning(sock_dir):
  73. '''
  74. Check if this user is able to access the socket
  75. directory and print a warning if not
  76. '''
  77. if (os.access(sock_dir, os.R_OK) and
  78. os.access(sock_dir, os.W_OK) and
  79. os.access(sock_dir, os.X_OK)):
  80. return
  81. else:
  82. print('WARNING: Events will not be reported'
  83. ' (not able to access {0})'.format(sock_dir))
  84. def listen(opts):
  85. '''
  86. Attach to the pub socket and grab messages
  87. '''
  88. event = salt.utils.event.get_event(
  89. opts['node'],
  90. sock_dir=opts['sock_dir'],
  91. transport=opts['transport'],
  92. opts=opts,
  93. listen=True
  94. )
  95. check_access_and_print_warning(opts['sock_dir'])
  96. print(event.puburi)
  97. jid_counter = 0
  98. found_minions = []
  99. while True:
  100. ret = event.get_event(full=True)
  101. if ret is None:
  102. continue
  103. if opts['func_count']:
  104. data = ret.get('data', False)
  105. if data:
  106. if 'id' in six.iterkeys(data) and data.get('id', False) not in found_minions:
  107. if data['fun'] == opts['func_count']:
  108. jid_counter += 1
  109. found_minions.append(data['id'])
  110. print('Reply received from [{0}]. Total replies now: [{1}].'.format(ret['data']['id'], jid_counter))
  111. continue
  112. else:
  113. print('Event fired at {0}'.format(time.asctime()))
  114. print('*' * 25)
  115. print('Tag: {0}'.format(ret['tag']))
  116. print('Data:')
  117. pprint.pprint(ret['data'])
  118. if __name__ == '__main__':
  119. opts = parse()
  120. listen(opts)