Home Brew and Collect Application Usage Information

Craig_Whittaker
New Contributor III

Hello all

Apologies if this has already been asked and I have missed it, but has anyone got away of collecting the applications installed by home brew?

11 REPLIES 11

StoneMagnet
Contributor III

@Craig.Whittaker Does the command brew list return the info you're looking for? If so, you can write an Extension Attribute to collect that.

Craig_Whittaker
New Contributor III

@StoneMagnet hey it does, but wanted to set up a smart group to filter on home brew installed machines only having this ran on them :/

StoneMagnet
Contributor III

@Craig.Whittaker EAs run on all machines, and aren't scoped. You could have the EA check for the existence of the file /usr/local/bin/brew to determine if brew is installed on the machine, and either return Not Installed or the list of installed brew tools.

Craig_Whittaker
New Contributor III

thanks @StoneMagnet

don't happen to have an example EA doing even something similar I can pick apart?

StoneMagnet
Contributor III

@Craig.Whittaker This is untested, but should do what you want:

#!/bin/bash
brewtools="Not Installed"
if [ -e /usr/local/bin/brew ]
    then
        brewtools=$(/usr/local/bin/brew list)
fi

echo "<result>$brewtools</result>"

exit 0

Craig_Whittaker
New Contributor III

@StoneMagnet great will give it a go

thanks

exno
Contributor

So i found this while looking for other information, About a year later from original post.. so i am turning this into a zombie post i guess..

jamf runs alot of what it does from an elevated state. Brew errors out if run as root. so the above may not work for people. and since I had to make an EA and there was a post needing an EA. i figured i would share in the serendipitous nature of finding this

This only pulls brew installs for logged in users, which means the scope can change based on inventory checkin, and active user. but if you are primarily on a One to One deployment, that shouldn't be an issue.

If you need to get brew installs for all users you could possibly loop through the results of dscl . list or something. but the output of that will be fun to format in a away that is user readable in jamf..

#!/bin/bash
###########################################
set -euo pipefail
IFS=$'
	'

## Variables #################
loggedInUser=$(stat -f %Su /dev/console)
brewArray=()

## Work Area ####################

if [[ -d /Users/"${loggedInUser}"/brew ]]; then
    while read -r brewApp; do
        name=$(basename "$brewApp")
            brewArray+=("${name}")
    done < <(ls /Users/"${loggedInUser}"/brew/Cellar/)
    echo "<result>$(printf '%s
' "${brewArray[@]}")</result>"
else
    echo "<result>brew not installed</result>"
fi

exit 0
- I am @exno or @exnozero on almost everything that exists.

JamfSquire
New Contributor
Do dis: 
in extension attrib: 
- string 
- extension attrib 
- script

!/bin/sh

file="/Library/Caches/Homebrew"
if [ -d "$file" ]
then echo "<result>"yes"</result>"
else echo "<result>"No"</result>"
fi

or better yet:

!/bin/sh

file="/usr/local/bin/brew"

can also do cask in same way - /usr/local/bin/cask

if [ -f "$file" ]
then echo "<result>"yes"</result>"
else echo "<result>"no Present"</result>"
fi

robertliebsch
Contributor

Adding to the zombie....
the if needs to be -x (executable) not -d (directory)

Also for M1/ARM machines: /opt/homebrew and it needs a -d

mhjor70
New Contributor

so to add on to this question what if you wanted to track what brew apps are installed, inside jamf, and possibly usage.

shannon_pasto
Contributor

here's my version...

#!/bin/sh

brewDir=$(/usr/bin/find /opt /usr/local -d -maxdepth 1 -iname "homebrew" 2>/dev/null)

if [ "${brewDir}" ]; then
  brewBin="${brewDir}/bin/brew"
  brewOwner=$(id -nu "$(stat -f %u "${brewBin}")")
  brewVers=$(sudo -u "${brewOwner}" "${brewBin}" config | grep HOMEBREW_VERSION | awk '{print $2}')
else
  brewVers="Not Installed"
fi

printf %s "<result>${brewVers}</result>"

exit 0