1 changed file
check_elasticsearch_x-pack_license_expiry.py + | ||
check_elasticsearch_x-pack_license_expiry.py
/check_elasticsearch_x-pack_license_expiry.py+108/check_elasticsearch_x-pack_license_expiry.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-05 19:09:03 +0000 (Mon, 05 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 Elasticsearch X-Pack license expiry in days via the X-Pack API
Add comment 21 Plus
Add comment 22 Plus Tested on Elasticsearch with X-Pack 6.0, 6.1, 6.2
Add comment 23 Plus
Add comment 24 Plus """
Add comment 25 Plus
Add comment 26 Plus from __future__ import absolute_import
Add comment 27 Plus from __future__ import division
Add comment 28 Plus from __future__ import print_function
Add comment 29 Plus from __future__ import unicode_literals
Add comment 30 Plus
Add comment 31 Plus import os
Add comment 32 Plus import sys
Add comment 33 Plus import time
Add comment 34 Plus import traceback
Add comment 35 Plus srcdir = os.path.abspath(os.path.dirname(__file__))
Add comment 36 Plus libdir = os.path.join(srcdir, 'pylib')
Add comment 37 Plus sys.path.append(libdir)
Add comment 38 Plus try:
Add comment 39 Plus # pylint: disable=wrong-import-position
Add comment 40 Plus from harisekhon import RestNagiosPlugin
Add comment 41 Plus except ImportError as _:
Add comment 42 Plus print(traceback.format_exc(), end='')
Add comment 43 Plus sys.exit(4)
Add comment 44 Plus
Add comment 45 Plus __author__ = 'Hari Sekhon'
Add comment 46 Plus __version__ = '0.1'
Add comment 47 Plus
Add comment 48 Plus
Add comment 49 Plus class CheckElasticsearchXPackLicenseExpiry(RestNagiosPlugin):
Add comment 50 Plus
Add comment 51 Plus def __init__(self):
Add comment 52 Plus # Python 2.x
Add comment 53 Plus super(CheckElasticsearchXPackLicenseExpiry, self).__init__()
Add comment 54 Plus # Python 3.x
Add comment 55 Plus # super().__init__()
Add comment 56 Plus self.name = 'Elasticsearch'
Add comment 57 Plus self.default_port = 9200
Add comment 58 Plus # /_xpack contains info on enabled features but less expiry info (no expiry date or start_date_in_millis)
Add comment 59 Plus self.path = '/_xpack/license?human=true'
Add comment 60 Plus self.auth = True
Add comment 61 Plus self.json = True
Add comment 62 Plus self.msg = 'Elasticsearch msg not defined yet'
Add comment 63 Plus
Add comment 64 Plus def add_options(self):
Add comment 65 Plus super(CheckElasticsearchXPackLicenseExpiry, self).add_options()
Add comment 66 Plus self.add_opt('-T', '--trial', action='store_true', help='Do not raise WARNING for trial license')
Add comment 67 Plus self.add_thresholds(default_warning=30, default_critical=7)
Add comment 68 Plus
Add comment 69 Plus def process_options(self):
Add comment 70 Plus super(CheckElasticsearchXPackLicenseExpiry, self).process_options()
Add comment 71 Plus self.validate_thresholds(simple='lower', positive=True, integer=False)
Add comment 72 Plus
Add comment 73 Plus def parse_json(self, json_data):
Add comment 74 Plus license_data = json_data['license']
Add comment 75 Plus status = license_data['status']
Add comment 76 Plus license_type = license_data['type']
Add comment 77 Plus start_millis = license_data['start_date_in_millis']
Add comment 78 Plus expiry_millis = license_data['expiry_date_in_millis']
Add comment 79 Plus # start_date is only available with ?human=true which is not enabled by default
Add comment 80 Plus expiry_date = license_data['expiry_date']
Add comment 81 Plus self.msg = "Elasticsearch X-Pack license '{}'".format(status)
Add comment 82 Plus if status != 'active':
Add comment 83 Plus self.critical()
Add comment 84 Plus self.msg += '(!)'
Add comment 85 Plus self.msg += ", type: '{}'".format(license_type)
Add comment 86 Plus if license_type == 'trial':
Add comment 87 Plus if not self.get_opt('trial'):
Add comment 88 Plus self.warning()
Add comment 89 Plus self.msg += '(!)'
Add comment 90 Plus days_left = int((expiry_millis - time.time() * 1000) / 1000 / 86400)
Add comment 91 Plus if days_left < 0:
Add comment 92 Plus self.critical()
Add comment 93 Plus self.msg += ' LICENSE EXPIRED {} days ago'.format(days_left)
Add comment 94 Plus else:
Add comment 95 Plus self.msg += ', expires in {} days'.format(days_left)
Add comment 96 Plus self.check_thresholds(days_left)
Add comment 97 Plus self.msg += " on '{}'".format(expiry_date)
Add comment 98 Plus if start_millis > (time.time() * 1000):
Add comment 99 Plus # start_date string field available with ?human=true, don't have to calculate
Add comment 100 Plus #start_date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(start_millis / 1000))
Add comment 101 Plus start_date = license_data['start_date']
Add comment 102 Plus self.msg += ", but start date '{}' is in the future!!!".format(start_date)
Add comment 103 Plus self.warning()
Add comment 104 Plus
Add comment 105 Plus
Add comment 106 Plus if __name__ == '__main__':
Add comment 107 Plus CheckElasticsearchXPackLicenseExpiry().main()
Add comment 108 Plus