diff --git a/.gitignore b/.gitignore index bd85392..51889dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,19 @@ -*# -*~ -venv/ -.vagrant/ -*deb -*rpm -playbooks/ \ No newline at end of file +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db +# Logs # +###################### +*.log +# Artifacts # +###################### +*.retry +*.vault_pass +# Ansible # +###################### +roles/ diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..bc41b24 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,45 @@ +--- +sudo: required +dist: xenial + +language: python +python: "2.7" + +# Install pip package manager +addons: + apt: + packages: + - python-pip + +install: + # Install ruby and serverspec gem as root (used for test run later) + - sudo apt-get install -y ruby + - sudo ruby -v + - sudo gem install serverspec + + # Install ansible + - pip install ansible + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' > ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + # Real run: + - ansible-playbook tests/test.yml -i tests/inventory --connection=local + # Second run to check for indempotence: + # Allowing 1 'change' for status read. + - > + ansible-playbook tests/test.yml -i tests/inventory --connection=local + | grep -q 'changed=1.*failed=0' + && (echo 'Idempotence test: pass' && exit 0) + || (echo 'Idempotence test: fail' && exit 1) + # Run serverspec + - sudo rake spec + # Output result of config file (only non commented lines) + - sed '/ *#/d; /^ *$/d' /etc/telegraf/telegraf.conf + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/README.md b/README.md index 2a99954..53d085b 100644 --- a/README.md +++ b/README.md @@ -1,56 +1,65 @@ -Telegraf -======== +[![Build Status](https://travis-ci.com/mediapeers/ansible-role-telegraf.svg?branch=master)](https://travis-ci.com/mediapeers/ansible-role-telegraf) -An Ansible role to install, configure, and manage [Telegraf](https://github.com/influxdb/telegraf), the plugin-driven server agent for reporting metrics into InfluxDB. - -Requirements ------------- +# Ansible role for Telegraf -Prior knowledge/experience with InfluxDB and Telegraf is highly recommended. Full documentation is available [here](https://docs.influxdata.com). +An Ansible role to install, configure, and manage [Telegraf](https://github.com/influxdb/telegraf), the plugin-driven server agent for reporting metrics into InfluxDB. -Installation ------------- +Note: this is a fork of https://github.com/rossmcdonald/telegraf -Either clone this repository, or install through Ansible Galaxy directly using the command: +## Requirements -``` -ansible-galaxy install rossmcdonald.telegraf -``` +Linux server (Debian/RedHat based), place to send the events too. +Prior knowledge/experience with InfluxDB and Telegraf is highly recommended. See output plugins [here](https://github.com/influxdata/telegraf#output-plugins). -Role Variables --------------- +## Role Variables The high-level variables are stored in the `defaults/main.yml` file. The most important ones being: -``` -# Channel of Telegraf to install (currently only 'stable' is supported) -telegraf_install_version: stable -``` +- `telegraf_output_plugins` - A list of dictionaries to configure your ouput plugins, for instance to influxdb or other destinations. +- `telegraf_input_plugins` - A list dictionaries to configure the metrics you want to collect. Either system metrics from the host the agent runs on or metrics of some services, DBs etc. +- `telegraf_custom_config_file` - A path to a file that contains (additional) telegraf config (can be a Jinja2 template too). + +For more params see `defaults/main.yml`. More advanced configuration options are stored in the `vars/main.yml` file, which includes all of the necessary bells and whistles to tweak your configuration. -Dependencies ------------- +## Dependencies No other Ansible dependencies are required. This role was tested and developed with Ansible 1.9.4. -Example Playbook ----------------- - -An example playbook is included in the `test.yml` file. There is also a `Vagrantfile`, which can be used for quick local testing leveraging [Vagrant](https://www.vagrantup.com/). - -Contributions and Feedback --------------------------- - -Any contributions are welcome. For any bugs or feature requests, please open an issue through Github. - -License -------- +## Example Playbook + +Example playbook using this role: + + - name: My Play with Telegraf + hosts: servers + vars: + telegraf_output_plugins: + - name: instrumental + options: + api_token: "abc-1234" + prefix: "" + template: "measurement.host.system_measurement_tag.tags.field" + timeout: "2s" + debug: false + telegraf_input_plugins: + - name: cpu + options: + key: "value" + tags: + key: "value" + tagdrop: + key: "value" + tagpass: + key: "value" + roles: + - mediapeers.telegraf + + +## License MIT -Author ------- - -Created by [Ross McDonald](https://github.com/rossmcdonald). +## Author +Created by [Ross McDonald](https://github.com/rossmcdonald). Extended by [Stefan Horning](https://github.com/stefanhorning). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a17f046 --- /dev/null +++ b/Rakefile @@ -0,0 +1,26 @@ +require 'rake' +require 'rspec/core/rake_task' + +task :spec => 'spec:all' +task :default => :spec + +namespace :spec do + targets = [] + Dir.glob('./spec/*_spec.rb').each do |file| + target = File.basename(file) + target = "_#{target}" if target == "default" + targets << target + end + + task :all => targets + task :default => :all + + targets.each do |target| + original_target = target == "_default" ? target[1..-1] : target + desc "Run serverspec tests to #{original_target}" + RSpec::Core::RakeTask.new(target.to_sym) do |t| + ENV['TARGET_HOST'] = original_target + t.pattern = "spec/*_spec.rb" + end + end +end diff --git a/Vagrantfile b/Vagrantfile deleted file mode 100644 index 1c38fd6..0000000 --- a/Vagrantfile +++ /dev/null @@ -1,40 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -Vagrant.configure(2) do |config| - config.vm.box = "ubuntu/trusty64" - # config.vm.box = "ubuntu/vivid64" - # config.vm.box = "relativkreativ/centos-7-minimal" - # config.vm.box = "box-cutter/fedora22" - # config.vm.box = "puppetlabs/centos-6.6-64-nocm" - # config.vm.box = "debian/jessie64" - - BOX_COUNT = 1 - (1..BOX_COUNT).each do |machine_id| - config.vm.define "telegraf#{machine_id}" do |machine| - machine.vm.hostname = "telegraf#{machine_id}" - # machine.vm.network "private_network", ip: "10.0.3.#{1+machine_id}", virtualbox__intnet: true - # machine.vm.network "public_network" - machine.vm.network "public_network", :bridge => 'en0: Wi-Fi (AirPort)' - - machine.vm.provider "virtualbox" do |v| - v.memory = 512 - v.cpus = 1 - end - - if machine_id == BOX_COUNT - machine.vm.provision "ansible" do |ansible| - # ansible.verbose = 'vvvv' - ansible.limit = 'all' - ansible.playbook = "test.yml" - ansible.sudo = true - ansible.host_key_checking = false - ansible.extra_vars = { - is_vagrant: true, - } - end - end - - end - end -end diff --git a/defaults/main.yml b/defaults/main.yml index d8197f4..5a0a5d6 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,69 +1,67 @@ --- -# Channel of Telegraf to install -telegraf_install_version: stable +# Version to install when using the RedHat play (yum/rpm package) +telegraf_rpm_version: 1.6.3-1 # Configuration Variables -telegraf_tags: +telegraf_tags: {} +telegraf_logfile: "" # emtpy means no log +telegraf_hostname: "" # empty means fall back to os.Hostname() +telegraf_omit_hostname: "false" +# Define telegraf_custom_bin_url to overwrite telegraf bin with your own build +# telegraf_custom_bin_url: 'https://download.com/location/telegraf' -telegraf_agent_interval: 10s -telegraf_round_interval: "true" -telegraf_metric_batch_size: "1000" -telegraf_metric_buffer_limit: "10000" +telegraf_flush_interval: 20s +telegraf_flush_jitter: 5s -telegraf_collection_jitter: 0s -telegraf_flush_interval: 10s -telegraf_flush_jitter: 0s -telegraf_debug: "false" -telegraf_quiet: "false" -telegraf_hostname: -telegraf_omit_hostname: "false" -telegraf_install_url: +# Define this variable if you want to provide your own config file, +# without passing plugin configs through the telegraf_input_plugins variable. +# Will be put into /etc/telegraf/telegraf.d/ config dir. +# telegraf_custom_config_file: /path/to/template/file.conf.j2 -telegraf_influxdb_urls: - - http://localhost:8086 -telegraf_influxdb_database: telegraf -telegraf_influxdb_precision: s -telegraf_influxdb_retention_policy: autogen -telegraf_influxdb_write_consistency: any -telegraf_influxdb_ssl_ca: -telegraf_influxdb_ssl_cert: -telegraf_influxdb_ssl_key: -telegraf_influxdb_insecure_skip_verify: +# Replace with your own plugin configurations: -telegraf_influxdb_timeout: 5s -telegraf_influxdb_username: -telegraf_influxdb_password: -telegraf_influxdb_user_agent: -telegraf_influxdb_udp_payload: +telegraf_output_plugins: + - name: influxdb + options: + urls: ["http://localhost:8086"] + database: "telegraf" + write_consistency: "any" + timeout: "10s" + - name: instrumental + options: + api_token: "dummy-project-token" + prefix: "" + template: "measurement.host.system_measurement_tag.tags.field" + timeout: "2s" + debug: false -telegraf_plugins: - - name: mem - - name: system +telegraf_input_plugins: - name: cpu options: - percpu: "true" - totalcpu: "true" - fielddrop: - - "time_*" + percpu: true + totalcpu: true + collect_cpu_time: false - name: disk options: - mountpoints: - - "/" + mount_points: ["/"] + ignore_fs: ["tmpfs", "devtmpfs", "devfs"] + name_override: "system" + tagexclude: ["fstype"] + fieldpass: ["free", "total", "used", "used_percent"] + tags: + system_measurement_tag: "disk" - name: diskio options: - skip_serial_number: "true" - - name: procstat - options: - exe: "influxd" - prefix: "influxdb" - - name: procstat - options: - pid_file: "/var/lib/neo4j/data/neo4j-service.pid" - prefix: "neo4j_proc" - + skip_serial_number: true + - name: kernel + - name: mem + - name: processes + - name: swap + - name: system - name: net options: - interfaces: - - "eth0" - -telegraf_influxdata_base_url: "https://repos.influxdata.com" + name_override: "system" + tags: + system_measurement_tag: "network" + tagdrop: + interface: ["all"] diff --git a/handlers/main.yml b/handlers/main.yml index 5782b8b..6268de8 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,26 +1,12 @@ --- -# The order here matters +# mediapeers.telegraf handlers + - name: restart telegraf service: name: telegraf state: restarted - when: telegraf_start_service -- name: pause +- name: wait for startup + listen: restart telegraf pause: - seconds: "{{telegraf_start_delay}}" - when: telegraf_start_service - -## After version 2.2 of ansible 'listen' could be used to -## group 'check status' and 'assert running' into a single listener -- name: check status - command: service telegraf status - ignore_errors: yes - register: telegraf_service_status - when: telegraf_start_service - -- name: assert running - assert: - that: - - "telegraf_service_status.rc == 0" - when: telegraf_start_service + seconds: 3 diff --git a/meta/main.yml b/meta/main.yml index 9b8fe5b..7bc3a19 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -4,7 +4,7 @@ galaxy_info: description: Install and configure Telegraf, the plugin-driven server agent for reporting metrics into InfluxDB company: InfluxData license: MIT - min_ansible_version: 1.2 + min_ansible_version: 2.4 platforms: - name: EL versions: diff --git a/spec/main_spec.rb b/spec/main_spec.rb new file mode 100644 index 0000000..91e0b2f --- /dev/null +++ b/spec/main_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "Telegraf setup" do + package('telegraf') do + it { should be_installed } + end + + describe file('/etc/telegraf/') do + it { should be_directory } + end + + output_plugins = ANSIBLE_VARS.fetch('telegraf_output_plugins', []) + + describe file('/etc/telegraf/telegraf.conf') do + it { should be_file } + output_plugins.each do |plugin| + its(:content) { should include("[[outputs.#{plugin['name']}]]") } + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..0a90ab5 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,30 @@ +require 'serverspec' +require 'yaml' + +set :backend, :exec +#set :pre_command, 'sudo -s' + +# Identifier for specs being run from inside this role +# and not from a playbook repo/location that doesn't know +# internal role params +INSIDE_ROLE=true + +# Getting the ansible variables from included vars_files and +# playbook vars to be usable in the tests +vars_files = ["defaults/main.yml"] +playbook_vars = YAML.load_file("tests/test.yml").first["vars"] + +if vars_files + ansible_vars = {} + vars_files.each do |file| + tmp_vars = YAML.load_file(file) + tmp_vars.each do |k, v| + next unless v.is_a?(String) + v.gsub!(/{{(.+)}}/, "#{tmp_vars[$1]}") + end + ansible_vars.merge!(tmp_vars) + end + ansible_vars.merge!(playbook_vars) +end + +ANSIBLE_VARS = ansible_vars diff --git a/tasks/configure.yml b/tasks/configure.yml index c2763c6..775b820 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,39 +1,28 @@ --- -- name: Set templatized Telegraf configuration +# configure and (re)start telegraf and make sure it's enabled + +- name: Create Telegraf config file template: src: telegraf.conf.j2 dest: "{{ telegraf_configuration_dir }}/telegraf.conf" - force: yes - backup: yes - owner: telegraf - group: telegraf - mode: 0744 - when: telegraf_template_configuration - # If config changes, restart telegraf and confirm it remained running notify: - "restart telegraf" - - "pause" - - "check status" - - "assert running" - -- name: Test for sysvinit script - stat: - path: /etc/init.d/telegraf - register: telegraf_sysvinit_script - -- name: Modify user Telegraf should run as [sysvinit] - command: sed -i s/USER=.*/USER={{ telegraf_runas_user }}/ /etc/init.d/telegraf - when: telegraf_runas_user != "telegraf" and telegraf_sysvinit_script.stat.exists -- name: Modify group Telegraf should run as [sysvinit] - command: sed -i s/GROUP=.*/GROUP={{ telegraf_runas_group }}/ /etc/init.d/telegraf - when: telegraf_runas_group != "telegraf" and telegraf_sysvinit_script.stat.exists +- name: Output destination file name for custom Telegraf config + debug: + msg: "{{ (telegraf_custom_config_file|basename).split('.')|first }}.conf" + when: telegraf_custom_config_file is defined -- name: Modify user Telegraf should run as [systemd] - command: sed -i s/User=.*/User={{ telegraf_runas_user }}/ /usr/lib/systemd/system/telegraf.service - when: telegraf_runas_user != "telegraf" and not telegraf_sysvinit_script.stat.exists - register: telegraf_unit_file_updated +- name: Create additional Telegraf config from given template/file + template: + src: "{{ telegraf_custom_config_file }}" + dest: "{{ telegraf_configuration_dir }}/telegraf.d/{{ (telegraf_custom_config_file|basename).split('.')|first }}.conf" + when: telegraf_custom_config_file is defined + notify: + - "restart telegraf" -- name: Reload systemd configuration [systemd] - command: systemctl daemon-reload - when: telegraf_unit_file_updated is defined and telegraf_unit_file_updated.changed +- name: Check status and register telegraf daemon + systemd: + name: telegraf + state: started + enabled: True diff --git a/tasks/install-debian.yml b/tasks/install-debian.yml index 9f56fd0..6487ee4 100644 --- a/tasks/install-debian.yml +++ b/tasks/install-debian.yml @@ -1,42 +1,24 @@ --- - name: Install any necessary dependencies [Debian/Ubuntu] - apt: - name: "{{ item }}" - state: present - update_cache: yes - cache_valid_time: 3600 + apt: + name: "{{ item }}" + state: present with_items: - - python-httplib2 - - python-apt - - curl + - python-pip - apt-transport-https - name: Import InfluxData GPG signing key [Debian/Ubuntu] apt_key: - url: "{{ telegraf_influxdata_base_url }}/influxdb.key" + url: "{{ telegraf_repo_base_url }}/influxdb.key" state: present - when: telegraf_install_url is not defined or telegraf_install_url == None - name: Add InfluxData repository [Debian/Ubuntu] apt_repository: - repo: deb {{ telegraf_influxdata_base_url }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ telegraf_install_version }} + repo: deb {{ telegraf_repo_base_url }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ telegraf_install_version }} state: present - when: telegraf_install_url is not defined or telegraf_install_url == None - name: Install Telegraf packages [Debian/Ubuntu] - apt: + apt: name: telegraf state: latest - update_cache: yes - cache_valid_time: 3600 - when: telegraf_install_url is not defined or telegraf_install_url == None - -- name: Download Telegraf package via URL [Debian/Ubuntu] - command: curl -o /tmp/telegraf-ansible-download.deb {{ telegraf_install_url }} - when: telegraf_install_url is defined and telegraf_install_url != None - -- name: Install downloaded Telegraf package [Debian/Ubuntu] - apt: - deb: /tmp/telegraf-ansible-download.deb - state: present - when: telegraf_install_url is defined and telegraf_install_url != None + update_cache: yes diff --git a/tasks/install-redhat.yml b/tasks/install-redhat.yml index c8744db..19e403e 100644 --- a/tasks/install-redhat.yml +++ b/tasks/install-redhat.yml @@ -1,28 +1,6 @@ --- -- name: Install any necessary dependencies [RedHat/CentOS] - yum: - name: "{{ item }}" - state: present - with_items: - - curl - -- name: Add InfluxData repository file [RHEL/CentOS] - template: - src: etc/yum.repos.d/influxdata.repo.j2 - dest: /etc/yum.repos.d/influxdata.repo - force: yes - backup: yes - when: telegraf_install_url is not defined or telegraf_install_url == None - name: Install Telegraf packages [RHEL/CentOS] yum: - name: telegraf - state: latest - update_cache: yes - when: telegraf_install_url is not defined or telegraf_install_url == None - -- name: Install Telegraf from URL [RHEL/CentOS] - yum: - name: "{{ telegraf_install_url }}" + name: 'https://dl.influxdata.com/telegraf/releases/telegraf-{{ telegraf_rpm_version }}.x86_64.rpm' state: present - when: telegraf_install_url is defined and telegraf_install_url != None diff --git a/tasks/install.yml b/tasks/install.yml index dd79f3d..cf283f7 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -1,6 +1,30 @@ --- -- include: install-redhat.yml +- include_tasks: install-redhat.yml when: ansible_os_family == "RedHat" -- include: install-debian.yml - when: ansible_os_family == "Debian" \ No newline at end of file +- include_tasks: install-debian.yml + when: ansible_os_family == "Debian" + +# Allows overwriting telegraf bin with custom build one: + +- name: Stop telegraf before overwriting binary + service: + name: telegraf + state: stopped + when: telegraf_custom_bin_url is defined + +- name: Overwrite telegraf binary with custom build one from URL + get_url: + url: '{{ telegraf_custom_bin_url }}' + dest: /usr/bin/telegraf + force: yes + owner: root + group: root + mode: 0775 + when: telegraf_custom_bin_url is defined + +- name: Start telegraf after overwriting binary + service: + name: telegraf + state: started + when: telegraf_custom_bin_url is defined diff --git a/tasks/main.yml b/tasks/main.yml index bafa60b..d81fa13 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,10 +1,8 @@ --- -- include: install.yml +- import_tasks: install.yml + become: true tags: [telegraf, install] -- include: configure.yml +- import_tasks: configure.yml + become: true tags: [telegraf, configure] - -- include: start.yml - tags: [telegraf, start] - when: telegraf_start_service diff --git a/tasks/start.yml b/tasks/start.yml deleted file mode 100644 index 2fff96b..0000000 --- a/tasks/start.yml +++ /dev/null @@ -1,11 +0,0 @@ - -- name: Start the Telegraf service - service: - name: telegraf - state: started - enabled: yes - # Only care to check the status if the state changed to 'started' - notify: - - "pause" - - "check status" - - "assert running" diff --git a/templates/etc/yum.repos.d/influxdata.repo.j2 b/templates/etc/yum.repos.d/influxdata.repo.j2 index 304a3be..fc82674 100644 --- a/templates/etc/yum.repos.d/influxdata.repo.j2 +++ b/templates/etc/yum.repos.d/influxdata.repo.j2 @@ -1,7 +1,7 @@ [influxdb] name = InfluxDB Repository - {{ ansible_distribution }} $releasever -baseurl = {{ telegraf_influxdata_base_url }}/{{ ansible_distribution|lower }}/$releasever/$basearch/{{ telegraf_install_version }} +baseurl = {{ telegraf_repo_base_url }}/{{ ansible_distribution|lower }}/$releasever/$basearch/{{ telegraf_install_version }} enabled = 1 gpgcheck = 1 -gpgkey = {{ telegraf_influxdata_base_url }}/influxdb.key +gpgkey = {{ telegraf_repo_base_url }}/influxdb.key sslverify = 1 diff --git a/templates/telegraf.conf.j2 b/templates/telegraf.conf.j2 index fd4e4e0..501d9a7 100644 --- a/templates/telegraf.conf.j2 +++ b/templates/telegraf.conf.j2 @@ -1,32 +1,34 @@ -# Telegraf configuration +# {{ ansible_managed }} +# Telegraf Configuration +# # Telegraf is entirely plugin driven. All metrics are gathered from the -# declared plugins. - -# Even if a plugin has no configuration, it must be declared in here -# to be active. Declaring a plugin means just specifying the name -# as a section with no variables. To deactivate a plugin, comment -# out the name and any variables. - -# Use 'telegraf -config telegraf.toml -test' to see what metrics a config +# declared inputs, and sent to the declared outputs. +# +# Plugins must be declared in here or in a telegraf.d/*.conf file to be activated. +# To deactivate a plugin, comment out the name and any variables. +# +# Use 'telegraf -config telegraf.conf -test' to see what metrics a config # file would generate. - -# One rule that plugins conform to is wherever a connection string -# can be passed, the values '' and 'localhost' are treated specially. -# They indicate to the plugin to use their own builtin configuration to -# connect to the local system. - -# NOTE: The configuration has a few required parameters. They are marked -# with 'required'. Be sure to edit those to make this configuration work. - -# Tags can also be specified via a normal map, but only one form at a time: -[tags] +# +# Environment variables can be used anywhere in this config file, simply prepend +# them with $. For strings the variable must be within quotes (ie, "$STR_VAR"), +# for numbers and booleans they should be plain (ie, $INT_VAR, $BOOL_VAR) + + +# Global tags can be specified here in key="value" format. +[global_tags] + # dc = "us-east-1" # will tag all metrics with dc=us-east-1 + # rack = "1a" + ## Environment variables can be used as tags, and throughout the config file + # user = "$USER" {% if telegraf_tags is defined and telegraf_tags != None %} -{% for key, value in telegraf_tags.iteritems()%} +{% for key, value in telegraf_tags.items()|list %} {{ key }} = "{{ value }}" {% endfor %} {% endif %} + # Configuration for telegraf agent [agent] ## Default data collection interval for all inputs @@ -35,12 +37,15 @@ ## ie, if interval="10s" then always collect on :00, :10, :20, etc. round_interval = {{ telegraf_round_interval }} - ## Telegraf will send metrics to outputs in batches of at - ## most metric_batch_size metrics. + ## Telegraf will send metrics to outputs in batches of at most + ## metric_batch_size metrics. + ## This controls the size of writes that Telegraf sends to output plugins. metric_batch_size = {{ telegraf_metric_batch_size }} + ## For failed writes, telegraf will cache metric_buffer_limit metrics for each ## output, and will flush this buffer on a successful write. Oldest metrics ## are dropped first when this buffer fills. + ## This buffer only fills when writes fail to output plugin(s). metric_buffer_limit = {{ telegraf_metric_buffer_limit }} ## Collection jitter is used to jitter the collection by a random amount. @@ -57,126 +62,70 @@ ## ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s flush_jitter = "{{ telegraf_flush_jitter }}" - ## Run telegraf in debug mode + ## By default or when set to "0s", precision will be set to the same + ## timestamp order as the collection interval, with the maximum being 1s. + ## ie, when interval = "10s", precision will be "1s" + ## when interval = "250ms", precision will be "1ms" + ## Precision will NOT be used for service inputs. It is up to each individual + ## service input to set the timestamp at the appropriate precision. + precision = "" + + ## Logging configuration: + ## Run telegraf with debug log messages. debug = {{ telegraf_debug }} - ## Run telegraf in quiet mode + ## Run telegraf in quiet mode (error log messages only). quiet = {{ telegraf_quiet }} + ## Specify the log file name. The empty string means to log to stderr. + logfile = "{{ telegraf_logfile }}" - # Override default hostname, if empty use os.Hostname() -{% if telegraf_hostname is defined and telegraf_hostname != None %} + ## Override default hostname, if empty use os.Hostname() hostname = "{{ telegraf_hostname }}" -{% else %} - hostname = "{{ ansible_hostname }}" -{% endif %} - ## If set to true, do no set the "host" tag in the telegraf agent. omit_hostname = {{ telegraf_omit_hostname }} + ############################################################################### -# OUTPUTS # +# OUTPUT PLUGINS # ############################################################################### -[outputs] - -# Configuration for influxdb server to send metrics to -[[outputs.influxdb]] - # The full HTTP or UDP endpoint URL for your InfluxDB instance. - # Multiple urls can be specified but it is assumed that they are part of the same - # cluster, this means that only ONE of the urls will be written to each interval. - # urls = ["udp://localhost:8089"] # UDP endpoint example - urls = [ "{{ telegraf_influxdb_urls|join('","') }}" ] # required - # The target database for metrics (telegraf will create it if not exists) - database = "{{ telegraf_influxdb_database }}" # required - # Precision of writes, valid values are n, u, ms, s, m, and h - # note: using second precision greatly helps InfluxDB compression - precision = "{{ telegraf_influxdb_precision }}" - - ## Retention policy to write to. - retention_policy = "{{ telegraf_influxdb_retention_policy }}" - ## Write consistency (clusters only), can be: "any", "one", "quorom", "all" - write_consistency = "{{ telegraf_influxdb_write_consistency }}" - - # Connection timeout (for the connection with InfluxDB), formatted as a string. - # If not provided, will default to 0 (no timeout) - timeout = "{{ telegraf_influxdb_timeout }}" -{% if telegraf_influxdb_username is defined and telegraf_influxdb_username != None %} - username = "{{ telegraf_influxdb_username }}" -{% endif %} -{% if telegraf_influxdb_password is defined and telegraf_influxdb_password != None %} - password = "{{ telegraf_influxdb_password }}" -{% endif %} - # Set the user agent for HTTP POSTs (can be useful for log differentiation) -{% if telegraf_influxdb_user_agent is defined and telegraf_influxdb_user_agent != None %} - user_agent = "{{ telegraf_influxdb_user_agent }}" -{% endif %} - # Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes) -{% if telegraf_influxdb_udp_payload is defined and telegraf_influxdb_udp_payload != None %} - udp_payload = {{ telegraf_influxdb_udp_payload }} +{% for plugin in telegraf_output_plugins %} +[[outputs.{{ plugin.name }}]] +{% if plugin.options is defined %} +{% for key, value in plugin.options.items()|list %} + {{ key }} = {{ value|to_json }} +{% endfor %} {% endif %} - ## Optional SSL Config -{% if telegraf_influxdb_ssl_ca is defined and telegraf_influxdb_ssl_ca != None %} - # ssl_ca = "{{ telegraf_influxdb_ssl_ca }}" -{% endif %} -{% if telegraf_influxdb_ssl_cert is defined and telegraf_influxdb_ssl_cert != None %} - # ssl_cert = "{{ telegraf_influxdb_ssl_cert }}" -{% endif %} -{% if telegraf_influxdb_ssl_key is defined and telegraf_influxdb_ssl_key != None %} - # ssl_key = "{{ telegraf_influxdb_ssl_key }}" -{% endif %} +{% endfor %} - ## Use SSL but skip chain & host verification -{% if telegraf_influxdb_insecure_skip_verify is defined and telegraf_influxdb_insecure_skip_verify != None %} - # insecure_skip_verify = telegraf_influxdb_insecure_skip_verify -{% endif %} ############################################################################### -# PLUGINS # +# INPUT PLUGINS # ############################################################################### -{% for plugin in telegraf_plugins %} +{% for plugin in telegraf_input_plugins %} [[inputs.{{ plugin.name }}]] {% if plugin.options is defined %} -{% for key, value in plugin.options.iteritems() %} -{% if value is not mapping %} -{% if value is sequence and value is not string %} -{% if value[0] is number %} - {{ key }} = [ {{ value|join(', ') }} ] -{% else %} - {{ key }} = [ "{{ value|join('", "') }}" ] -{% endif %} -{% else %} -{% if value == "true" or value == "false" or value is number %} - {{ key }} = {{ value }} -{% else %} - {{ key }} = "{{ value }}" -{% endif %} -{% endif %} -{% endif %} +{% for key, value in plugin.options.items()|list %} + {{ key }} = {{ value|to_json }} {% endfor %} -{% for key, value in plugin.options.iteritems() %} -{% if value is mapping %} - [inputs.{{ plugin.name }}.{{ key }}] -{% for lv2_key, lv2_value in value.iteritems() %} -{% if lv2_value is sequence and lv2_value is not string %} -{% if lv2_value[0] is number %} - {{ lv2_key }} = [ {{ lv2_value|join(', ') }} ] -{% else %} - {{ lv2_key }} = [ "{{ lv2_value|join('", "') }}" ] -{% endif %} -{% else %} -{% if lv2_value == "true" or lv2_value == "false" or lv2_value is number %} - {{ lv2_key }} = {{ lv2_value }} -{% else %} - {{ lv2_key }} = "{{ lv2_value }}" -{% endif %} {% endif %} +{% if plugin.tags is defined %} + [inputs.{{ plugin.name }}.tags] +{% for key, value in plugin.tags.items()|list %} + {{ key }} = {{ value|to_json }} {% endfor %} {% endif %} +{% if plugin.tagpass is defined %} + [inputs.{{ plugin.name }}.tagpass] +{% for key, value in plugin.tagpass.items()|list %} + {{ key }} = {{ value|to_json }} {% endfor %} {% endif %} +{% if plugin.tagdrop is defined %} + [inputs.{{ plugin.name }}.tagdrop] +{% for key, value in plugin.tagdrop.items()|list %} + {{ key }} = {{ value|to_json }} {% endfor %} - -############################################################################### -# service PLUGINS # -############################################################################### +{% endif %} +{% endfor %} \ No newline at end of file diff --git a/test.yml b/test.yml deleted file mode 100644 index e0e8b5e..0000000 --- a/test.yml +++ /dev/null @@ -1,8 +0,0 @@ -- hosts: all - vars_files: - - defaults/main.yml - - vars/main.yml - tasks: - - include: tasks/main.yml - handlers: - - include: handlers/main.yml diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..d18580b --- /dev/null +++ b/tests/inventory @@ -0,0 +1 @@ +localhost \ No newline at end of file diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..49f833e --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,13 @@ +--- +- hosts: localhost + become: true + vars: + telegraf_output_plugins: + - name: influxdb + options: + urls: ["http://localhost:8086"] + database: "telegraf-test" + write_consistency: "any" + timeout: "5s" + roles: + - ansible-role-telegraf diff --git a/vars/main.yml b/vars/main.yml index edb050b..b001615 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,20 +1,19 @@ --- -# Whether or not the playbook is run locally -# This should only be set in the Vagrantfile and not modified elsewhere -is_vagrant: no +# Channel of Telegraf to install +telegraf_install_version: stable -# If yes, service will be started. Will not be started if set to no. -telegraf_start_service: yes -telegraf_start_delay: 6 +# Telegraf repo (by Influxdata) +telegraf_repo_base_url: "https://repos.influxdata.com" -# If yes, will overwrite the packaged configuration with an Asnible/jinja2 template -telegraf_template_configuration: yes +# Sane defaults for global settings +telegraf_agent_interval: 10s +telegraf_round_interval: "true" +telegraf_metric_batch_size: 1000 +telegraf_metric_buffer_limit: 10000 +telegraf_collection_jitter: 0s +telegraf_debug: "false" +telegraf_quiet: "false" -# Path for finding Telegraf data. Added for backwards-compatibility. -telegraf_binary_path: /usr/bin/telegraf +# Location of config files telegraf_configuration_dir: /etc/telegraf - -# The user and group telegraf should run under (should be set to telegraf unless needed otherwise) -telegraf_runas_user: telegraf -telegraf_runas_group: telegraf