#!/bin/bash
#
#   Copyright (C) 2017 Rackspace, Inc.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#

# Print the output of "uptime" to the specified file
print_uptime() {
  local LOGFILE="$1"
  log INFO "Starting 'uptime' report - ${LOGFILE##*/}"
  echo "UPTIME report" >> "${LOGFILE}"
  uptime >> "${LOGFILE}"
  log INFO "Ended 'uptime' report"
}

# Print the output of "free" to the specified file
print_free() {
  local LOGFILE="$1"
  log INFO "Starting 'free' report - ${LOGFILE##*/}"
  echo "FREE report" >> "${LOGFILE}"
  free ${OPTS_FREE} >> "${LOGFILE}"
  log INFO "Ended 'free' report"
}

# Print the output of "vmstat" to the specified file
print_vmstat() {
  local LOGFILE="$1"
  log INFO "Starting 'vmstat' report - ${LOGFILE##*/}"
  echo "VMSTAT report" >> "${LOGFILE}"
  vmstat ${OPTS_VMSTAT} >> "${LOGFILE}"
  log INFO "Ended 'vmstat' report"
}

# Print the output of "iostat" to the specified file
print_iostat() {
  local LOGFILE="$1"
  log INFO "Starting 'iostat' report - ${LOGFILE##*/}"
  echo "IOSTAT report" >> "${LOGFILE}"
  iostat ${OPTS_IOSTAT} >> "${LOGFILE}"
  log INFO "Ended 'iostat' report"
}

# Print the output of "iotop" to the specified file
print_iotop() {
  local LOGFILE="$1"
  log INFO "Starting 'iotop' report - ${LOGFILE##*/}"
  echo "IOTOP report" >> "${LOGFILE}"
  iotop ${OPTS_IOTOP} >> "${LOGFILE}"
  log INFO "Ended 'iotop' report"
}

# Print the output of sar to the specified file
print_sar() {
  local LOGFILE="$1"
  local OPTION="$2"
  log INFO "Starting 'sar' report - ${LOGFILE##*/}"
  local FLAGS=''
  # check to see if we're going to use any parameters for sar
  if [[ "${OPTION}" == "r" ]]; then
    FLAGS="-r"
  elif [[ "${OPTION}" == "q" ]]; then
    FLAGS="-q"
  else
    FLAGS=""
  fi

  echo "SAR${FLAGS} report" >> "${LOGFILE}"
  sar "${FLAGS}" >> "${LOGFILE}"
  log INFO "Ended 'sar' report"
}

# Print the disk utilization to the defined file
print_df() {
  local LOGFILE="$1"
  log INFO "Starting 'disk utilization' report - ${LOGFILE##*/}"
  local LOGFILE="$1"
  echo "Disk Utilization" >> "${LOGFILE}"
  df ${OPTS_DF} >> "${LOGFILE}"
  log INFO "Ended 'disk utilization' report"
}

# Print the slabinfo command to the defined file
print_slabinfo() {
  local LOGFILE="$1"
  log INFO "Starting 'slab info' report - ${LOGFILE##*/}"
  echo "Slab Information" >> "${LOGFILE}"
  printf "name\tactive\tnum_obj\tobj_size\thuh\n" >> "${LOGFILE}"
  tail -n+2 /proc/slabinfo |
    awk 'size=$3*$4 {print $1"\t"$2"\t"$3"\t"$4"\t"size/1048576}' |
    sort -k5gr >> "${LOGFILE}"
  log INFO "Ended 'slab info' report"
}

# Print the top 10 processes (by cpu usage) to the defined file
print_top_10_cpu() {
  local LOGFILE="$1"
  log INFO "Starting 'top 10 cpu' report - ${LOGFILE##*/}"
  local pidstat_cpufield=0
  # Systat versions have the CPU field in different places.
  # Calculate dynamically the %CPU field
  local pidstat_fields=( $(LC_ALL=C pidstat | sed -n '3p') )
  for index in $(seq 0 ${#pidstat_fields[@]}); do
    if [[ "${pidstat_fields[${index}]}" == "%CPU" ]]; then
        # Array is 0 index, sorting is not, we add 1
        pidstat_cpufield=$(( ${index} + 1 ))
        break
    fi
  done
  echo "Top 10 cpu using processes" >> "${LOGFILE}"
  # capture header
  LC_ALL=C pidstat | sed -n '3p' >> "${LOGFILE}"
  LC_ALL=C pidstat -l 2 2 | grep -v '%system' |
    egrep ^Average: | sort -nr -k ${pidstat_cpufield} |
    head -11 >> "${LOGFILE}"
  log INFO "Ended 'top 10 cpu' report"
}

# Print the top 10 processes (by memory usage) to the defined file
print_top_10_mem() {
  local LOGFILE="$1"
  log INFO "Starting 'top 10 memory' report - ${LOGFILE##*/}"
  echo "Top 10 memory using processes" >> "${LOGFILE}"
  ps auxww --sort=-rss | head -11 >> "${LOGFILE}"
  log INFO "Ended 'top 10 memory' report"
}
