@metadata_reactor.provides(
    'cron/jobs/check-mail-received',
    'icinga2_api/check-mail-received/services',
)
def process_metadata(metadata):
    cron = set()
    services = {}

    my_mail_address = 'root@{}'.format(metadata.get('hostname'))

    for name, config in metadata.get('check-mail-received', {}).items():
        cron.add('{minute} {hour} * * *    root    date | mail -s "daily test mail from {node}" -r {source} {target}'.format(
            minute=node.magic_number%60,
            hour=node.magic_number%24,
            node=node.name,
            source=my_mail_address,
            target=config['email'],
        ))

        services[f'MAIL RECEIVED ON {name}'] = {
            'check_command': 'check_imap_for_mail_from',
            'check_interval': '15m',
            'retry_interval': '5m',
            'vars.sshmon_timeout': 30,
            'vars.imap_host': config['imap_host'],
            'vars.imap_user': config.get('imap_user', config['email']),
            'vars.imap_pass': config['imap_pass'],
            'vars.imap_from': my_mail_address,
        }

    return {
        'cron': {
            'jobs': {
                'check-mail-received': '\n'.join(sorted(cron)),
            },
        },
        'icinga2_api': {
            'check-mail-received': {
                'services': services,
            },
        },
    }