#!/bin/bash


# We DO NOT want any lib hook here
export LD_PRELOAD=""
# And we want a US system here
export LC_LANG=C

SCRIPT_VERSION="02.08.01-Patch13"

function display_help {
  printf "./dump_performance.sh\n"
  printf "This updater is designed to dump several server performances indicators\n"
  printf "\n"
  printf "  -h/--help:                      display help\n"
  printf "  --disable-shinken-healtcheck :  removes the shinken-healtcheck launch\n"
  exit 0
}


enable_shinken_healthcheck="true"

while :
do
    case "$1" in
      -h | --help)
        display_help  # Call your function
	  # no shifting needed here, we're done.
	  exit 0
	    ;;
      --disable-shinken-healtcheck)
        enable_shinken_healthcheck="false"
        shift
        ;;
      --) # End of all options
        shift
	break
	;;
      -*)
        echo "Error: Unknown option: $1" >&2
	  exit 1
	    ;;
      *)  # No more options
	      break
	        ;;
    esac
done


DEST=/var/lib/shinken/performance_dump
EPOCH_DAY=$(date +"%d-%m-%Y")
EPOCH_TIME=$(date +"%T")

mkdir -p $DEST/$EPOCH_DAY 2>/dev/null
DUMP=$DEST/$EPOCH_DAY/dump.$EPOCH_TIME

# We do not want that tools limit their outputs, so increase a lot it
export COLUMNS=500

## ----------- Top Extract -----------
# NOTE: with COLUMS we will have lot of trailing space, so remove them
TOP_RES=$(HOME='/' LANG=C top -b -n1 -c -o '%MEM' 2>/dev/null)
if [ $? != 0 ]; then  # old distro, like centos6, -o -> -a
    TOP_RES=$(HOME='/' LANG=C top -b -n1 -c -a)
fi
echo "$TOP_RES" | sed 's/ *$//' > $DUMP.top


## System/OS version
# DEBIAN
if [ -f /etc/os-release ];then
   cat  /etc/os-release   2>/dev/null   > $DUMP.os
fi
# Centos/RedHat
if [ -f /etc/redhat-release ];then
   cat  /etc/redhat-release 2>/dev/null > $DUMP.os
fi


# Dump the script version to know if it's updated
echo "code version: $SCRIPT_VERSION" > $DUMP.script_version


# DMI if available
dmidecode  2>/dev/null > $DUMP.dmi


## ------ PS with tree Extract -------
ps axjf > $DUMP.ps


## -------- CPU info Extract ---------
cat /proc/cpuinfo  | grep -E 'model name|bogomips|cpu MHz' > $DUMP.cpu


## -------- CPU info Extract ---------
iostat -kx 1 2 > $DUMP.iostat


## -------- IO by Process ---------
iotop -b -n 2 -k -t -P > $DUMP.iotop


## --------- Kernel Extract ----------
dmesg | tail -n 20 > $DUMP.dmesg


## --------- Disk spaces ----------
df -h > $DUMP.df


## --------- Kernel stats -----------
slabtop --once -s cslabtop --once -s c > $DUMP.slabtop


## --------- Sysctl values ---------
# note: only configured one
cat /etc/sysctl.conf | grep -vE '^#' |grep -vE '^$' > $DUMP.sysctl


## ---------- IPC --------------
ipcs > $DUMP.ipcs


## ---------- Memory raw -------
cat /proc/meminfo > $DUMP.meminfo


## --- Shinken Healthcheck Extract ---
if [ $enable_shinken_healthcheck == "true" ];then
    shinken-healthcheck  --timeout=10 > $DUMP.healthcheck
fi


echo "Performance files are dumped into $DUMP.*"

## ----------- Clean files and folders in performance_dump folder older than 7 days -----------
find $DEST/* -mtime +7 -exec rm -rf {} \; 2>/dev/null
