2020-08-18 13:27:55 +00:00
|
|
|
defaults = {
|
|
|
|
'apt': {
|
|
|
|
'repos': {
|
|
|
|
'nginx': {
|
|
|
|
'items': [
|
2020-08-30 10:06:19 +00:00
|
|
|
'deb http://nginx.org/packages/{os} {os_release} nginx',
|
2020-08-18 13:27:55 +00:00
|
|
|
],
|
2020-03-14 09:56:19 +00:00
|
|
|
},
|
2020-08-18 13:27:55 +00:00
|
|
|
},
|
|
|
|
'packages': {
|
|
|
|
'nginx': {},
|
2020-04-13 07:52:26 +00:00
|
|
|
},
|
2020-08-18 13:27:55 +00:00
|
|
|
},
|
2020-11-13 11:37:26 +00:00
|
|
|
'backups': {
|
|
|
|
'paths': {
|
|
|
|
'/var/www',
|
|
|
|
},
|
|
|
|
},
|
2020-11-10 09:57:04 +00:00
|
|
|
'icinga2_api': {
|
|
|
|
'nginx': {
|
|
|
|
'services': {
|
|
|
|
'NGINX PROCESS': {
|
|
|
|
'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_systemd_unit nginx',
|
|
|
|
},
|
|
|
|
'NGINX STATUS': {
|
|
|
|
'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_nginx_status',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-18 13:27:55 +00:00
|
|
|
'nginx': {
|
2020-08-30 08:32:29 +00:00
|
|
|
'worker_connections': 768,
|
2020-09-20 12:36:43 +00:00
|
|
|
'use_ssl_for_all_connections': True,
|
2020-08-18 13:27:55 +00:00
|
|
|
},
|
|
|
|
}
|
2020-06-01 08:52:52 +00:00
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'nginx/worker_processes',
|
|
|
|
)
|
2020-08-30 08:32:29 +00:00
|
|
|
def worker_processes(metadata):
|
|
|
|
return {
|
|
|
|
'nginx': {
|
|
|
|
'worker_processes': metadata.get('vm/cpu', 2),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'letsencrypt/domains',
|
|
|
|
'letsencrypt/reload_after',
|
|
|
|
)
|
2020-06-01 08:52:52 +00:00
|
|
|
def letsencrypt(metadata):
|
|
|
|
if not node.has_bundle('letsencrypt'):
|
2020-08-18 13:27:55 +00:00
|
|
|
raise DoNotRunAgain
|
2020-06-01 08:52:52 +00:00
|
|
|
|
2020-07-19 08:58:54 +00:00
|
|
|
domains = {}
|
2020-06-01 08:52:52 +00:00
|
|
|
|
2020-10-31 09:30:07 +00:00
|
|
|
for vhost, config in metadata.get('nginx/vhosts', {}).items():
|
|
|
|
domain = config.get('domain', vhost)
|
2020-11-11 10:41:06 +00:00
|
|
|
domains[domain] = config.get('domain_aliases', set())
|
2020-06-01 10:29:16 +00:00
|
|
|
|
2020-07-19 08:58:54 +00:00
|
|
|
return {
|
|
|
|
'letsencrypt': {
|
|
|
|
'domains': domains,
|
|
|
|
'reload_after': {
|
|
|
|
'nginx',
|
|
|
|
},
|
|
|
|
},
|
2020-08-18 13:27:55 +00:00
|
|
|
}
|
2020-10-31 09:41:33 +00:00
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'nginx/vhosts',
|
|
|
|
)
|
2020-10-31 09:41:33 +00:00
|
|
|
def index_files(metadata):
|
|
|
|
vhosts = {}
|
|
|
|
|
|
|
|
for vhost, config in metadata.get('nginx/vhosts', {}).items():
|
|
|
|
vhosts[vhost] = {
|
|
|
|
'index': [
|
|
|
|
'index.html',
|
|
|
|
'index.htm',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.get('php', False):
|
|
|
|
# If we're using PHP, make sure index.php is tried first
|
|
|
|
vhosts[vhost]['index'].insert(0, 'index.php')
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
'nginx': {
|
|
|
|
'vhosts': vhosts,
|
|
|
|
},
|
|
|
|
}
|
2020-11-10 09:57:04 +00:00
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'icinga2_api/nginx/services',
|
|
|
|
)
|
2020-11-10 09:57:04 +00:00
|
|
|
def monitoring(metadata):
|
|
|
|
services = {}
|
|
|
|
|
|
|
|
for vname, vconfig in metadata.get('nginx/vhosts', {}).items():
|
|
|
|
domain = vconfig.get('domain', vname)
|
|
|
|
|
2020-11-21 09:30:05 +00:00
|
|
|
if metadata.get('nginx/use_ssl_for_all_connections'):
|
|
|
|
scheme = 'https'
|
|
|
|
else:
|
|
|
|
scheme = 'http'
|
|
|
|
|
2020-11-10 09:57:04 +00:00
|
|
|
if 'website_check_path' in vconfig and 'website_check_string' in vconfig:
|
|
|
|
services['NGINX VHOST {} CONTENT'.format(vname)] = {
|
|
|
|
'check_command': 'check_http_wget',
|
|
|
|
'vars.http_wget_contains': vconfig['website_check_string'],
|
2020-11-21 09:30:05 +00:00
|
|
|
'vars.http_wget_url': '{}://{}{}'.format(scheme, domain, vconfig['website_check_path']),
|
2020-11-22 07:24:44 +00:00
|
|
|
'vars.notification.sms': True,
|
2020-11-10 09:57:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 12:28:08 +00:00
|
|
|
if vconfig.get('check_ssl', metadata.get('nginx/use_ssl_for_all_connections')):
|
2020-11-10 09:57:04 +00:00
|
|
|
services['NGINX VHOST {} CERTIFICATE'.format(vname)] = {
|
2020-12-18 12:28:08 +00:00
|
|
|
'check_command': 'check_https_cert_at_url',
|
2020-11-10 09:57:04 +00:00
|
|
|
'vars.domain': domain,
|
2020-11-22 07:24:44 +00:00
|
|
|
'vars.notification.mail': True,
|
2020-11-10 09:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
max_connections = metadata.get('nginx/worker_connections') * metadata.get('nginx/worker_processes')
|
|
|
|
connections_warn = int(max_connections * 0.8)
|
|
|
|
connections_crit = int(max_connections * 0.9)
|
|
|
|
|
|
|
|
services['NGINX STATUS'] = {
|
|
|
|
'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_nginx_status --warn={},-1,-1 --critical={},-1,-1 -H 127.0.0.1:22999'.format(connections_warn, connections_crit),
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'icinga2_api': {
|
|
|
|
'nginx': {
|
|
|
|
'services': services,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-11-16 15:35:01 +00:00
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'iptables/bundle_rules/nginx',
|
|
|
|
)
|
2020-11-16 15:35:01 +00:00
|
|
|
def iptables(metadata):
|
|
|
|
interfaces = metadata.get('nginx/restrict-to-interfaces', set())
|
2021-02-12 19:37:36 +00:00
|
|
|
rules = []
|
2020-11-16 15:35:01 +00:00
|
|
|
|
2021-02-12 19:37:36 +00:00
|
|
|
if interfaces:
|
2020-11-16 15:35:01 +00:00
|
|
|
for iface in sorted(interfaces):
|
2021-02-12 19:37:36 +00:00
|
|
|
rules.append(f'iptables_both -A INPUT -i {iface} -p tcp --dport 80 -j ACCEPT')
|
|
|
|
rules.append(f'iptables_both -A INPUT -i {iface} -p tcp --dport 443 -j ACCEPT')
|
2020-11-16 15:35:01 +00:00
|
|
|
|
|
|
|
else:
|
2021-02-12 19:37:36 +00:00
|
|
|
rules.append('iptables_both -A INPUT -p tcp --dport 80 -j ACCEPT')
|
|
|
|
rules.append('iptables_both -A INPUT -p tcp --dport 443 -j ACCEPT')
|
2020-11-16 15:35:01 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'iptables': {
|
|
|
|
'bundle_rules': {
|
2021-02-12 19:37:36 +00:00
|
|
|
'nginx': rules,
|
2020-11-16 15:35:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|