forked from puppetlabs/puppetlabs-puppet_metrics_collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request puppetlabs#1 from m0dular/aph/shell-work
This commit changes the create and tidy scripts to:
- Loading branch information
Showing
5 changed files
with
43 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 - |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters