def init(self): """Init the connection to the ES server.""" if not self.export_enable: return None try: es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) except Exception as e: logger.critical( "Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e)) sys.exit(2) else: logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port)) try: index_count = es.count(index=self.index)['count'] except Exception as e: # Index did not exist, it will be created at the first write # Create it... es.indices.create(self.index) else: logger.info( "There is already %s entries in the ElasticSearch %s index" % (index_count, self.index)) return es
def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: return None try: db = InfluxDBClient(host=self.host, port=self.port, username=self.user, password=self.password, database=self.db) get_all_db = [i['name'] for i in db.get_list_database()] self.version = INFLUXDB_09PLUS except InfluxDBClientError: # https://github.com/influxdb/influxdb-python/issues/138 logger.info("Trying fallback to InfluxDB v0.8") db = InfluxDBClient08(host=self.host, port=self.port, username=self.user, password=self.password, database=self.db) get_all_db = [i['name'] for i in db.get_list_database()] self.version = INFLUXDB_08 except InfluxDBClientError08 as e: logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e)) sys.exit(2) if self.db in get_all_db: logger.info( "Stats will be exported to InfluxDB server: {}".format(db._baseurl)) else: logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db) sys.exit(2) return db
def __init__(self, requestHandler=GlancesXMLRPCHandler, cached_time=1, config=None, args=None): # Args self.args = args # Init the XML RPC server try: self.server = GlancesXMLRPCServer(args.bind_address, args.port, requestHandler) except Exception as e: logger.critical("Cannot start Glances server: {}".format(e)) sys.exit(2) # The users dict # username / password couple # By default, no auth is needed self.server.user_dict = {} self.server.isAuth = False # Register functions self.server.register_introspection_functions() self.server.register_instance(GlancesInstance(cached_time, config)) if not self.args.disable_autodiscover: # Note: The Zeroconf service name will be based on the hostname # Correct issue: Zeroconf problem with zeroconf service name #889 self.autodiscover_client = GlancesAutoDiscoverClient(socket.gethostname().split('.', 1)[0], args) else: logger.info("Glances autodiscover announce is disabled")
def load_conf(self, section="influxdb"): """Load the InfluxDb configuration in the Glances configuration file.""" if self.config is None: return False try: self.host = self.config.get_value(section, 'host') self.port = self.config.get_value(section, 'port') self.user = self.config.get_value(section, 'user') self.password = self.config.get_value(section, 'password') self.db = self.config.get_value(section, 'db') except NoSectionError: logger.critical("No InfluxDB configuration found") return False except NoOptionError as e: logger.critical("Error in the InfluxDB configuration (%s)" % e) return False else: logger.debug("Load InfluxDB from the Glances configuration file") # Prefix is optional try: self.prefix = self.config.get_value(section, 'prefix') except NoOptionError: pass # Tags are optional, comma separated key:value pairs. try: self.tags = self.config.get_value(section, 'tags') except NoOptionError: pass return True
def serve_forever(self): """Main client loop.""" # Test if client and server are in the same major version if not self.login(): logger.critical("The server version is not compatible with the client") self.end() return self.client_mode exitkey = False try: while True and not exitkey: # Update the stats cs_status = self.update() # Update the screen if not self.quiet: exitkey = self.screen.update(self.stats, cs_status=cs_status, return_to_browser=self.return_to_browser) # Export stats using export modules self.stats.export(self.stats) except Exception as e: logger.critical(e) self.end() return self.client_mode
def _load_plugin(self, plugin_script, args=None, config=None): """Load the plugin (script), init it and add to the _plugin dict.""" # The key is the plugin name # for example, the file glances_xxx.py # generate self._plugins_list["xxx"] = ... name = plugin_script[len(self.header):-3].lower() try: # Import the plugin plugin = __import__(plugin_script[:-3]) # Init and add the plugin to the dictionary if name in ('help', 'amps', 'ports', 'folders'): self._plugins[name] = plugin.Plugin(args=args, config=config) else: self._plugins[name] = plugin.Plugin(args=args) except Exception as e: # If a plugin can not be loaded, display a critical message # on the console but do not crash logger.critical( "Error while initializing the {} plugin ({})".format(name, e)) logger.error(traceback.format_exc()) # Disable the plugin if args is not None: setattr(args, 'disable_' + name, False) else: # Set the disable_<name> to False by default if args is not None: setattr(args, 'disable_' + name, getattr(args, 'disable_' + name, False))
def __init__(self, config=None, args=None): """Init the MQTT export IF.""" super(Export, self).__init__(config=config, args=args) # Mandatories configuration keys (additional to host and port) self.user = None self.password = None self.topic = None self.tls = 'true' # Load the MQTT configuration file self.export_enable = self.load_conf('mqtt', mandatories=['host', 'password'], options=['port', 'user', 'topic', 'tls', 'topic_structure']) if not self.export_enable: exit('Missing MQTT config') # Get the current hostname self.hostname = socket.gethostname() self.port = int(self.port) or 8883 self.topic = self.topic or 'glances' self.user = self.user or 'glances' self.tls = (self.tls and self.tls.lower() == 'true') self.topic_structure = (self.topic_structure or 'per-metric').lower() if self.topic_structure not in ['per-metric', 'per-plugin']: logger.critical("topic_structure must be either 'per-metric' or 'per-plugin'.") return None # Init the MQTT client self.client = self.init()
def log_and_exit(self, msg=''): """Log and exit.""" if not self.return_to_browser: logger.critical(msg) sys.exit(2) else: logger.error(msg)
def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: return None # Cluster try: cluster = Cluster([self.host], port=int(self.port), protocol_version=int(self.protocol_version)) session = cluster.connect() except Exception as e: logger.critical("Cannot connect to Cassandra cluster '%s:%s' (%s)" % (self.host, self.port, e)) sys.exit(2) # Keyspace try: session.set_keyspace(self.keyspace) except InvalidRequest as e: logger.info("Create keyspace {} on the Cassandra cluster".format(self.keyspace)) c = "CREATE KEYSPACE %s WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '%s' }" % (self.keyspace, self.replication_factor) session.execute(c) session.set_keyspace(self.keyspace) logger.info( "Stats will be exported to Cassandra cluster {} ({}) in keyspace {}".format( cluster.metadata.cluster_name, cluster.metadata.all_hosts(), self.keyspace)) # Table try: session.execute("CREATE TABLE %s (plugin text, time timeuuid, stat map<text,float>, PRIMARY KEY (plugin, time)) WITH CLUSTERING ORDER BY (time DESC)" % self.table) except Exception: logger.debug("Cassandra table %s already exist" % self.table) return cluster, session
def load_conf(self, section="cassandra"): """Load the Cassandra configuration in the Glances configuration file.""" if self.config is None: return False try: self.host = self.config.get_value(section, 'host') self.port = self.config.get_value(section, 'port') self.keyspace = self.config.get_value(section, 'keyspace') except NoSectionError: logger.critical("No Cassandra configuration found") return False except NoOptionError as e: logger.critical("Error in the Cassandra configuration (%s)" % e) return False else: logger.debug("Load Cassandra from the Glances configuration file") # Optionals keys try: self.protocol_version = self.config.get_value( section, 'protocol_version') except NoOptionError: pass try: self.replication_factor = self.config.get_value( section, 'replication_factor') except NoOptionError: pass try: self.table = self.config.get_value(section, 'table') except NoOptionError: self.table = self.host return True
def __init__(self, config=None, args=None): """Init the JSON export IF.""" super(Export, self).__init__(config=config, args=args) # JSON file name self.json_filename = args.export_json_file # Set the JSON output file try: if PY3: self.json_file = open(self.json_filename, 'w') self.json_file.close() else: self.json_file = open(self.json_filename, 'wb') self.json_file.close() except IOError as e: logger.critical("Cannot create the JSON file: {}".format(e)) sys.exit(2) logger.info("Exporting stats to file: {}".format(self.json_filename)) self.export_enable = True # Buffer for dict of stats self.buffer = {}
def load_conf(self, section="cassandra"): """Load the Cassandra configuration in the Glances configuration file.""" if self.config is None: return False try: self.host = self.config.get_value(section, 'host') self.port = self.config.get_value(section, 'port') self.keyspace = self.config.get_value(section, 'keyspace') except NoSectionError: logger.critical("No Cassandra configuration found") return False except NoOptionError as e: logger.critical("Error in the Cassandra configuration (%s)" % e) return False else: logger.debug("Load Cassandra from the Glances configuration file") # Optionals keys try: self.protocol_version = self.config.get_value(section, 'protocol_version') except NoOptionError: pass try: self.replication_factor = self.config.get_value(section, 'replication_factor') except NoOptionError: pass try: self.table = self.config.get_value(section, 'table') except NoOptionError: self.table = self.host return True
def __init__(self, requestHandler=GlancesXMLRPCHandler, cached_time=1, config=None, args=None): # Args self.args = args # Init the XML RPC server try: self.server = GlancesXMLRPCServer(args.bind_address, args.port, requestHandler) except Exception as e: logger.critical("Cannot start Glances server: {0}".format(e)) sys.exit(2) # The users dict # username / password couple # By default, no auth is needed self.server.user_dict = {} self.server.isAuth = False # Register functions self.server.register_introspection_functions() self.server.register_instance(GlancesInstance(cached_time, config)) if not self.args.disable_autodiscover: # Note: The Zeroconf service name will be based on the hostname self.autodiscover_client = GlancesAutoDiscoverClient( socket.gethostname(), args) else: logger.info("Glances autodiscover announce is disabled")
def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: return None try: db = InfluxDBClient(host=self.host, port=self.port, ssl=(self.protocol.lower() == 'https'), verify_ssl=False, username=self.user, password=self.password, database=self.db) get_all_db = [i['name'] for i in db.get_list_database()] except InfluxDBClientError as e: logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e)) sys.exit(2) if self.db in get_all_db: logger.info( "Stats will be exported to InfluxDB server: {}".format(db._baseurl)) else: logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db) sys.exit(2) return db
def init(self): """Init the connection to the CouchDB server.""" if not self.export_enable: return None if self.user is None: server_uri = 'http://{}:{}/'.format(self.host, self.port) else: server_uri = 'http://{}:{}@{}:{}/'.format(self.user, self.password, self.host, self.port) try: s = couchdb.Server(server_uri) except Exception as e: logger.critical("Cannot connect to CouchDB server %s (%s)" % (server_uri, e)) sys.exit(2) else: logger.info("Connected to the CouchDB server %s" % server_uri) try: s[self.db] except Exception as e: # Database did not exist # Create it... s.create(self.db) else: logger.info("There is already a %s database" % self.db) return s
def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: return None url = '{}://{}:{}'.format(self.protocol, self.host, self.port) try: client = InfluxDBClient(url=url, enable_gzip=False, org=self.org, token=self.token) except Exception as e: logger.critical("Cannot connect to InfluxDB server '%s' (%s)" % (url, e)) sys.exit(2) else: logger.info("Connected to InfluxDB server version {} ({})".format( client.health().version, client.health().message)) # Create the write client write_client = client.write_api( write_options=WriteOptions(batch_size=500, flush_interval=10000, jitter_interval=2000, retry_interval=5000, max_retries=5, max_retry_delay=30000, exponential_base=2)) return write_client
def load_conf(self, section="opentsdb"): """Load the OpenTSDB configuration in the Glances configuration file.""" if self.config is None: return False try: self.host = self.config.get_value(section, 'host') self.port = self.config.get_value(section, 'port') except NoSectionError: logger.critical("No OpenTSDB configuration found") return False except NoOptionError as e: logger.critical("Error in the OpenTSDB configuration (%s)" % e) return False else: logger.debug("Load OpenTSDB from the Glances configuration file") # Prefix is optional try: self.prefix = self.config.get_value(section, 'prefix') except NoOptionError: pass # Tags are optional, comma separated key:value pairs. try: self.tags = self.config.get_value(section, 'tags') except NoOptionError: pass return True
def __init__(self, requestHandler=GlancesXMLRPCHandler, config=None, args=None): # Args self.args = args # Init the XML RPC server try: self.server = GlancesXMLRPCServer(args.bind_address, args.port, requestHandler) except Exception as e: logger.critical("Cannot start Glances server: {}".format(e)) sys.exit(2) else: print('Glances XML-RPC server is running on {}:{}'.format(args.bind_address, args.port)) # The users dict # username / password couple # By default, no auth is needed self.server.user_dict = {} self.server.isAuth = False # Register functions self.server.register_introspection_functions() self.server.register_instance(GlancesInstance(config, args)) if not self.args.disable_autodiscover: # Note: The Zeroconf service name will be based on the hostname # Correct issue: Zeroconf problem with zeroconf service name #889 self.autodiscover_client = GlancesAutoDiscoverClient(socket.gethostname().split('.', 1)[0], args) else: logger.info("Glances autodiscover announce is disabled")
def init(self): """Init the Prometheus Exporter""" try: start_http_server(port=int(self.port), addr=self.host) except Exception as e: logger.critical("Can not start Prometheus exporter on {}:{} ({})".format(self.host, self.port, e)) sys.exit(2) else: logger.info("Start Prometheus exporter on {}:{}".format(self.host, self.port))
def init(self): """Init the connection to the Riemann server.""" if not self.export_enable: return None try: client = bernhard.Client(host=self.riemann_host, port=self.riemann_port) return client except Exception as e: logger.critical("Connection to Riemann failed : %s " % e) return None
def update(self): """Update stats from Glances/SNMP server.""" if self.client_mode == 'glances': return self.update_glances() elif self.client_mode == 'snmp': return self.update_snmp() else: self.end() logger.critical("Unknown server mode: {}".format(self.client_mode)) sys.exit(2)
def init(self): """Init the connection to the Riemann server.""" if not self.export_enable: return None try: client = bernhard.Client(host=self.host, port=self.port) return client except Exception as e: logger.critical("Connection to Riemann failed : %s " % e) return None
def init(self): """Init the connection to the ES server.""" if not self.export_enable: return None self.index='{}-{}'.format(self.index, datetime.utcnow().strftime("%Y.%m.%d")) template_body = { "mappings": { "glances": { "dynamic_templates": [ { "integers": { "match_mapping_type": "long", "mapping": { "type": "integer" } } }, { "strings": { "match_mapping_type": "string", "mapping": { "type": "text", "fields": { "raw": { "type": "keyword", "ignore_above": 256 } } } } } ] } } } try: es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) except Exception as e: logger.critical("Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e)) sys.exit(2) else: logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port)) try: index_count = es.count(index=self.index)['count'] except Exception as e: # Index did not exist, it will be created at the first write # Create it... es.indices.create(index=self.index,body=template_body) else: logger.info("The index %s exists and holds %s entries." % (self.index, index_count)) return es
def init(self): """Init the connection to the OpenTSDB server.""" if not self.export_enable: return None try: db = potsdb.Client(self.host, port=int(self.port), check_host=True) except Exception as e: logger.critical("Cannot connect to OpenTSDB server %s:%s (%s)" % (self.host, self.port, e)) sys.exit(2) return db
def __init__(self, config=None, args=None): # Init self.config = config self.args = args # Init windows positions self.term_w = 80 self.term_h = 24 # Space between stats self.space_between_column = 3 self.space_between_line = 2 # Init the curses screen self.screen = curses.initscr() if not self.screen: logger.critical("Cannot init the curses library.\n") sys.exit(1) # Load the 'outputs' section of the configuration file # - Init the theme (default is black) self.theme = {'name': 'black'} # Load configuration file self.load_config(config) # Init cursor self._init_cursor() # Init the colors self._init_colors() # Init main window self.term_window = self.screen.subwin(0, 0) # Init refresh time self.__refresh_time = args.time # Init edit filter tag self.edit_filter = False # Init the process min/max reset self.args.reset_minmax_tag = False # Catch key pressed with non blocking mode self.term_window.keypad(1) self.term_window.nodelay(1) self.pressedkey = -1 # History tag self._init_history()
def init(self): """Init the connection to the rabbitmq server.""" if not self.export_enable: return None try: parameters = pika.URLParameters('amqp://' + self.user + ':' + self.password + '@' + self.host + ':' + self.port + '/') connection = pika.BlockingConnection(parameters) channel = connection.channel() return channel except Exception as e: logger.critical("Connection to rabbitMQ failed : %s " % e) return None
def init(self): """Init the connection to the MQTT server.""" if not self.export_enable: return None try: client = paho.Client(client_id='glances_' + self.hostname, clean_session=False) client.username_pw_set(username=self.user, password=self.password) client.tls_set(certs.where()) client.connect(host=self.host, port=self.port) client.loop_start() return client except Exception as e: logger.critical("Connection to MQTT server failed : %s " % e) return None
def __init__(self, config=None, args=None): """Init the export IF.""" super(Export, self).__init__(config=config, args=args) # Load the Graph configuration file section (is exists) self.export_enable = self.load_conf( 'graph', options=['path', 'generate_every', 'width', 'height', 'style']) # Manage options (command line arguments overwrite configuration file) self.path = args.export_graph_path or self.path self.generate_every = int(getattr(self, 'generate_every', 0)) self.width = int(getattr(self, 'width', 800)) self.height = int(getattr(self, 'height', 600)) self.style = getattr(pygal.style, getattr(self, 'style', 'DarkStyle'), pygal.style.DarkStyle) # Create export folder try: os.makedirs(self.path) except OSError as e: if e.errno != errno.EEXIST: logger.critical( "Cannot create the Graph output folder {} ({})".format( self.path, e)) sys.exit(2) # Check if output folder is writeable try: tempfile.TemporaryFile(dir=self.path) except OSError as e: logger.critical("Graph output folder {} is not writeable".format( self.path)) sys.exit(2) logger.info("Graphs will be created in the {} folder".format( self.path)) logger.info( "Graphs will be created when 'g' key is pressed (in the CLI interface)" ) if self.generate_every != 0: logger.info( "Graphs will be created automatically every {} seconds".format( self.generate_every)) # Start the timer self._timer = Timer(self.generate_every) else: self._timer = None
def load_conf(self, section="riemann"): """Load the Riemann configuration in the Glances configuration file.""" if self.config is None: return False try: self.riemann_host = self.config.get_value(section, 'host') self.riemann_port = int(self.config.get_value(section, 'port')) except NoSectionError: logger.critical("No riemann configuration found") return False except NoOptionError as e: logger.critical("Error in the Riemann configuration (%s)" % e) return False else: logger.debug("Load Riemann from the Glances configuration file") return True
def init(self): """Init the connection to the rabbitmq server.""" if not self.export_enable: return None try: parameters = pika.URLParameters( 'amqp://' + self.rabbitmq_user + ':' + self.rabbitmq_password + '@' + self.rabbitmq_host + ':' + self.rabbitmq_port + '/') connection = pika.BlockingConnection(parameters) channel = connection.channel() return channel except Exception as e: logger.critical("Connection to rabbitMQ failed : %s " % e) return None
def init(self): """Init the connection to the Cassandra server.""" if not self.export_enable: return None # if username and/or password are not set the connection will try to connect with no auth auth_provider = PlainTextAuthProvider(username=self.username, password=self.password) # Cluster try: cluster = Cluster([self.host], port=int(self.port), protocol_version=int(self.protocol_version), auth_provider=auth_provider) session = cluster.connect() except Exception as e: logger.critical( "Cannot connect to Cassandra cluster '%s:%s' (%s)" % (self.host, self.port, e)) sys.exit(2) # Keyspace try: session.set_keyspace(self.keyspace) except InvalidRequest as e: logger.info("Create keyspace {} on the Cassandra cluster".format( self.keyspace)) c = "CREATE KEYSPACE %s WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '%s' }" % ( self.keyspace, self.replication_factor) session.execute(c) session.set_keyspace(self.keyspace) logger.info( "Stats will be exported to Cassandra cluster {} ({}) in keyspace {}" .format(cluster.metadata.cluster_name, cluster.metadata.all_hosts(), self.keyspace)) # Table try: session.execute( "CREATE TABLE %s (plugin text, time timeuuid, stat map<text,float>, PRIMARY KEY (plugin, time)) WITH CLUSTERING ORDER BY (time DESC)" % self.table) except Exception: logger.debug("Cassandra table %s already exist" % self.table) return cluster, session
def get_password(self, description='', confirm=False, clear=False): """Get the password from a Glances client or server. For Glances server, get the password (confirm=True, clear=False): 1) from the password file (if it exists) 2) from the CLI Optionally: save the password to a file (hashed with salt + SHA-256) For Glances client, get the password (confirm=False, clear=True): 1) from the CLI 2) the password is hashed with SHA-256 (only SHA string transit through the network) """ if os.path.exists(self.password_file) and not clear: # If the password file exist then use it logger.info("Read password from file {}".format( self.password_file)) password = self.load_password() else: # password_sha256 is the plain SHA-256 password # password_hashed is the salt + SHA-256 password password_sha256 = self.sha256_hash(getpass.getpass(description)) password_hashed = self.hash_password(password_sha256) if confirm: # password_confirm is the clear password (only used to compare) password_confirm = self.sha256_hash( getpass.getpass('Password (confirm): ')) if not self.check_password(password_hashed, password_confirm): logger.critical("Sorry, passwords do not match. Exit.") sys.exit(1) # Return the plain SHA-256 or the salted password if clear: password = password_sha256 else: password = password_hashed # Save the hashed password to the password file if not clear: save_input = input( 'Do you want to save the password? [Yes/No]: ') if len(save_input) > 0 and save_input[0].upper() == 'Y': self.save_password(password_hashed) return password
def init(self): """Init the connection to the ES server.""" if not self.export_enable: return None try: es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) except Exception as e: logger.critical( "Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e)) sys.exit(2) else: logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port)) return es
def init(self): """Init the connection to the CouchDB server.""" if not self.export_enable: return None server_uri = 'tcp://{}:{}'.format(self.host, self.port) try: self.context = zmq.Context() publisher = self.context.socket(zmq.PUB) publisher.bind(server_uri) except Exception as e: logger.critical("Cannot connect to ZeroMQ server %s (%s)" % (server_uri, e)) sys.exit(2) else: logger.info("Connected to the ZeroMQ server %s" % server_uri) return publisher
def load_conf(self, section="elasticsearch"): """Load the ES configuration in the Glances configuration file.""" if self.config is None: return False try: self.host = self.config.get_value(section, 'host') self.port = self.config.get_value(section, 'port') self.index = self.config.get_value(section, 'index') except NoSectionError: logger.critical("No ElasticSearch configuration found") return False except NoOptionError as e: logger.critical("Error in the ElasticSearch configuration (%s)" % e) return False else: logger.debug("Load ElasticSearch from the Glances configuration file") return True
def __init__(self, config=None, args=None): """Init the export IF.""" super(Export, self).__init__(config=config, args=args) # Load the Graph configuration file section (is exists) self.export_enable = self.load_conf('graph', options=['path', 'generate_every', 'width', 'height', 'style']) # Manage options (command line arguments overwrite configuration file) self.path = args.export_graph_path or self.path self.generate_every = int(getattr(self, 'generate_every', 0)) self.width = int(getattr(self, 'width', 800)) self.height = int(getattr(self, 'height', 600)) self.style = getattr(pygal.style, getattr(self, 'style', 'DarkStyle'), pygal.style.DarkStyle) # Create export folder try: os.makedirs(self.path) except OSError as e: if e.errno != errno.EEXIST: logger.critical("Cannot create the Graph output folder {} ({})".format(self.path, e)) sys.exit(2) # Check if output folder is writeable try: tempfile.TemporaryFile(dir=self.path) except OSError as e: logger.critical("Graph output folder {} is not writeable".format(self.path)) sys.exit(2) logger.info("Graphs will be created in the {} folder".format(self.path)) logger.info("Graphs will be created when 'g' key is pressed (in the CLI interface)") if self.generate_every != 0: logger.info("Graphs will be created automatically every {} seconds".format(self.generate_every)) # Start the timer self._timer = Timer(self.generate_every) else: self._timer = None
def load_conf(self, section="rabbitmq"): """Load the rabbitmq configuration in the Glances configuration file.""" if self.config is None: return False try: self.rabbitmq_host = self.config.get_value(section, 'host') self.rabbitmq_port = self.config.get_value(section, 'port') self.rabbitmq_user = self.config.get_value(section, 'user') self.rabbitmq_password = self.config.get_value(section, 'password') self.rabbitmq_queue = self.config.get_value(section, 'queue') except NoSectionError: logger.critical("No rabbitmq configuration found") return False except NoOptionError as e: logger.critical("Error in the RabbitM configuration (%s)" % e) return False else: logger.debug("Load RabbitMQ from the Glances configuration file") return True
def _load_plugin(self, plugin_script, args=None, config=None): """Load the plugin (script), init it and add to the _plugin dict""" # The key is the plugin name # for example, the file glances_xxx.py # generate self._plugins_list["xxx"] = ... name = plugin_script[len(self.header):-3].lower() try: # Import the plugin plugin = __import__(plugin_script[:-3]) # Init and add the plugin to the dictionary if name in ('help', 'amps', 'ports'): self._plugins[name] = plugin.Plugin(args=args, config=config) else: self._plugins[name] = plugin.Plugin(args=args) except Exception as e: # If a plugin can not be log, display a critical message # on the console but do not crash logger.critical("Error while initializing the {} plugin ({})".format(name, e)) logger.error(traceback.format_exc())
def init(self): """Init the connection to the Kafka server.""" if not self.export_enable: return None # Build the server URI with host and port server_uri = '{}:{}'.format(self.host, self.port) try: s = KafkaProducer(bootstrap_servers=server_uri, value_serializer=lambda v: json.dumps(v).encode('utf-8'), compression_type=self.compression) except Exception as e: logger.critical("Cannot connect to Kafka server %s (%s)" % (server_uri, e)) sys.exit(2) else: logger.info("Connected to the Kafka server %s" % server_uri) return s
def get_password(self, description='', confirm=False, clear=False): """Get the password from a Glances client or server. For Glances server, get the password (confirm=True, clear=False): 1) from the password file (if it exists) 2) from the CLI Optionally: save the password to a file (hashed with salt + SHA-256) For Glances client, get the password (confirm=False, clear=True): 1) from the CLI 2) the password is hashed with SHA-256 (only SHA string transit through the network) """ if os.path.exists(self.password_filepath) and not clear: # If the password file exist then use it logger.info("Read password from file {0}".format(self.password_filepath)) password = self.load_password() else: # password_sha256 is the plain SHA-256 password # password_hashed is the salt + SHA-256 password password_sha256 = self.sha256_hash(getpass.getpass(description)) password_hashed = self.hash_password(password_sha256) if confirm: # password_confirm is the clear password (only used to compare) password_confirm = self.sha256_hash(getpass.getpass('Password (confirm): ')) if not self.check_password(password_hashed, password_confirm): logger.critical("Sorry, passwords do not match. Exit.") sys.exit(1) # Return the plain SHA-256 or the salted password if clear: password = password_sha256 else: password = password_hashed # Save the hashed password to the password file if not clear: save_input = input('Do you want to save the password? [Yes/No]: ') if len(save_input) > 0 and save_input[0].upper() == 'Y': self.save_password(password_hashed) return password
def __init__(self, args=None): # Init args self.args = args # Init windows positions self.term_w = 80 self.term_h = 24 # Space between stats self.space_between_column = 3 self.space_between_line = 2 # Init the curses screen self.screen = curses.initscr() if not self.screen: logger.critical("Cannot init the curses library.\n") sys.exit(1) # Init cursor self._init_cursor() # Init the colors self._init_colors() # Init main window self.term_window = self.screen.subwin(0, 0) # Init refresh time self.__refresh_time = args.time # Init edit filter tag self.edit_filter = False # Init the process min/max reset self.args.reset_minmax_tag = False # Catch key pressed with non blocking mode self.no_flash_cursor() self.term_window.nodelay(1) self.pressedkey = -1 # History tag self._init_history()
def serve_forever(self): """Main client loop.""" exitkey = False try: while True and not exitkey: # Update the stats cs_status = self.update() # Update the screen exitkey = self.screen.update(self.stats, cs_status=cs_status, return_to_browser=self.return_to_browser) # Export stats using export modules self.stats.export(self.stats) except Exception as e: logger.critical(e) self.end() return self.client_mode
def load_conf(self, section="statsd"): """Load the Statsd configuration in the Glances configuration file.""" if self.config is None: return False try: self.host = self.config.get_value(section, 'host') self.port = self.config.get_value(section, 'port') except NoSectionError: logger.critical("No Statsd configuration found") return False except NoOptionError as e: logger.critical("Error in the Statsd configuration (%s)" % e) return False else: logger.debug("Load Statsd from the Glances configuration file") # Prefix is optional try: self.prefix = self.config.get_value(section, 'prefix') except NoOptionError: pass return True
def __init__(self, config=None, args=None): """Init the CSV export IF.""" super(Export, self).__init__(config=config, args=args) # CSV file name self.csv_filename = args.export_csv # Set the CSV output file try: if PY3: self.csv_file = open(self.csv_filename, 'w', newline='') else: self.csv_file = open(self.csv_filename, 'wb') self.writer = csv.writer(self.csv_file) except IOError as e: logger.critical("Cannot create the CSV file: {}".format(e)) sys.exit(2) logger.info("Stats exported to CSV file: {}".format(self.csv_filename)) self.export_enable = True self.first_line = True
def init(self): """Init the connection to the ES server.""" if not self.export_enable: return None try: es = Elasticsearch(hosts=['{0}:{1}'.format(self.host, self.port)]) except Exception as e: logger.critical("Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e)) sys.exit(2) else: logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port)) try: index_count = es.count(index=self.index)['count'] except Exception as e: # Index did not exist, it will be created at the first write # Create it... es.indices.create(self.index) else: logger.info("There is already %s entries in the ElasticSearch %s index" % (index_count, self.index)) return es
def __init__(self, config=None, args=None): """Init the JSON export IF.""" super(Export, self).__init__(config=config, args=args) # JSON file name self.json_filename = args.export_json_file # Set the JSON output file try: if PY3: self.json_file = open(self.json_filename, 'w') else: self.json_file = open(self.json_filename, 'wb') except IOError as e: logger.critical("Cannot create the JSON file: {}".format(e)) sys.exit(2) logger.info("Exporting stats to file: {}".format(self.json_filename)) self.export_enable = True # Buffer for dict of stats self.buffer = {}
def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: return None try: db = InfluxDBClient(host=self.host, port=self.port, username=self.user, password=self.password, database=self.db) get_all_db = [i['name'] for i in db.get_list_database()] except InfluxDBClientError as e: logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e)) sys.exit(2) if self.db in get_all_db: logger.info( "Stats will be exported to InfluxDB server: {}".format(db._baseurl)) else: logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db) sys.exit(2) return db