def collect(self): # Collect exporter information version = get_version() local_time = time.localtime() formatted_local_time = time.strftime('%Y-%m-%d %H.%M.%S', local_time) try: if local_time[8]: local_tzname = time.tzname[1] else: local_tzname = time.tzname[0] except AttributeError: local_tzname = 'UNKNOWN' # Update Prometheus metrics c = CounterMetricFamily( 'mqa_exporter_current_datetime_seconds', 'The current date and time of the server on which the exporter is running in epoch seconds ', labels=['appliance']) c.add_metric([self.appliance], datetime_to_epoch(formatted_local_time, '%Y-%m-%d %H.%M.%S')) yield c i = InfoMetricFamily('mqa_exporter', 'MQ Appliance exporter information') i.add_metric( ['appliance', 'version', 'localTimezone'], { 'appliance': self.appliance, 'version': version, 'localTimezone': local_tzname }) yield i
def main(): # Build parser to handle the command line options parser = argparse.ArgumentParser( description='MQ Appliance Prometheus Exporter List Utility - ' + get_version()) parser.add_argument('-a', '--appliance', type=str, required=False, default='*', help='Name of the appliance') parser.add_argument( '-d', '--directory', type=str, required=False, default='', help='Path to directory for PID files (defaults to current directory)') parser.add_argument( '-f', '--file', type=str, required=False, help='Name of the file with the exporters configuration (CSV)') # Process command line options args = parser.parse_args() args.directory = resolve_directory(args) print('MQ Appliance Prometheus Exporter List Utility - ' + get_version()) print('Exporter directory is \'' + args.directory + '\'') # Build list of pid files file_list = get_pid_file_list(args.file, args.directory, args.appliance) # Search for exporters by looking at pid files exporter_count = 0 for file in file_list: appliance = os.path.basename(os.path.splitext(file)[0]) exporter_running, pid = get_pid_file_present( os.path.split(file)[0] + '/', appliance) if exporter_running: print('Exporter for appliance \'' + appliance + '\' is running with PID ' + pid) else: print('Exporter for appliance \'' + appliance + '\' is not running') exporter_count += 1 if exporter_count == 0: print( 'No exporter found or the specific exporter(s) are not running, or' ) print( 'you may be NOT be looking at the correct directory for the PID files!' ) else: print(str(exporter_count) + ' exporter(s) are currently running.')
def main(): # Build parser to handle the command line options parser = argparse.ArgumentParser( description='MQ Appliance Prometheus Exporter Start Utility - ' + get_version()) parser.add_argument('-a', '--appliance', type=str, required=False, help='Name of the appliance') parser.add_argument('-c', '--config', type=str, required=False, help='Name of the exporter configuration file (INI)') parser.add_argument( '-d', '--directory', type=str, required=False, default='', help='Path to directory for PID files (defaults to current directory)') parser.add_argument( '-f', '--file', type=str, required=True, help='Name of the file with the exporters configuration (CSV)') parser.add_argument('-ln', '--lognumbers', type=int, required=False, default=10, help='Number of logs in a rotation (defaults to 10)') parser.add_argument( '-ls', '--logsize', type=int, required=False, default=10485760, help='Size of logs in bytes (defaults to 10MB - 10485760)') parser.add_argument('-u', '--user', type=str, required=True, help='User to login to the appliance') parser.add_argument('-x', '--pw', type=str, required=False, help='Password to login to the appliance') # Display usage if needed if len(sys.argv) < 3: parser.print_help(sys.stderr) sys.exit(1) # Process command line options args = parser.parse_args() args.directory = resolve_directory(args) # Prompt for the password if args.pw == None: args.pw = get_password() print('MQ Appliance Prometheus Exporter Start Utility - ' + get_version()) print('Exporter directory is \'' + args.directory + '\'') # Process the exporters configuration file (CSV) try: with open(args.file) as csvfile: csvreader = csv.reader(csvfile, delimiter=',') exporter_count = 0 for exporter in csvreader: if args.appliance != None and args.appliance != exporter[0]: continue exporter_running, pid = get_pid_file_present( args.directory, exporter[0]) if exporter_running: print('Exporter for appliance \'' + exporter[0] + '\' is already running with PID ' + pid) else: command = shlex.split( 'python mqa_metrics.py -a ' + exporter[0] + ' -i ' + exporter[1] + ' -p ' + exporter[2] + ' -l ' + args.directory + exporter[0] + '_exporter.log' + ' -ls ' + str(args.logsize) + ' -ln ' + str(args.lognumbers) + ' -u ' + args.user + ' -x ' + args.pw + ' -hp ' + exporter[3] + ' -t ' + exporter[4] + (' -c ' + args.config if args.config != None else '')) # Start the exporter process = subprocess.Popen(command) print('Started exporter for appliance \'' + exporter[0] + '\' on HTTP port ' + str(exporter[3]) + ', PID is ' + str(process.pid)) # Write pid of the exporter to file with open(args.directory + exporter[0] + '.pid', 'w') as pidfile: pidfile.write(str(process.pid)) exporter_count += 1 except IOError as err: print(str(err)) sys.exit(1) print('Started ' + str(exporter_count) + ' exporter(s).')
def main(): # Build parser to handle the command line options parser = argparse.ArgumentParser(description='MQ Appliance Prometheus Exporter - ' + get_version()) parser.add_argument('-a', '--appliance', type=str, required=True, help = 'Name of the appliance') parser.add_argument('-c', '--config', type=str, required=False, help = 'Name of the exporter configuration file (INI)') parser.add_argument('-i', '--ip', type=str, required=True, help = 'IP address or DNS of the appliance REST API') parser.add_argument('-hp', '--httpPort', type=int, default=9813, help = 'Port number of the exported HTTP server (default: 9813)') parser.add_argument('-l', '--log', type=str, help = 'Name of the log file (defaults to STDOUT)') parser.add_argument('-ln', '--lognumbers', type=int, default=10, help = 'Number of logs in a rotation (defaults to 10)') parser.add_argument('-ls', '--logsize', type=int, default=10485760, help = 'Size of logs in bytes (defaults to 10MB - 10485760)') parser.add_argument('-p', '--port', type=str, default=5554, help = 'Port number of the appliance REST API (default: 5554)') parser.add_argument('-t', '--timeout', type=int, required=False, default=15, help = 'Timeout in seconds to perform the REST API call (default: 15)') parser.add_argument('-u', '--user', type=str, required=True, help = 'User to login to the appliance') parser.add_argument('-x', '--pw', type=str, required=False, help = 'Password to login to the appliance') # Display usage if needed if len(sys.argv) < 3: parser.print_help(sys.stderr) sys.exit(1) # Process command line options args = parser.parse_args() if args.log != None: args.log = args.log.replace('\\', '/') # Prompt for the password if args.pw == None: args.pw = get_password() # Configure the logger if args.log is None: logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%dT%H:%M:%S') else: if sys.version[:1] == '3': # Python 3 logging, supports log rotation logging.basicConfig(handlers=[RotatingFileHandler(args.log, maxBytes=args.logsize, backupCount=args.lognumbers)], level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", datefmt='%Y-%m-%dT%H:%M:%S') else: # Python 2 logging, does not support log rotation logging.basicConfig(filename=args.log, filemode='a', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%dT%H:%M:%S') logging.info('MQ Appliance Prometheus Exporter ' + get_version() + ' started on HTTP port ' + str(args.httpPort)) logging.info('MQ Appliance monitored is ' + args.appliance + ' at ' + args.ip + '(' + str(args.port) + ')') # Read exporter configuration file if args.config != None: config = ConfigParser() config.read(args.config) # Initialize HTTPS session session = init_rest_api(args.ip, args.port, (args.user, args.pw), args.timeout) # Register metric collectors try: if args.config == None or config.getboolean('collectors', 'appliance_information'): REGISTRY.register(MQAInformationMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'active_users'): REGISTRY.register(MQAActiveUsersMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'current_sensors'): REGISTRY.register(MQACurrentSensorsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'environmental_fan_sensors'): REGISTRY.register(MQAEnvironmentalFanSensorsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'environmental_sensors'): REGISTRY.register(MQAEnvironmentalSensorsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'ethernet_counters'): REGISTRY.register(MQAEthernetCountersMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'failure_notification'): REGISTRY.register(MQAFailureNotificationMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'file_system'): REGISTRY.register(MQAFileSystemMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'ipmi_sel_events'): REGISTRY.register(MQAIPMISelEventsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'log_targets'): REGISTRY.register(MQALogTargetsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'mq_system_resources'): REGISTRY.register(MQAMQSystemResourcesMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'network_interfaces'): REGISTRY.register(MQANetworkInterfacesMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'other_sensors'): REGISTRY.register(MQAOtherSensorsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'system_cpu'): REGISTRY.register(MQASystemCpuMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'queue_managers'): REGISTRY.register(MQAQueueManagersMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'queue_managers_channels'): REGISTRY.register(MQAQueueManagersChannelsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'queue_managers_queues'): REGISTRY.register(MQAQueueManagersQueuesMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'raid_battery_module'): REGISTRY.register(MQARaidBatteryModuleMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'raid_physical_drive'): REGISTRY.register(MQARaidPhysicalDriveMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'raid_ssd'): REGISTRY.register(MQARaidSsdMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'system_memory'): REGISTRY.register(MQASystemMemoryMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'tcp_summary'): REGISTRY.register(MQATCPSummaryMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'temperature_sensors'): REGISTRY.register(MQATemperatureSensorsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) if args.config == None or config.getboolean('collectors', 'voltage_sensors'): REGISTRY.register(MQAVoltageSensorsMetrics(args.appliance, args.ip, args.port, session, args.timeout)) except NoSectionError as err: logging.error('Invalid exporter configuration file \'' + args.config + '\', ' + str(err)) logging.info('Exporter has terminated') sys.exit(1) except NoOptionError as err: logging.error('Invalid exporter configuration file \'' + args.config + '\', ' + str(err)) logging.info('Exporter has terminated') sys.exit(1) REGISTRY.register(MQAExporterInformationMetrics(args.appliance)) # Start the HTTP server serving the metrics start_http_server(args.httpPort) while True: time.sleep(5)
def main(): # Build parser to handle the command line options parser = argparse.ArgumentParser( description='MQ Appliance Prometheus Exporter Stop Utility - ' + get_version()) parser.add_argument('-a', '--appliance', type=str, required=False, default='*', help='Name of the appliance') parser.add_argument( '-d', '--directory', type=str, required=False, default='', help='Path to directory for PID files (defaults to current directory)') parser.add_argument( '-f', '--file', type=str, required=False, help='Name of the file with the exporters configuration (CSV)') # Process command line options args = parser.parse_args() args.directory = resolve_directory(args) print('MQ Appliance Prometheus Exporter Stop Utility - ' + get_version()) print('Exporter directory is \'' + args.directory + '\'') # Build list of pid files file_list = get_pid_file_list(args.file, args.directory, args.appliance) # Search for exporters by looking at pid files exporter_count_stopped = 0 exporter_count_not_stopped = 0 for file in file_list: appliance = os.path.basename(os.path.splitext(file)[0]) exporter_running, pid = get_pid_file_present( os.path.split(file)[0] + '/', appliance) if not exporter_running: print('Exporter for appliance \'' + appliance + '\' is already stopped') continue try: os.kill(int(pid), signal.SIGTERM) os.remove(file) print('Exporter for appliance \'' + appliance + '\' with PID ' + pid + ' has been stopped') exporter_count_stopped += 1 except Exception as err: print( 'Error occurred while trying to stop the exporter for appliance \'' + appliance + '\'') print('The error is: ' + str(err)) exporter_count_not_stopped += 1 if exporter_count_stopped == 0 and exporter_count_not_stopped == 0: print( 'No exporter found or the specific exporter(s) are not running, or' ) print( 'you may be NOT be looking at the correct directory for the PID files!' ) else: print(str(exporter_count_stopped) + ' exporter(s) have been stopped.') print( str(exporter_count_not_stopped) + ' exporter(s) have NOT been stopped.')