From 365f3fd10eda834a59367a94500abac1e62a22f2 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Wed, 2 Dec 2015 15:40:06 +0100
Subject: [PATCH] Issue #2 Update agent to version 2

---
 defaults/main.yml         |  6 +++---
 tasks/apache.yml          | 15 ++++++++++++++
 tasks/main.yml            | 22 +++++++++++++++++---
 tasks/mysql.yml           | 17 ++++++++++++++++
 tasks/uninstallv1.yml     | 20 +++++++++++++++++++
 templates/apache.yaml     |  4 ++++
 templates/config.cfg      | 40 +++++++++++++++----------------------
 templates/mysql.yaml      |  6 ++++++
 templates/plugins.cfg     |  7 +++++++
 templates/supervisor.conf | 42 +++++++++++++++++++++++++++++++++++++++
 10 files changed, 149 insertions(+), 30 deletions(-)
 create mode 100644 tasks/mysql.yml
 create mode 100644 tasks/uninstallv1.yml
 create mode 100644 templates/apache.yaml
 create mode 100644 templates/mysql.yaml
 create mode 100644 templates/plugins.cfg
 create mode 100644 templates/supervisor.conf

diff --git a/defaults/main.yml b/defaults/main.yml
index 5bfb907..8f63888 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -2,6 +2,7 @@
 # file: roles/serverdensity/defaults/main.yml
 
 sd_update_remote: yes
+sd_account: ''
 sd_url: ''
 sd_api_token: ''
 sd_api_cache_file: no
@@ -15,7 +16,6 @@ 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_repo_public_key_uri: 'https://archive.serverdensity.com/sd-packaging-public.key'
+sd_agent_plugins_dir: '/usr/share/python/sd-agent/checks.d/plugins'
 sd_plugins_files_dir: "{{ playbook_dir }}/files/sd-plugins"
-sd_template_config: 'config.cfg'
diff --git a/tasks/apache.yml b/tasks/apache.yml
index 85a7711..a769ed5 100644
--- a/tasks/apache.yml
+++ b/tasks/apache.yml
@@ -1,6 +1,21 @@
 ---
 # file: roles/serverdensity/tasks/apache.yml
 
+- name: "ServerDensity | Apache | Install The Agent"
+  apt:
+    pkg=sd-agent-apache
+    state=installed
+  notify: "ServerDensity | Restart Agent"
+
+- name: "ServerDensity | Apache | Configure The Agent"
+  template:
+    src=apache.yaml
+    dest=/etc/sd-agent/conf.d/apache.yaml
+    owner=root
+    group=root
+    mode=0644
+  notify: "ServerDensity | Restart Agent"
+
 - name: "ServerDensity | Apache | Enable some required modules"
   apache2_module:
     name={{ item }}
diff --git a/tasks/main.yml b/tasks/main.yml
index 1076825..af3af51 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -1,6 +1,15 @@
 ---
 # file: roles/serverdensity/tasks/main.yml
 
+- name: "ServerDensity | Check for old version 1"
+  shell: ls /etc/sd-agent/conf.d/disk.yaml.default
+  register: sd_v2_available
+  ignore_errors: yes
+  changed_when: False
+
+- include: uninstallv1.yml
+  when: sd_v2_available.stdout != '/etc/sd-agent/conf.d/disk.yaml.default'
+
 - name: "ServerDensity | Init SD plugin"
   local_action:
     serverdensity
@@ -17,7 +26,7 @@
 
 - name: "ServerDensity | Add ServerDensity Repository To Apt"
   copy:
-    content='deb http://www.serverdensity.com/downloads/linux/deb all main'
+    content='deb http://archive.serverdensity.com/ubuntu/ all main'
     dest=/etc/apt/sources.list.d/sd-agent.list
     mode=644
 
@@ -44,13 +53,20 @@
 
 - name: "ServerDensity | Configure The Agent"
   template:
-    src={{ sd_template_config }}
-    dest=/etc/sd-agent/config.cfg
+    src={{ item }}
+    dest=/etc/sd-agent/{{ item }}
     owner=root
     group=root
     mode=0644
+  with_items:
+    - 'config.cfg'
+    - 'plugins.cfg'
+    #- 'supervisor.conf'
   when: sd_agent_key != ''
   notify: "ServerDensity | Restart Agent"
 
 - include: apache.yml
   when: sd_groups.apache != 'none' and inventory_hostname in groups[sd_groups.apache]
+
+- include: mysql.yml
+  when: sd_groups.mysql != 'none' and inventory_hostname in groups[sd_groups.mysql]
diff --git a/tasks/mysql.yml b/tasks/mysql.yml
new file mode 100644
index 0000000..3b3513a
--- /dev/null
+++ b/tasks/mysql.yml
@@ -0,0 +1,17 @@
+---
+# file: roles/serverdensity/tasks/mysql.yml
+
+- name: "ServerDensity | MySQL | Install The Agent"
+  apt:
+    pkg=sd-agent-mysql
+    state=installed
+  notify: "ServerDensity | Restart Agent"
+
+- name: "ServerDensity | MySQL | Configure The Agent"
+  template:
+    src=mysql.yaml
+    dest=/etc/sd-agent/conf.d/mysql.yaml
+    owner=root
+    group=root
+    mode=0644
+  notify: "ServerDensity | Restart Agent"
diff --git a/tasks/uninstallv1.yml b/tasks/uninstallv1.yml
new file mode 100644
index 0000000..c567f1e
--- /dev/null
+++ b/tasks/uninstallv1.yml
@@ -0,0 +1,20 @@
+---
+# file: roles/serverdensity/tasks/uninstallv1.yml
+
+# Stop agent
+- name: "ServerDensity | Stop v1 Agent"
+  service:
+    name=sd-agent
+    state=stopped
+
+# Uninstall agent
+- name: "ServerDensity | Uninstall v1 Agent"
+  apt:
+    pkg=sd-agent
+    state=absent
+
+# Remove /usr/bin/sd-agent
+- name: "ServerDensity | Create Plugins Directory"
+  file:
+    dest=/usr/bin/sd-agent
+    state=absent
diff --git a/templates/apache.yaml b/templates/apache.yaml
new file mode 100644
index 0000000..2c2d3e8
--- /dev/null
+++ b/templates/apache.yaml
@@ -0,0 +1,4 @@
+init_config:
+
+instances:
+  - apache_status_url: http://{{ inventory_hostname }}/server-status?auto
diff --git a/templates/config.cfg b/templates/config.cfg
index 354675c..7c47ea2 100644
--- a/templates/config.cfg
+++ b/templates/config.cfg
@@ -1,33 +1,25 @@
 # Server Density Agent Config
-# Docs: https://support.serverdensity.com/hc/en-us/articles/201003178-Agent-config-variables
-# Plugins: http://plugins.serverdensity.com/
+# Version 2
 
 [Main]
-sd_url: {{ sd_url }}
+sd_account: {{ sd_account }}
 agent_key: {{ sd_agent_key }}
 
-plugin_directory: {{  sd_agent_plugins_dir  }}
-logging_level: {{ sd_logging_level }}
+plugin_directory: {{ sd_agent_plugins_dir }}
 
-{% 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
+# ========================================================================== #
+# Logging
+# See https://support.serverdensity.com/hc/en-us/articles/213093038-Log-levels-agent-debug-mode
+# ========================================================================== #
 
-apache_status_url: http://{{ inventory_hostname }}/server-status/?auto
-apache_status_user:
-apache_status_pass:
-{% endif %}
+log_level: {{ sd_logging_level }}
 
-{% if sd_groups.mysql != 'none' and inventory_hostname in groups[sd_groups.mysql] %}
-# MySQL
-# See https://support.serverdensity.com/hc/en-us/articles/201013827-MySQL-monitoring-Linux-Mac-and-FreeBSD
+# collector_log_file: /var/log/sd-agent/collector.log
+# forwarder_log_file: /var/log/sd-agent/forwarder.log
 
-mysql_server: 127.0.0.1
-mysql_user: root
-mysql_pass: root
-{% endif %}
-
-{% if sd_groups.proxy != 'none' and inventory_hostname in groups[sd_groups.proxy] %}
-haproxy_svname: {{ inventory_hostname }}
-haproxy_url: http://127.0.0.1:7000/haproxy_stats
-{% endif %}
+# if syslog is enabled but a host and port are not set, a local domain socket
+# connection will be attempted
+#
+# log_to_syslog: yes
+# syslog_host:
+# syslog_port:
diff --git a/templates/mysql.yaml b/templates/mysql.yaml
new file mode 100644
index 0000000..7cb92e1
--- /dev/null
+++ b/templates/mysql.yaml
@@ -0,0 +1,6 @@
+init_config:
+
+instances:
+  - server: 127.0.0.1
+    user: root
+    pass: root
diff --git a/templates/plugins.cfg b/templates/plugins.cfg
new file mode 100644
index 0000000..ff3ecb8
--- /dev/null
+++ b/templates/plugins.cfg
@@ -0,0 +1,7 @@
+# PLugins config
+{% if sd_groups.proxy != 'none' and inventory_hostname in groups[sd_groups.proxy] %}
+
+[haproxy]
+haproxy_svname: {{ inventory_hostname }}
+haproxy_url: http://127.0.0.1:7000/haproxy_stats
+{% endif %}
diff --git a/templates/supervisor.conf b/templates/supervisor.conf
new file mode 100644
index 0000000..df052d5
--- /dev/null
+++ b/templates/supervisor.conf
@@ -0,0 +1,42 @@
+[supervisorctl]
+serverurl = unix:///var/tmp/sd-supervisor.sock
+
+[unix_http_server]
+file=/var/tmp/sd-supervisor.sock
+
+[rpcinterface:supervisor]
+supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
+
+[supervisord]
+http_port = /var/tmp/sd-supervisor.sock
+minfds = 1024
+minprocs = 200
+loglevel = info
+logfile = /var/log/sd-agent/supervisord.log
+logfile_maxbytes = 50MB
+nodaemon = false
+pidfile = /var/run/sd-supervisord.pid
+logfile_backups = 10
+environment=PYTHONPATH=/usr/share/python/sd-agent,LANG=POSIX
+
+[program:collector]
+command=/usr/share/python/sd-agent/bin/python /usr/share/python/sd-agent/agent.py foreground --use-local-forwarder
+stdout_logfile=NONE
+stderr_logfile=NONE
+priority=999
+startsecs=5
+startretries=3
+user=sd-agent
+environment=PYTHONPATH='/usr/share/python/sd-agent:$PYTHONPATH'
+
+[program:forwarder]
+command=/usr/share/python/sd-agent/bin/python /usr/share/python/sd-agent/sdagent.py
+stdout_logfile=NONE
+stderr_logfile=NONE
+startsecs=5
+startretries=3
+priority=998
+user=sd-agent
+
+[group:sd-agent]
+programs=forwarder,collector
-- 
GitLab