|
1 | 1 | #!/bin/bash |
| 2 | +set -o pipefail |
2 | 3 | shopt -s nullglob |
3 | 4 |
|
4 | | -for f in /opt/mft-automations/puppet_enterprise_support*gz /opt/mft-automations/puppet_enterprise_support*gz.gpg /opt/mft-automations/puppet_enterprise_support*tar /opt/mft-automations/puppet_enterprise_support*tar.gz; do |
5 | | - # Does the file have a 5 digit ticket number after puppet_enterprise_support_ |
6 | | - has_ticket=$(echo "$f" | grep -Eo -- 'puppet_enterprise_support_[[:digit:]]+_') |
7 | | - |
8 | | - if [[ $(find "$f" -mmin -2 2>/dev/null) ]]; then |
9 | | - echo "INFO: $f is still downloading (mtime < 2 minutes). Skipping..." |
10 | | - continue |
11 | | - fi |
12 | | - |
13 | | - if ! [[ $has_ticket ]]; then |
14 | | - echo "ERROR: no ticket ID found in $f" |
15 | | - mv "$f" /opt/mft-automations/err |
16 | | - continue |
17 | | - fi |
18 | | - |
19 | | - if [[ "${f##*.}" == 'gpg' ]]; then |
20 | | - # Decrypt the file to the same location as the source, stripping the .gpg suffix |
21 | | - # Delete source file if it decrypts ok, otherwise move it to err/ |
22 | | - if cat /root/.support_gpg | gpg --pinentry-mode loopback --passphrase-fd 0 --batch --yes --output "${f%.*}" --decrypt "$f"; then |
23 | | - rm -- "$f" |
24 | | - f="${f%.*}" |
25 | | - else |
26 | | - echo "ERROR: failed to decrypt $f" |
27 | | - mv "$f" /opt/mft-automations/err |
28 | | - continue |
29 | | - fi |
30 | | - fi |
31 | | - |
32 | | - if ! tar tf "$f" | grep -q -m 1 'metrics\/.*json'; then |
33 | | - echo "No metrics found in $f. Skipping" |
34 | | - rm -- "$f" |
35 | | - continue |
36 | | - fi |
| 5 | +# --------------------------- |
| 6 | +# Config |
| 7 | +# --------------------------- |
| 8 | +SEND_SLACK=true # set to false to disable Slack posting |
| 9 | +SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-}" # must come from environment when SEND_SLACK=true |
| 10 | + |
| 11 | +# --------------------------- |
| 12 | +# Helpers |
| 13 | +# --------------------------- |
| 14 | +slack_post() { |
| 15 | + # Usage: slack_post "message" |
| 16 | + local msg="$1" |
| 17 | + |
| 18 | + # Only post if enabled and webhook present |
| 19 | + [[ "$SEND_SLACK" == "true" ]] || return 0 |
| 20 | + [[ -n "$SLACK_WEBHOOK_URL" ]] || return 0 |
| 21 | + |
| 22 | + # Minimal JSON escaping for double-quotes, plus newlines -> \n |
| 23 | + local esc="${msg//\\/\\\\}" |
| 24 | + esc="${esc//\"/\\\"}" |
| 25 | + esc="${esc//$'\n'/\\n}" |
| 26 | + |
| 27 | + curl -sS -X POST -H 'Content-type: application/json' \ |
| 28 | + --data "{\"text\":\"$esc\"}" \ |
| 29 | + "$SLACK_WEBHOOK_URL" >/dev/null 2>&1 || true |
| 30 | +} |
37 | 31 |
|
38 | | - # Strip the trailing _, then everything up to the last _ to get just the number |
39 | | - ticket="${has_ticket%_}" |
40 | | - ticket="${ticket##*_}" |
| 32 | +log() { |
| 33 | + # Usage: log "message" |
| 34 | + local msg="$1" |
| 35 | + echo "$msg" |
| 36 | + slack_post "$msg" |
| 37 | +} |
41 | 38 |
|
42 | | - _tmp="$(mktemp)" |
43 | | - /opt/puppetlabs/bolt/bin/bolt plan run puppet_operational_dashboards::load_metrics --targets localhost --run-as root influxdb_bucket="$ticket" support_script_file="$f" |& tee "$_tmp" |
| 39 | +# --------------------------- |
| 40 | +# Main |
| 41 | +# --------------------------- |
| 42 | +for f in /opt/mft-automations/puppet_enterprise_support*gz \ |
| 43 | + /opt/mft-automations/puppet_enterprise_support*gz.gpg \ |
| 44 | + /opt/mft-automations/puppet_enterprise_support*tar \ |
| 45 | + /opt/mft-automations/puppet_enterprise_support*tar.gz; do |
44 | 46 |
|
45 | | - if ! grep -q 'Wrote batch of [[:digit:]]* metrics' "$_tmp"; then |
46 | | - echo "ERROR: failed to import metrics from $f" |
| 47 | + # Does the file have a 5 digit ticket number after puppet_enterprise_support_ |
| 48 | + has_ticket=$(echo "$f" | grep -Eo -- 'puppet_enterprise_support_[[:digit:]]+_') |
| 49 | + |
| 50 | + if [[ $(find "$f" -mmin -2 2>/dev/null) ]]; then |
| 51 | + log "INFO: $f is still downloading (mtime < 2 minutes). Skipping..." |
| 52 | + continue |
| 53 | + fi |
| 54 | + |
| 55 | + if ! [[ $has_ticket ]]; then |
| 56 | + log "ERROR: no ticket ID found in $f" |
| 57 | + mv "$f" /opt/mft-automations/err |
| 58 | + continue |
| 59 | + fi |
| 60 | + |
| 61 | + if [[ "${f##*.}" == 'gpg' ]]; then |
| 62 | + # Decrypt the file to the same location as the source, stripping the .gpg suffix |
| 63 | + # Delete source file if it decrypts ok, otherwise move it to err/ |
| 64 | + if cat /root/.support_gpg | gpg --pinentry-mode loopback --passphrase-fd 0 --batch --yes --output "${f%.*}" --decrypt "$f"; then |
| 65 | + rm -- "$f" |
| 66 | + f="${f%.*}" |
| 67 | + else |
| 68 | + log "ERROR: failed to decrypt $f" |
47 | 69 | mv "$f" /opt/mft-automations/err |
48 | 70 | continue |
49 | | - fi |
| 71 | + fi |
| 72 | + fi |
| 73 | + |
| 74 | + if ! tar tf "$f" | grep -q -m 1 'metrics\/.*json'; then |
| 75 | + log "No metrics found in $f. Skipping" |
| 76 | + rm -- "$f" |
| 77 | + continue |
| 78 | + fi |
| 79 | + |
| 80 | + # Strip the trailing _, then everything up to the last _ to get just the number |
| 81 | + ticket="${has_ticket%_}" |
| 82 | + ticket="${ticket##*_}" |
| 83 | + |
| 84 | + _tmp="$(mktemp)" |
| 85 | + /opt/puppetlabs/bolt/bin/bolt plan run puppet_operational_dashboards::load_metrics \ |
| 86 | + --targets localhost --run-as root influxdb_bucket="$ticket" support_script_file="$f" \ |
| 87 | + |& tee "$_tmp" |
| 88 | + |
| 89 | + if ! grep -q 'Wrote batch of [[:digit:]]* metrics' "$_tmp"; then |
| 90 | + log "ERROR: failed to import metrics from $f" |
| 91 | + mv "$f" /opt/mft-automations/err |
| 92 | + continue |
| 93 | + fi |
50 | 94 |
|
51 | | - rm -- "$_tmp" |
52 | | - rm -- "$f" |
| 95 | + rm -- "$_tmp" |
| 96 | + rm -- "$f" |
53 | 97 | done |
54 | 98 |
|
55 | 99 | # Delete sup scripts older than 30 days |
|
0 commit comments