API script to count mobile devices by Site...

ega
Contributor II

Before I run off and reinvent the wheel does anyone have a script that will use the Jamf Pro API to count the mobile devices and report totals by Site?

2 REPLIES 2

LRZ_Jamf
Contributor

You probably want to do this by Database query as this is way more performant.

SELECT s.site_name,
       (SELECT count(*) FROM site_objects macs WHERE macs.site_id = s.site_id AND macs.object_type = 1) AS MacOSDevices,
       (SELECT count(*) FROM site_objects ios WHERE ios.site_id = s.site_id AND ios.object_type = 21) AS iOSDevices
FROM sites s

chadlawson
Contributor
Contributor

If you're like me and you use a Jamfcloud server, and you don't have database query access, the API is the only way to go. I through the following together just now but it's a total hack job. It doesn't do any error checking and the output is just a row of "xx SITE" where "xx" is the number of devices in the site and "SITE" is, well, you guessed it.

#!/bin/bash

jssURL="SOMETHING.jamfcloud.com"
userPass='USER:PASS'

deviceList=$(curl -su ${userPass} -H "Accept: text/xml" https://${jssURL}/JSSResource/mobiledevices)
for u in $(echo ${deviceList} | xmllint --format - | grep '<id>' | awk -F> '{print $2}' | awk -F< '{print $1}'); do
    curl -su ${userPass} -H "Accept: text/xml" https://${jssURL}/JSSResource/mobiledevices/id/${u} | xmllint --xpath '/mobile_device/general/site/name/text()' -
    echo ""
done | sort | uniq -c

There are probably a dozen ways to do this better and/or more efficiently. As I said, this is just what I threw together to see if I could do it. Please don't trust code from random weirdos on the internet. πŸ™‚

Chad