def status(ini_file_location): """Check the status of LUM Set IFOR_CONFIG env variable and execute 'i4tv' command""" cmdline = [config.I4TV_PATH] # Ini file exist ? if not os.path.exists(ini_file_location): raise NagiosUnknown( "INI file not found: '{0}'".format(ini_file_location)) # Create environment variable for i4* commands os.putenv("IFOR_CONFIG", ini_file_location) cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) cmd_output = cmd.communicate()[0] # Check if i4* command is successful done_pattern = re.compile(r"Completed license transaction.*") done_match = done_pattern.search(cmd_output) if not done_match: raise LumStatusError("Error occured when checking for LUM !", cmd.returncode, os.path.basename(ini_file_location)) return cmd_output.split('\n')
def process_plugin_options(): """Process plugin arguments""" argparser.add_option( '-l', dest='license', help='License file or remote host as <port>@<remote_host>') argparser.add_option( '-p', dest='port', help= 'License port (only for backend that does not support remote host as <port>@<remote_host>)' ) argparser.add_option('-d', '--debug', dest='debug', action='store_true', help='Enable debug mode') argparser.add_option( '--no-long-output', dest='nolongoutput', action='store_true', help='Disable Nagios long output (compatibility with Nagios < 3.x)') opt = argparser.parse_args()[0] # Checking for options if not opt.license: raise NagiosUnknown( "Syntax error: missing license or remote host information !") if opt.debug: print "Debug mode is on." return opt
def __init__(self, name, used_licenses, total_licenses, expire_date): self.name = name try: self.used_licenses = long(used_licenses) self.total_licenses = long(total_licenses) self.expires = datetime.strptime(expire_date, '%Y-%m-%d') except ValueError as e: raise NagiosUnknown('Exception: %s' % e)
def test_from_file(output_file): """Used for testing purpose""" try: testfile = open(output_file, 'r') except IOError as e: raise NagiosUnknown("Error loading test output file: %s" % e) testfile_output = testfile.readlines() testfile.close() return testfile_output
def run(): """Execute the plugin""" # Plugin arguments options = process_plugin_options() # Get the output of license manager command, catching errors try: if options.debug: output = backend.util.test_from_file("../tests/lstc_status.txt") else: output = backend.lstc.status("%s" % options.license) except backend.lstc.LstcStatusError as e: raise NagiosCritical("%s (code: %s, license: '%s') !" % (e.errmsg, e.retcode, e.license)) # Globals connected_users = [] for line in output: # Checking number of connected users connected_users_pattern = re.compile( r'^(?:\s+|)(\w+)\s+(\d+@[\w\d.]+)') connected_users_match = connected_users_pattern.search(line) if connected_users_match: connected_users.append((connected_users_match.group(1), connected_users_match.group(2))) # Checking for unknown errors if not connected_users: raise NagiosUnknown("Unexpected error ! Check with debug mode.") # Format Nagios output # -------------------- # nagios_output = "LSTC: There %s %d license%s used." nagios_longoutput = '' # Plural syntax if more than 1 user if len(connected_users) > 1: verb = 'are' plural = 's' else: verb = 'is' plural = '' # Output to Nagios nagios_output = nagios_output % (verb, len(connected_users), plural) if not options.nolongoutput: nagios_longoutput = '\n' for user in connected_users: nagios_longoutput += "User %s from host %s.\n" % (user[0], user[1]) raise NagiosOk(nagios_output + nagios_longoutput)
def run(): """Execute the plugin""" # Plugin arguments # ---------------- # # Define custom arguments for this plugin using argparser argparser.add_option('-m', dest='mode', choices=('status', 'expire'), help='Mode for the plugins, either "status" (default) or "expire".') # Define common arguments to all plugins options = process_plugin_options() # Check mandatory arguments if not options.port: raise NagiosUnknown("Syntax error: missing port information !") if not options.mode: raise NagiosUnknown("Syntax error: missing mode information !") # Get the output of license manager command, catching errors try: output = backend.lmx.status_xml("%s" % options.license, options.port) except backend.lmx.LmxStatusError as e: raise NagiosCritical("%s (code: %s, license: '%s') !" % (e.errmsg, e.retcode, e.license)) # Process XML data xml_data = XML.parseString(output) all_features_tags = xml_data.getElementsByTagName('FEATURE') if not len(all_features_tags): raise NagiosCritical('Problem to query LM-X license manager on port %s from host %s !' % (options.port, options.license)) # Get all features and compute stats (used, free, total licenses...) features = backend.lmx.Features() for node_feature in all_features_tags: feature = node_feature.getAttribute('NAME') used_licenses = node_feature.getAttribute('USED_LICENSES') total_licenses = node_feature.getAttribute('TOTAL_LICENSES') expire_date = node_feature.getAttribute('END') feature = backend.lmx.Feature(feature, used_licenses, total_licenses, expire_date) features.append(feature) # Format Nagios output # -------------------- # # STATUS if options.mode == 'status': nagios_output = str(features) nagios_longoutput = '' nagios_perfdata = features.print_perfdata() # Must we show long output ? if not options.nolongoutput: nagios_longoutput = '\nFeatures details:\n\n' for feature in features: nagios_longoutput += '%s\n' % feature raise NagiosOk(nagios_output + nagios_longoutput + nagios_perfdata) # EXPIRATION elif options.mode == 'expire': days_remaining = features.calc_expired_license() if min(days_remaining.values()) > 15: raise NagiosOk('Features are up-to-date.') count = 0 nagios_output = '' nagios_longoutput = '\n' # Check first for expired licenses if 0 in days_remaining.values(): for feature in days_remaining: if not days_remaining[feature]: nagios_longoutput += '** %s is expired ! **\n' % feature count += 1 nagios_output = 'There are %d features expired !' % count # Do not show long output if specified if not options.nolongoutput: nagios_output += nagios_longoutput raise NagiosCritical(nagios_output) else: # Check for about to expire licenses for feature in days_remaining: if days_remaining[feature] > 0 and days_remaining[feature] <= 15: nagios_longoutput += '%s expires in %d day(s).\n' % (feature, days_remaining[feature]) count +=1 nagios_output = 'There are %d features about to expire !' % count # Do not show long output if specified if not options.nolongoutput: nagios_output += nagios_longoutput raise NagiosWarning(nagios_output)