diff --git a/README.md b/README.md index 0ccd4a2..b22ccb7 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,20 @@ Design goals are to have a minimal memory footprint with a plugin system so that ## Requirements +### Operating systems +This role will work on the following operating systems: -No requirements. (Yes, an Influxdb server somewhere on the network will help though ;-) ) + * Red Hat + * Debian + * Ubuntu + * Windows (Best effort) + +So, you'll need one of those operating systems.. :-) +Please sent Pull Requests or suggestions when you want to use this role for other Operating systems. + +### InfluxDB + +You'll need an InfluxDB instance running somewhere on your network. ## Upgrade ### 0.7.0 @@ -26,6 +38,8 @@ There was an issue: ## Role Variables +### Overall variables + The following parameters can be set for the Telegraf agent: * `telegraf_agent_version`: The version of Telegraf to install. Default: `1.9.0` @@ -70,6 +84,18 @@ The config will be printed line by line into the configuration, so you could als and it will be printed in the configuration file. +## Windows specific Variables + +**NOTE** + +_Supporting Windows is an best effort (I don't have the possibility to either test/verify changes on the various amount of available Windows instances). PR's specific to Windows will almost immediately be merged, unless some one is able to provide a Windows test mechanism via Travis for Pull Requests._ + +* `telegraf_win_install_dir`: The directory where Telegraf will be installed. +* `telegraf_win_logfile`: The location to the logfile of Telegraf. +* `telegraf_win_include`: The directory that will contain all plugin configuration. + +## Extra information + There are two properties which are similar, but are used differently. Those are: * `telegraf_plugins_default` diff --git a/defaults/main.yml b/defaults/main.yml index 8db0876..b66d4bd 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -54,3 +54,11 @@ telegraf_plugins_extra_exclusive: False # RedHat specific settings for convenience telegraf_redhat_releasever: "$releasever" + +telegraf_win_install_dir: 'C:\Telegraf' +telegraf_win_logfile: 'C:\\Telegraf\\telegraf.log' +telegraf_win_include: 'C:\Telegraf\telegraf_agent.d\' +telegraf_win_service_args: + - -service install + - -config {{ telegraf_win_install_dir }}\telegraf\telegraf.conf + - --config-directory {{ telegraf_win_include }} diff --git a/handlers/main.yml b/handlers/main.yml index 78a82a7..2dae45e 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -7,3 +7,9 @@ state: restarted enabled: yes become: yes + +- name: "Restart Windows Telegraf" + win_service: + name: Telegraf + start_mode: auto + state: restarted diff --git a/meta/main.yml b/meta/main.yml index 980a276..234562d 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -15,6 +15,9 @@ galaxy_info: - name: Debian versions: - all + - name: Windows + versions: + - all galaxy_tags: - monitoring diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index bb98be5..cf0078c 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -64,20 +64,6 @@ provisioner: - name_override = "percpu_usage" - fielddrop = ["cpu_time*"] - telegraf_plugins_default: - - plugin: cpu - config: - - percpu = true - - plugin: disk - - plugin: io - - plugin: mem - - plugin: net - - plugin: system - - plugin: swap - - plugin: netstat - - plugin: processes - - plugin: kernel - scenario: name: default verifier: diff --git a/tasks/configure.yml b/tasks/configure_linux.yml similarity index 100% rename from tasks/configure.yml rename to tasks/configure_linux.yml diff --git a/tasks/configure_windows.yml b/tasks/configure_windows.yml new file mode 100644 index 0000000..1d1d16a --- /dev/null +++ b/tasks/configure_windows.yml @@ -0,0 +1,78 @@ +--- + +- name: "Windows | Create directory structure" + win_file: + path: "{{ item }}" + state: directory + with_items: + - "{{ telegraf_win_install_dir }}" + - "{{ telegraf_win_include }}" + +- name: "Windows | Check if file is already downloaded" + win_stat: + path: '{{ telegraf_win_install_dir }}\telegraf-{{ telegraf_agent_version }}_windows_amd64.zip' + register: file_info + +- name: "Windows | Download Telegraf Agent Zip file" + win_get_url: + url: https://dl.influxdata.com/telegraf/releases/telegraf-{{ telegraf_agent_version }}_windows_amd64.zip + dest: '{{ telegraf_win_install_dir }}\telegraf-{{ telegraf_agent_version }}_windows_amd64.zip' + when: + - not file_info.stat.exists + +- name: "Windows | Unzip file" + win_unzip: + src: '{{ telegraf_win_install_dir }}\telegraf-{{ telegraf_agent_version }}_windows_amd64.zip' + dest: "{{ telegraf_win_install_dir }}" + creates: '{{ telegraf_win_install_dir }}\telegraf\telegraf.exe' + +- name: "Windows | Configure Telegraf" + win_template: + src: telegraf.conf.j2 + dest: '{{ telegraf_win_install_dir }}\telegraf\telegraf.conf' + notify: "Restart Windows Telegraf" + +- name: "Windows | Copy telegraf extra plugins" + win_template: + src: "telegraf-extra-plugin.conf.j2" + dest: "/etc/telegraf/telegraf.d/{{ item.key }}.conf" + with_dict: "{{ telegraf_plugins_extra }}" + loop_control: + label: "{{ item.key }}" + when: + - telegraf_plugins_extra is defined + - telegraf_plugins_extra is iterable + - item.value.state|default('present') != 'absent' + notify: "Restart Windows Telegraf" + +- name: "Windows | Remove telegraf extra plugins" + win_file: + path: "/etc/telegraf/telegraf.d/{{ item.key }}.conf" + state: absent + with_dict: "{{ telegraf_plugins_extra }}" + loop_control: + label: "{{ item.key }}" + when: + - telegraf_plugins_extra is defined + - telegraf_plugins_extra is iterable + - item.value.state|default('present') == 'absent' + notify: "Restart Windows Telegraf" + +- name: "Windows | Register Service" + win_command: '{{ telegraf_win_install_dir }}\telegraf\telegraf.exe {{ telegraf_win_service_args | join(" ") }}' + register: telegraf_windows_install + args: + creates: '{{ telegraf_win_install_dir }}\.installed' + +- name: "Windows | Create done file so it won't register itself again" + win_file: + path: '{{ telegraf_win_install_dir }}\.installed' + state: touch + when: + - telegraf_windows_install is changed + +- name: "Windows | Set service startup mode to auto and ensure it is started" + win_service: + name: Telegraf + start_mode: auto + state: started diff --git a/tasks/main.yml b/tasks/main.yml index 0f850ef..4b8f5ac 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -3,14 +3,23 @@ - name: "Install the correct repository" include_tasks: "RedHat.yml" - when: ansible_os_family == "RedHat" + when: + - ansible_os_family == "RedHat" - name: "Install the correct repository" include_tasks: "Debian.yml" - when: ansible_os_family == "Debian" + when: + - ansible_os_family == "Debian" tags: - telegraf - packages - name: "Configure telegraf" - include_tasks: "configure.yml" + include_tasks: "configure_linux.yml" + when: + - ansible_os_family != "Windows" + +- name: "Install / Configure telegraf on Windows" + include_tasks: "configure_windows.yml" + when: + - ansible_os_family == "Windows" diff --git a/templates/telegraf.conf.j2 b/templates/telegraf.conf.j2 index 98dd721..b79267b 100644 --- a/templates/telegraf.conf.j2 +++ b/templates/telegraf.conf.j2 @@ -28,7 +28,11 @@ quiet = {{ telegraf_agent_quiet | lower }} {% endif %} {% if telegraf_agent_version is version_compare('1.1', '>=') %} +{% if ansible_os_family == "Windows" %} + logfile = "{{ telegraf_win_logfile }}" +{% else %} logfile = "{{ telegraf_agent_logfile }}" +{% endif %} omit_hostname = {{ telegraf_agent_omit_hostname | lower }} {% endif %}