Skip to content

Commit

Permalink
Adds installation of reporting module to icingaweb2 role
Browse files Browse the repository at this point in the history
  • Loading branch information
mocdaniel committed Nov 22, 2023
1 parent 0892966 commit 831d4a3
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
major_changes:
- Added Installation of reporting model
29 changes: 29 additions & 0 deletions doc/role-icingaweb2/module-reporting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Module Reporting

The module Reporting provides a way to generate reports based on data provided by Icinga2.

## Configuration

The general module parameter like `enabled` and `source` can be applied here.

Configuration is done via the `reporting` section of the `icingaweb2_modules` variable. In addition to the general module parameters `backend.resource` and `mail.from`, a **database connection** can be defined for automatically creating the required tables. in the referenced database resource.

Example:
```yaml
icingaweb2_modules:
reporting:
enabled: true
source: git
config:
backend:
resource: reporting-db
mail:
from: [email protected]
database:
import_schema: true
host: localhost
port: 3306
user: reporting
password: reporting
type: mysql
```
7 changes: 4 additions & 3 deletions roles/icingaweb2/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@

- name: Manage enabled/disabled modules
ansible.builtin.file:
src: "{{ icingaweb2_config.global.module_path + '/' + item.key if item.value.enabled|bool == true else omit }}"
src: "{{ icingaweb2_config.global.module_path + '/' + item.key if item.value.enabled|bool else omit }}"
dest: "{{ icingaweb2_config_dir }}/enabledModules/{{ item.key }}"
owner: "{{ icingaweb2_httpd_user }}"
group: "{{ icingaweb2_group }}"
state: "{{ 'link' if item.value.enabled|bool == true else 'absent' }}"
state: "{{ 'link' if item.value.enabled|bool else 'absent' }}"
force: yes
mode: "0777"
when: icingaweb2_modules is defined
loop: "{{ icingaweb2_modules | dict2items }}"

Expand All @@ -49,5 +50,5 @@
ansible.builtin.service:
name: "icinga-{{ item.key }}"
state: restarted
when: icingaweb2_modules is defined and item.value.enabled|bool == true and item.key in ['vspheredb', 'x509']
when: icingaweb2_modules is defined and item.value.enabled|bool and item.key in ['vspheredb', 'x509', 'reporting']
loop: "{{ icingaweb2_modules | dict2items }}"
18 changes: 9 additions & 9 deletions roles/icingaweb2/tasks/manage_mysql_imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
- name: Build mysql command
ansible.builtin.set_fact:
_tmp_mysqlcmd: >-
mysql {% if _db['host'] | default('localhost') != 'localhost' %} -h "{{ _db['host'] }}" {%- endif %}
{% if _db['port'] is defined %} -P "{{ _db['port'] }}" {%- endif %}
{% if _db['ssl_mode'] is defined %} --ssl-mode "{{ _db['ssl_mode'] }}" {%- endif %}
{% if _db['ssl_ca'] is defined %} --ssl-ca "{{ _db['ssl_ca'] }}" {%- endif %}
{% if _db['ssl_cert'] is defined %} --ssl-cert "{{ _db['ssl_cert'] }}" {%- endif %}
{% if _db['ssl_key'] is defined %} --ssl-key "{{ _db['ssl_key'] }}" {%- endif %}
{% if _db['ssl_cipher'] is defined %} --ssl-cipher "{{ _db['ssl_cipher'] }}" {%- endif %}
{% if _db['ssl_extra_options'] is defined %} {{ _db['ssl_extra_options'] }} {%- endif %}
mysql {% if _db['host'] | default('localhost') != 'localhost' %} -h "{{ _db['host'] }}"{%- endif %}
{% if _db['port'] is defined %} -P "{{ _db['port'] }}"{%- endif %}
{% if _db['ssl_mode'] is defined %} --ssl-mode "{{ _db['ssl_mode'] }}"{%- endif %}
{% if _db['ssl_ca'] is defined %} --ssl-ca "{{ _db['ssl_ca'] }}"{%- endif %}
{% if _db['ssl_cert'] is defined %} --ssl-cert "{{ _db['ssl_cert'] }}"{%- endif %}
{% if _db['ssl_key'] is defined %} --ssl-key "{{ _db['ssl_key'] }}"{%- endif %}
{% if _db['ssl_cipher'] is defined %} --ssl-cipher "{{ _db['ssl_cipher'] }}"{%- endif %}
{% if _db['ssl_extra_options'] is defined %} {{ _db['ssl_extra_options'] }}{%- endif %}
-u "{{ _db['user'] }}"
-p"{{ _db['password'] }}"
"{{ _db['name'] }}"
Expand All @@ -33,6 +33,6 @@
- name: MySQL import db schema
ansible.builtin.shell: >
{{ _tmp_mysqlcmd }}
< {{ _db['schema_path'] }}
< {{ _db['schema_path_mysql'] }}
when: _db_schema.rc != 0
run_once: yes
111 changes: 111 additions & 0 deletions roles/icingaweb2/tasks/modules/reporting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
- name: Module Reporting | Install from source
when: vars['icingaweb2_modules'][_module]['source'] == 'git'
vars:
_module: "{{ item.key }}"
block:
- name: Module Reporting | Download release {{ icingaweb2_module_source_versions[_module] }}
ansible.builtin.get_url:
url: https://github.com/Icinga/icingaweb2-module-reporting/archive/refs/tags/{{ icingaweb2_module_source_versions[_module] }}.tar.gz
dest: /tmp/
mode: "0644"
register: _download

- name: Module Reporting | Extract source archive
ansible.builtin.unarchive:
src: "{{ _download.dest }}"
dest: "{{ _download.dest | dirname }}"
owner: "{{ icingaweb2_httpd_user }}"
group: "{{ icingaweb2_group }}"
mode: "0755"
remote_src: true

- name: Module Reporting | Create module directory
ansible.builtin.file:
state: directory
dest: "{{ icingaweb2_config.global.module_path }}/{{ _module }}"
owner: "{{ icingaweb2_httpd_user }}"
group: "{{ icingaweb2_group }}"
mode: "0755"

- name: Module Reporting | Move module to module path
ansible.builtin.copy:
src: "{{ _download.dest | dirname }}/{{ _download.dest | basename | regex_replace('\\.tar\\.gz', '') }}/"
dest: "{{ icingaweb2_config.global.module_path }}/{{ _module }}"
owner: "{{ icingaweb2_httpd_user }}"
group: "{{ icingaweb2_group }}"
mode: "0755"
force: true
remote_src: true

- name: Module Reporting | Ensure config directory
ansible.builtin.file:
state: directory
dest: "{{ icingaweb2_modules_config_dir }}/{{ item.key }}"
owner: "{{ icingaweb2_httpd_user }}"
group: "{{ icingaweb2_group }}"
mode: "2770"

- name: Module Reporting | Manage config files
ansible.builtin.include_tasks: manage_module_config.yml
loop: "{{ _files }}"
loop_control:
loop_var: _file
when: vars['icingaweb2_modules'][_module][_file] is defined
vars:
_module: "{{ item.key }}"
_files:
- config

- name: Module Reporting | Manage Schema
when: vars['icingaweb2_modules'][_module]['database']['import_schema'] | default(false)
vars:
_module: "{{ item.key }}"
block:
- name: Module Reporting | Prepare _db informations
ansible.builtin.set_fact:
_db:
host: "{{ vars['icingaweb2_modules'][_module]['database']['host'] | default('localhost') }}"
port: "{{ vars['icingaweb2_modules'][_module]['database']['port'] | default('3306') }}"
user: "{{ vars['icingaweb2_modules'][_module]['database']['user'] | default('reporting') }}"
password: "{{ vars['icingaweb2_modules'][_module]['database']['password'] | default(omit) }}"
name: "{{ vars['icingaweb2_modules'][_module]['database']['name'] | default('reporting') }}"
ssl_mode: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_mode'] | default(omit) }}"
ssl_ca: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_ca'] | default(omit) }}"
ssl_cert: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_cert'] | default(omit) }}"
ssl_key: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_key'] | default(omit) }}"
ssl_cipher: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_cipher'] | default(omit) }}"
ssl_extra_options: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_extra_options'] | default(omit) }}"
schema_path_mysql: /usr/share/icingaweb2/modules/reporting/schema/mysql.schema.sql
schema_path_pgsql: /usr/share/icingaweb2/modules/reporting/schema/pgsql.schema.sql
select_query: "select * from reporting_schema"
type: "{{ vars['icingaweb2_modules'][_module]['database']['type'] | default(omit) }}"

- ansible.builtin.fail:
fail_msg: "No database type was provided"
when: vars['icingaweb2_modules'][_module]['database']['type'] is not defined

- ansible.builtin.fail:
fail_msg: "Invalid database type was provided. [Supported: mysql, pgsql]"
when: _db.type not in ['mysql', 'pgsql']

- name: Module Reporting | Import MySQL Schema
ansible.builtin.include_tasks: ../manage_mysql_imports.yml
when: _db.type == 'mysql'

- name: Module Reporting | Import PostgreSQL Schema
ansible.builtin.include_tasks: ../manage_pgsql_imports.yml
when: _db.type == 'pgsql'

- name: Module Reporting | empty _db var
ansible.builtin.set_fact:
_db: {}

- name: Module Reporting | Configure daemon
ansible.builtin.copy:
src: "{{ icingaweb2_config.global.module_path }}/reporting/config/systemd/icinga-reporting.service"
dest: /etc/systemd/system/icinga-reporting.service
mode: "0644"
owner: root
group: root
remote_src: true
3 changes: 3 additions & 0 deletions roles/icingaweb2/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ icingaweb2_module_packages:
director: icinga-director
x509: icinga-x509
businessprocess: icinga-businessprocess

icingaweb2_module_source_versions:
reporting: v1.0.0

0 comments on commit 831d4a3

Please sign in to comment.