eventlisten.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 os
  11. import pprint
  12. import time
  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=(
  28. "Statically define the directory holding the salt unix "
  29. "sockets for communication"
  30. ),
  31. )
  32. parser.add_option(
  33. "-n",
  34. "--node",
  35. dest="node",
  36. default="master",
  37. help=(
  38. "State if this listener will attach to a master or a "
  39. 'minion daemon, pass "master" or "minion"'
  40. ),
  41. )
  42. parser.add_option(
  43. "-f",
  44. "--func_count",
  45. default="",
  46. help=(
  47. "Return a count of the number of minions which have "
  48. "replied to a job with a given func."
  49. ),
  50. )
  51. parser.add_option(
  52. "-i",
  53. "--id",
  54. default="",
  55. help=("If connecting to a live master or minion, pass in the id"),
  56. )
  57. parser.add_option(
  58. "-t",
  59. "--transport",
  60. default="zeromq",
  61. help=("Transport to use. (Default: 'zeromq'"),
  62. )
  63. options, args = parser.parse_args()
  64. opts = {}
  65. for k, v in six.iteritems(options.__dict__):
  66. if v is not None:
  67. opts[k] = v
  68. opts["sock_dir"] = os.path.join(opts["sock_dir"], opts["node"])
  69. if "minion" in options.node:
  70. if args:
  71. opts["id"] = args[0]
  72. return opts
  73. if options.id:
  74. opts["id"] = options.id
  75. else:
  76. opts["id"] = options.node
  77. return opts
  78. def check_access_and_print_warning(sock_dir):
  79. """
  80. Check if this user is able to access the socket
  81. directory and print a warning if not
  82. """
  83. if (
  84. os.access(sock_dir, os.R_OK)
  85. and os.access(sock_dir, os.W_OK)
  86. and os.access(sock_dir, os.X_OK)
  87. ):
  88. return
  89. else:
  90. print(
  91. "WARNING: Events will not be reported"
  92. " (not able to access {0})".format(sock_dir)
  93. )
  94. def listen(opts):
  95. """
  96. Attach to the pub socket and grab messages
  97. """
  98. event = salt.utils.event.get_event(
  99. opts["node"],
  100. sock_dir=opts["sock_dir"],
  101. transport=opts["transport"],
  102. opts=opts,
  103. listen=True,
  104. )
  105. check_access_and_print_warning(opts["sock_dir"])
  106. print(event.puburi)
  107. jid_counter = 0
  108. found_minions = []
  109. while True:
  110. ret = event.get_event(full=True)
  111. if ret is None:
  112. continue
  113. if opts["func_count"]:
  114. data = ret.get("data", False)
  115. if data:
  116. if (
  117. "id" in six.iterkeys(data)
  118. and data.get("id", False) not in found_minions
  119. ):
  120. if data["fun"] == opts["func_count"]:
  121. jid_counter += 1
  122. found_minions.append(data["id"])
  123. print(
  124. "Reply received from [{0}]. Total replies now: [{1}].".format(
  125. ret["data"]["id"], jid_counter
  126. )
  127. )
  128. continue
  129. else:
  130. print("Event fired at {0}".format(time.asctime()))
  131. print("*" * 25)
  132. print("Tag: {0}".format(ret["tag"]))
  133. print("Data:")
  134. pprint.pprint(ret["data"])
  135. if __name__ == "__main__":
  136. opts = parse()
  137. listen(opts)