cat /usr/lib64/nagios/plugins/check_locations.sh

#!/bin/bash
# use home folder as working folder
cd ~

# auth against the unifi controller and save the cookie
curl -s -o /dev/null -k -X POST --data '{"username": "api", "password": "actualuserpasswordherebecauseunifidoesntdobearertokens"}' --header 'Content-Type: application/json' --cookie-jar cookie.txt https://unifi.example.com:8443/api/login

# Save a list of AP names and MAC addresses to disk
curl -k -X GET --silent --cookie cookie.txt https://unifi.example.com:8443/api/site/default/stat/device-basic | jq --raw-output '.data[] | {name,mac}' > device-basic.txt

# Query Unifi controller for locations, and generate a bunch of images to display in LibreNMS dashboard
# Employee1
curl -k -X GET --silent --cookie cookie.txt https://unifi.example.com:8443/api/site/default/stat/sta/2C:54:91:88:C9:E3 | jq --raw-output '.data[] | .ap_mac' > ap.txt
ap=$(cat ap.txt)
location=$(jq --arg ap "$ap" --raw-output 'select(.mac == $ap) | .name' device-basic.txt)
if [[ -z "$location" ]]; then
   location="Offline" # need to check whether location is empty, because if the laptop is not connected to any AP the var will be blank and convert command will fail
fi
location=$(echo $location | sed 's/ //g') # trim whitespace because label: is finicky and I can't figure out how to to :label to accept spaces
convert -size 550x275 -background none -fill white -gravity center -font Courier-Bold label:$location /opt/librenms/html/images/location_employee1.png

# Employee2
curl -k -X GET --silent --cookie cookie.txt https://unifi.example.com:8443/api/site/default/stat/sta/2C:54:91:88:C9:E4 | jq --raw-output '.data[] | .ap_mac' > ap.txt
ap=$(cat ap.txt)
location=$(jq --arg ap "$ap" --raw-output 'select(.mac == $ap) | .name' device-basic.txt)
if [[ -z "$location" ]]; then
   location="Offline" # need to check whether location is empty, because if the laptop is not connected to any AP the var will be blank and convert command will fail
fi
location=$(echo $location | sed 's/ //g') # trim whitespace because label: is finicky and I can't figure out how to to :label to accept spaces
convert -size 550x275 -background none -fill white -gravity center -font Courier-Bold label:$location /opt/librenms/html/images/location_employee2.png

exit 0