Advertisement
firex20

Untitled

Nov 2nd, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Check connection to the RabbitMQ server
  3.  
  4. # import parser for command-line options
  5. import argparse
  6. # import a pure-Python implementation of the AMQP 0-9-1
  7. import pika
  8. import ssl
  9.  
  10. # define and parse command-line options
  11. parser = argparse.ArgumentParser(description='Check connection to RabbitMQ server')
  12. parser.add_argument('--server', required=True, help='Define RabbitMQ server')
  13. parser.add_argument('--virtual_host', default='/', help='Define virtual host')
  14. parser.add_argument('--ssl', action='store_true', help='Enable SSL (default: %(default)s)')
  15. parser.add_argument('--port', type=int, default=5672, help='Define port (default: %(default)s)')
  16. parser.add_argument('--username', default='guest', help='Define username (default: %(default)s)')
  17. parser.add_argument('--password', default='guest', help='Define password (default: %(default)s)')
  18. args = vars(parser.parse_args())
  19.  
  20. # set amqp credentials
  21. credentials = pika.PlainCredentials(args['username'], args['password'])
  22.  
  23. if args['ssl']:
  24. context = ssl.create_default_context()
  25. ssl_options = pika.SSLOptions(context, args['server'])
  26. else:
  27. ssl_options = None
  28.  
  29. parameters = pika.ConnectionParameters(host=args['server'], port=args['port'], virtual_host=args['virtual_host'], credentials=credentials, ssl_options=ssl_options)
  30.  
  31. # try to establish connection and check its status
  32. try:
  33. connection = pika.BlockingConnection(parameters)
  34. if connection.is_open:
  35. print('OK')
  36. connection.close()
  37. exit(0)
  38. except Exception as error:
  39. print('Error:', error.__class__.__name__)
  40. exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement