From a0415ab0e5a25608ab9b130dd6e141b563bb84e3 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Mon, 27 Jul 2015 17:31:45 +0200
Subject: [PATCH] Cleanup and Apache support

---
 defaults/main.yml                             |  4 +---
 handlers/main.yml                             | 11 +++++++--
 tasks/apache.yml                              | 23 +++++++++++++++++++
 tasks/main.yml                                |  3 +++
 templates/config.cfg                          | 12 +++++-----
 .../etc-apache2-sites-available-sd-agent      |  9 ++++++++
 6 files changed, 51 insertions(+), 11 deletions(-)
 create mode 100644 tasks/apache.yml
 create mode 100644 templates/etc-apache2-sites-available-sd-agent

diff --git a/defaults/main.yml b/defaults/main.yml
index 2f1d4e0..4938622 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -14,9 +14,7 @@ sd_groups:
   mysql: 'none'
   proxy: 'none'
 
-
 sd_repo_public_key_uri: 'https://www.serverdensity.com/downloads/boxedice-public.key'
 sd_agent_plugins_dir: '/usr/bin/sd-agent/plugins'
-sd_plugins_files_dir: "{{inventory_dir}}/files/sd-plugins"
+sd_plugins_files_dir: "{{ inventory_dir }}/files/sd-plugins"
 sd_template_config: 'config.cfg'
-...
diff --git a/handlers/main.yml b/handlers/main.yml
index 33555a2..a69488a 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -1,5 +1,12 @@
 ---
 # file: roles/serverdensity/handlers/main.yml
 
-- name: 'ServerDensity | Restart Agent'
-  service: name=sd-agent state=restarted
+- name: "ServerDensity | Restart Agent"
+  service:
+    name=sd-agent
+    state=restarted
+
+- name: "ServerDensity | Restart Apache"
+  service:
+    name=apache2
+    state=restarted
diff --git a/tasks/apache.yml b/tasks/apache.yml
new file mode 100644
index 0000000..b976082
--- /dev/null
+++ b/tasks/apache.yml
@@ -0,0 +1,23 @@
+---
+# file: roles/serverdensity/tasks/apache.yml
+
+- name: "ServerDensity | Apache | Enable some required modules"
+  apache_module:
+    name={{ item }}
+    state=present
+  with_items:
+    - status
+  notify: "ServerDensity | Restart Apache"
+
+- name: "ServerDensity | Apache | Configuration file for ServerDensity"
+  template:
+    src=etc-apache2-sites-available-sd-agent
+    dest=/etc/apache2/sites-available/sd-agent{{ apache_conf_ext }}
+    owner=root
+    group=root
+    mode=0644
+  notify: "ServerDensity | Restart Apache"
+
+- name: "ServerDensity | Apache | Enable the sd-agent site"
+  command: a2ensite sd-agent creates=/etc/apache2/sites-enabled/sd-agent{{ apache_conf_ext }}
+  notify: "ServerDensity | Restart Apache"
diff --git a/tasks/main.yml b/tasks/main.yml
index 2faf99a..54ef924 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -49,3 +49,6 @@
             mode=0644
   when: sd_agent_key != ''
   notify: 'ServerDensity | Restart Agent'
+
+- include: apache.yml
+  when: sd_groups.apache != 'none' and inventory_hostname in groups[sd_groups.apache]
diff --git a/templates/config.cfg b/templates/config.cfg
index e697d88..354675c 100644
--- a/templates/config.cfg
+++ b/templates/config.cfg
@@ -3,17 +3,17 @@
 # Plugins: http://plugins.serverdensity.com/
 
 [Main]
-sd_url: {{sd_url}}
-agent_key: {{sd_agent_key}}
+sd_url: {{ sd_url }}
+agent_key: {{ sd_agent_key }}
 
-plugin_directory: {{ sd_agent_plugins_dir }}
-logging_level: {{sd_logging_level}}
+plugin_directory: {{  sd_agent_plugins_dir  }}
+logging_level: {{ sd_logging_level }}
 
 {% if sd_groups.apache != 'none' and inventory_hostname in groups[sd_groups.apache] %}
 # Apache
 # See https://support.serverdensity.com/hc/en-us/articles/201253343-Apache-monitoring-Linux-Mac-and-FreeBSD
 
-apache_status_url: http://{{inventory_hostname}}/server-status/?auto
+apache_status_url: http://{{ inventory_hostname }}/server-status/?auto
 apache_status_user:
 apache_status_pass:
 {% endif %}
@@ -28,6 +28,6 @@ mysql_pass: root
 {% endif %}
 
 {% if sd_groups.proxy != 'none' and inventory_hostname in groups[sd_groups.proxy] %}
-haproxy_svname: {{inventory_hostname}}
+haproxy_svname: {{ inventory_hostname }}
 haproxy_url: http://127.0.0.1:7000/haproxy_stats
 {% endif %}
diff --git a/templates/etc-apache2-sites-available-sd-agent b/templates/etc-apache2-sites-available-sd-agent
new file mode 100644
index 0000000..95ad829
--- /dev/null
+++ b/templates/etc-apache2-sites-available-sd-agent
@@ -0,0 +1,9 @@
+<VirtualHost *:80>
+    <Location /server-status>
+        SetHandler server-status
+        Order Deny,Allow
+        Deny from all
+        Allow from 127.0.0.1{% for address in ansible_all_ipv4_addresses %} {{ address }}{% endfor %}
+    </Location>
+    ServerName {{ inventory_hostname }}
+</VirtualHost>
-- 
GitLab