Browse Source

Merge pull request #3 from Bh0di/feature/monitoring-channels

Allow seperate monitoring channels for regions
Maximilian Wilhelm 3 years ago
parent
commit
ec45fd0689
4 changed files with 175 additions and 0 deletions
  1. 21 0
      icinga2/host.conf.tmpl
  2. 19 0
      icinga2/init.sls
  3. 117 0
      icinga2/notifications.conf.tmpl
  4. 18 0
      icinga2/users.conf.tmpl

+ 21 - 0
icinga2/host.conf.tmpl

@@ -6,6 +6,7 @@
 {%- set address = salt['ffho_net.get_primary_ip'] (node_config, 'v4').ip %}
 {%- set address6 = salt['ffho_net.get_primary_ip'] (node_config, 'v6').ip %}
 {%- set monitoring_config = node_config.get ('monitoring', {}) %}
+{%- set location = node_config.get ('location', {}) %}
 {%- set status = node_config.get ('status', 'active') %}
 
 {%- if 'icinga2server' not in roles %}
@@ -68,6 +69,26 @@ object Host "{{ node_id }}" {
 	}
 {%- endif %}
 
+{#- Location #}
+{%- if location %}
+
+	# Location
+	vars.location = {
+  {%- if "region" in location %}
+		"region" = {
+			"code"  = "{{ location['region'].get("code") }}",
+			"name"  = "{{ location['region'].get("name") }}",
+		},
+  {%- endif %}
+  {%- if "site" in location %}
+		"site" = {
+			"code"  = "{{ location['site'].get("code") }}",
+			"desc"  = "{{ location['site'].get("desc") }}",
+		}
+  {%- endif %}
+	}
+{%- endif %}
+
 {#- Special monitoring related config for this host? #}
 {%- if 'host_vars' in monitoring_config %}
 

+ 19 - 0
icinga2/init.sls

@@ -153,6 +153,25 @@ ffho-plugins:
 ################################################################################
 {% if 'icinga2server' in roles %}
 
+# Users and Notifications
+/etc/icinga2/ffho-conf.d/users.conf:
+  file.managed:
+    - source: salt://icinga2/users.conf.tmpl
+    - template: jinja
+    - require:
+      - pkg: icinga2
+    - watch_in:
+      - service: icinga2
+
+/etc/icinga2/ffho-conf.d/notifications.conf:
+  file.managed:
+    - source: salt://icinga2/notifications.conf.tmpl
+    - template: jinja
+    - require:
+      - pkg: icinga2
+    - watch_in:
+      - service: icinga2
+
 # Install command definitions
 /etc/icinga2/ffho-conf.d/services:
   file.recurse:

+ 117 - 0
icinga2/notifications.conf.tmpl

@@ -0,0 +1,117 @@
+################################################################################
+#                              Notification commands                           #
+################################################################################
+
+object NotificationCommand "telegram-notification-command" {
+        import "plugin-notification-command"
+
+	command = [ SysconfDir + "/icinga2/scripts/telegram-notification.py" ]
+	env = {
+		TELEGRAM_BOT_TOKEN = "{{ salt['pillar.get']('monitoring:private:telegram_bot_token') }}"
+		TELEGRAM_CHAT_ID = "$user.vars.telegram_chat_id$"
+		NOTIFICATION_TYPE = "$notification.type$"
+		MESSAGE = "$telegram_message$"
+		ACKNOWLEDGE_CALLBACK = "$acknowledge_callback$"
+	}
+}
+
+object NotificationCommand "telegram-host-notification" {
+	import "telegram-notification-command"
+
+	vars.telegram_message = "$notification.type$ - $host.display_name$ is $host.state$"
+	vars.acknowledge_callback = "HOST!$host.name$"
+}
+
+object NotificationCommand "telegram-service-notification" {
+	import "telegram-notification-command"
+
+	vars.telegram_message = "$notification.type$ $service.display_name$ auf $host.display_name$ $service.output$"
+	vars.acknowledge_callback = "SERVICE!$host.name$!$service.name$"
+}
+
+
+################################################################################
+#                           Notification Templates                             #
+################################################################################
+
+template Notification "telegram-host-notification" {
+	command = "telegram-host-notification"
+	states = [ Up, Down ]
+	types = [ Problem, Acknowledgement, Recovery, Custom,
+	          FlappingStart, FlappingEnd,
+	]
+
+	period = "24x7"
+	interval = 0
+}
+
+template Notification "telegram-service-notification" {
+	command = "telegram-service-notification"
+
+	states = [ OK, Warning, Critical, Unknown ]
+	types = [ Problem, Acknowledgement, Recovery, Custom,
+	          FlappingStart, FlappingEnd,
+	]
+
+	period = "24x7"
+	interval = 0
+}
+				  
+
+
+################################################################################
+#                                  Notifications                               #
+################################################################################
+
+apply Notification "notify_host_ffho_ops_by_telegram" to Host {
+	import "telegram-host-notification"
+
+	users = [ "ffho-ops" ]
+	period = "24x7"
+	
+	times.begin = 2m
+
+	assign where host.address
+}
+
+apply Notification "notify_service_ffho_ops_telegram" to Service {
+	import "telegram-service-notification"
+
+	users = [ "ffho-ops" ]
+	period = "24x7"
+
+	times.begin = 2m
+
+	assign where host.address
+	ignore where service.name == "apt-Updates"
+	ignore where service.name == "libs"
+	ignore where service.name == "running_kernel"
+	ignore where service.name == "salt"
+}
+
+
+apply Notification "notify_host_ffho_dek_by_telegram" to Host {
+	import "telegram-host-notification"
+
+	users = [ "ffho-dek" ]
+	period = "24x7"
+	
+	times.begin = 2m
+
+	assign where host.vars.location.region.code == "ffho_dek"
+}
+
+apply Notification "notify_service_ffho_dek_telegram" to Service {
+	import "telegram-service-notification"
+
+	users = [ "ffho-dek" ]
+	period = "24x7"
+
+	times.begin = 2m
+
+	assign where host.vars.location.region.code == "ffho_dek"
+	ignore where service.name == "apt-Updates"
+	ignore where service.name == "libs"
+	ignore where service.name == "running_kernel"
+	ignore where service.name == "salt"
+}

+ 18 - 0
icinga2/users.conf.tmpl

@@ -0,0 +1,18 @@
+{% set users = salt['pillar.get']("monitoring:users", {}) %}
+{% for user,params in users.items()|sort %}
+object User "{{ user }}" {
+        import "generic-user"
+
+  {%- if "display_name" in params %}
+        display_name = "{{ params['display_name'] }}"
+  {%- endif %}
+
+  {%- if "email" in params %}
+        email = "{{ params['email'] }}"
+  {%- endif %}
+
+  {%- if "telegram_chat_id" in params %}
+        vars.telegram_chat_id = "{{ params['telegram_chat_id'] }}"
+  {%- endif %}
+}
+{% endfor %}