#!/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.
#
#
#~ Usage: _tool_ [OPTIONS]
#~ Options:
#~   {proc|mem|memproc} PROCESS_NAME  Show proc/mem info of PROCESS_NAME.
#~   {net|netestab} PORT              Show network connections of a PORT.
#~   querycount                       Show mysql connections.
#~   -d PATH                          Specify the recap log PATH.
#~   -h,--help                        Print this help.
#~   -V,--version                     Print version and exit.
#~

## Version
declare -r _VERSION='1.3.0'

## Default settings
BASEDIR="/var/log/recap"
cmds=""
CONFFILE="/etc/recap.conf"

## 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 -c ":${PORT} " "${i}" >> "${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 -c "ESTAB .*:${PORT} " "${i}" >> "${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}"
}

print_usage() {
  grep -E '^#~' "${0}" | sed -e 's/^#~\s\?//' \
                             -e "s/_tool_/$( basename ${0} )/"
}

if [[ ${#} -eq 0 ]]; then
  echo "Error: No arguments provided."
	print_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."
				print_usage
				exit 1
			fi
			;;
    -h|--help)
        print_usage
        exit 0
      ;;
		memproc|proc|mem|net|netestab)
			cmds="${cmds}${1} ${2} "
			shift 2
			;;
		querycount)
			cmds="${cmds}${1} "
			shift
			;;
		*)
			echo "Error: Invalid command ${1}"
			print_usage
			exit 1
			;;
	esac
done

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

${cmds}

exit 0
