Browse Source

Templates in client_definitions

#211 add client_template option
Michael Porter 6 years ago
parent
commit
d378402c51

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
 site
-molecule/default/data/
+molecule/shared/data/*
+!molecule/shared/data/static/
 molecule/*/cache/

+ 7 - 0
molecule/amazonlinux/molecule.yml

@@ -13,11 +13,15 @@ platforms:
     command: /sbin/init
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
   - name: amazonlinux-2
     image: dokken/amazonlinux-2
     command: /usr/lib/systemd/systemd
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
 provisioner:
@@ -51,6 +55,9 @@ provisioner:
         sensu_api_host: "{{ ansible_hostname }}"
         ansible_default_ipv4:
           address: 127.0.0.1
+        sensu_remote_plugins:
+          - sensu-plugins-disk-checks
+        sensu_check_interval: 60
     host_vars:
       amazonlinux-1:
         inspec_version: el6

+ 7 - 0
molecule/centos/molecule.yml

@@ -13,11 +13,15 @@ platforms:
     command: /sbin/init
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
   - name: centos-7
     image: dokken/centos-7
     command: /usr/lib/systemd/systemd
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
 provisioner:
@@ -51,6 +55,9 @@ provisioner:
         sensu_api_host: "{{ ansible_hostname }}"
         ansible_default_ipv4:
           address: 127.0.0.1
+        sensu_remote_plugins:
+          - sensu-plugins-disk-checks
+        sensu_check_interval: 60
     host_vars:
       centos-6:
         inspec_version: el6

+ 7 - 0
molecule/debian/molecule.yml

@@ -14,6 +14,8 @@ platforms:
     privileged: yes
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
+    groups:
+      - sensu_checks
   - name: debian-9
     image: dokken/debian-9
     command: /lib/systemd/systemd
@@ -21,6 +23,8 @@ platforms:
       - SYS_ADMIN
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
+    groups:
+      - sensu_checks
 provisioner:
   name: ansible
   config_options:
@@ -52,6 +56,9 @@ provisioner:
         sensu_api_host: "{{ ansible_hostname }}"
         ansible_default_ipv4:
           address: 127.0.0.1
+        sensu_remote_plugins:
+          - sensu-plugins-disk-checks
+        sensu_check_interval: 60
     host_vars:
       debian-8:
         inspec_version: ubuntu1604

+ 9 - 0
molecule/fedora/molecule.yml

@@ -15,6 +15,8 @@ platforms:
       - SYS_ADMIN
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
+    groups:
+      - sensu_checks
   - name: fedora-27
     image: dokken/fedora-27
     command: /usr/lib/systemd/systemd
@@ -22,6 +24,8 @@ platforms:
       - SYS_ADMIN
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
+    groups:
+      - sensu_checks
   - name: fedora-28
     image: dokken/fedora-latest
     command: /usr/lib/systemd/systemd
@@ -29,6 +33,8 @@ platforms:
       - SYS_ADMIN
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
+    groups:
+      - sensu_checks
 provisioner:
   name: ansible
   config_options:
@@ -60,6 +66,9 @@ provisioner:
         sensu_api_host: "{{ ansible_hostname }}"
         ansible_default_ipv4:
           address: 127.0.0.1
+        sensu_remote_plugins:
+          - sensu-plugins-disk-checks
+        sensu_check_interval: 60
     host_vars:
       fedora-26:
         inspec_version: el7

+ 11 - 0
molecule/shared/data/static/sensu/client_definitions/sensu_masters/check_disk_usage.json

@@ -0,0 +1,11 @@
+{
+    "checks": {
+        "check_disk_usage": {
+            "command": "check-disk-usage.rb",
+            "standalone": true,
+            "ttl": 1800,
+            "interval": 120,
+            "refresh": 1800
+        }
+    }
+}

+ 11 - 0
molecule/shared/data/static/sensu/client_templates/not_used/not_a_check.json.j2

@@ -0,0 +1,11 @@
+{
+    "checks": {
+        "not_a_check": {
+            "command": "check-disk-usage.rb",
+            "standalone": true,
+            "ttl": 1800,
+            "interval": {{ sensu_check_interval }},
+            "refresh": 1800
+        }
+    }
+}

+ 11 - 0
molecule/shared/data/static/sensu/client_templates/sensu_checks/metrics_disk_usage.json.j2

@@ -0,0 +1,11 @@
+{
+    "checks": {
+        "metrics_disk_usage": {
+            "command": "metrics-disk-usage.rb",
+            "standalone": true,
+            "ttl": 1800,
+            "interval": {{ sensu_check_interval }},
+            "refresh": 1800
+        }
+    }
+}

+ 18 - 0
molecule/shared/tests/test_default.rb

@@ -50,3 +50,21 @@ describe http('http://127.0.0.1:4567/health',
               params: { consumers: 1 }) do
   its('status') { should eq 204 }
 end
+
+# Ensure disk check exists
+describe file('/etc/sensu/conf.d/sensu_masters/check_disk_usage.json') do
+  it { should exist }
+  its('content') { should match(/check-disk-usage/) }
+end
+
+# Ensure disk metrics exists
+describe file('/etc/sensu/conf.d/sensu_checks/metrics_disk_usage.json') do
+  it { should exist }
+  its('content') { should match(/metrics-disk-usage/) }
+  its('content') { should match(/60/) }
+end
+
+# Ensure not_used does not exist
+describe file('/etc/sensu/conf.d/not_used/not_a_check.json') do
+  it { should_not exist }
+end

+ 9 - 0
molecule/ubuntu/molecule.yml

@@ -13,11 +13,15 @@ platforms:
     command: /sbin/init
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
   - name: ubuntu-16.04
     image: dokken/ubuntu-16.04
     command: /bin/systemd
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
   - name: ubuntu-18.04
@@ -25,6 +29,8 @@ platforms:
     command: /bin/systemd
     capabilities:
       - SYS_ADMIN
+    groups:
+      - sensu_checks
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
 provisioner:
@@ -58,6 +64,9 @@ provisioner:
         sensu_api_host: "{{ ansible_hostname }}"
         ansible_default_ipv4:
           address: 127.0.0.1
+        sensu_remote_plugins:
+          - sensu-plugins-disk-checks
+        sensu_check_interval: 60
     host_vars:
       ubuntu-14.04:
         inspec_version: ubuntu1404

+ 34 - 1
tasks/plugins.yml

@@ -25,6 +25,7 @@
     - mutators
     - definitions
     - client_definitions
+    - client_templates
 
 - name: Ensure any remote plugins defined are present
   shell: umask 0022; sensu-install -p {{ item }}
@@ -109,7 +110,39 @@
     group: "{{ sensu_group_name }}"
   when:
     - sensu_available_client_definitions is defined
-    - sensu_available_checks is not skipped
+    - sensu_available_client_definitions is not skipped
     - item in sensu_available_client_definitions.stdout_lines
   loop: "{{ group_names|flatten }}"
   notify: restart sensu-client service
+
+- name: Register available client templates
+  command: "ls {{ static_data_store }}/sensu/client_templates"
+  delegate_to: localhost
+  register: sensu_available_client_templates
+  changed_when: false
+  become: false
+
+- name: Deploy client template folders
+  file:
+    path: '{{ sensu_config_path }}/conf.d/{{ item | basename }}'
+    state: directory
+    owner: "{{ sensu_user_name }}"
+    group: "{{ sensu_group_name }}"
+  when:
+    - sensu_available_client_templates is defined
+    - sensu_available_client_templates is not skipped
+    - item in sensu_available_client_templates.stdout_lines
+  loop: "{{ group_names|flatten }}"
+  notify: restart sensu-client service
+
+- name: Deploy client templates
+  template:
+    src: "{{ static_data_store }}/sensu/client_templates/{{ item.path | dirname }}/{{ item.path | basename }}"
+    dest: "{{ sensu_config_path }}/conf.d/{{ item.path | dirname }}/{{ item.path | basename | regex_replace('.j2', '') }}"
+    owner: "{{ sensu_user_name }}"
+    group: "{{ sensu_group_name }}"
+  with_filetree: "{{ static_data_store }}/sensu/client_templates"
+  when:
+    - item.state == 'file'
+    - item.path | dirname in group_names
+  notify: restart sensu-client service