Files
claudetools/projects/gps-rmm-audit/pull_backup.sh
Howard Enos d5f7eadf82 sync: auto-sync from HOWARD-HOME at 2026-07-06 08:29:31
Author: Howard Enos
Machine: HOWARD-HOME
Timestamp: 2026-07-06 08:29:31
2026-07-06 08:30:01 -07:00

74 lines
2.5 KiB
Bash

#!/usr/bin/env bash
set -u
BASE="https://computerguru.syncromsp.com/api/v1"
KEY="Tde5174a6e9e312d14-02fd5bfe0f0ee40c87d027507c680e18"
CLIENTS="C:/claudetools/projects/gps-rmm-audit/gps-clients.json"
# iterate customers
n=$(jq 'length' "$CLIENTS")
echo "["
first=1
for i in $(seq 0 $((n-1))); do
client=$(jq -r ".[$i].client" "$CLIENTS")
cid=$(jq -r ".[$i].cid" "$CLIENTS")
# fetch schedules list
slist=$(curl -s "$BASE/schedules?customer_id=$cid&api_key=$KEY" | tr -d '\000-\037')
sids=$(echo "$slist" | jq -r '.schedules[]?.id' 2>/dev/null)
seats=0
monthly=0
lines_json="[]"
sids_json="[]"
freq="null"
note="ok"
if [ -z "$sids" ]; then
note="no recurring schedule"
seats=0
else
# collect sids into json array
sids_json=$(echo "$sids" | jq -R . | jq -s -c 'map(tonumber)')
matched_lines="[]"
any_backup=0
for sid in $sids; do
det=$(curl -s "$BASE/schedules/$sid?api_key=$KEY" | tr -d '\000-\037')
# frequency (take first non-null)
f=$(echo "$det" | jq -r '.schedule.frequency // empty' 2>/dev/null)
if [ -n "$f" ] && [ "$freq" = "null" ]; then freq="$f"; fi
# backup lines: name or description contains backup (case-insensitive)
bl=$(echo "$det" | jq -c '[.schedule.lines[]? | select((.name // "") | ascii_downcase | test("backup"))]' 2>/dev/null)
if [ -z "$bl" ]; then bl="[]"; fi
cnt=$(echo "$bl" | jq 'length')
if [ "$cnt" -gt 0 ]; then
any_backup=1
# sum quantity (quantity is a string like "18.0")
q=$(echo "$bl" | jq '[.[].quantity // 0 | tonumber] | add')
# sum quantity*price_retail
m=$(echo "$bl" | jq '[.[] | ((.quantity // 0 | tonumber) * (.price_retail // 0 | tonumber))] | add')
seats=$(awk "BEGIN{printf \"%g\", $seats + $q}")
monthly=$(awk "BEGIN{printf \"%g\", $monthly + $m}")
# append names
nm=$(echo "$bl" | jq -c '[.[].name]')
matched_lines=$(echo "$matched_lines $nm" | jq -s -c 'add')
fi
done
lines_json="$matched_lines"
if [ "$any_backup" -eq 0 ]; then
note="no backup line"
fi
fi
# normalize monthly number
if [ "$monthly" = "0" ]; then monthly="0"; fi
if [ "$freq" != "null" ]; then freq="\"$freq\""; fi
# emit
if [ $first -eq 0 ]; then echo ","; fi
first=0
printf '{"client":%s,"cid":%s,"backup_seats":%s,"backup_monthly":%s,"backup_lines":%s,"schedule_ids":%s,"frequency":%s,"note":%s}' \
"$(echo "$client" | jq -R .)" "$cid" "$seats" "$monthly" "$lines_json" "$sids_json" "$freq" "$(echo "$note" | jq -R .)"
done
echo ""
echo "]"