def run(self):
        logger.debug("Starting metrics emitter with interval %d" %
                     self.interval)
        while True:
            stats = {}
            try:
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_health(stats)
                try:
                    stats = self._inject_m2ee_stats(stats)
                except Exception:
                    logger.debug("Unable to get metrics from runtime")

                self.emit(stats)
            except psycopg2.OperationalError as up:
                logger.exception("METRICS: error while gathering metrics")
                self.emit({
                    "health": {
                        "health": 0,
                        "diagnosis": "Database error: %s" % str(up),
                    }
                })
            except Exception as e:
                logger.exception("METRICS: error while gathering metrics")
                self.emit({
                    "health": {
                        "health": 4,
                        "diagnosis": "Unable to retrieve metrics",
                    }
                })

            time.sleep(self.interval)
    def run(self):
        logger.debug('Starting metrics emitter with interval %d' %
                     self.interval)
        while True:
            stats = {}
            try:
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_health(stats)
                stats = self._inject_m2ee_stats(stats)
                self.emit(stats)
            except psycopg2.OperationalError as up:
                logger.exception('METRICS: error while gathering metrics')
                self.emit({
                    'health': {
                        'health': 0,
                        'diagnosis': "Database error: %s" % str(up)
                    }
                })
            except Exception as e:
                logger.exception('METRICS: error while gathering metrics')
                self.emit({
                    'health': {
                        'health': 4,
                        'diagnosis': "Unable to retrieve metrics"
                    }
                })

            time.sleep(self.interval)
def service_backups():
    vcap_services = buildpackutil.get_vcap_services_data()
    if not vcap_services or 'schnapps' not in vcap_services:
        logger.debug("No backup service detected")
        return

    backup_service = {}
    if 'amazon-s3' in vcap_services:
        s3_credentials = vcap_services['amazon-s3'][0]['credentials']
        backup_service['filesCredentials'] = {
            'accessKey': s3_credentials['access_key_id'],
            'secretKey': s3_credentials['secret_access_key'],
            'bucketName': s3_credentials['bucket'],
        }
        if 'key_suffix' in s3_credentials:  # Not all s3 plans have this field
            backup_service['filesCredentials']['keySuffix'] = s3_credentials['key_suffix']

    try:
        db_config = buildpackutil.get_database_config()
        if db_config['DatabaseType'] != 'PostgreSQL':
            raise Exception(
                'Schnapps only supports postgresql, not %s'
                % db_config['DatabaseType']
            )
        host_and_port = db_config['DatabaseHost'].split(':')
        backup_service['databaseCredentials'] = {
            'host': host_and_port[0],
            'username': db_config['DatabaseUserName'],
            'password': db_config['DatabasePassword'],
            'dbname': db_config['DatabaseName'],
            'port': int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
        }
    except Exception as e:
        logger.exception(
            'Schnapps will not be activated because error occurred with '
            'parsing the database credentials'
        )
        return
    schnapps_url = vcap_services['schnapps'][0]['credentials']['url']
    schnapps_api_key = vcap_services['schnapps'][0]['credentials']['apiKey']

    try:
        result = requests.put(
            schnapps_url,
            headers={
                'Content-Type': 'application/json',
                'apiKey': schnapps_api_key
            },
            data=json.dumps(backup_service),
        )
    except Exception as e:
        logger.warning('Failed to contact backup service: ' + e)
        return

    if result.status_code == 200:
        logger.info("Successfully updated backup service")
    else:
        logger.warning("Failed to update backup service: " + result.text)
Exemple #4
0
def terminate_process():
    logger.info('stopping app...')
    if not m2ee.stop():
        if not m2ee.terminate():
            m2ee.kill()
    try:
        this_process = os.getpgid(0)
        logger.debug(
            'Terminating process group with pgid={}'.format(this_process))
        os.killpg(this_process, signal.SIGTERM)
        time.sleep(3)
        os.killpg(this_process, signal.SIGKILL)
    except Exception:
        logger.exception('Failed to terminate all child processes')
Exemple #5
0
    def run(self):
        logger.debug(
            'Starting metrics emitter with interval %d' % self.interval
        )
        while True:

            try:
                stats = {
                    'version': '1.0',
                    'timestamp': datetime.datetime.now().isoformat(),
                }
                stats = self._inject_m2ee_stats(stats)
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_health(stats)
                logger.info('MENDIX-METRICS: ' + json.dumps(stats))
            except Exception as e:
                logger.exception('METRICS: error while gathering metrics')

            time.sleep(self.interval)
    def run(self):
        logger.debug('Starting metrics emitter with interval %d' %
                     self.interval)
        while True:

            try:
                stats = {
                    'version': '1.0',
                    'timestamp': datetime.datetime.now().isoformat(),
                    'instance_index': os.getenv('CF_INSTANCE_INDEX', 0)
                }
                stats = self._inject_m2ee_stats(stats)
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_health(stats)
                self.emit(stats)
            except Exception as e:
                logger.exception('METRICS: error while gathering metrics')

            time.sleep(self.interval)
Exemple #7
0
 def _gather_metrics(self):
     stats = {}
     try:
         for inject_method in self._select_stats_to_emit:
             stats = inject_method(stats)
     except psycopg2.OperationalError as exc:
         logger.exception("METRICS: error while gathering metrics")
         stats = {
             "health": {
                 "health": 0,
                 "diagnosis": "Database error: {}".format(str(exc)),
             }
         }
     except Exception:
         logger.exception("METRICS: error while gathering metrics")
         stats = {
             "health": {
                 "health": 4,
                 "diagnosis": "Unable to retrieve metrics",
             }
         }
     finally:
         return stats
def service_backups():
    vcap_services = buildpackutil.get_vcap_services_data()
    schnapps = None
    amazon_s3 = None
    for key in vcap_services:
        if key.startswith("amazon-s3"):
            amazon_s3 = key
        if key.startswith("schnapps"):
            schnapps = key

    if not vcap_services or schnapps not in vcap_services:
        logger.debug("No backup service detected")
        return

    backup_service = {}
    if amazon_s3 in vcap_services:
        s3_credentials = vcap_services[amazon_s3][0]["credentials"]
        backup_service["filesCredentials"] = {
            "accessKey": s3_credentials["access_key_id"],
            "secretKey": s3_credentials["secret_access_key"],
            "bucketName": s3_credentials["bucket"],
        }
        if "key_suffix" in s3_credentials:  # Not all s3 plans have this field
            backup_service["filesCredentials"]["keySuffix"] = s3_credentials[
                "key_suffix"
            ]

    try:
        db_config = buildpackutil.get_database_config()
        if db_config["DatabaseType"] != "PostgreSQL":
            raise Exception(
                "Schnapps only supports postgresql, not %s"
                % db_config["DatabaseType"]
            )
        host_and_port = db_config["DatabaseHost"].split(":")
        backup_service["databaseCredentials"] = {
            "host": host_and_port[0],
            "username": db_config["DatabaseUserName"],
            "password": db_config["DatabasePassword"],
            "dbname": db_config["DatabaseName"],
            "port": int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
        }
    except Exception as e:
        logger.exception(
            "Schnapps will not be activated because error occurred with "
            "parsing the database credentials"
        )
        return
    schnapps_url = vcap_services[schnapps][0]["credentials"]["url"]
    schnapps_api_key = vcap_services[schnapps][0]["credentials"]["apiKey"]

    try:
        result = requests.put(
            schnapps_url,
            headers={
                "Content-Type": "application/json",
                "apiKey": schnapps_api_key,
            },
            data=json.dumps(backup_service),
        )
    except requests.exceptions.SSLError as e:
        logger.warning("Failed to contact backup service. SSLError: " + str(e))
        return
    except Exception as e:
        logger.warning("Failed to contact backup service: ", exc_info=True)
        return

    if result.status_code == 200:
        logger.info("Successfully updated backup service")
    else:
        logger.warning("Failed to update backup service: " + result.text)