#!/bin/bash

# POWER STATUS

for device in $(smartctl --scan -d scsi | cut -d' ' -f1)
do
  output=$(smartctl -n standby -i $device)
  exitstatus=$?
  unset power_level
  
  # maybe find out power state by trying through all states and check when
  # it stops exiting 2, sarting with deepest.

  if $(echo $output | grep -q "Power mode is:")
  then
    power_level=2
  elif $(echo $output | grep -q "Power mode was:")
  then
    power_level=1
  elif [[ $exitstatus == 2 ]]
  then
    power_level=0
  fi
  
  if [[ -n $power_level ]]
  then
    echo "smartctl,host=${node.name},device=$device power_level=$power_level $(date --utc +%s%N)"
  fi
done

# TEMPS

for device in $(smartctl --scan | cut -d' ' -f1)
do
  temp=$(smartctl -n standby -A --json=c $device | jq .temperature.current)
  
  # hdd might be sleeping
  if [[ $temp == ?(-)+([0-9]) ]]
  then
    echo "smartctl,host=${node.name},device=$device temperature=$temp $(date --utc +%s%N)"
  fi
done

#{"json_format_version":[1,0],"smartctl":{"version":[7,2],"svn_revision":"5155","platform_info":"x86_64-linux-5.10.0-9-amd64","build_info":"(local build)","argv":["smartctl","-n","idle","-A","--json=c","/dev/sdb"],"messages":[{"string":"Device is in IDLE_A mode, exit(2)","severity":"information"}],"exit_status":2},"device":{"name":"/dev/sdb","info_name":"/dev/sdb [SAT]","type":"sat","protocol":"ATA"}}
