from re import sub

defaults = {
    'apt': {
        'packages': {
            'gawk': {},  # needed by check_ram
            'libwww-perl': {},  # needed by check_nginx_status
            'monitoring-plugins': {},
            'python3-requests': {},
            'python3-setuptools': {},  # needed by check_github_for_new_release
            'sysstat': {
                # legacy
                'installed': False,
            },
        },
    },
    'icinga2_api': {
        'basic': {
            'services': {
                'CPU': {
                    'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_cpu_stats',
                },
                'LOAD': {
                    'command_on_monitored_host': '/usr/lib/nagios/plugins/check_load -r -w 4,2,1 -c 8,4,2',
                },
                'OOM KILLER': {
                    'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_oom_killer',
                },
            },
        },
    },
    'openssh': {
        'allowed_users': {
            'sshmon',
        },
    },
    'pacman': {
        'packages': {
            'gawk': {},
            'perl-libwww': {},
            'monitoring-plugins': {},
            'python-requests': {},
        },
    },
}


@metadata_reactor.provides(
    'icinga2_api',
)
def autogenerate_sshmon_command(metadata):
    result = {
        'icinga2_api': {},
    }

    for bundle, bundle_config in metadata.get('icinga2_api', {}).items():
        for service, service_config in bundle_config.get('services', {}).items():
            # The default for check_command is also set in items.py and
            # in icinga2 bundle
            if (
                service_config.get('check_command', 'sshmon') == 'sshmon' and
                'command_on_monitored_host' in service_config
            ):
                service_normalized = sub('[^a-zA-Z0-9]', '_', service)

                result['icinga2_api'].setdefault(bundle, {}).setdefault('services', {}).setdefault(service, {})
                result['icinga2_api'][bundle]['services'][service]['vars.sshmon_command'] = service_normalized

    return result


@metadata_reactor.provides(
    'icinga2_api/basic/services',
)
def default_checks(metadata):
    disk_space_warning = metadata.get('sshmon/disk_space/warning', 15)
    disk_space_critical = metadata.get('sshmon/disk_space/critical', 5)
    disk_space_warning_inodes = metadata.get('sshmon/disk_space/warning_inodes', 15)
    disk_space_critical_inodes = metadata.get('sshmon/disk_space/critical_inodes', 5)
    disk_space_ignore_patterns = metadata.get('sshmon/disk_space/ignore_patterns', set())

    ram_warning = metadata.get('sshmon/ram_usage/warning', 9)
    ram_critical = metadata.get('sshmon/ram_usage/critical', 8)

    mounts_options = metadata.get('sshmon/check_mounts_options', '')

    disk_space_extra_args = set()

    for pattern in disk_space_ignore_patterns:
        disk_space_extra_args.add(f'-I {pattern}')

    for pool in metadata.get('zfs/pools', {}).keys():
        disk_space_extra_args.add(f'--ignore-ereg-partition={pool}')

    disk_space_ignore_patterns_string = ' '.join(sorted(disk_space_extra_args))

    return {
        'icinga2_api': {
            'basic': {
                'services': {
                    'DISK SPACE': {
                        'command_on_monitored_host': str(
                            '/usr/lib/nagios/plugins/check_disk -X nfs -X nfs4 -X squashfs '
                            f'-w {disk_space_warning}% -c {disk_space_critical}% '
                            f'-W {disk_space_warning_inodes}% -K {disk_space_critical_inodes}% '
                            '-A -I "^/dev$" -I "^/run" -I "^/sys" -i "/sys/kernel/debug/tracing" '
                            f'{disk_space_ignore_patterns_string}',
                        ),
                        'vars.notification.mail': True,
                    },
                    'MOUNTS': {
                        'command_on_monitored_host': f'sudo /usr/local/share/icinga/plugins/check_mounts {mounts_options}',
                    },
                    'RAM': {
                        'command_on_monitored_host': f'/usr/local/share/icinga/plugins/check_ram {ram_warning} {ram_critical}',
                        'max_check_attempts': '12',
                        'retry_interval': '30m',
                    },
                },
            },
        },
    }