#!/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.

## Version
declare -r _VERSION='1.1.0'

## Default settings
ME=`basename $0`
BASEDIR="/var/log/recap"
cmds=""
CONFFILE="/etc/recap"

## Overriding defaults
if [[ -r "${CONFFILE}" ]]; then
  source "${CONFFILE}"
fi

memproc() {
	PROCESS="$1"
	TMP=`mktemp`
	>$TMP
	for i in `ls "${BASEDIR}"/ps_*.log`; do
		head -n 1 $i | tr "\n" "\t" >> $TMP
		egrep "$PROCESS" $i -c | tr "\n" "\t" >> $TMP
		egrep "$PROCESS" $i | awk '{ SUM += $6 } END {print SUM/1024, "M"};' >> $TMP
	done
	sort $TMP
	rm -f $TMP
}

proc() {
	PROCESS="$1"
	TMP=`mktemp`
	>$TMP
	for i in `ls "${BASEDIR}"/ps_*.log`; do
		head -n 1 $i | tr "\n" "\t" >> $TMP
		egrep "$PROCESS" $i -c >> $TMP
	done
	sort $TMP
	rm -f $TMP
}

mem() {
	PROCESS="$1"
	TMP=`mktemp`
	>$TMP
	for i in `ls "${BASEDIR}"/ps_*.log`; do
		head -n 1 $i | tr "\n" "\t" >> $TMP
		egrep "$PROCESS" $i | awk '{ SUM += $6 } END {print SUM/1024, "M"};' >> $TMP
	done
	sort $TMP
	rm -f $TMP
}

net() {
	PORT="$1"
	TMP=`mktemp`
	>$TMP
	for i in `ls "${BASEDIR}"/netstat_*.log`; do
		head -n 1 $i | tr "\n" "\t" >> $TMP
		egrep ":$PORT " $i -c >> $TMP
	done
	sort $TMP
	rm -f $TMP
}

netestab() {
	PORT="$1"
	TMP=`mktemp`
	>$TMP
	for i in `ls "${BASEDIR}"/netstat_*.log`; do
		head -n 1 $i | tr "\n" "\t" >> $TMP
		egrep ":$PORT .*ESTAB" $i -c >> $TMP
	done
	sort $TMP
	rm -f $TMP
}

querycount() {
	TMP=`mktemp`
	>$TMP
	for i in `ls "${BASEDIR}"/mysql_*.log`; do
		head -n 1 $i | tr "\n" "\t" >> $TMP
		grep "Query" -c $i >> $TMP
	done
	sort $TMP
	rm -f $TMP
}

usage() {
  echo -e "Usage: ${ME} ( {proc|mem|memproc} PROCESS_NAME |"
  echo "                   {net|netestab} PORT |"
  echo "                   querycount |"
  echo "                   -d PATH |"
  echo "                   [-V|--version])"
  echo
  echo "Parsing tool for recap logs to show specific information."
  echo
  echo "optional arguments:"
  echo "  {proc|mem|memproc} PROCESS_NAME  Show proc/mem info of PROCESS_NAME."
  echo "  {net|netestab} PORT              Show network connections of a PORT."
  echo "  querycount                       Show mysql connections."
  echo "  -d PATH                          Specify the recap log PATH."
  echo "  -V, --version                    Print version and exit."
  echo
}

if [[ $# -eq 0 ]]; then
	usage
	exit 1
fi

while [ $# -gt 0 ]; do
	case "$1" in
    -V|--version)
      echo "${_VERSION}"
      exit 0
      ;;
		-d)
			if [[ -d $2 ]]; then
				BASEDIR=$2
				shift 2
			else
				echo "Error: $2 is not a valid directory."
				usage
				exit 1
			fi
			;;
		memproc|proc|mem|net|netestab)
			cmds="${cmds}$1 $2 "
			shift 2
			;;
		querycount)
			cmds="${cmds}$1 "
			shift
			;;
		*)
			echo "Error: Invalid command $1"
			usage
			exit 1
			;;
	esac
done

echo "Information: Using ${BASEDIR} as recap log path"
echo "Information: Executing $cmds"

$cmds

exit 0
