From 56d1c24df809b9d0df3756c0a2604a5c95fa3c9a Mon Sep 17 00:00:00 2001 From: Sergey Matochkin Date: Fri, 7 Oct 2016 17:56:41 -0400 Subject: [PATCH] BOSH release for Telegraf --- .final_builds/jobs/telegraf-system/index.yml | 7 + .final_builds/packages/telegraf/index.yml | 7 + .gitignore | 18 ++ config/blobs.yml | 5 + config/final.yml | 6 + jobs/telegraf-system/monit | 5 + jobs/telegraf-system/spec | 25 +++ .../templates/bin/pid_utils.sh | 186 ++++++++++++++++++ .../templates/bin/telegraf-system_ctl | 43 ++++ .../templates/config/telegraf.conf.erb | 34 ++++ packages/telegraf/packaging | 9 + packages/telegraf/spec | 4 + releases/c-telegraf/c-telegraf-1.yml | 16 ++ releases/c-telegraf/index.yml | 5 + src/.gitkeep | 0 15 files changed, 370 insertions(+) create mode 100644 .final_builds/jobs/telegraf-system/index.yml create mode 100644 .final_builds/packages/telegraf/index.yml create mode 100644 .gitignore create mode 100644 config/blobs.yml create mode 100644 config/final.yml create mode 100644 jobs/telegraf-system/monit create mode 100644 jobs/telegraf-system/spec create mode 100755 jobs/telegraf-system/templates/bin/pid_utils.sh create mode 100755 jobs/telegraf-system/templates/bin/telegraf-system_ctl create mode 100644 jobs/telegraf-system/templates/config/telegraf.conf.erb create mode 100644 packages/telegraf/packaging create mode 100644 packages/telegraf/spec create mode 100644 releases/c-telegraf/c-telegraf-1.yml create mode 100644 releases/c-telegraf/index.yml create mode 100644 src/.gitkeep diff --git a/.final_builds/jobs/telegraf-system/index.yml b/.final_builds/jobs/telegraf-system/index.yml new file mode 100644 index 0000000..6b04220 --- /dev/null +++ b/.final_builds/jobs/telegraf-system/index.yml @@ -0,0 +1,7 @@ +--- +builds: + 16318fd8b9dea2a69c153199dec6e382c035e233: + version: 16318fd8b9dea2a69c153199dec6e382c035e233 + sha1: a8b114a4f220ad570299318f5cece01ffbebd8c0 + blobstore_id: 26a67da1-70ab-4ad7-92c3-922ed66d0e69 +format-version: '2' diff --git a/.final_builds/packages/telegraf/index.yml b/.final_builds/packages/telegraf/index.yml new file mode 100644 index 0000000..7917513 --- /dev/null +++ b/.final_builds/packages/telegraf/index.yml @@ -0,0 +1,7 @@ +--- +builds: + 11d238e06a154b9a8539712c54432ffc968bf7c5: + version: 11d238e06a154b9a8539712c54432ffc968bf7c5 + sha1: a63307c67af09d5a936c8f0180521727ad3ffa14 + blobstore_id: ace8c27e-4c19-4f0e-80c8-58787ec9225f +format-version: '2' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..beac6f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +config/dev.yml +config/private.yml +config/settings.yml +releases/*.tgz +releases/**/*.tgz +dev_releases +.blobs +blobs +.dev_builds +.idea +.DS_Store +.final_builds/jobs/**/*.tgz +.final_builds/packages/**/*.tgz +*.swp +*~ + +/deployments +.torus.json diff --git a/config/blobs.yml b/config/blobs.yml new file mode 100644 index 0000000..5e89940 --- /dev/null +++ b/config/blobs.yml @@ -0,0 +1,5 @@ +--- +telegraf/telegraf-1.2.1_linux_amd64.tar.gz: + object_id: e61a55cf-53f9-46d3-9a02-279e82f424e1 + sha: 5c61cabe4ac5c5fde972262194dd26e561cd8579 + size: 9635266 diff --git a/config/final.yml b/config/final.yml new file mode 100644 index 0000000..fba8fdd --- /dev/null +++ b/config/final.yml @@ -0,0 +1,6 @@ +--- +blobstore: + provider: s3 + options: + bucket_name: bosh-release-telegraf +final_name: c-telegraf diff --git a/jobs/telegraf-system/monit b/jobs/telegraf-system/monit new file mode 100644 index 0000000..1ae98bb --- /dev/null +++ b/jobs/telegraf-system/monit @@ -0,0 +1,5 @@ +check process telegraf-system + with pidfile /var/vcap/sys/run/telegraf-system/telegraf-system.pid + start program "/var/vcap/jobs/telegraf-system/bin/telegraf-system_ctl start" + stop program "/var/vcap/jobs/telegraf-system/bin/telegraf-system_ctl stop" + group vcap diff --git a/jobs/telegraf-system/spec b/jobs/telegraf-system/spec new file mode 100644 index 0000000..c601714 --- /dev/null +++ b/jobs/telegraf-system/spec @@ -0,0 +1,25 @@ +--- +name: telegraf-system + +templates: + bin/telegraf-system_ctl: bin/telegraf-system_ctl + bin/pid_utils.sh: bin/pid_utils.sh + config/telegraf.conf.erb: config/telegraf.conf + +packages: +- telegraf + +properties: + telegraf.tags: + description: Telegraf global tags as a dictionary + default: {} + + telegraf.influxdb.url: + description: InfluxDB url + default: http://localhost:8086 + telegraf.influxdb.database: + description: InfluxDB database + default: system + telegraf.influxdb.retention_policy: + description: InfluxDB database retention policy + default: autogen diff --git a/jobs/telegraf-system/templates/bin/pid_utils.sh b/jobs/telegraf-system/templates/bin/pid_utils.sh new file mode 100755 index 0000000..3e69503 --- /dev/null +++ b/jobs/telegraf-system/templates/bin/pid_utils.sh @@ -0,0 +1,186 @@ +SCRIPT=$(basename $0) +mkdir -p /var/vcap/sys/log/monit + +exec 1>> /var/vcap/sys/log/monit/$SCRIPT.log +exec 2>> /var/vcap/sys/log/monit/$SCRIPT.err.log + +echo "------------ `basename $0` $* at `date` --------------" | tee /dev/stderr + +function pid_is_running() { + declare pid="$1" + ps -p "${pid}" >/dev/null 2>&1 +} + +# pid_guard +# +# @param pidfile +# @param name [String] an arbitrary name that might show up in STDOUT on errors +# +# Run this before attempting to start new processes that may use the same :pidfile:. +# If an old process is running on the pid found in the :pidfile:, exit 1. Otherwise, +# remove the stale :pidfile: if it exists. +# +function pid_guard() { + declare pidfile="$1" name="$2" + + echo "------------ STARTING $(basename "$0") at $(date) --------------" | tee /dev/stderr + + if [ ! -f "${pidfile}" ]; then + return 0 + fi + + local pid + pid=$(head -1 "${pidfile}") + + if pid_is_running "${pid}"; then + echo "${name} is already running, please stop it first" + exit 1 + fi + + echo "Removing stale pidfile" + rm "${pidfile}" +} + +# wait_pid_death +# +# @param pid +# @param timeout +# +# Watch a :pid: for :timeout: seconds, waiting for it to die. +# If it dies before :timeout:, exit 0. If not, exit 1. +# +# Note that this should be run in a subshell, so that the current +# shell does not exit. +# +function wait_pid_death() { + declare pid="$1" timeout="$2" + + local countdown + countdown=$(( timeout * 10 )) + + while true; do + if ! pid_is_running "${pid}"; then + return 0 + fi + + if [ ${countdown} -le 0 ]; then + return 1 + fi + + countdown=$(( countdown - 1 )) + sleep 0.1 + done +} + +# kill_and_wait +# +# @param pidfile +# @param timeout [default 25s] +# +# For a pid found in :pidfile:, send a `kill -6`, then wait for :timeout: seconds to +# see if it dies on its own. If not, send it a `kill -9`. If the process does die, +# exit 0 and remove the :pidfile:. If after all of this, the process does not actually +# die, exit 1. +# +# Note: +# Monit default timeout for start/stop is 30s +# Append 'with timeout {n} seconds' to monit start/stop program configs +# +function kill_and_wait() { + declare pidfile="$1" timeout="${2:-25}" sigkill_on_timeout="${3:-1}" + + if [ ! -f "${pidfile}" ]; then + echo "Pidfile ${pidfile} doesn't exist" + exit 0 + fi + + local pid + pid=$(head -1 "${pidfile}") + + if [ -z "${pid}" ]; then + echo "Unable to get pid from ${pidfile}" + exit 1 + fi + + if ! pid_is_running "${pid}"; then + echo "Process ${pid} is not running" + rm -f "${pidfile}" + exit 0 + fi + + echo "Killing ${pidfile}: ${pid} " + kill "${pid}" + + if ! wait_pid_death "${pid}" "${timeout}"; then + if [ "${sigkill_on_timeout}" = "1" ]; then + echo "Kill timed out, using kill -9 on ${pid}" + kill -9 "${pid}" + sleep 0.5 + fi + fi + + if pid_is_running "${pid}"; then + echo "Timed Out" + exit 1 + else + echo "Stopped" + rm -f "${pidfile}" + fi +} + +check_mount() { + opts=$1 + exports=$2 + mount_point=$3 + + if grep -qs $mount_point /proc/mounts; then + echo "Found NFS mount $mount_point" + else + echo "Mounting NFS..." + mount $opts $exports $mount_point + if [ $? != 0 ]; then + echo "Cannot mount NFS from $exports to $mount_point, exiting..." + exit 1 + fi + fi +} + +# Check the syntax of a sudoers file. +check_sudoers() { + /usr/sbin/visudo -c -f "$1" +} + +# Check the syntax of a sudoers file and if it's ok install it. +install_sudoers() { + src="$1" + dest="$2" + + check_sudoers "$src" + + if [ $? -eq 0 ]; then + chown root:root "$src" + chmod 0440 "$src" + cp -p "$src" "$dest" + else + echo "Syntax error in sudoers file $src" + exit 1 + fi +} + +# Add a line to a file if it is not already there. +file_must_include() { + file="$1" + line="$2" + + # Protect against empty $file so it doesn't wait for input on stdin. + if [ -n "$file" ]; then + grep --quiet "$line" "$file" || echo "$line" >> "$file" + else + echo 'File name is required' + exit 1 + fi +} + +running_in_container() { + grep -q -E '/instance|/docker/' /proc/self/cgroup +} diff --git a/jobs/telegraf-system/templates/bin/telegraf-system_ctl b/jobs/telegraf-system/templates/bin/telegraf-system_ctl new file mode 100755 index 0000000..5984265 --- /dev/null +++ b/jobs/telegraf-system/templates/bin/telegraf-system_ctl @@ -0,0 +1,43 @@ +#!/bin/bash -eu + +JOB_NAME=telegraf-system + +JOB_DIR=/var/vcap/jobs/$JOB_NAME +RUN_DIR=/var/vcap/sys/run/$JOB_NAME +LOG_DIR=/var/vcap/sys/log/$JOB_NAME +PIDFILE=$RUN_DIR/$JOB_NAME.pid + +source $JOB_DIR/bin/pid_utils.sh + +case $1 in + start) + pid_guard $PIDFILE $JOB_NAME + + mkdir -p $RUN_DIR + mkdir -p $LOG_DIR + + PATH=/var/vcap/packages/telegraf/bin:$PATH + + # check configuration + echo "checking telegraf configuration..." + telegraf --config $JOB_DIR/config/telegraf.conf --test > /dev/null + echo "telegraf configuration OK" + echo + + # store pid in $PIDFILE + echo $$ > $PIDFILE + + exec telegraf \ + --config $JOB_DIR/config/telegraf.conf \ + >>$LOG_DIR/$JOB_NAME.log 2>&1 + + ;; + + stop) + kill_and_wait $PIDFILE + ;; + + *) + echo "Usage: ctl {start|stop}" + ;; +esac diff --git a/jobs/telegraf-system/templates/config/telegraf.conf.erb b/jobs/telegraf-system/templates/config/telegraf.conf.erb new file mode 100644 index 0000000..4b26aed --- /dev/null +++ b/jobs/telegraf-system/templates/config/telegraf.conf.erb @@ -0,0 +1,34 @@ +[global_tags] + service = "<%= spec.deployment.sub(/-[0-9a-f]{20}$/, '') %>" + node_type = "<%= spec.name %>" + instance = "<%= spec.index %>" + <% if_p("telegraf.tags") do |tags| tags.each do |tag, value| %> + <%= tag %> = "<%= value %>" + <% end; end %> + +[agent] + interval = "1m" + flush_interval = "1m" + flush_jitter = "15s" + +[[outputs.influxdb]] + urls = ["<%= p("telegraf.influxdb.url") %>"] + database = "<%= p("telegraf.influxdb.database") %>" + retention_policy = "<%= p("telegraf.influxdb.retention_policy") %>" + +[[inputs.cpu]] + percpu = false + totalcpu = true +[[inputs.disk]] + ignore_fs = ["tmpfs", "devtmpfs", "none"] +[[inputs.diskio]] + devices = ["sda", "sdb", "sdc"] +[[inputs.kernel]] +[[inputs.mem]] +[[inputs.processes]] +[[inputs.swap]] +[[inputs.system]] +[[inputs.net]] +[[inputs.netstat]] +[[inputs.nstat]] + dump_zeros = true diff --git a/packages/telegraf/packaging b/packages/telegraf/packaging new file mode 100644 index 0000000..3efc0c0 --- /dev/null +++ b/packages/telegraf/packaging @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e +set -u + +tar xfz telegraf/telegraf-*.tar.gz \ + --strip-components 3 \ + --wildcards \ + -C $BOSH_INSTALL_TARGET './*/usr/bin/telegraf' diff --git a/packages/telegraf/spec b/packages/telegraf/spec new file mode 100644 index 0000000..2932307 --- /dev/null +++ b/packages/telegraf/spec @@ -0,0 +1,4 @@ +--- +name: telegraf +files: +- telegraf/* diff --git a/releases/c-telegraf/c-telegraf-1.yml b/releases/c-telegraf/c-telegraf-1.yml new file mode 100644 index 0000000..05fae47 --- /dev/null +++ b/releases/c-telegraf/c-telegraf-1.yml @@ -0,0 +1,16 @@ +--- +packages: +- name: telegraf + version: 11d238e06a154b9a8539712c54432ffc968bf7c5 + fingerprint: 11d238e06a154b9a8539712c54432ffc968bf7c5 + sha1: a63307c67af09d5a936c8f0180521727ad3ffa14 + dependencies: [] +jobs: +- name: telegraf-system + version: 16318fd8b9dea2a69c153199dec6e382c035e233 + fingerprint: 16318fd8b9dea2a69c153199dec6e382c035e233 + sha1: a8b114a4f220ad570299318f5cece01ffbebd8c0 +commit_hash: 80f31f96 +uncommitted_changes: true +name: c-telegraf +version: '1' diff --git a/releases/c-telegraf/index.yml b/releases/c-telegraf/index.yml new file mode 100644 index 0000000..92b4242 --- /dev/null +++ b/releases/c-telegraf/index.yml @@ -0,0 +1,5 @@ +--- +builds: + a8b815c2-9a70-4860-b921-1a07538305cc: + version: '1' +format-version: '2' diff --git a/src/.gitkeep b/src/.gitkeep new file mode 100644 index 0000000..e69de29