1 changed file
check_git_checkout_valid.py + | ||
Add comment 1 Plus #!/usr/bin/env python
Add comment 2 Plus # coding=utf-8
Add comment 3 Plus # vim:ts=4:sts=4:sw=4:et
Add comment 4 Plus #
Add comment 5 Plus # Author: Hari Sekhon
Add comment 6 Plus # Date: 2018-03-14 18:04:54 +0000 (Wed, 14 Mar 2018)
Add comment 7 Plus #
Add comment 8 Plus # https://github.com/harisekhon/nagios-plugins
Add comment 9 Plus #
Add comment 10 Plus # License: see accompanying Hari Sekhon LICENSE file
Add comment 11 Plus #
Add comment 12 Plus # If you're using my code you're welcome to connect with me on LinkedIn
Add comment 13 Plus # and optionally send me feedback to help steer this or other code I publish
Add comment 14 Plus #
Add comment 15 Plus # https://www.linkedin.com/in/harisekhon
Add comment 16 Plus #
Add comment 17 Plus
Add comment 18 Plus """
Add comment 19 Plus
Add comment 20 Plus Nagios Plugin to check HashiCorp Vault high availability via its API
Add comment 21 Plus
Add comment 22 Plus Checks:
Add comment 23 Plus
Add comment 24 Plus - is High Availability enabled
Add comment 25 Plus - is current instance the leader (optionally raises warning if --leader specified and is not the leader)
Add comment 26 Plus - verbose mode outputs the leader address
Add comment 27 Plus - raises warning if no leader found (checks leader address is populated)
Add comment 28 Plus
Add comment 29 Plus Tested On Vault 0.6, 0.7, 0.8, 0.9
Add comment 30 Plus
Add comment 31 Plus """
Add comment 32 Plus
Add comment 33 Plus from __future__ import absolute_import
Add comment 34 Plus from __future__ import division
Add comment 35 Plus from __future__ import print_function
Add comment 36 Plus from __future__ import unicode_literals
Add comment 37 Plus
Add comment 38 Plus import os
Add comment 39 Plus import sys
Add comment 40 Plus import traceback
Add comment 41 Plus srcdir = os.path.abspath(os.path.dirname(__file__))
Add comment 42 Plus libdir = os.path.join(srcdir, 'pylib')
Add comment 43 Plus sys.path.append(libdir)
Add comment 44 Plus try:
Add comment 45 Plus # pylint: disable=wrong-import-position
Add comment 46 Plus from harisekhon import RestNagiosPlugin
Add comment 47 Plus except ImportError as _:
Add comment 48 Plus print(traceback.format_exc(), end='')
Add comment 49 Plus sys.exit(4)
Add comment 50 Plus
Add comment 51 Plus __author__ = 'Hari Sekhon'
Add comment 52 Plus __version__ = '0.2'
Add comment 53 Plus
Add comment 54 Plus
Add comment 55 Plus class CheckVaultHighAvailability(RestNagiosPlugin):
Add comment 56 Plus
Add comment 57 Plus def __init__(self):
Add comment 58 Plus # Python 2.x
Add comment 59 Plus super(CheckVaultHighAvailability, self).__init__()
Add comment 60 Plus # Python 3.x
Add comment 61 Plus # super().__init__()
Add comment 62 Plus self.name = 'Vault'
Add comment 63 Plus self.default_port = 8200
Add comment 64 Plus self.path = '/v1/sys/leader'
Add comment 65 Plus self.auth = False
Add comment 66 Plus self.json = True
Add comment 67 Plus self.msg = 'Vault msg not defined yet'
Add comment 68 Plus
Add comment 69 Plus def add_options(self):
Add comment 70 Plus super(CheckVaultHighAvailability, self).add_options()
Add comment 71 Plus self.add_opt('--leader', action='store_true', help='Expect to be leader')
Add comment 72 Plus
Add comment 73 Plus #def process_options(self):
Add comment 74 Plus # super(CheckVaultHighAvailability, self).process_options()
Add comment 75 Plus
Add comment 76 Plus def parse_json(self, json_data):
Add comment 77 Plus ha_enabled = json_data['ha_enabled']
Add comment 78 Plus is_leader = json_data['is_self']
Add comment 79 Plus leader_address = json_data['leader_address']
Add comment 80 Plus leader_cluster_address = None
Add comment 81 Plus # not available on older versions
Add comment 82 Plus if 'leader_cluster_address' in json_data:
Add comment 83 Plus leader_cluster_address = json_data['leader_cluster_address']
Add comment 84 Plus self.msg = 'Vault high availability enabled = {}'.format(ha_enabled)
Add comment 85 Plus if ha_enabled:
Add comment 86 Plus self.msg += ', '
Add comment 87 Plus else:
Add comment 88 Plus self.critical()
Add comment 89 Plus self.msg += '! '
Add comment 90 Plus self.msg += 'is leader = {}'.format(is_leader)
Add comment 91 Plus if not is_leader and self.get_opt('leader'):
Add comment 92 Plus self.warning()
Add comment 93 Plus self.msg += ' (expected to be leader)'
Add comment 94 Plus if self.verbose:
Add comment 95 Plus self.msg += ", leader_address = '{}'".format(leader_address)
Add comment 96 Plus # not available on older versions
Add comment 97 Plus if leader_cluster_address is not None:
Add comment 98 Plus self.msg += ", leader_cluster_address = '{}'".format(leader_cluster_address)
Add comment 99 Plus if not leader_address or (leader_cluster_address is not None and not leader_cluster_address):
Add comment 100 Plus self.critical()
Add comment 101 Plus self.msg += ', no leader found!'
Add comment 102 Plus
Add comment 103 Plus
Add comment 104 Plus if __name__ == '__main__':
Add comment 105 Plus CheckVaultHighAvailability().main()
Add comment 106 Plus