Finding looping policies

cmdGriggs
New Contributor II

I was trying to figure out how to catch looping policies and came up with this. Would work best as an extension attribute feeding a smart group that is looking for anything other than "passed" in that EA.

Hope this helps someone else too.
Output looks like this: (error count) ( policy name)
<result> 5 Policy Broken Tester...
3 Policy Daily Maintenance (auto)...</result>

Caution: I have not had time to fully test this out so there may be some corner cases I have not seen yet.

#!/bin/bash
##################################################################
#: Date Created  : (April 22, 2015)
#: Author        : Dan Griggs
#: Version       : 1.00
##################################################################

### User Variables:  ####
# Number of times a policy needs to run to show in results
count=3
#
### End User Variables ###


# todays date format = Wed Apr 22
todayDate=$(echo $(date) | cut -d ' ' -f 1-3)
# date format = Wed Apr 21 -v is the offset number in days (d)
yesterdayDate=$(echo $(date -v-1d) | cut -d ' ' -f 1-3)
# date format = Wed Apr 20 -v is the offset number in days (d)
thirdDate=$(echo $(date -v-2d) | cut -d ' ' -f 1-3)


# find all the log entries from the past 3 days.  Date format matches the logs
# Also grep out the update inventory policies.  They will always be looping
jamf3days=$(cat /var/log/jamf.log | grep -e "$todayDate|$yesterdayDate|$thirdDate" | grep -v "Update Inventory")

# test print out to check current state of variable
# printf '%s
' "$jamf3days"

IFS=$'
'
# Find the looping policies populate the array.
# Cut statement only gets the relevant info: 8th field to the end
policyCount=( $(echo "$jamf3days" | grep Policy | cut -d ' ' -f 8- | sort | uniq -c | awk '$1 >'$count'{print $0;}' | sort -bnr) )


# check the result variable
if [[ ${#policyCount[@]} == 0 ]]; then
  # if there is nothing in the policy count array
  echo "<result>passed</result>"
  exit 0
else
  # print out that sweet, sweet array.
  # Format is (run count) (Name of policy)
  printf '%s
' "<result>${policyCount[@]}</result>"
  exit 0
fi
0 REPLIES 0