-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdatecheck_Json_Reporting
More file actions
114 lines (99 loc) · 4.28 KB
/
Updatecheck_Json_Reporting
File metadata and controls
114 lines (99 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
#A tool to help admins quickly see the update statuses fleet wide
jsonFilePathTemp="$HOME/Downloads/AllUpdates.json"
jsonFilePath="$HOME/Downloads/AllUpdatesFinal.json"
jsonScratchpad="$HOME/Downloads/scratchpad.json"
csvFilePath="$HOME/Downloads/CSVData.csv"
#############################################################################
#API Creds
username="username"
password="password"
url="https://yourserver.jamfcloud.com"
#############################################################################
#Bearer Token Auth
#Variable declarations
bearerToken=""
tokenExpirationEpoch="0"
getBearerToken() {
response=$(curl -s -u "$username":"$password" "$url"/api/v1/auth/token -X POST)
bearerToken=$(echo "$response" | plutil -extract token raw -)
tokenExpiration=$(echo "$response" | plutil -extract expires raw - | awk -F . '{print $1}')
tokenExpirationEpoch=$(date -j -f "%Y-%m-%dT%T" "$tokenExpiration" +"%s")
}
checkTokenExpiration() {
nowEpochUTC=$(date -j -f "%Y-%m-%dT%T" "$(date -u +"%Y-%m-%dT%T")" +"%s")
if [[ tokenExpirationEpoch -gt nowEpochUTC ]]
then
echo "Token valid until the following epoch time: " "$tokenExpirationEpoch"
else
echo "No valid token available, getting new token"
getBearerToken
fi
}
invalidateToken() {
responseCode=$(curl -w "%{http_code}" -H "Authorization: Bearer ${bearerToken}" $url/api/v1/auth/invalidate-token -X POST -s -o /dev/null)
if [[ ${responseCode} == 204 ]]
then
echo "Token successfully invalidated"
bearerToken=""
tokenExpirationEpoch="0"
elif [[ ${responseCode} == 401 ]]
then
echo "Token already invalid"
else
echo "An unknown error occurred invalidating the token"
fi
}
#############################################################################
#Test Bearer Token Auth (uncomment to use)
#checkTokenExpiration
#curl -s -H "Authorization: Bearer ${bearerToken}" $url/api/v1/jamf-pro-version -X GET
#checkTokenExpiration
#invalidateToken
#curl -s -H "Authorization: Bearer ${bearerToken}" $url/api/v1/jamf-pro-version -X GET
#############################################################################
#Get Bearer Token
getBearerToken
#############################################################################
pageSize="100"
page="0"
#Pull all devices plans from newest to oldest
initialUpdatePlanCheck=$(curl -X 'GET' \
"$url/api/v1/managed-software-updates/plans?page=$page&page-size=$pageSize&sort=planUuid%3Adesc" \
-H 'accept: application/json' \
-H "Authorization: Bearer $bearerToken")
#echo $initialUpdatePlanCheck
echo $initialUpdatePlanCheck > $jsonFilePath
#JQ Filter Function on output json from API calls.
jq -s '.[].results[]' "$jsonFilePath" > $jsonFilePathTemp && mv $jsonFilePathTemp $jsonFilePath
getCountOfResults=$(jq -r -c '.[]' <<< "$initialUpdatePlanCheck" | awk '{print $1}')
filterGetCountOfResults=$(awk -F, 'NR==1{print $1}' <<<"$getCountOfResults")
#Do some math to check if we need to keep going up from page size
#if pagesize/filtergetcountofresults le=1 then +1 page
#WARNING 'expr' will round to nearest whole number
realPageNumber=$(expr $page + 1)
#echo $realPageNumber
totalPossibleResults=$(expr $pageSize \* $realPageNumber)
#echo $totalPossibleResults
#Make a for loop that goes through multiple pages if found and get next page if needed
while [ $totalPossibleResults -lt $filterGetCountOfResults ]; do
echo "more update results pending, fetching now"
((page++))
realPageNumber=$(expr $page + 1)
echo "current real page number is $realPageNumber"
totalPossibleResults=$(expr $pageSize \* $realPageNumber)
echo "total possible results are $filterGetCountOfResults"
checkTokenExpiration
UpdatePlanCheck=$(curl -X 'GET' \
"$url/api/v1/managed-software-updates/plans?page=$page&page-size=$pageSize&sort=planUuid%3Adesc" \
-H 'accept: application/json' \
-H "Authorization: Bearer $bearerToken")
echo $UpdatePlanCheck > $jsonFilePathTemp
#echo -e "\n" >> $jsonFilePath
#JQ Filter Function on output json from API calls.
jq -s '.[].results[]' "$jsonFilePathTemp" >> $jsonFilePath
done
jq -s 'group_by(.device[]) | map({DeviceId: .[0].device.deviceId, Status: map(.status.state) |
unique, Device_Type: map(.device.objectType) |
unique, Version_Type: map(.versionType) |
unique, Error_Reasons: map(.status.errorReasons[])})' $jsonFilePath > $jsonFilePathTemp && mv $jsonFilePathTemp $jsonFilePath