#!/bin/bash

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 -b -n1 -c | sed 's/ *$//' > $DUMP.top


## ------ 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
if [ $? == 127 ];then
   yum  --nogpgcheck  -y  --rpmverbosity=error  --errorlevel=1  --color=auto install sysstat >> $DUMP.yum_install
   iostat -kx 1 2 > $DUMP.iostat
fi


## -------- IO by Process ---------
iotop -b -n 2 -k -t -P > $DUMP.iotop
if [ $? == 127 ];then
   yum  --nogpgcheck  -y  --rpmverbosity=error  --errorlevel=1  --color=auto install iotop >> $DUMP.yum_install
   iotop -b -n 2 -k -t -P > $DUMP.iotop
fi


## --------- 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 ---
shinken-healthcheck  --timeout=10 > $DUMP.healthcheck


echo "Performance *(.cpu .dmesg .healthcheck .ps .top) dumped to $DUMP.*"

## ----------- Clean files and folders in performance_dump folder older than 7 days -----------
find $DEST/* -mtime +7 -exec rm -rf {} \; 2>/dev/null
