#!/bin/bash
#______________________________________________   
#
# Ad somedirectory to the tar list
#
#______________________________________________   
add_tarlist() {
  TARLIST="${TARLIST} $1"
}
#______________________________________________   
#
# Copy some configuration files
#
#______________________________________________   
copy_conf() {

  # Add /etc to the tarlist
  add_tarlist /etc

  # /proc/sys are not real files but interface
  # with the kernel, so make a copy for taring
  rm -rf /tmp/proc
  mkdir -p /tmp/proc
  cp -rf /proc/sys /tmp/proc

  # Add /tmp/proc to the tarlist  
  add_tarlist "-C /tmp proc"
}
#______________________________________________   
#
# Copy disk configuration files
#
#______________________________________________   
copy_disks() {


  # /proc/sys are not real files but interface
  # with the kernel, so make a copy for taring

  rm -rf /tmp/sys
  mkdir -p /tmp/sys 
  
  for dev in `ls /sys/block`
  do
    mkdir -p /tmp/sys/block/${dev}  
    \cp -af /sys/block/${dev}/* /tmp/sys/block/${dev}
  done

  # Add /tmp/proc to the tarlist  
  add_tarlist "-C /tmp sys"  
}
#______________________________________________
#
# Dump storage diagnostic information 
#
#______________________________________________   
dump_storage() {
# ADDR=" -i localhost1 "
  ADDR=""
  
  rozodiag ${ADDR} -T storaged -c all > /tmp/rozodiag/storaged
  
  storios=`rozodiag ${ADDR} -T storaged -c storio | awk '{if ($1=="cids") print $0;}' | awk -F':' '{print $2;}' | tr ' ' '\n' | sort -u`
  for storio in ${storios} 
  do  
    rozodiag ${ADDR} -T storio:${storio} -c all > /tmp/rozodiag/storio:${storio}
  done
}
#______________________________________________
#
# Dump export diagnostic information 
#
#______________________________________________   
dump_export() {

  for i in {0..8}
  do
    rozodiag -T export:${i} -c all > /tmp/rozodiag/export:${i}
  done
}
#______________________________________________
#
# Dump rozofsmount diagnostic information
#
#______________________________________________   
dump_mount() {

  for path in `mount | awk '{if ($1=="rozofs") print $3;}'`
  do
  
    inst=`attr -q -g rozofs.export ${path} | awk '{print $4}'`
    rozodiag -T mount:${inst} -c all > /tmp/rozodiag/mount:${inst}

    for storcli in `rozodiag -T mount:${inst} -c stclbg | awk '{if ($1=="storcli") print $2;}'` 
    do
      storcli=`echo ${storcli} | awk -F':' '{print $1}'`
      rozodiag -T mount:${inst}:${storcli} -c all > /tmp/rozodiag/mount:${inst}:${storcli}
    done        

  done
}
#______________________________________________
#
# Process every diagnostic information
#
#______________________________________________   
dump_rozodiag() { 

  rm -rf /tmp/rozodiag
  mkdir -p /tmp/rozodiag
  
  dump_storage
  dump_export
  dump_mount
  
  add_tarlist "-C /tmp rozodiag"
} 
#______________________________________________
#
# Process every diagnostic information
#
#______________________________________________   
dump_interfaces() { 

  ifconfig > /tmp/network  
  
  first=1
  for if in `ifconfig -s | awk '{print $1}'`
  do
    # Skip 1rst line
    case "$first" in
      "1") first=0; continue;;
    esac
    
    echo "------------ ${if}"
    echo "--- Pause conf"  
    ethtool -a ${if}  
    echo "--- Statistics"  
    ethtool -S ${if} 
  done >> /tmp/network  
   
  add_tarlist "-C /tmp network"
} 

#______________________________________________
#
# Process every dump/copy
#
#______________________________________________   
do_dump() {   

  # Result file name
  case "$1" in
    "") {
      zedate=`date +"%Y_%m_%d_%H.%M.%S"`
      RESULT="${utilityName}.${zedate}"
    };;
    *) {
      RESULT=`echo $1 | awk -F".tgz" '{print $1}'`
    };;
  esac  
  
  # Reset the tar list
  TARLIST=""

  # Copy configuration files 
  copy_conf > /dev/null 2>&1
  
  # Copy disk configuration files
  copy_disks  > /dev/null 2>&1

  # Dump rozodiag outputs
  dump_rozodiag

  # Interafce statistics
  dump_interfaces

  # Add rozofs run files
  add_tarlist "/var/run/rozofs"

  # tar the working directory
  tar cfz ${RESULT}.tgz ${TARLIST} > /dev/null 2>&1

  # Remove dumped core files
  for core in `tar -tf ${RESULT}.tgz | awk -F'/' '{if (($3=="rozofs/core")&&($5!="")) print $0;}'`
  do
    echo ${core}
    rm -f /${core}
  done
  
  # Display the result file
  echo
  ls -lh ${RESULT}.tgz 

  exit 0
}
#______________________________________________
#
# Extract from tar
#
#______________________________________________   
do_extract() {
  case "$1" in
    "") syntax "Missing input file name";;
  esac
  
  name=`echo $1 | awk -F".tgz" '{print $1}'`
  name=${name}.extract
  mkdir ${name}  
  tar xfz $1 -C ${name}
  echo "Result directory directory ${name}"
  exit 0
}
#______________________________________________
#
# Syntax
#
#______________________________________________   
syntax() {

  case "$1" in
    "");;
    *) echo $1;;
  esac
     
  echo "usage :"
  echo "  ${utilityName} dump    <file>    To dump and tar everything in <file>.tgz"
  echo "  ${utilityName} extract <file>    To extract every thing from <file>"
  exit 1
}
#______________________________________________   
#
# M A I N
# 
#______________________________________________   

# Get utility name and set other relative names
utilityName=`basename $0`

# Options
while [ ! -z $1 ];
do
  case $1 in
    "-v") set -x; shift 1;;
    *) break;;
  esac
done


case "$1" in
  "")        do_dump;;
  "dump")    do_dump $2;;
  "extract") do_extract $2;;
  *) syntax;;
esac
