def worker_vacuum(commands, command, config): start_time = time.time() * 1000 set_logger_name("vacuum_worker") logger = get_logger(config) logger.info("Starting with pid=%s" % (os.getpid())) logger.debug("commandid=%s" % (command.commandid, )) try: command.state = COMMAND_START command.time = time.time() commands.update(command) parameters = pickle.loads(base64.b64decode(command.parameters)) logger.debug("table=%s, mode=%s, database=%s" % ( parameters['table'], parameters['mode'], parameters['database'], )) conn = connector(host=config.postgresql['host'], port=config.postgresql['port'], user=config.postgresql['user'], password=config.postgresql['password'], database=parameters['database']) conn.connect() if parameters['mode'] == 'standard': query = "VACUUM %s" % (parameters['table'], ) else: query = "VACUUM %s %s" % ( parameters['mode'], parameters['table'], ) conn.execute(query) conn.close() except (error, SharedItem_not_found, Exception) as e: command.state = COMMAND_ERROR command.result = str(e) command.time = time.time() logger.traceback(get_tb()) logger.error(str(e)) try: commands.update(command) conn.close() except Exception as e: pass logger.info("Failed.") return try: command.state = COMMAND_DONE command.time = time.time() commands.update(command) except Exception as e: logger.traceback(get_tb()) logger.error(str(e)) logger.info("Done.") logger.debug(" in %s s." % (str( (time.time() * 1000 - start_time) / 1000), ))
def post_activity_kill(http_context, queue_in=None, config=None, sessions=None, commands=None): """ @api {post} /activity/kill Terminate N backends. @apiVersion 0.0.1 @apiName ActivityKill @apiGroup Activity @apiHeader {String} X-Session Session ID. @apiParam {String[]} pids List of process ID to terminate. @apiSuccess {Object[]} response.backends List of backend status. @apiSuccess {Number} response.backends.pid Process ID of this backend. @apiSuccess {Boolean} response.backends.killes Was the backend killed ? @apiExample {curl} Example usage: curl -k -X POST -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ -H "Content-Type: application/json" --data '{"pids": [ 13309 ]}' \ "https://localhost:2345/activity/kill" @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:57:52 GMT Content-type: application/json { "backends": [ {"pid": 13309, "killed": true}, ... ] } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name('activity') return api_function_wrapper_pg(config, http_context, sessions, activity_functions, 'post_activity_kill')
def post_pg_ident(conn, config, http_context): set_logger_name("settings") logger = get_logger(config) if 'content' not in http_context['post']: raise HTTPError(406, "Parameter 'content' not sent.") try: conn.execute( "SELECT setting FROM pg_settings WHERE name = 'ident_file'") pg_ident_file = list(conn.get_rows())[0]['setting'] except error as e: logger.error(str(e.message)) raise HTTPError(500, 'Internal error.') with open(pg_ident_file, 'r') as fd: pg_ident_data = fd.read() fd.close() try: with open(pg_ident_file + ".previous", 'w') as fdp: fdp.write(pg_ident_data) fdp.close() except Exception as e: raise HTTPError(500, 'Internal error.') with open(pg_ident_file, 'w') as fd: fd.write(http_context['post']['content']) fd.close() return {'update': True}
def __init__(self, cmd_queue, commands, config, sessions, *args, **kwargs): """ Constructor. """ # Commands queue. self.cmd_queue = cmd_queue # Commands array in shared memory. self.commands = commands # Sessions array in shared memory. self.sessions = sessions # Configuration instance. self.config = config # Logger. set_logger_name("httpd") self.logger = get_logger(config) # HTTP server version. self.server_version = "temboard-agent/0.0.1" # HTTP request method self.http_method = None # HTTP query. self.query = None # HTTP POST content in json format. self.post_json = None # Call HTTP request handler constructor. BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
def dashboard_n_cpu(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /dashboard/n_cpu Number of CPU @apiVersion 0.0.1 @apiName GetDasboardNCpu @apiGroup Dashboard @apiHeader {String} X-Session Session ID. @apiSuccess {Number} n_cpu Number of CPU. @apiExample {curl} Example usage: curl -H "X-Session: <session-id>" http://localhost:12345/dashboard/n_cpu @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 11:49:19 GMT Content-type: application/json {"n_cpu": 4} @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} """ set_logger_name("dashboard") return api_function_wrapper(config, http_context, sessions, metrics, 'get_n_cpu')
def dashboard_hitratio(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /dashboard/hitratio PostgreSQL cache hit ratio @apiVersion 0.0.1 @apiName GetDasboardHitratio @apiGroup Dashboard @apiHeader {String} X-Session Session ID. @apiSuccess {Number} hitratio PostgreSQL global cache/hit ratio (%) @apiExample {curl} Example usage: curl -H "X-Session: <session-id>" http://localhost:12345/dashboard/hitratio @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 11:27:27 GMT Content-type: application/json {"hitratio": 98.0} @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} """ set_logger_name("dashboard") return api_function_wrapper_pg(config, http_context, sessions, metrics, 'get_hitratio')
def dashboard_history(http_context, queue_in=None, config=None, sessions=None, commands=None): set_logger_name("dashboard") return api_function_wrapper(config, http_context, sessions, metrics, 'get_history_metrics_queue')
def get_hba_versions(conn, config, http_context): set_logger_name("settings") logger = get_logger(config) hba_file = get_setting(conn, 'hba_file') return { 'filepath': hba_file, 'versions': HBAManager.get_versions(hba_file) }
def dashboard_info(http_context, queue_in=None, config=None, sessions=None, commands=None): set_logger_name("dashboard") return api_function_wrapper_pg(config, http_context, sessions, metrics, 'get_info')
def api_pg_version(http_context, queue_in=None, config=None, sessions=None, commands=None): set_logger_name("administration") return api_function_wrapper_pg(config, http_context, sessions, admin_functions, 'pg_version')
def post_pg_hba_raw(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {post} /settings/hba/raw Replace pg_hba.conf file content (raw mode). @apiVersion 0.0.1 @apiName PostSettingsHBARaw @apiGroup Settings @apiHeader {String} X-Session Session ID. @apiParam {String} content pg_hba.conf content (raw). @apiParam {Boolean} new_version Create a new version of current pg_hba.conf before writing new content. @apiSuccess {Object} response @apiSuccess {String} response.last_version pg_hba.conf last file version. @apiSuccess {String} response.filepath pg_hba.conf file path. @apiExample {curl} Example usage: curl -k -X POST -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ -H "Content-Type: application/json" \ --data '{"content": "local all all md5\r\n ...", "new_version": true}' \ https://localhost:2345/settings/hba/raw" @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.9 Date: Thu, 11 Feb 2016 14:26:19 GMT Access-Control-Allow-Origin: * Content-type: application/json { "last_version": "2016-02-11T15:26:19", "filepath": "/etc/postgresql/9.4/main/pg_hba.conf" } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name("settings") return api_function_wrapper_pg(config, http_context, sessions, settings_functions, 'post_hba_raw')
def get_pg_hba_versions(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /settings/hba/versions Get the list of pg_hba.conf versions. @apiVersion 0.0.1 @apiName GetSettingsHBAVersions @apiGroup Settings @apiHeader {String} X-Session Session ID. @apiSuccess {Object} response @apiSuccess {String[]} response.versions List of versions, desc. sorting. @apiSuccess {String} response.filepath pg_hba.conf file path. @apiExample {curl} Example usage: curl -k -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ https://localhost:2345/settings/hba/versions" @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.9 Date: Fri, 12 Feb 2016 10:38:10 GMT Access-Control-Allow-Origin: * Content-type: application/json { "versions": [ "2016-02-11T18:01:35", "2016-02-11T16:43:51", "2016-02-11T16:43:36", ... ], "filepath": "/etc/postgresql/9.4/main/pg_hba.conf"} @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name("settings") return api_function_wrapper_pg(config, http_context, sessions, settings_functions, 'get_hba_versions')
def get_hba_options(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /settings/hba/options Get HBA potential values for each column. @apiVersion 0.0.1 @apiName GetSettingsHBAOptions @apiGroup Settings @apiHeader {String} X-Session Session ID. @apiSuccess {Object} response @apiSuccess {String[]} response.connection Connection field potential values. @apiSuccess {String[]} response.database Database field potential values. @apiSuccess {String[]} response.user User field potential values. @apiSuccess {String[]} response.auth_method Authentication methods. @apiExample {curl} Example usage: curl -k -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ https://localhost:2345/settings/hba/options" @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.9 Date: Fri, 12 Feb 2016 10:48:57 GMT Access-Control-Allow-Origin: * Content-type: application/json { "connection": [ "local", "host", "hostssl", "hostnossl" ], "database": [ "all", "sameuser", "samerole", "db1" ], "user": [ "all", "user1", "+group1" ], "auth_method": [ "trust", "reject", ... ] } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name("settings") return api_function_wrapper_pg(config, http_context, sessions, settings_functions, 'get_hba_options')
def api_vacuum(http_context, queue_in = None, config = None, sessions = None, commands = None): set_logger_name("administration") worker = b'vacuum' # Get a new logger. logger = get_logger(config) try: check_sessionid(http_context['headers'], sessions) post = http_context['post'] # Check POST parameters. validate_parameters(post, [ ('database', T_OBJECTNAME, False), ('table', T_OBJECTNAME, False), ('mode', T_VACUUMMODE, False) ]) # Serialize parameters. parameters = base64.b64encode( pickle.dumps({ 'database': post['database'], 'table': post['table'], 'mode': post['mode'] })).decode('utf-8') except (Exception, HTTPError) as e: logger.traceback(get_tb()) logger.error(str(e)) if isinstance(e, HTTPError): raise e else: raise HTTPError(500, "Internal error.") # Check command uniqueness. try: commands.check_uniqueness(worker, parameters) except SharedItem_exists as e: logger.traceback(get_tb()) logger.error(str(e)) raise HTTPError(402, "Vaccum '%s' already running on table '%s'." % (post['mode'], post['table'])) cid = hash_id(worker + b'-' + parameters.encode('utf-8')) command = Command( cid.encode('utf-8'), time.time(), 0, worker, parameters, 0, u'') try: commands.add(command) # Put the Command in the command queue queue_in.put(command) return {"cid": cid} except SharedItem_no_free_slot_left as e: logger.traceback(get_tb()) logger.error(str(e)) raise HTTPError(500, "Internal error.")
def get_pg_configuration_categories(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /settings/configuration/categories Fetch settings categories names. @apiVersion 0.0.1 @apiName GetSettingsConfigurationCategories @apiGroup Settings @apiHeader {String} X-Session Session ID. @apiSuccess {String[]} categories List of settings category name. @apiExample {curl} Example usage: curl -k -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ https://localhost:2345/settings/configuration/categories @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:57:52 GMT Content-type: application/json { "categories": [ "Autovacuum", "Client Connection Defaults / Locale and Formatting", "Client Connection Defaults / Other Defaults", ... ] } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name("settings") return api_function_wrapper_pg(config, http_context, sessions, settings_functions, 'get_settings_categories')
def profile(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /profile Get current user name. @apiVersion 0.0.1 @apiName Profile @apiGroup User @apiHeader {String} X-Session Session ID. @apiSuccess {String} username Username. @apiExample {curl} Example usage: curl -k -H "X-Session: fa452548403ac53f2158a65f5eb6db9723d2b07238dd83f5b6d9ca52ce817b63" https://localhost:2345/profile @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 12:33:19 GMT Content-type: application/json { "username": "******" } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session ID. @apiError (406 error) error Session ID malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 12:36:33 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 12:37:23 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ headers = http_context['headers'] set_logger_name("api") logger = get_logger(config) check_sessionid(headers, sessions) logger.info("[profile] User session: %s" % (headers['X-Session'])) try: session = sessions.get_by_sessionid(headers['X-Session'].encode('utf-8')) return {'username': session.username} except SharedItem_not_found: raise HTTPError(401, "Invalid session.")
def supervision_sender_worker(commands, command, config): signal.signal(signal.SIGTERM, supervision_worker_sigterm_handler) start_time = time.time() * 1000 set_logger_name("supervision_sender_worker") logger = get_logger(config) # TODO: logging methods in supervision plugin must be aligned. logging.root = logger logger.info("Start pid=%s id=%s" % ( os.getpid(), command.commandid, )) command.state = COMMAND_START command.time = time.time() command.pid = os.getpid() commands.update(command) c = 0 while True: # Let's do it smoothly.. time.sleep(0.5) q = Queue('%s/metrics.q' % (config.temboard['home']), max_size=1024 * 1024 * 10, overflow_mode='slide') msg = q.shift(delete=False) if msg is None: break try: send_output(config.plugins['supervision']['ssl_ca_cert_file'], config.plugins['supervision']['collector_url'], config.plugins['supervision']['agent_key'], msg.content) except urllib2.HTTPError as e: logger.error("Failed to send data.") logger.debug(e.message) logger.info("End. Duration: %s." % (str(time.time() * 1000 - start_time))) # On an error 409 (DB Integrity) we need to remove the message. if int(e.code) != 409: return except Exception as e: logger.error("Failed to send data.") logger.debug(str(e)) logger.info("End. Duration: %s." % (str(time.time() * 1000 - start_time))) return _ = q.shift(delete=True, check_msg=msg) if c > 60: logger.info("End. Duration: %s." % (str(time.time() * 1000 - start_time))) return c += 1 logger.info("End. Duration: %s." % (str(time.time() * 1000 - start_time)))
def post_pg_ident(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {post} /settings/pg_ident Replace pg_ident.conf file content (raw mode). @apiVersion 0.0.1 @apiName PostSettingsPGIdentRaw @apiGroup Settings @apiHeader {String} X-Session Session ID. @apiParam {String} content pg_ident.conf content (raw). @apiSuccess {Object} response @apiSuccess {Boolean} response.update Has pg_ident.conf been updated ? @apiExample {curl} Example usage: curl -k -X POST -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ -H "Content-Type: application/json" \ --data '{"content": "# PostgreSQL User Name Maps\r\n ..."}' \ https://localhost:2345/settings/pg_ident" @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.9 Date: Thu, 11 Feb 2016 14:26:19 GMT Access-Control-Allow-Origin: * Content-type: application/json { "update": true } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name("settings") return api_function_wrapper_pg(config, http_context, sessions, settings_functions, 'post_pg_ident')
def get_pg_ident(http_context, queue_in = None, config = None, sessions = None, commands = None): """ @api {get} /settings/pg_ident Get pg_ident.conf raw content @apiVersion 0.0.1 @apiName GetSettingsPGIdentRaw @apiGroup Settings @apiHeader {String} X-Session Session ID. @apiSuccess {Object} response @apiSuccess {Object} response.content pg_ident.conf file raw content. @apiSuccess {String} response.filepath pg_ident.conf file path. @apiExample {curl} Example usage: curl -k -H "X-Session: 3b28ed94743e3ada57b217bbf9f36c6d1eb45e669a1ab693e8ca7ac3bd070b9e" \ https://localhost:2345/settings/pg_ident" @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.9 Date: Fri, 12 Feb 2016 10:48:57 GMT Access-Control-Allow-Origin: * Content-type: application/json { "content": "# PostgreSQL User Name Maps\r\n# =========================\r\n ... ", "filepath": "/etc/postgresql/9.4/main/pg_ident.conf" } @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiError (406 error) error Parameter 'X-Session' is malformed. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} @apiErrorExample 406 error example HTTP/1.0 406 Not Acceptable Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Parameter 'X-Session' is malformed."} """ set_logger_name("settings") return api_function_wrapper_pg(config, http_context, sessions, settings_functions, 'get_pg_ident')
def post_hba(conn, config, http_context): new_version = False set_logger_name("settings") logger = get_logger(config) # Push a notification. try: NotificationMgmt.push( config, Notification(username=http_context['username'], message="HBA file updated")) except NotificationError as e: logger.error(e.message) if 'entries' not in http_context['post']: raise HTTPError(406, "Parameter 'entries' not sent.") if http_context and 'new_version' in http_context['post']: # Check parameter 'version' validate_parameters(http_context['post'], [('new_version', T_NEW_VERSION, False)]) if http_context['post']['new_version'] is True: new_version = True hba_file = get_setting(conn, 'hba_file') hba_entries = [] logger.debug(http_context['post']['entries']) for entry in http_context['post']['entries']: if 'comment' in entry and len(entry['connection']) == 0: new_hba_entry = HBAComment() new_hba_entry.comment = entry['comment'] else: new_hba_entry = HBAEntry() try: new_hba_entry.connection = entry[ 'connection'] if 'connection' in entry else '' new_hba_entry.database = entry[ 'database'] if 'database' in entry else '' new_hba_entry.user = entry['user'] if 'user' in entry else '' new_hba_entry.address = entry[ 'address'] if 'address' in entry else '' new_hba_entry.auth_method = entry[ 'auth_method'] if 'auth_method' in entry else '' new_hba_entry.auth_options = entry[ 'auth_options'] if 'auth_options' in entry else '' except Exception as e: logger.error(e.message) raise HTTPError(406, "Invalid HBA entry.") new_hba_entry.lazy_check() hba_entries.append(new_hba_entry) return HBAManager.save_entries(hba_file, hba_entries, new_version)
def Worker(commands, command, config): """ Routing function in charge of calling the right worker function. """ # Add a signal handler on SIGTERM and SIGHUP signals. signal.signal(signal.SIGTERM, worker_sigterm_handler) signal.signal(signal.SIGHUP, worker_sighup_handler) try: get_worker(command.worker)(commands, command, config) except (AttributeError, Exception) as e: set_logger_name("scheduler") logger = get_logger(config) logger.error(str(e))
def get_command(http_context, queue_in = None, config = None, sessions = None, commands = None): set_logger_name("api") logger = get_logger(config) check_sessionid(http_context['headers'], sessions) cid = http_context['urlvars'][0] try: command = commands.get_by_commandid(cid.encode('utf-8')) c_time = command.time c_state = command.state c_result = command.result if c_state == COMMAND_DONE or c_state == COMMAND_ERROR: commands.delete(cid.encode('utf-8')) return {'cid': cid, 'time': c_time, 'state': c_state, 'result': c_result} except SharedItem_not_found: raise HTTPError(401, "Invalid command.")
def get_hba(conn, config, http_context): version = None set_logger_name("settings") logger = get_logger(config) if http_context and 'version' in http_context['query']: # Check parameter 'version' validate_parameters(http_context['query'], [('version', T_FILE_VERSION, True)]) version = http_context['query']['version'][0] ret = {'filepath': None, 'version': version, 'entries': []} hba_file = get_setting(conn, 'hba_file') ret['filepath'] = hba_file for hba_entry in HBAManager.get_entries(hba_file, version): ret['entries'].append(hba_entry.__dict__) return ret
def get_hba_raw(conn, config, http_context): version = None set_logger_name("settings") logger = get_logger(config) if http_context and 'version' in http_context['query']: # Check parameter 'version' validate_parameters(http_context['query'], [('version', T_FILE_VERSION, True)]) version = http_context['query']['version'][0] ret = {'filepath': None, 'version': version, 'content': ''} hba_file = get_setting(conn, 'hba_file') ret['filepath'] = hba_file ret['content'] = HBAManager.get_file_content(hba_file, version) return ret
def supervision_probe_blocks(http_context, queue_in=None, config=None, sessions=None, commands=None): set_logger_name("supervision") logger = get_logger(config) check_sessionid(http_context['headers'], sessions) try: output = api_run_probe(probe_blocks(config.plugins['supervision']), config) return output except (Exception, error) as e: logger.error(str(e.message)) raise HTTPError(500, "Internal error.")
def api_run_probe(probe_instance, config): """ Run a probe instance. """ set_logger_name("supervision") logger = get_logger(config) # TODO: logging methods in supervision_agent code and supervision_agent should be aligned. logging.root = logger try: system_info = host_info(config.plugins['supervision']) except ValueError as e: logger.error( "supervision_worker - unable to get system information: %s\n" % str(e)) return config.plugins['supervision']['conninfo'] = [{ 'host': config.postgresql['host'], 'port': config.postgresql['port'], 'user': config.postgresql['user'], 'database': config.postgresql['dbname'], 'password': config.postgresql['password'], 'dbnames': config.plugins['supervision']['dbnames'], 'instance': config.postgresql['instance'] }] # Validate connection information from the config, and ensure # the instance is available instances = [] for conninfo in config.plugins['supervision']['conninfo']: logging.debug("Validate connection information on instance \"%s\"", conninfo['instance']) instances.append(instance_info(conninfo, system_info['hostname'])) # Gather the data from probes data = run_probes([probe_instance], system_info['hostname'], instances, delta=False) return data
def dashboard_cpu(http_context, queue_in=None, config=None, sessions=None, commands=None): """ @api {get} /dashboard/cpu CPU usage @apiVersion 0.0.1 @apiName GetDasboardCPU @apiGroup Dashboard @apiHeader {String} X-Session Session ID. @apiSuccess {Object} cpu @apiSuccess {Number} cpu.iowait CPU cycles waiting for I/O operation (%). @apiSuccess {Number} cpu.idle CPU IDLE time (%). @apiSuccess {Number} cpu.steal CPU steal time (%). @apiSuccess {Number} cpu.user CPU user time (%). @apiSuccess {Number} cpu.system CPU system time (%). @apiExample {curl} Example usage: curl -H "X-Session: <session-id>" http://localhost:12345/dashboard/cpu @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 11:39:58 GMT Content-type: application/json {"cpu": {"iowait": 0.0, "idle": 97.5, "steal": 0.0, "user": 2.5, "system": 0.0}} @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} """ set_logger_name("dashboard") return api_function_wrapper(config, http_context, sessions, metrics, 'get_cpu_usage')
def dashboard_databases(http_context, queue_in=None, config=None, sessions=None, commands=None): """ @api {get} /dashboard/databases PostgreSQL instance size & number of DB @apiVersion 0.0.1 @apiName GetDasboardDatabases @apiGroup Dashboard @apiHeader {String} X-Session Session ID. @apiSuccess {Object} databases @apiSuccess {String} databases.total_size PostgreSQL instance total size (with unit). @apiSuccess {String} databases.time Time when DBs informations have been retreived (HH:MM). @apiSuccess {Number} databases.databases Number of databases. @apiSuccess {Number} databases.total_commit Number of commited xact. @apiSuccess {Number} databases.total_rollback Number of rollbacked xact. @apiExample {curl} Example usage: curl -H "X-Session: <session-id>" http://localhost:12345/dashboard/databases @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 11:52:05 GMT Content-type: application/json {"databases": {"total_size": "1242 MB", "time": "13:52", "databases": 4, "total_commit": 16728291, "total_rollback": 873}} @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} """ set_logger_name("dashboard") return api_function_wrapper_pg(config, http_context, sessions, metrics, 'get_databases')
def dashboard_memory(http_context, queue_in=None, config=None, sessions=None, commands=None): """ @api {get} /dashboard/memory Memory usage @apiVersion 0.0.1 @apiName GetDasboardMemory @apiGroup Dashboard @apiHeader {String} X-Session Session ID. @apiSuccess {Object} memory @apiSuccess {Number} memory.total Total amount of memory (kB). @apiSuccess {Number} memory.active Active memory (%). @apiSuccess {Number} memory.cached Memory used as OS cache (%). @apiSuccess {Number} memory.free Unused memory (%). @apiExample {curl} Example usage: curl -H "X-Session: <session-id>" http://localhost:12345/dashboard/memory @apiSuccessExample Success-Reponse: HTTP/1.0 200 OK Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 11:44:14 GMT Content-type: application/json {"memory": {"total": 3950660,"active": 56.6, "cached": 33.8, "free": 9.6}} @apiError (500 error) error Internal error. @apiError (401 error) error Invalid session. @apiErrorExample 401 error example HTTP/1.0 401 Unauthorized Server: temboard-agent/0.0.1 Python/2.7.8 Date: Wed, 22 Apr 2015 09:58:00 GMT Content-type: application/json {"error": "Invalid session."} """ set_logger_name("dashboard") return api_function_wrapper(config, http_context, sessions, metrics, 'get_memory_usage')
def httpd_run(commands, queue_in, config, sessions): """ Serve HTTP for ever and reload configuration from the conf file on SIGHUP signal catch. """ server_address = (config.temboard['address'], config.temboard['port']) handler_class = handleRequestsUsing(commands, queue_in, config, sessions) httpd = ThreadedHTTPServer(server_address, handler_class) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=config.temboard['ssl_key_file'], certfile=config.temboard['ssl_cert_file'], server_side=True) # We need a timeout here because the code after httpd.handle_request() call # is written to handle configuration re-loading and needs to be ran periodicaly. httpd.timeout = 1 set_logger_name("httpd") logger = get_logger(config) while True: httpd.handle_request() if reload_true(): # SIGHUP caught # Try to load configuration from the configuration file. try: logger.info("SIGHUP signal caught, trying to reload " "configuration.") new_config = Configuration(config.configfile) # Prevent any change on plugins list.. new_config.temboard['plugins'] = config.temboard['plugins'] new_config.plugins = load_plugins_configurations(new_config) # New RequestHandler using the new configuration. httpd.RequestHandlerClass = handleRequestsUsing( commands, queue_in, new_config, sessions) del logger # ... and re-create a new one with the new # configuration. set_logger_name("httpd") logger = get_logger(new_config) logger.info("Done.") except (ConfigurationError, ImportError) as e: logger.traceback(get_tb()) logger.error(str(e)) logger.info("Keeping previous configuration.") # Reset the global var indicating a SIGHUP signal. set_global_reload(False)