diff --git a/files/create-metrics-archive b/files/create-metrics-archive index 7a1c8386..ef62d2a6 100644 --- a/files/create-metrics-archive +++ b/files/create-metrics-archive @@ -1,29 +1,23 @@ #!/bin/bash -set -e - -# Arguments and defaults. - while [[ $1 ]]; do case "$1" in - '-m'|'--metrics-directory') - arg_metrics_dir="$2" + '-d'|'--directory') + metrics_dir="$2" ;; '-r'|'--retention-days') - arg_retention_days="$2" + ret_days="$2" esac shift 2 done -METRICS_OUTPUT_DIR=${arg_metrics_dir:-/opt/puppetlabs/puppet-metrics-collector} -RETENTION_DAYS=${arg_retention_days:-30} - -DATETIME=$(date +%Y.%m.%d.%H.%M.%S) -OUTPUT_FILE="$(pwd)/puppet-metrics-$DATETIME.tar.gz" +# Arguments and defaults. +metrics_dir="${metrics_dir:-/opt/puppetlabs/puppet-metrics-collector}" +ret_days="${ret_days:-30}" +timestamp="$(date +%Y.%m.%d.%H.%M.%S)" +output_file="puppet-metrics-collector-${timestamp}.tar.gz" -cd "$METRICS_OUTPUT_DIR" -find . -type f -ctime -"$RETENTION_DAYS" -regex '\(.*gz$\|.*json$\)' > "$METRICS_OUTPUT_DIR.tmp" -tar --create --gzip --file "$OUTPUT_FILE" --files-from "$METRICS_OUTPUT_DIR.tmp" --transform "s,^,/puppet-metrics-$DATETIME/," -rm "$METRICS_OUTPUT_DIR.tmp" +find "$metrics_dir" -type f -ctime -"$ret_days" -a \( -name "*json" -o -name "*gz" \) | \ + tar czf "$output_file" --transform "s,^${metrics_dir#/},puppet-metrics-${timestamp}," --files-from - || exit 1 -echo "Created metrics archive: ${OUTPUT_FILE}" +echo "Created metrics archive: $output_file" diff --git a/files/metrics_tidy b/files/metrics_tidy index 2ece9432..861a6635 100644 --- a/files/metrics_tidy +++ b/files/metrics_tidy @@ -1,43 +1,34 @@ #!/bin/bash -set -e - -# Arguments and defaults. - -METRICS_DIR=${1:-/opt/puppetlabs/puppet-metrics-collector/puppetserver} -RETENTION_DAYS=${2:-90} - -METRICS_TYPE=$(basename "$METRICS_DIR") - -# Guard against deleting or archiving other files. - -INVALID_PATHS=(/ /etc /var /opt /usr) -METRICS_REALPATH=$(realpath "$METRICS_DIR") - -for INVALID_PATH in "${INVALID_PATHS[@]}" -do - if [ " $METRICS_REALPATH " = " $INVALID_PATH " ]; then - echo "Invalid metrics directory" - exit 1 - fi +while [[ $1 ]]; do + case "$1" in + '-d'|'--directory') + metrics_dir="$2" + ;; + '-r'|'--retention-days') + ret_days="$2" + esac + shift 2 done -if [ " $METRICS_TYPE " = " puppet-metrics-collector " ]; then - echo "Invalid metrics directory" +# Arguments and defaults. +metrics_dir="${metrics_dir:-/opt/puppetlabs/puppet-metrics-collector/puppetserver}" +ret_days="${ret_days:-90}" +# Parameter expansion to strip everything before the last '/', giving us the basename +metrics_type="${metrics_dir##*/}" + +# Guard against deleting or archiving other files. +valid_paths=(puppetserver puppetdb orchestrator ace bolt activemq) +paths_regex="$(IFS='|'; echo "${valid_paths[*]}")" + +# Check that the directory ends in one of the Puppet services we collect metrics for +[[ $metrics_dir =~ ${paths_regex}$ ]] || { + echo "Invalid metrics path. Must end in one of: $(echo -n "${valid_paths[@]}")." exit 1 -fi - -# Delete metrics files older than the retention period, in days. - -find "$METRICS_DIR" -type f -ctime +"$RETENTION_DAYS" -regex '\(.*gz$\|.*json$\)' -delete - -# Archive the remaining metrics files. - -cd "$METRICS_DIR" -find . -type f -name "*.json" > "$METRICS_DIR.tmp" -tar --create --gzip --file "$METRICS_DIR/$METRICS_TYPE-$(date +%Y.%m.%d.%H.%M.%S).tar.gz" --files-from "$METRICS_DIR.tmp" +} -# Delete metrics files that have been archived. +# Delete files older than the retention period, in days. +find "$metrics_dir" -type f -ctime +"$ret_days" -delete -xargs -r -a "$METRICS_DIR.tmp" rm -rm "$METRICS_DIR.tmp" +# Compress the remaining files. +find "$metrics_dir" -type f -name "*json" | tar zcf "${metrics_dir}/${metrics_type}-$(date +%Y.%m.%d.%H.%M.%S).tar.gz" --files-from - diff --git a/files/pe_metrics.rb b/files/pe_metrics.rb index ac588917..f1e8445f 100644 --- a/files/pe_metrics.rb +++ b/files/pe_metrics.rb @@ -47,7 +47,7 @@ def coalesce(higher_precedence, lower_precedence, default = nil) # Metrics endpoints for our Trapper Keeper services do not require a client certificate. if USE_CLIENTCERT - SSLDIR = `/opt/puppetlabs/bin/puppet config print ssldir`.chomp + SSLDIR = `/usr/local/bin/puppet config print ssldir`.chomp end $error_array = [] diff --git a/manifests/pe_metric.pp b/manifests/pe_metric.pp index 43dfe980..eea32348 100644 --- a/manifests/pe_metric.pp +++ b/manifests/pe_metric.pp @@ -93,10 +93,11 @@ # The hardcoded numbers with the fqdn_rand calls are to trigger the metrics_tidy # command to run at a randomly selected time between 12:00 AM and 3:00 AM. + # NOTE - if adding a new service, the name of the service must be added to the valid_paths array in files/metrics_tidy cron { "${metrics_type}_metrics_tidy" : ensure => $metric_ensure, - command => "${puppet_metrics_collector::scripts_dir}/metrics_tidy ${metrics_output_dir} ${retention_days}", + command => "${puppet_metrics_collector::scripts_dir}/metrics_tidy -d ${metrics_output_dir} -r ${retention_days}", user => 'root', hour => fqdn_rand(3, $metrics_type), minute => (5 * fqdn_rand(11, $metrics_type)), diff --git a/manifests/sar_metric.pp b/manifests/sar_metric.pp index 99034733..17a0ac79 100644 --- a/manifests/sar_metric.pp +++ b/manifests/sar_metric.pp @@ -39,10 +39,11 @@ # The hardcoded numbers with the fqdn_rand calls are to trigger the metrics_tidy # command to run at a randomly selected time between 12:00 AM and 3:00 AM. + # NOTE - if adding a new service, the name of the service must be added to the valid_paths array in files/metrics_tidy cron { "${metrics_type}_metrics_tidy" : ensure => $metric_ensure, - command => "${puppet_metrics_collector::scripts_dir}/metrics_tidy ${metrics_output_dir} ${retention_days}", + command => "${puppet_metrics_collector::scripts_dir}/metrics_tidy -d ${metrics_output_dir} -r ${retention_days}", user => 'root', hour => fqdn_rand(3, $metrics_type), minute => (5 * fqdn_rand(11, $metrics_type)),