def main():
    # Fetch configs. You can find the arguments in puppet.
    args = parse_cli()
    persistence_config = read_config(args)
    store_type = schema.StatePersistenceTypes(persistence_config.store_type)
    job_name = args.job_name

    # Alert for DynamoDB
    if store_type == schema.StatePersistenceTypes.dynamodb:
        # Fetch job state from dynamodb
        dynamodb_region = persistence_config.dynamodb_region
        table_name = persistence_config.table_name
        store = DynamoDBStateStore(table_name, dynamodb_region)
        key = store.build_key('job_state', job_name)
        try:
            job = store.restore([key])[key]
        except Exception as e:
            logging.exception(
                f'UNKN: Failed to retreive status for job {job_name} due to {e}'
            )
            sys.exit(3)

        # Exit if the job never runs.
        last_run_time = get_last_run_time(job)
        if not last_run_time:
            logging.error(
                f'WARN: No last run for {key} found. If the job was just added, it might take some time for it to run'
            )
            sys.exit(1)

        # Alert if timestamp is not updated after staleness_threshold
        stateless_for_secs = time.time() - last_run_time.astimezone(
            pytz.utc).timestamp()
        if stateless_for_secs > args.staleness_threshold:
            logging.error(
                f'CRIT: {key} has not been updated in DynamoDB for {stateless_for_secs} seconds'
            )
            sys.exit(2)
        else:
            logging.info(
                f"OK: DynamoDB is up to date. It's last updated at {last_run_time}"
            )
            sys.exit(0)
    # Alert for BerkeleyDB
    elif store_type == schema.StatePersistenceTypes.shelve:
        os.execl('/usr/lib/nagios/plugins/check_file_age',
                 '/nail/tron/tron_state', '-w', str(args.staleness_threshold),
                 '-c', str(args.staleness_threshold))
    else:
        logging.exception(
            f'UNKN: Not designed to check this type of datastore: {store_type}'
        )
        sys.exit(3)
Esempio n. 2
0
def main():
    # Fetch configs. You can find the arguments in puppet.
    args = parse_cli()
    persistence_config = read_config(args)

    # If backup only if datastore is dynamodb
    store_type = schema.StatePersistenceTypes(persistence_config.store_type)
    if store_type == schema.StatePersistenceTypes.dynamodb:
        #set up dynamodb client
        region_name = persistence_config.dynamodb_region
        table_name = persistence_config.table_name
        max_backups = args.max_backups
        ddb = boto3.client('dynamodb', region_name=region_name)
        #create backup and remove old ones
        create_backup(ddb, table_name)
        delete_old_backups(ddb, table_name, max_backups)
Esempio n. 3
0
    def from_config(cls, persistence_config):
        store_type = schema.StatePersistenceTypes(persistence_config.store_type)
        name = persistence_config.name
        buffer_size = persistence_config.buffer_size
        store = None

        if store_type == schema.StatePersistenceTypes.shelve:
            store = ShelveStateStore(name)

        if store_type == schema.StatePersistenceTypes.yaml:
            store = YamlStateStore(name)

        if store_type == schema.StatePersistenceTypes.dynamodb:
            table_name = persistence_config.table_name
            dynamodb_region = persistence_config.dynamodb_region
            store = DynamoDBStateStore(table_name, dynamodb_region)

        buffer = StateSaveBuffer(buffer_size)
        return PersistentStateManager(store, buffer)