Automatic Emails Based on Smart Groups not Policies

dpodgors
Contributor

So we have an issue with our Mac users not having their machines connected to our network for more than 21 days. Sounds simple enough to create a Smart Group to check for Last Check-In and set days interval (in our case 7-14, 14-21, and > 21). But getting automatic emails to send out to these users has me perplexed. Since they are not checking I can't use a policy to fire the email off and Smart Groups only have a manual email option. I really don't want to have to monitor this group and do it manually, so I'm asking for some ideas...

2 REPLIES 2

thoule
Valued Contributor II

I created a script that pulls computers out of that smart group via API and sends an email to them. I run it daily with a launchD. I cut a bit out of this one, but hopefully enough here to get you what you need. I also needed to modify postfix.conf to use a relayhost of our company SMTP server.

It pulls computers not seen in 45 days, 55 days, and 60 days, then sends an email to each. My version has a different email for each of those groups. Set the staleComputerGrpID to the ID of your smartgroup.

########### EDIT THESE ##################################
JSSURL="https://url.school.edu:8443"
user="abcdefg"
pass="hijklmn"
############################################################

sendMail(){

x45SHBody="$compName: Inactive for $daysSinceSeen days.
You are receiving this message as the system mentioned below has not communicated with the Mac Management server since $lastReport and you are the registered system owner." 

x55SHBody="$compName: Inactive for $daysSinceSeen days. You are receiving this message as the system mentioned below has not communicated with the Mac Management server since $lastReport and you are the registered system owner.  Get it online now." 

x60SHBody="$compName: Inactive for $daysSinceSeen days. You are receiving this message as the system mentioned below has not communicated with the Mac Management server since $lastReport and you are the registered system owner. I'm sending a tech to beat you." 

if [ $1 = 60 ]; then
    BODY=$x60SHBody
elif [ $1 = 55 ]; then
    BODY=$x55SHBody
elif [ $1 = 45 ]; then
    BODY=$x45SHBody
else
    echo "Error on choosing body parameter"
    exit 1
fi

FROM='FromAddr@school.edu'
TO=$2
SUBJECT="$daysSinceSeen Day Communication Notice"

echo "email send to $TO, $ownerName, $compName, $serialNum, $lastReport."
printf "From: <%s>
To: <%s>
Subject: %s
MIME-Version: 1.0
Content-Type: text/html

%s" "$FROM" "$TO" "$SUBJECT" "$BODY" | sendmail -f "$FROM" -t "$TO"

}


echo "Building Report..."

JSS="$JSSURL/JSSResource"
outFile="/private/tmp/UnUsed.html"
staleComputerGrpID="73"

rm -rf /tmp/JSSNotify 2>/dev/null
mkdir /tmp/JSSNotify 2>/dev/null
tempDir="/tmp/JSSNotify"

#now
currentDate=`date +%s`

#get SmartGroups
curl -H "Accept: application/xml" -sfku "$user:$pass" "$JSS/advancedcomputersearches/id/$staleComputerGrpID" -X GET | xmllint --format - > $tempDir/group.xml

groupList=`cat $tempDir/group.xml |grep "<id>"| awk -F> '{print $2}'|awk -F< '{print $1}'|grep -v 74 |grep -v "-1"`
groupListArray=($groupList)


getMoreData(){
    ownerName=`xmllint --xpath '//computer/location/real_name'  $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
    compName=`xmllint --xpath '//computer/general/name' $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
    serialNum=`xmllint --xpath '//computer/general/serial_number' $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
    lastReport=`xmllint --xpath '//computer/general/report_date' $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
    assetTag=`xmllint --xpath '//computer/general/asset_tag' $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
    model=`xmllint --xpath '//computer/hardware/model' $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
    osVers=`xmllint --xpath '//computer/hardware/os_version'  $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`
}
for onecomputer in "${groupListArray[@]}"; do
    curl -H "Accept: application/xml" -sfku "$user:$pass" "$JSS/computers/id/$onecomputer" -X GET | xmllint --format - > $tempDir/computer$onecomputer.xml   
    lastEpochMS=`xmllint --xpath '//computer/general/report_date_epoch' $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'|cut -c1-10`
    toAddr=`xmllint --xpath '//computer/location/email_address'  $tempDir/computer$onecomputer.xml| awk -F> '{print $2}'|awk -F< '{print $1}'`

    daysSinceSeen=$((($currentDate-$lastEpochMS)/60/60/24))

    echo "$compName inactive for $daysSinceSeen"

    if [[ "$toAddr" != "source" ]]; then
        if [ $daysSinceSeen == "60" ]; then
            getMoreData
            sendMail 60 helpdesk@school.edu
            if [ ! -z $toAddr ]; then
                sendMail 60 $toAddr
            else
                echo "blank to address - I've got $toAddr"
            fi
        elif [ $daysSinceSeen == "55" ]; then
            if [ ! -z $toAddr ]; then
                getMoreData
                sendMail 55 $toAddr
            else 
                echo "No Email Avail: $compName, $serialNum, $lastReport."
            fi
        elif [ $daysSinceSeen == "45" ]; then
            if [ ! -z $toAddr ]; then
                getMoreData
                sendMail 45 $toAddr
            else 
                echo "No Email Avail: $compName, $serialNum, $lastReport."
            fi
        fi
    fi
done

echo "Removing temp data"
rm -rf /tmp/JSSNotify 2>/dev/null
echo "Done"

dpodgors
Contributor

Thanks, I had to tweak the API call to use computergroups instead and add a couple ldapsearches, but I think this should work. Much appreciated!