diff options
author | Gaëtan Duchaussois <gaetan.duchaussois@fretlink.com> | 2021-04-28 16:49:16 +0200 |
---|---|---|
committer | Gaëtan Duchaussois <gaetan.duchaussois@fretlink.com> | 2021-04-28 17:01:14 +0200 |
commit | 8916f8fa2167abad24cfdd2a9e305160d83a6958 (patch) | |
tree | 4ca417c914c3a84cd6ddeb30158a7ae4746ddb54 | |
parent | 0b423e4532da6a8e65b0cc5de00fe786c82d5013 (diff) | |
download | ansible-netdata_straight-8916f8fa2167abad24cfdd2a9e305160d83a6958.tar.gz ansible-netdata_straight-8916f8fa2167abad24cfdd2a9e305160d83a6958.tar.zst ansible-netdata_straight-8916f8fa2167abad24cfdd2a9e305160d83a6958.zip |
feature: add sensor configuration
-rw-r--r-- | .github/workflows/pr.yml | 11 | ||||
-rw-r--r-- | README.md | 17 | ||||
-rw-r--r-- | defaults/main.yml | 5 | ||||
-rw-r--r-- | molecule/features/INSTALL.rst | 15 | ||||
-rw-r--r-- | molecule/features/converge.yml | 7 | ||||
-rw-r--r-- | molecule/features/host_vars/instance-features.yml | 11 | ||||
-rw-r--r-- | molecule/features/molecule.yml | 21 | ||||
-rw-r--r-- | molecule/features/prepare.yml | 19 | ||||
-rw-r--r-- | molecule/features/verify.yml | 40 | ||||
-rw-r--r-- | tasks/config.yml | 162 | ||||
-rw-r--r-- | tasks/main.yml | 4 | ||||
-rw-r--r-- | templates/health_template.j2 | 6 | ||||
-rw-r--r-- | templates/netdata_alarm.conf.j2 | 21 |
13 files changed, 338 insertions, 1 deletions
diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index dfd5489..36a66a1 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml | |||
@@ -8,7 +8,7 @@ on: | |||
8 | branches: | 8 | branches: |
9 | - master | 9 | - master |
10 | jobs: | 10 | jobs: |
11 | build: | 11 | molecule_default: |
12 | runs-on: ubuntu-latest | 12 | runs-on: ubuntu-latest |
13 | strategy: | 13 | strategy: |
14 | max-parallel: 4 | 14 | max-parallel: 4 |
@@ -31,3 +31,12 @@ jobs: | |||
31 | env: | 31 | env: |
32 | MOLECULE_OS: ${{ matrix.molecule-os }} | 32 | MOLECULE_OS: ${{ matrix.molecule-os }} |
33 | MOLECULE_NETDATA_INSTALLER: ${{ matrix.netdata-installer }} | 33 | MOLECULE_NETDATA_INSTALLER: ${{ matrix.netdata-installer }} |
34 | molecule_features: | ||
35 | runs-on: ubuntu-latest | ||
36 | steps: | ||
37 | - uses: actions/checkout@v2 | ||
38 | with: | ||
39 | path: "${{ github.repository }}" | ||
40 | - uses: gofrolist/molecule-action@v2 | ||
41 | with: | ||
42 | molecule_args: --scenario-name features | ||
@@ -13,6 +13,23 @@ Role Variables | |||
13 | 13 | ||
14 | See [defaults/main.yml] | 14 | See [defaults/main.yml] |
15 | 15 | ||
16 | * `netdata_extra_config`: An array of application configurations. Defaults to `[]` | ||
17 | * `name`: the name of the specified collector | ||
18 | * `specific_task_file`: a task file to be included. | ||
19 | * `apt_dependencies`: an array of package dependencies | ||
20 | * `pip_dependencies`: an array of pip dependencies to be installed | ||
21 | * `extra_groups`: an array of groups to add netdata user to | ||
22 | * `read_files`: an array of file path to which netdata user should be granted access | ||
23 | * `collector_type`: `python`, `node`, `go` or `charts` | ||
24 | * `replace`: when a sensor is changed from one type to another allow ro remove the old configuration | ||
25 | * `config`: the content of the configuration file as yaml to be put in `$type.d` config | ||
26 | * `health_config`: the templates as in netdata acceptation for this collector, an array of templates configuration | ||
27 | * `name`: the template name | ||
28 | * `definition`: a list of string defining the template (like 'on: apache.requests') | ||
29 | * `netdata_alarms_overrides`: an array of override for default netdata templates | ||
30 | * `name`: friendly name of the override (filesystem compatible) | ||
31 | * `override`: the content of the override | ||
32 | |||
16 | Dependencies | 33 | Dependencies |
17 | ------------ | 34 | ------------ |
18 | 35 | ||
diff --git a/defaults/main.yml b/defaults/main.yml index 12c5fe6..3026012 100644 --- a/defaults/main.yml +++ b/defaults/main.yml | |||
@@ -26,3 +26,8 @@ netdata_streaming_configuration: | |||
26 | netdata_alarm_notify_configs: {} | 26 | netdata_alarm_notify_configs: {} |
27 | # Define a custom_sender function to be used in alarm configuration | 27 | # Define a custom_sender function to be used in alarm configuration |
28 | netdata_custom_sender_function: "" | 28 | netdata_custom_sender_function: "" |
29 | netdata_extra_config: [] | ||
30 | netdata_alarms_overrides: [] | ||
31 | |||
32 | netdata_pip_package: | ||
33 | Ubuntu20.04: python3-pip | ||
diff --git a/molecule/features/INSTALL.rst b/molecule/features/INSTALL.rst new file mode 100644 index 0000000..c615318 --- /dev/null +++ b/molecule/features/INSTALL.rst | |||
@@ -0,0 +1,15 @@ | |||
1 | ******* | ||
2 | Delegated driver installation guide | ||
3 | ******* | ||
4 | |||
5 | Requirements | ||
6 | ============ | ||
7 | |||
8 | This driver is delegated to the developer. Up to the developer to implement | ||
9 | requirements. | ||
10 | |||
11 | Install | ||
12 | ======= | ||
13 | |||
14 | This driver is delegated to the developer. Up to the developer to implement | ||
15 | requirements. | ||
diff --git a/molecule/features/converge.yml b/molecule/features/converge.yml new file mode 100644 index 0000000..37f6dea --- /dev/null +++ b/molecule/features/converge.yml | |||
@@ -0,0 +1,7 @@ | |||
1 | --- | ||
2 | - name: Converge | ||
3 | hosts: all | ||
4 | tasks: | ||
5 | - name: "Include netdata-straight" | ||
6 | include_role: | ||
7 | name: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}" | ||
diff --git a/molecule/features/host_vars/instance-features.yml b/molecule/features/host_vars/instance-features.yml new file mode 100644 index 0000000..fe33d25 --- /dev/null +++ b/molecule/features/host_vars/instance-features.yml | |||
@@ -0,0 +1,11 @@ | |||
1 | netdata_installer: kickstart-static64 | ||
2 | netdata_extra_config: | ||
3 | - name: test user | ||
4 | extra_groups: | ||
5 | - fakegroup | ||
6 | read_files: | ||
7 | - /etc/fakefile.conf | ||
8 | pip_dependencies: | ||
9 | - test-pip-install | ||
10 | apt_dependencies: | ||
11 | - hxtools | ||
diff --git a/molecule/features/molecule.yml b/molecule/features/molecule.yml new file mode 100644 index 0000000..ae3d948 --- /dev/null +++ b/molecule/features/molecule.yml | |||
@@ -0,0 +1,21 @@ | |||
1 | --- | ||
2 | dependency: | ||
3 | name: galaxy | ||
4 | driver: | ||
5 | name: docker | ||
6 | platforms: | ||
7 | - name: instance-features | ||
8 | image: jrei/systemd-ubuntu:20.04 | ||
9 | privileged: true | ||
10 | command: /lib/systemd/systemd | ||
11 | tmpfs: | ||
12 | - /run | ||
13 | volumes: | ||
14 | - /sys/fs/cgroup:/sys/fs/cgroup:ro | ||
15 | provisioner: | ||
16 | name: ansible | ||
17 | inventory: | ||
18 | links: | ||
19 | host_vars: host_vars/ | ||
20 | verifier: | ||
21 | name: ansible | ||
diff --git a/molecule/features/prepare.yml b/molecule/features/prepare.yml new file mode 100644 index 0000000..b4e8439 --- /dev/null +++ b/molecule/features/prepare.yml | |||
@@ -0,0 +1,19 @@ | |||
1 | - name: Prepare | ||
2 | hosts: all | ||
3 | tasks: | ||
4 | - name: Update apt cache | ||
5 | apt: | ||
6 | update_cache: yes | ||
7 | become: true | ||
8 | when: ansible_os_family == 'Debian' | ||
9 | - name: Create fakegroup | ||
10 | group: | ||
11 | name: fakegroup | ||
12 | state: present | ||
13 | - name: Create fake conf file | ||
14 | copy: | ||
15 | content: "Test file" | ||
16 | dest: /etc/fakefile.conf | ||
17 | owner: root | ||
18 | group: root | ||
19 | mode: 0600 | ||
diff --git a/molecule/features/verify.yml b/molecule/features/verify.yml new file mode 100644 index 0000000..9f64471 --- /dev/null +++ b/molecule/features/verify.yml | |||
@@ -0,0 +1,40 @@ | |||
1 | --- | ||
2 | # This is an example playbook to execute Ansible tests. | ||
3 | - name: Verify | ||
4 | hosts: all | ||
5 | gather_facts: false | ||
6 | tasks: | ||
7 | - name: Get netdata groups | ||
8 | command: id -Gzn | ||
9 | register: netdata_groups_st | ||
10 | become: true | ||
11 | become_user: netdata | ||
12 | changed_when: false | ||
13 | |||
14 | - name: Check file access | ||
15 | stat: | ||
16 | path: /etc/fakefile.conf | ||
17 | become: yes | ||
18 | become_user: netdata | ||
19 | register: netdata_stat_st | ||
20 | |||
21 | - name: Check pip package | ||
22 | command: test-pip-install | ||
23 | become: yes | ||
24 | become_user: netdata | ||
25 | changed_when: false | ||
26 | |||
27 | - name: Check apt package | ||
28 | command: rot13 | ||
29 | args: | ||
30 | stdin: test | ||
31 | changed_when: false | ||
32 | |||
33 | - name: Asserts config was applied | ||
34 | assert: | ||
35 | that: | ||
36 | - "'fakegroup' in group_array" | ||
37 | - netdata_can_read | ||
38 | vars: | ||
39 | group_array: "{{ netdata_groups_st.stdout.split('\0') | reject('eq','') | list }}" | ||
40 | netdata_can_read: "{{ netdata_stat_st.stat.readable }}" | ||
diff --git a/tasks/config.yml b/tasks/config.yml new file mode 100644 index 0000000..4ada6e6 --- /dev/null +++ b/tasks/config.yml | |||
@@ -0,0 +1,162 @@ | |||
1 | - name: Run specific tasks | ||
2 | include_tasks: "{{ item.specific_task_file }}" | ||
3 | when: item.specific_task_file is defined | ||
4 | loop: "{{ netdata_extra_config }}" | ||
5 | |||
6 | - name: Configure plugins apt dependencies | ||
7 | apt: | ||
8 | name: "{{ netdata_plugins_apt }}" | ||
9 | state: present | ||
10 | vars: | ||
11 | netdata_plugins_apt: "{{ netdata_extra_config | map(attribute='apt_dependencies') | | ||
12 | reject('undefined') | list | flatten | unique }}" | ||
13 | register: netdata_plugins_apt_deps_st | ||
14 | until: netdata_plugins_apt_deps_st is succeeded | ||
15 | |||
16 | - name: Get list of pip dependencies | ||
17 | set_fact: | ||
18 | netdata_plugins_pip: "{{ netdata_extra_config | map(attribute='pip_dependencies') | | ||
19 | reject('undefined') | list | flatten | unique }}" | ||
20 | |||
21 | - name: Install pip for python dependencies | ||
22 | apt: | ||
23 | name: "{{ python_package }}" | ||
24 | state: present | ||
25 | when: | ||
26 | - netdata_plugins_pip|length > 0 | ||
27 | - ansible_os_family == 'Debian' | ||
28 | vars: | ||
29 | python_package: "{{ netdata_pip_package[ansible_distribution ~ ansible_distribution_version] | default('python-pip') }}" | ||
30 | register: netdata_pip_install | ||
31 | until: netdata_pip_install is succeeded | ||
32 | |||
33 | - name: Configure plugins pip dependencies | ||
34 | pip: | ||
35 | name: "{{ netdata_plugins_pip }}" | ||
36 | state: present | ||
37 | when: netdata_plugins_pip|length > 0 | ||
38 | register: netdata_plugins_pip_deps_st | ||
39 | until: netdata_plugins_pip_deps_st is succeeded | ||
40 | |||
41 | - name: Add netdata to extra groups | ||
42 | user: | ||
43 | name: netdata | ||
44 | groups: "{{ netdata_plugins_groups }}" | ||
45 | append: yes | ||
46 | notify: restart netdata | ||
47 | vars: | ||
48 | netdata_plugins_groups: "{{ netdata_extra_config | map(attribute='extra_groups') | | ||
49 | reject('undefined') | list | flatten | unique }}" | ||
50 | |||
51 | - name: Get list of files to grant read on | ||
52 | set_fact: | ||
53 | netdata_plugins_files: "{{ netdata_extra_config | map(attribute='read_files') | | ||
54 | reject('undefined') | list | flatten | unique }}" | ||
55 | |||
56 | - name: Install acl if files need to be granted access | ||
57 | apt: | ||
58 | name: acl | ||
59 | state: present | ||
60 | when: netdata_plugins_files|length > 0 | ||
61 | |||
62 | - name: Grant read acces to files | ||
63 | acl: | ||
64 | path: "{{ item }}" | ||
65 | entity: netdata | ||
66 | etype: user | ||
67 | permissions: r | ||
68 | state: present | ||
69 | notify: restart netdata | ||
70 | loop: "{{ netdata_plugins_files }}" | ||
71 | |||
72 | - name: Configure plugins | ||
73 | copy: | ||
74 | dest: "{{ netdata_collector_conf_dir }}/{{ collector.name }}.conf" | ||
75 | content: "{{ content[collector.format | default('yaml')] }}" | ||
76 | mode: 0640 | ||
77 | owner: netdata | ||
78 | when: collector.config is defined | ||
79 | notify: restart netdata | ||
80 | loop: "{{ netdata_extra_config }}" | ||
81 | loop_control: | ||
82 | loop_var: collector | ||
83 | label: "{{ collector.name }}" | ||
84 | vars: | ||
85 | netdata_collector_conf_dir: "/etc/netdata/{{ collector.type }}.d" | ||
86 | bash_content_tmp: "{{ collector.config.items() | sort | list | map('join','=') | list }}" | ||
87 | bash_content: "{{ bash_content_tmp | map('regex_replace','(.*)=(.*)',collector.name ~ '_\\1=\\2') | join('\n') }}" | ||
88 | yaml_content: "{{ collector.config | to_nice_yaml }}" | ||
89 | json_content: "{{ collector.config | to_nice_json }}" | ||
90 | content: | ||
91 | bash: "{{ bash_content ~ '\n' }}" | ||
92 | json: "{{ json_content }}" | ||
93 | yaml: "{{ yaml_content }}" | ||
94 | no_log: "{{ collector.no_log | default(false) }}" | ||
95 | |||
96 | - name: Remove old config | ||
97 | file: | ||
98 | path: "{{ netdata_old_collector_conf_dir }}/{{ collector.name }}.conf" | ||
99 | state: absent | ||
100 | when: collector.replace is defined | ||
101 | notify: restart netdata | ||
102 | loop: "{{ netdata_extra_config }}" | ||
103 | loop_control: | ||
104 | loop_var: collector | ||
105 | label: "{{ collector.name }}" | ||
106 | vars: | ||
107 | netdata_old_collector_conf_dir: "/etc/netdata/{{ collector.replace }}.d" | ||
108 | |||
109 | - name: Configure chart | ||
110 | copy: | ||
111 | dest: "{{ netdata_collector_chart_dir }}" | ||
112 | src: "{{ collector.chart_name }}.chart.sh" | ||
113 | mode: 0755 | ||
114 | owner: netdata | ||
115 | when: collector.chart_name is defined | ||
116 | notify: restart netdata | ||
117 | loop: "{{ netdata_extra_config }}" | ||
118 | loop_control: | ||
119 | loop_var: collector | ||
120 | label: "{{ collector.name }}" | ||
121 | vars: | ||
122 | netdata_collector_chart_dir: "/usr/libexec/netdata/charts.d/" | ||
123 | |||
124 | - name: Configure plugins health | ||
125 | template: | ||
126 | dest: "{{ netdata_health_conf_dir }}/{{ collector.name }}.conf" | ||
127 | src: health_template.j2 | ||
128 | mode: 0640 | ||
129 | owner: netdata | ||
130 | when: collector.health_config is defined | ||
131 | notify: restart netdata # a reload would be enough | ||
132 | loop: "{{ netdata_extra_config }}" | ||
133 | loop_control: | ||
134 | loop_var: collector | ||
135 | label: "{{ collector.name }}" | ||
136 | |||
137 | - name: Configure overrides | ||
138 | copy: | ||
139 | dest: "{{ netdata_health_conf_dir }}/{{ override.name }}.conf" | ||
140 | content: | | ||
141 | # {{ override.name }} | ||
142 | {{ override.override }} | ||
143 | mode: 0640 | ||
144 | owner: netdata | ||
145 | notify: reload netdata | ||
146 | loop: "{{ netdata_alarms_overrides }}" | ||
147 | loop_control: | ||
148 | loop_var: override | ||
149 | label: "{{ override.name }}" | ||
150 | |||
151 | - name: Configure alarm | ||
152 | template: | ||
153 | dest: "{{ netdata_health_conf_dir }}/{{ collector.name }}.conf" | ||
154 | src: "netdata_alarm.conf.j2" | ||
155 | mode: 0755 | ||
156 | owner: netdata | ||
157 | when: collector.alarm_config is defined | ||
158 | notify: restart netdata | ||
159 | loop: "{{ netdata_extra_config }}" | ||
160 | loop_control: | ||
161 | loop_var: collector | ||
162 | label: "{{ collector.name }}" | ||
diff --git a/tasks/main.yml b/tasks/main.yml index ca849a3..f05eff4 100644 --- a/tasks/main.yml +++ b/tasks/main.yml | |||
@@ -65,3 +65,7 @@ | |||
65 | - netdata_alarm_notify_configs is defined | 65 | - netdata_alarm_notify_configs is defined |
66 | - netdata_alarm_notify_configs | count > 0 | 66 | - netdata_alarm_notify_configs | count > 0 |
67 | notify: restart netdata | 67 | notify: restart netdata |
68 | |||
69 | - name: Configure netdata sensors | ||
70 | include_tasks: config.yml | ||
71 | when: (netdata_extra_config|length > 0) or (netdata_alarms_overrides|length > 0) | ||
diff --git a/templates/health_template.j2 b/templates/health_template.j2 new file mode 100644 index 0000000..2e67b96 --- /dev/null +++ b/templates/health_template.j2 | |||
@@ -0,0 +1,6 @@ | |||
1 | {% for template in collector.health_config %} | ||
2 | template = {{ template.name }} | ||
3 | {% for config in template.definition %} | ||
4 | {{ config }} | ||
5 | {% endfor %} | ||
6 | {% endfor %} | ||
diff --git a/templates/netdata_alarm.conf.j2 b/templates/netdata_alarm.conf.j2 new file mode 100644 index 0000000..c689a95 --- /dev/null +++ b/templates/netdata_alarm.conf.j2 | |||
@@ -0,0 +1,21 @@ | |||
1 | {% for alarm in collector.alarm_config %} | ||
2 | {% if alarm.template is defined -%} | ||
3 | template: {{ alarm.template }} | ||
4 | families: {{ alarm.families | default("*") }} | ||
5 | {% else %} | ||
6 | alarm: {{ alarm.alarm | default( collector.name ) }} | ||
7 | {% endif %} | ||
8 | on: {{ alarm.on_what }} | ||
9 | os: {{ alarm.os | default("*") }} | ||
10 | hosts: {{ alarm.os | default("*") }} | ||
11 | lookup: {{ alarm.lookup }} | ||
12 | {% if alarm.calc is defined %} | ||
13 | calc: {{ alarm.calc }} | ||
14 | {% endif %} | ||
15 | every: {{ alarm.every | default("1m") }} | ||
16 | warn: {{ alarm.warn | default("$status == $WARNING") }} | ||
17 | crit: {{ alarm.crit | default("$status == $CRITICAL") }} | ||
18 | delay: {{ alarm.delay | default("up 0 down 0 multiplier 1.0 max 0") }} | ||
19 | to: {{ alarm.to | default("silent") }} | ||
20 | |||
21 | {% endfor %} | ||