Friday, September 24, 2010

[update]sysmon

I updated my sysmon script so that it actually works. I was having problems with the battery charge comparrison, I'm guess becuase of all the leading zeros. So, I put them in quotes and rather than using the actual greather-than, less-than ('>' '<') symbols, I used the '-gt/-lt' operators. Now, all is well in the system monitoring script.

#!/bin/zsh


# make sure user has root privileges
chkroot && [[ $? = 0 ]] || exit 1

log="/home/jason/devel/scripts/sysmon/sysmon.log"
ver=(0.0.3-1)

echo "" > $log

# define battery variables
charge_now='/sys/class/power_supply/BAT1/charge_now'
charge_full='/sys/class/power_supply/BAT1/charge_full'
charge_factor=20  # 5% battery power

# define temperature variables
max_temp=85
for i in {0..1}; do
  temp_path="/sys/class/hwmon/hwmon$i/device/temp1_input"
  [[ -f $temp_path ]] && break
done

# get full charge value
full="`cat $charge_full`"

# calculate the minimum charge based on the charge_factor
charge_min=$(($full/$charge_factor))

# just keep going
while [[ "1" = "1" ]]; do
  # get current charge
  charge="`cat $charge_now`"

  # system doesn't use hwmon1 or hwmon0 exclusively, so there's a need to differentiate
  temp="`cat $temp_path`"
  temp=$temp[1,2]

  #[[ $temp > $max_temp ]] || [[ $charge < $charge_min ]]

  if [[ "$temp" -gt "$max_temp" ]]; then
    echo `date` >> $log
    echo "Shutdown due to high temp: $temp C" >> $log
        shutdown -h now
  fi

  if [[ "$charge" -lt "$charge_min" ]]; then
    echo `date` >> $log
    echo "Shutdown due to low charge: $charge < $charge_min" >> $log
        shutdown -h now
  fi
  sleep 10
done

No comments:

Post a Comment