Skip to content

Commit

Permalink
Merge pull request puppetlabs#1 from m0dular/aph/shell-work
Browse files Browse the repository at this point in the history
This commit changes the create and tidy scripts to:
  • Loading branch information
Thomas Kishel authored Feb 18, 2020
2 parents 1774718 + 06ea81f commit 8e3bfa9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 56 deletions.
28 changes: 11 additions & 17 deletions files/create-metrics-archive
Original file line number Diff line number Diff line change
@@ -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"
63 changes: 27 additions & 36 deletions files/metrics_tidy
Original file line number Diff line number Diff line change
@@ -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 -
2 changes: 1 addition & 1 deletion files/pe_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
3 changes: 2 additions & 1 deletion manifests/pe_metric.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion manifests/sar_metric.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit 8e3bfa9

Please sign in to comment.