def add_check_api(): app = flask.Flask('check_api') health = HealthCheck(app, "/health") envdump = EnvironmentDump(app, "/environment") def health_check(): db_engines_ok = STORE.is_ok() # calls specific DB check resource_engine_ok = RM.is_ok() # calls specific RM check this_ok = (1 + 1 == 2) # basic local check # maintenance = False if this_ok and db_engines_ok and resource_engine_ok: return {'status', 'up'} # elif not maintenance: # return {'status', 'out_of_service'} else: return {'status', 'down'} def application_data(): return { 'maintainer': 'ElasTest', 'git_repo': 'https://github.com/elastest/elastest-service-manager' } health.add_check(health_check) envdump.add_section("application", application_data) return add_mware(app)
def add_check_api(): app = flask.Flask('check_api') health = HealthCheck(app, "/health") envdump = EnvironmentDump(app, "/environment") def health_check(): # TODO check that the datasource backend is working # TODO check that the active resource backends are available db_engines_ok = True maintenance = False if 1 + 1 == 2: return {'status', 'up'} elif db_engines_ok: return {'status', 'up'} elif not maintenance: return {'status', 'out_of_service'} else: return {'status', 'down'} def application_data(): return { 'maintainer': 'ElasTest', 'git_repo': 'https://github.com/elastest/elastest-service-manager' } health.add_check(health_check) envdump.add_section("application", application_data) return app
def init_health_check(app): # wrap the flask app and give a heathcheck url health = HealthCheck(app, "/admin/healthcheck") envdump = EnvironmentDump(app, "/admin/environment") for check in checks: health.add_check(check) for dump in dumps: envdump.add_section(dump, dumps[dump])
def add_health_check(app): health = HealthCheck() envdump = EnvironmentDump() health.add_section("application", application_data) envdump.add_section("application", application_data) app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run()) app.add_url_rule("/environment", "environment", view_func=lambda: envdump.run())
def add_check_api(app): from healthcheck import HealthCheck, EnvironmentDump health = HealthCheck(app, API_PREFIX + API_HEALTHCHECK, log_on_failure=False, success_ttl=None, failed_ttl=None, failed_status=200) envdump = EnvironmentDump(app, API_PREFIX + API_ENVIRONMENT) def health_check(): try: r = requests.get(app.config['SPARK_MASTER_URL']) soup = BeautifulSoup(r.content, 'html.parser') listItems = soup.find_all('li') statusItems = [ item.text for item in listItems if 'Status:' in str(item) ] alive = True if sum(1 for item in statusItems if 'ALIVE' in str(item)) > 0 else False statusString = str(statusItems[0]) except requests.exceptions.RequestException as e: # This is the correct syntax alive = False statusString = str(e) return alive, statusString def application_data(): try: r = requests.get(app.config['SPARK_MASTER_URL']) soup = BeautifulSoup(r.content, 'html.parser') listItems = soup.find_all('li') spark = { '_'.join(k.lower().split()): " ".join(v.split()) for k, v in (item.text.replace('\n', '').split(':', 1) for item in listItems) } except requests.exceptions.RequestException as e: # This is the correct syntax spark = {'error': str(e)} return { 'spark': spark, 'maintainer': 'Savas Gioldasis ([email protected])', 'git_repo': 'https://github.com/elastest/elastest-bigdata-service', } health.add_check(health_check) envdump.add_section("application", application_data) return app
def attach_monitor(app): health = HealthCheck(app, '/status') envdump = EnvironmentDump(app, '/env', include_os=False, include_config=False, include_process=False, include_python=False) health.add_check(db_available) envdump.add_section("application", app_data)
def test_basic_check(self): def custom_section(): return "My custom section" ed = EnvironmentDump() ed.add_section("custom_section", custom_section) message, status, headers = ed.run() jr = json.loads(message) self.assertEqual("My custom section", jr["custom_section"])
def execute(self, petisco: Petisco) -> Result[Dict, Error]: try: application_info = {"config": petisco.info} envdump = EnvironmentDump( include_python=self.include_python, include_os=self.include_os, include_process=self.include_process, ) envdump.add_section("Environment", application_info) envdump_result = envdump.run() except Exception as e: return Failure(EnvironmentProviderError(str(e))) return Success(json.loads(envdump_result[0]))
def create_app(test_config=None): log.info("Creating python app "+__name__) flask_app = Flask(__name__) flask_app.config.update(settings.PROPS) if test_config is not None: flask_app.config.update(test_config) flask_app.register_blueprint(api_blueprint, url_prefix='/api') if test_config is None: if flask_app.config.get("COGNITO_CHECK_TOKEN_EXPIRATION") is False: log.warning("COGNITO_CHECK_TOKEN_EXPIRATION is disabled, ensure it is enabled in production environments.") if flask_app.config.get("FLASK_DEBUG") is True: log.warning("FLASK_DEBUG is enabled, ensure it is disabled in production environments.") # db initialization try: db.init_app(flask_app) except Exception as e: log.exception("Failed to initialize APP: {}".format(repr(e)), exc_info=True) # Migrations (upgrade to the latest version) with flask_app.app_context(): try: from flask_migrate import upgrade as _upgrade migrate = Migrate(flask_app, db) _upgrade() except Exception as e: log.exception("Failed to upgrade DB: {}".format(repr(e)), exc_info=True) health = HealthCheck() env_dump = EnvironmentDump(include_python=True, include_os=True, include_process=True) health.add_check(db_health) application_data = settings.application_data() # application_data['verified_aws_credentials'] = verify_aws_credentials() log.info(application_data) env_dump.add_section("application", application_data) env_dump.add_section("features", settings.features()) # Add a flask route to expose information flask_app.add_url_rule("/health", "healthcheck", view_func=lambda: health.run()) flask_app.add_url_rule("/info", "environment", view_func=lambda: env_dump.run()) return flask_app
def add_check_api(): app = flask.Flask('check_api') health = HealthCheck(app, "/healthcheck") envdump = EnvironmentDump(app, "/environment") def health_check(): # TODO check that the api backend working # TODO check that the active resource backends are available def onem2m_url_ok(): url= 'http://localhost:8000/onem2m' r = requests.response(url) return r.status_code == 200 def mems_url_ok(): url = 'http://localhost:8000/onem2m/MemsIPE' r = requests.response(url) return r.status_code == 200 #maintenance = False if 1 + 1 == 2 and onem2m_url_ok and mems_url_ok: return {'status', 'up'} #elif url_ok: # return {'status', 'up'} #elif not maintenance: # return {'status', 'out_of_service'} else: return {'status', 'down'} def application_data(): return {'maintainer': 'ElasTest', 'git_repo': 'https://github.com/elastest/elastest-device-emulator-service'} health.add_check(health_check) envdump.add_section("application", application_data) return app
from flask import Flask from healthcheck import HealthCheck, EnvironmentDump app = Flask(__name__) health = HealthCheck(app, "/healthcheck") envdump = EnvironmentDump(app, "/environment") def application_data(): return { "maintainer": "Charlie Lewis", "git_repo": "https://github.com/CyberReboot/poseidon", "app": "poseidon" } envdump.add_section("application", application_data)
@app.errorhandler(400) def bad_request(error: Exception) -> tuple: """Handle bad requests :param Exception error: Exception thrown :return: A http response :rtype: tuple """ return jsonify(_status="ERR", _error={ "code": 400, "message": str(error) }), 400 # Add environment and health check routes envdump.add_section("application", application_data) envdump.add_section("settings", application_settings) health.add_check(mongo_available) health.add_section("version", __version__) app.add_url_rule("/healthcheck", "healthcheck", view_func=health.run) app.add_url_rule("/environment", "environment", view_func=envdump.run) if __name__ != '__main__': # pragma: no cover gunicorn_logger = logging.getLogger('gunicorn.error') app.logger.handlers = gunicorn_logger.handlers logging.basicConfig(level=gunicorn_logger.level) if __name__ == "__main__": # pragma: no cover app.run(host=HOST, debug=DEBUG, port=PORT)
from flask import Flask from healthcheck import EnvironmentDump from healthcheck import HealthCheck app = Flask(__name__) health = HealthCheck(app, '/healthcheck') envdump = EnvironmentDump(app, '/environment') def application_data(): return {'maintainer': 'Charlie Lewis', 'git_repo': 'https://github.com/CyberReboot/vent', 'app': 'rmq_es_connector'} envdump.add_section('application', application_data)
def init_app(app): health = HealthCheck(app, '/healthcheck') health.add_check(redis_healthcheck) health.add_check(postgres_healthcheck) envdump = EnvironmentDump(app, '/environment') envdump.add_section("application", application_data)
from flask import Flask from healthcheck import EnvironmentDump from healthcheck import HealthCheck app = Flask(__name__) health = HealthCheck(app, '/healthcheck') envdump = EnvironmentDump(app, '/environment') def application_data(): return { 'maintainer': 'Charlie Lewis', 'git_repo': 'https://github.com/CyberReboot/vent', 'app': 'rq_dashboard' } envdump.add_section('application', application_data)
if self.failed_handler: message = self.failed_handler(results) return message, self.failed_status, self.failed_headers health = FilteredHealthCheck(app, "/healthcheck") envdump = EnvironmentDump(app, "/environment") def json_version(): from mensabot.format import get_version return get_version() envdump.add_section("version", json_version) @health.add_check def tasks_running(): from mensabot.bot.tasks import SCHED, SCHED_TASK_COUNT, task_name tasks = len(SCHED.queue) task_names = [task_name(task) for task in SCHED.queue] dedup_count = len(set(task_names)) return SCHED_TASK_COUNT <= tasks == dedup_count, \ { "count": tasks, "dedup_count": dedup_count, "tasks": task_names }
'postgresql+psycopg2://catalog:halemaumau!@localhost/catalog') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() ##Security and Oauth # Google routes and functions ## Basic Health Check App Monitoring def database_available(): dbhealth = inspect(session) return True, "Database OK" # Provide additional information for environment dump def application_data(): return {"developer": "Kris Hansen", "git_repo": "https://github.com/kris-hansen/catalog.git"} envdump.add_section("App Info", application_data) @app.route('/gconnect', methods=['POST']) def gconnect(): # Validate state token if request.args.get('state') != login_session['state']: response = make_response(json.dumps('Invalid state parameter'), 401) response.headers['Content-Type'] = 'application/json' return response # Obtain auth code code = request.data try: # Exchange the auth code for a credentials object oauth_flow = flow_from_clientsecrets('/var/www/catalog/client_secrets.json', scope='')
if process in proc.name(): processes_name_list[process] = "ok" return True, f"{processes_name_list}" def check_self(): process_name = os.path.basename(__file__) for proc in psutil.process_iter(): for process in process_name: if process in proc.name(): return True, "register_agent ok" health.add_check(check_ossec_agentd) health.add_check(check_self) envs.add_section("application", application_data) app.add_url_rule("/healz", "healthcheck", view_func=lambda: health.run()) app.add_url_rule("/envs", "environment", view_func=lambda: envs.run()) def code_desc(http_status_code): return requests.status_codes._codes[http_status_code][0] def add_agent(agt_name, agt_ip=None): if agt_ip: status_code, response = req("post", "agents", { "name": agt_name, "ip": agt_ip, "force_time": 1 })
app = Flask(__name__) config_name = os.getenv('FLASK_CONFIGURATION', 'default') app.config.from_object(config[config_name]) app.config.from_pyfile('config.cfg', silent=True) health = HealthCheck(app, "/healthcheck") envdump = EnvironmentDump(app, "/environment", include_python=True, include_os=False, include_process=False, include_config=True) def sqlite_available(): # add precise check against the database return True, "sqlite ok" health.add_check(sqlite_available) def application_data(): return {"maintainer": "Damyan Bogoev", "git_repo": "https://github.com/damyanbogoev/flask-bookshelf"} envdump.add_section("application", application_data) if __name__ == '__main__': app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)))