#!/bin/bash 
#
# memcalc.sh
# Author: Christophe Casalegno / Brain 0verride
# Contact: brain@christophe-casalegno.com
# Version 1.0
#
# Copyright (c) 2022 Christophe Casalegno
# 
# 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 3 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, see <https://www.gnu.org/licenses/>
#
# The license is available on this server here: 
# https://www.christophe-casalegno.com/licences/gpl-3.0.txt


OPTION=${1}
PROCESS="${@:2}"

if [[ ${OPTION} = "--service" ]]
then

	if [[ -z ${PROCESS} ]]
	then
		echo "Vous devez donner le nom d'un ou plusieurs processus en paramètre"
	else
		for EXPRESSION in $(echo ${PROCESS})
		do
			RAMUSAGE=$(ps -ely |awk -v process=${EXPRESSION} '$13 == process' |awk '{SUM += $8/1024} END {print SUM}' |cut -d '.' -f1)
			if [[ -z ${RAMUSAGE} ]]
			then
				echo "le processus ${EXPRESSION} n'existe pas"
			else
				echo "RAM consommée pour ${EXPRESSION} : ${RAMUSAGE} MB"
			fi
		done
	fi

elif [[ ${OPTION} = "--pid" ]]
then
	if [[ -z ${PROCESS} ]]
        then
                echo "Vous devez donner le numéro d'un ou plusieurs PIDs en paramètre"
        else
                for EXPRESSION in $(echo ${PROCESS})
                do
                        RAMUSAGE=$(ps -ely |awk -v pid=${EXPRESSION} '$3 == pid' |awk '{SUM += $8/1024} END {print SUM}' |cut -d '.' -f1)
			SERVICE=$(ps -ely |awk -v pid=${EXPRESSION} '$3 == pid' | awk '{print $13}')

                        if [[ -z ${RAMUSAGE} ]]
                        then
                                echo "le pid ${EXPRESSION} n'existe pas"
                        else
				echo "RAM consommée pour le pid ${EXPRESSION} (${SERVICE}) : ${RAMUSAGE} MB"
                        fi
                done
        fi

elif [[ ${OPTION} = "--user" ]]
then
        if [[ -z ${PROCESS} ]]
        then
                echo "Vous devez donner le nom d'un ou plusieurs utilisateurs en paramètre"
        else
                for EXPRESSION in $(echo ${PROCESS})
                do
                        RAMUSAGE=$(ps -elyf |awk -v user=${EXPRESSION} '$2 == user' |awk '{SUM += $8/1024} END {print SUM}' |cut -d '.' -f1)

                        if [[ -z ${RAMUSAGE} ]]
                        then
                                echo "l'utilisateur ${EXPRESSION} n'existe pas ou ne consomme pas actuellement de RAM"
                        else
                                echo "RAM consommée par l'utilisateur ${EXPRESSION} : ${RAMUSAGE} MB"
                        fi
                done
        fi


else
	echo "Vous devez indiquer comme premier paramètre --service, --user ou --pid"
fi


