mirror of
https://github.com/dj-wasabi/ansible-telegraf.git
synced 2025-07-17 08:46:37 +00:00
Added support for (Open)Suse
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
telegraf_agent_version: 1.9.0
|
||||
telegraf_agent_version_patch: 1
|
||||
telegraf_agent_package: telegraf
|
||||
telegraf_agent_package_state: latest
|
||||
telegraf_agent_package_state: present
|
||||
telegraf_agent_hostname: "{{ ansible_fqdn }}"
|
||||
telegraf_agent_interval: 10
|
||||
telegraf_agent_debug: False
|
||||
|
212
files/telegraf.init
Normal file
212
files/telegraf.init
Normal file
@ -0,0 +1,212 @@
|
||||
#! /usr/bin/env bash
|
||||
|
||||
# chkconfig: 2345 99 01
|
||||
# description: Telegraf daemon
|
||||
|
||||
### BEGIN INIT INFO
|
||||
# Provides: telegraf
|
||||
# Required-Start: $all
|
||||
# Required-Stop: $remote_fs $syslog
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop: 0 1 6
|
||||
# Short-Description: Start telegraf at boot time
|
||||
### END INIT INFO
|
||||
|
||||
# this init script supports three different variations:
|
||||
# 1. New lsb that define start-stop-daemon
|
||||
# 2. Old lsb that don't have start-stop-daemon but define, log, pidofproc and killproc
|
||||
# 3. Centos installations without lsb-core installed
|
||||
#
|
||||
# In the third case we have to define our own functions which are very dumb
|
||||
# and expect the args to be positioned correctly.
|
||||
|
||||
# Command-line options that can be set in /etc/default/telegraf. These will override
|
||||
# any config file values.
|
||||
TELEGRAF_OPTS=
|
||||
|
||||
USER=telegraf
|
||||
GROUP=telegraf
|
||||
|
||||
if [ -r /lib/lsb/init-functions ]; then
|
||||
source /lib/lsb/init-functions
|
||||
fi
|
||||
|
||||
DEFAULT=/etc/default/telegraf
|
||||
|
||||
if [ -r $DEFAULT ]; then
|
||||
set -o allexport
|
||||
source $DEFAULT
|
||||
set +o allexport
|
||||
fi
|
||||
|
||||
if [ -z "$STDOUT" ]; then
|
||||
STDOUT=/dev/null
|
||||
fi
|
||||
if [ ! -f "$STDOUT" ]; then
|
||||
mkdir -p `dirname $STDOUT`
|
||||
fi
|
||||
|
||||
if [ -z "$STDERR" ]; then
|
||||
STDERR=/var/log/telegraf/telegraf.log
|
||||
fi
|
||||
if [ ! -f "$STDERR" ]; then
|
||||
mkdir -p `dirname $STDERR`
|
||||
fi
|
||||
|
||||
OPEN_FILE_LIMIT=65536
|
||||
|
||||
function pidofproc() {
|
||||
if [ $# -ne 3 ]; then
|
||||
echo "Expected three arguments, e.g. $0 -p pidfile daemon-name"
|
||||
fi
|
||||
|
||||
if [ ! -f "$2" ]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
local pidfile=`cat $2`
|
||||
|
||||
if [ "x$pidfile" == "x" ]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ps --pid "$pidfile" | grep -q $(basename $3); then
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function killproc() {
|
||||
if [ $# -ne 3 ]; then
|
||||
echo "Expected three arguments, e.g. $0 -p pidfile signal"
|
||||
fi
|
||||
|
||||
pid=`cat $2`
|
||||
|
||||
kill -s $3 $pid
|
||||
}
|
||||
|
||||
function log_failure_msg() {
|
||||
echo "$@" "[ FAILED ]"
|
||||
}
|
||||
|
||||
function log_success_msg() {
|
||||
echo "$@" "[ OK ]"
|
||||
}
|
||||
|
||||
# Process name ( For display )
|
||||
name=telegraf
|
||||
|
||||
# Daemon name, where is the actual executable
|
||||
daemon=/usr/bin/telegraf
|
||||
|
||||
# pid file for the daemon
|
||||
pidfile=/var/run/telegraf/telegraf.pid
|
||||
piddir=`dirname $pidfile`
|
||||
|
||||
if [ ! -d "$piddir" ]; then
|
||||
mkdir -p $piddir
|
||||
chown $USER:$GROUP $piddir
|
||||
fi
|
||||
|
||||
# Configuration file
|
||||
config=/etc/telegraf/telegraf.conf
|
||||
confdir=/etc/telegraf/telegraf.d
|
||||
|
||||
# If the daemon is not there, then exit.
|
||||
[ -x $daemon ] || exit 5
|
||||
|
||||
case $1 in
|
||||
start)
|
||||
# Checked the PID file exists and check the actual status of process
|
||||
if [ -e $pidfile ]; then
|
||||
pidofproc -p $pidfile $daemon > /dev/null 2>&1 && status="0" || status="$?"
|
||||
# If the status is SUCCESS then don't need to start again.
|
||||
if [ "x$status" = "x0" ]; then
|
||||
log_failure_msg "$name process is running"
|
||||
exit 0 # Exit
|
||||
fi
|
||||
fi
|
||||
|
||||
# Bump the file limits, before launching the daemon. These will carry over to
|
||||
# launched processes.
|
||||
ulimit -n $OPEN_FILE_LIMIT
|
||||
if [ $? -ne 0 ]; then
|
||||
log_failure_msg "set open file limit to $OPEN_FILE_LIMIT"
|
||||
fi
|
||||
|
||||
log_success_msg "Starting the process" "$name"
|
||||
if command -v startproc >/dev/null; then
|
||||
startproc -u "$USER" -g "$GROUP" -p "$pidfile" -q -- "$daemon" -pidfile "$pidfile" -config "$config" -config-directory "$confdir" $TELEGRAF_OPTS
|
||||
elif which start-stop-daemon > /dev/null 2>&1; then
|
||||
start-stop-daemon --chuid $USER:$GROUP --start --quiet --pidfile $pidfile --exec $daemon -- -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &
|
||||
else
|
||||
su -s /bin/sh -c "nohup $daemon -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &" $USER
|
||||
fi
|
||||
log_success_msg "$name process was started"
|
||||
;;
|
||||
|
||||
stop)
|
||||
# Stop the daemon.
|
||||
if [ -e $pidfile ]; then
|
||||
pidofproc -p $pidfile $daemon > /dev/null 2>&1 && status="0" || status="$?"
|
||||
if [ "$status" = 0 ]; then
|
||||
if killproc -p $pidfile SIGTERM && /bin/rm -rf $pidfile; then
|
||||
log_success_msg "$name process was stopped"
|
||||
else
|
||||
log_failure_msg "$name failed to stop service"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
log_failure_msg "$name process is not running"
|
||||
fi
|
||||
;;
|
||||
|
||||
reload)
|
||||
# Reload the daemon.
|
||||
if [ -e $pidfile ]; then
|
||||
pidofproc -p $pidfile $daemon > /dev/null 2>&1 && status="0" || status="$?"
|
||||
if [ "$status" = 0 ]; then
|
||||
if killproc -p $pidfile SIGHUP; then
|
||||
log_success_msg "$name process was reloaded"
|
||||
else
|
||||
log_failure_msg "$name failed to reload service"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
log_failure_msg "$name process is not running"
|
||||
fi
|
||||
;;
|
||||
|
||||
restart)
|
||||
# Restart the daemon.
|
||||
$0 stop && sleep 2 && $0 start
|
||||
;;
|
||||
|
||||
status)
|
||||
# Check the status of the process.
|
||||
if [ -e $pidfile ]; then
|
||||
if pidofproc -p $pidfile $daemon > /dev/null; then
|
||||
log_success_msg "$name Process is running"
|
||||
exit 0
|
||||
else
|
||||
log_failure_msg "$name Process is not running"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
log_failure_msg "$name Process is not running"
|
||||
exit 3
|
||||
fi
|
||||
;;
|
||||
|
||||
version)
|
||||
$daemon version
|
||||
;;
|
||||
|
||||
*)
|
||||
# For invalid arguments, print the usage message.
|
||||
echo "Usage: $0 {start|stop|restart|status|version}"
|
||||
exit 2
|
||||
;;
|
||||
esac
|
@ -15,6 +15,10 @@ galaxy_info:
|
||||
- name: Debian
|
||||
versions:
|
||||
- all
|
||||
- name: opensuse
|
||||
versions:
|
||||
- all
|
||||
|
||||
galaxy_tags:
|
||||
- monitoring
|
||||
|
||||
|
@ -41,6 +41,13 @@ platforms:
|
||||
- telegraf
|
||||
networks:
|
||||
- name: telegraf
|
||||
- name: telegraf-opensuse
|
||||
image: opensuse/leap
|
||||
privileged: True
|
||||
groups:
|
||||
- telegraf
|
||||
networks:
|
||||
- name: telegraf
|
||||
|
||||
provisioner:
|
||||
name: ansible
|
||||
|
@ -2,14 +2,14 @@
|
||||
|
||||
- hosts: telegraf
|
||||
tasks:
|
||||
- name: "Installing which on CentOS"
|
||||
- name: "Installing packages on CentOS"
|
||||
yum:
|
||||
name: which
|
||||
state: present
|
||||
when:
|
||||
- ansible_os_family == 'RedHat'
|
||||
|
||||
- name: "Installing wget on Debian"
|
||||
- name: "Installing packages on Debian"
|
||||
apt:
|
||||
name:
|
||||
- wget
|
||||
@ -17,3 +17,11 @@
|
||||
state: present
|
||||
when:
|
||||
- ansible_os_family == 'Debian'
|
||||
|
||||
- name: "Installing packages on Suse"
|
||||
zypper:
|
||||
name:
|
||||
- sysvinit-tools
|
||||
state: present
|
||||
when:
|
||||
- ansible_os_family == 'Suse'
|
||||
|
@ -6,10 +6,11 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
|
||||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('telegraf')
|
||||
|
||||
|
||||
def test_telegraf_running_and_enabled(Service):
|
||||
def test_telegraf_running_and_enabled(Service, SystemInfo):
|
||||
telegraf = Service("telegraf")
|
||||
assert telegraf.is_enabled
|
||||
assert telegraf.is_running
|
||||
if SystemInfo.distribution not in ['opensuse-leap']:
|
||||
assert telegraf.is_enabled
|
||||
assert telegraf.is_running
|
||||
|
||||
|
||||
def test_telegraf_dot_conf(File):
|
||||
|
@ -1,17 +1,17 @@
|
||||
---
|
||||
|
||||
- name: "Set name if state == latest"
|
||||
- name: "Debian | Set name if state == latest"
|
||||
set_fact:
|
||||
telegraf_agent_package: telegraf={{ telegraf_agent_version }}-{{ telegraf_agent_version_patch }}
|
||||
when:
|
||||
- telegraf_agent_package_state != "latest"
|
||||
|
||||
- name: Ensure the system can use the HTTPS transport for APT.
|
||||
- name: "Debian | Ensure the system can use the HTTPS transport for APT"
|
||||
stat:
|
||||
path: /usr/lib/apt/methods/https
|
||||
register: apt_https_transport
|
||||
|
||||
- name: Install APT HTTPS transport.
|
||||
- name: "Debian | Install APT HTTPS transport"
|
||||
apt:
|
||||
name: "apt-transport-https"
|
||||
state: present
|
||||
@ -19,14 +19,14 @@
|
||||
- not apt_https_transport.stat.exists
|
||||
become: yes
|
||||
|
||||
- name: Download Telegraf apt key.
|
||||
- name: "Debian | Download Telegraf apt key"
|
||||
apt_key:
|
||||
url: "https://repos.influxdata.com/influxdb.key"
|
||||
id: 2582E0C5
|
||||
state: present
|
||||
become: yes
|
||||
|
||||
- name: Add Telegraf repository (using LSB).
|
||||
- name: "Debian | Add Telegraf repository (using LSB)"
|
||||
apt_repository:
|
||||
repo: "deb https://repos.influxdata.com/{{ ansible_distribution|lower }} {{ ansible_lsb.codename }} stable"
|
||||
filename: "telegraf"
|
||||
@ -36,7 +36,7 @@
|
||||
- ansible_lsb is defined
|
||||
- ansible_lsb.codename is defined
|
||||
|
||||
- name: Add Telegraf repository.
|
||||
- name: "Debian | Add Telegraf repository"
|
||||
apt_repository:
|
||||
repo: "deb https://repos.influxdata.com/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} stable"
|
||||
filename: "telegraf"
|
||||
@ -45,7 +45,7 @@
|
||||
when:
|
||||
- ansible_lsb is not defined or ansible_lsb.codename is not defined
|
||||
|
||||
- name: "Install telegraf package | Debian"
|
||||
- name: "Debian | Install telegraf package"
|
||||
apt:
|
||||
name: "{{ telegraf_agent_package }}"
|
||||
state: "{{ telegraf_agent_package_state }}"
|
||||
|
@ -1,13 +1,13 @@
|
||||
---
|
||||
# description: RedHat specific installation
|
||||
|
||||
- name: "Set name if state == latest"
|
||||
- name: "RedHat | Set name if state == latest"
|
||||
set_fact:
|
||||
telegraf_agent_package: telegraf-{{ telegraf_agent_version }}
|
||||
when:
|
||||
- telegraf_agent_package_state != "latest"
|
||||
|
||||
- name: "Add yum repository | RedHat"
|
||||
- name: "RedHat | Add yum repository"
|
||||
yum_repository:
|
||||
name: influxdb
|
||||
description: InfluxDB Repository - RHEL $releasever
|
||||
@ -15,7 +15,7 @@
|
||||
gpgcheck: yes
|
||||
gpgkey: https://repos.influxdata.com/influxdb.key
|
||||
|
||||
- name: "Install telegraf package | RedHat"
|
||||
- name: "RedHat | Install telegraf package"
|
||||
yum:
|
||||
name: "{{ telegraf_agent_package }}"
|
||||
state: "{{ telegraf_agent_package_state }}"
|
||||
|
51
tasks/Suse.yml
Normal file
51
tasks/Suse.yml
Normal file
@ -0,0 +1,51 @@
|
||||
---
|
||||
|
||||
- name: "Suse | Adding telegraf group"
|
||||
group:
|
||||
name: telegraf
|
||||
state: present
|
||||
|
||||
- name: "Suse | Adding telegraf user"
|
||||
user:
|
||||
name: telegraf
|
||||
group: telegraf
|
||||
state: present
|
||||
become: yes
|
||||
|
||||
- name: "Suse | Install repo dependencies"
|
||||
zypper:
|
||||
name: ["python-libxml2", "python-xml"]
|
||||
update_cache: True
|
||||
state: present
|
||||
become: yes
|
||||
|
||||
- name: "Suse | Install basic repo file"
|
||||
zypper_repository:
|
||||
repo: "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Factory/"
|
||||
name: The Go Programming Language (openSUSE_Factory)
|
||||
state: present
|
||||
auto_import_keys: True
|
||||
become: yes
|
||||
|
||||
- name: "Suse | Install Telegraf"
|
||||
zypper:
|
||||
name: "{{ telegraf_agent_package }}"
|
||||
state: "{{ telegraf_agent_package_state }}"
|
||||
become: yes
|
||||
|
||||
- name: "Suse | Create directories for telegraf"
|
||||
file:
|
||||
path: "{{ item.path }}"
|
||||
owner: "{{ item.owner }}"
|
||||
state: directory
|
||||
with_items:
|
||||
- path: /etc/telegraf/telegraf.d/
|
||||
owner: root
|
||||
- path: /var/log/telegraf
|
||||
owner: telegraf
|
||||
|
||||
- name: "Suse | Install init file"
|
||||
copy:
|
||||
src: telegraf.init
|
||||
dest: /etc/init.d/telegraf
|
||||
mode: 0755
|
@ -102,3 +102,10 @@
|
||||
|
||||
- name: "Force restart service after reread config"
|
||||
meta: flush_handlers
|
||||
|
||||
- name: "Start Telegraf (If it wasn't running)"
|
||||
service:
|
||||
name: telegraf
|
||||
state: started
|
||||
enabled: yes
|
||||
become: yes
|
||||
|
@ -1,16 +1,20 @@
|
||||
---
|
||||
# tasks file for ansible-telegraf
|
||||
|
||||
- name: "Install the correct repository"
|
||||
- name: "Install on RedHat"
|
||||
include_tasks: "RedHat.yml"
|
||||
when: ansible_os_family == "RedHat"
|
||||
|
||||
- name: "Install the correct repository"
|
||||
- name: "Install on Debian"
|
||||
include_tasks: "Debian.yml"
|
||||
when: ansible_os_family == "Debian"
|
||||
tags:
|
||||
- telegraf
|
||||
- packages
|
||||
|
||||
- name: "Install on Suse"
|
||||
include_tasks: "Suse.yml"
|
||||
when: ansible_os_family == "Suse"
|
||||
|
||||
- name: "Configure telegraf"
|
||||
include_tasks: "configure.yml"
|
||||
|
Reference in New Issue
Block a user