#!/bin/bash
set -e

##
COLLECTOR_NAME=$(basename "${0}" | sed 's/\.sh//g')
OUTPUT_FILE=$1

##  Gather Data
RPI_CLOCK_ARM=$(vcgencmd measure_clock arm | cut -d'=' -f 2)
RPI_CLOCK_CORE=$(vcgencmd measure_clock core | cut -d'=' -f 2)
RPI_CPU_TEMP=$(vcgencmd measure_temp 2>/dev/null | tr -dc .0-9 2>/dev/null)
RPI_MEM_RELOC_FAILURES=$(vcgencmd mem_reloc_stats | grep failures | tr -dc .0-9 2>/dev/null)
RPI_MEM_RELOC_COMPACTIONS=$(vcgencmd mem_reloc_stats | grep compactions | tr -dc .0-9 2>/dev/null)
RPI_MEM_RELOC_BLOCKFAILS=$(vcgencmd mem_reloc_stats | grep block | tr -dc .0-9 2>/dev/null)
RPI_OOM_EVENTS=$(vcgencmd mem_oom | grep events | tr -dc .0-9 2>/dev/null)
RPI_OOM_REQUIRED=$(vcgencmd mem_oom | grep required | tr -dc .0-9 2>/dev/null)
RPI_OOM_TOTAL=$(vcgencmd mem_oom | grep total | tr -dc .0-9 2>/dev/null)
RPI_OOM_MAX=$(vcgencmd mem_oom | grep max | tr -dc .0-9 2>/dev/null)
RPI_VOLTS_CORE=$(vcgencmd measure_volts core | tr -dc .0-9 2>/dev/null)
RPI_VOLTS_DRAM_C=$(vcgencmd measure_volts sdram_c | tr -dc .0-9 2>/dev/null)
RPI_VOLTS_DRAM_I=$(vcgencmd measure_volts sdram_i | tr -dc .0-9 2>/dev/null)
RPI_VOLTS_DRAM_P=$(vcgencmd measure_volts sdram_p | tr -dc .0-9 2>/dev/null)
RPI_UNDER_VOLTED=0x1
RPI_FREQUENCY_CAPPED=0x2
RPI_SYSTEM_THROTTLED=0x4
RPI_SOFT_TEMPERATURE_LIMIT=0x8
RPI_GET_THROTTLED_STATUS=$(vcgencmd get_throttled)
RPI_GET_THROTTLED_STATUS=${RPI_GET_THROTTLED_STATUS#*=}
RPI_CHIP=$(grep 'Hardware' /proc/cpuinfo | awk '{print $3}')
RPI_MODEL=$(grep Model /proc/cpuinfo | cut -d':' -f 2 | sed 's/^ *//g' | sed 's/Model //g; s/Raspberry //g; s/Pi //g')
RPI_FIRMWARE=$(vcgencmd version | grep version | awk '{print $2}' | grep -o '........$')
RPI_REVISION=$(grep 'Revision' /proc/cpuinfo | awk '{print $3}')

##  Format Data
COLLECTOR_DATA=$(
  echo rpi_clock_arm "${RPI_CLOCK_ARM}"
  echo rpi_clock_core "${RPI_CLOCK_CORE}"
  echo rpi_cpu_temp "${RPI_CPU_TEMP}"
  echo rpi_mem_reloc_failures "${RPI_MEM_RELOC_FAILURES}"
  echo rpi_mem_reloc_compactions "${RPI_MEM_RELOC_COMPACTIONS}"
  echo rpi_mem_reloc_blockfails "${RPI_MEM_RELOC_BLOCKFAILS}"
  echo rpi_oom_events "${RPI_OOM_EVENTS}"
  echo rpi_oom_required "${RPI_OOM_REQUIRED}"
  echo rpi_oom_total "${RPI_OOM_TOTAL}"
  echo rpi_oom_max "${RPI_OOM_MAX}"
  echo rpi_volts_core "${RPI_VOLTS_CORE}"
  echo rpi_volts_sdram_c "${RPI_VOLTS_DRAM_C}"
  echo rpi_volts_sdram_i "${RPI_VOLTS_DRAM_I}"
  echo rpi_volts_sdram_p "${RPI_VOLTS_DRAM_P}"
  echo -n "rpi_get_throttled_status "
  ((RPI_GET_THROTTLED_STATUS != 0)) && echo "1" || echo "0"
  echo -n "rpi_get_throttled_under_voltage "
  (((RPI_GET_THROTTLED_STATUS & RPI_UNDER_VOLTED) != 0)) && echo "1" || echo "0"
  echo -n "rpi_get_throttled_frequency_capped "
  (((RPI_GET_THROTTLED_STATUS & RPI_FREQUENCY_CAPPED) != 0)) && echo "1" || echo "0"
  echo -n "rpi_get_throttled_soft_temperature_limit "
  (((RPI_GET_THROTTLED_STATUS & RPI_SOFT_TEMPERATURE_LIMIT) != 0)) && echo "1" || echo "0"
  echo -n "rpi_get_throttled_system_throttled "
  (((RPI_GET_THROTTLED_STATUS & RPI_SYSTEM_THROTTLED) != 0)) && echo "1" || echo "0"
  echo "rpi_hardware{model=\"${RPI_MODEL}\",firmware=\"${RPI_FIRMWARE}\",chip=\"${RPI_CHIP}\",revision=\"${RPI_REVISION}\"}" 1
)

##  Output to stdout or write .prom File
if [[ -z $OUTPUT_FILE ]]; then
  echo -e "\nChip:\t\t${RPI_CHIP}\nRevision:\t${RPI_REVISION}\nModel:\t\t${RPI_MODEL}\nFirmware:\t${RPI_FIRMWARE}\nKernel:\t\t$(uname -r)\nUptime:\t\t$(uptime -p | sed 's/up //')"
  echo -e "----------------------------------------------------------------\n"
  echo -e "${COLLECTOR_DATA}" | sort -u | grep -v 'hardware' | sed 's/rpi_//; s/get_//; s/_throttled//; s/soft_//; s/ /\t/g' | column -t
  if [[ $EUID -eq 0 ]]; then
    if systemctl --no-pager --state=failed,error,degraded,unknown,offline | grep -E '.service|.timer'; then
      echo
    fi
  fi
  echo
else
  echo -e "${COLLECTOR_DATA}" | sort -u | sponge "${COLLECTOR_NAME}.prom$$"
  mv "${COLLECTOR_NAME}.prom$$" "${OUTPUT_FILE}"
fi
