Uptime +7 days Smart Group - need help with extension attribute?

Frances_R
New Contributor II

Hi, forgive me, super new to this. I would like to create a smart group with machines that have not been rebooted in more than 7 days. I thought I had the Extension Attributes correct as well as the settings for the smart group, see attached images. However, the smart group is showing a bunch of computers that have definitely been restarted in the previous 7 days.
See below for my settings and script:
711b1bcc830e4040a7ffacaa1a172bca
e45822c62a5244b983619f928b0a3e2f
dbec9291f2b645e18df4c3bae5c57a15

Can anyone tell me what I'm doing wrong?
Thanks!!

2 REPLIES 2

mm2270
Legendary Contributor III

I would consider doing some searches here for EAs related to getting the uptime of machines. There are many different approaches. It's possible the way your script is working is creating false positives. I'm not able to test it out right now, but I can take a look at it later to see. My guess is a different approach might give you better results.

Here's an example of getting the number of days uptime using sysctl, current unix time, and some math

#!/bin/bash

echo "<result>$(expr $(expr $(date +"%s") - $(sysctl kern.boottime | awk -F'[= |,]' '{print $6}')) / 60 / 60 / 24)</result>"

alexjdale
Valued Contributor III

Here's my EA for uptime (I round to 1 day if it's been up less than that). Your IF statement looks whack, to use a technical term, I don't know what it's doing.

I'd probably write this differently nowadays, but it works.

#!/bin/bash

uptimeRes=$(uptime)
if [ "$(echo $uptimeRes | grep day)" ]; then
    uptimeDays=$(echo $uptimeRes | awk '{print $3}')
else
    uptimeDays=1
fi
echo "<result>$uptimeDays</result>"