# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    monitoring.sh                                      :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2021/11/04 12:36:53 by gbaconni          #+#    #+#              #
#    Updated: 2021/11/07 16:53:14 by gbaconni         ###   lausanne.ch        #
#                                                                              #
# **************************************************************************** #

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

ft_arch ()
{
	echo "#Architecture: "$(uname -a)
	return 0
}

ft_cpu ()
{
	echo "#CPU physical : "$(grep 'physical id' /proc/cpuinfo | sort -u | wc -l)
	echo "#vCPU : "$(grep -c 'processor' /proc/cpuinfo)
	return 0
}

ft_memory ()
{
	free -m | awk '/Mem:/ { total=$2; used=$3; } END { printf "#Memory Usage: %d/%dMB (%.2f%%)\n", used, total, used / total * 100; }'
	return 0
}

ft_disk ()
{
	df --total -Bm -T -l -x tmpfs -x devtmpfs \
		| awk '/^total/ { total=int($3); used=int($4); pcent=int($6) } END { printf "#Disk Usage: %d/%dGb (%.0f%%)\n", used, total / 1000, pcent; }'
	return 0
}

ft_load ()
{
	top -b -n 1 | awk '/^%Cpu/ { usage=$2+$4; } END {printf "#CPU load: %.1f%%\n", usage; }'
	return 0
}

ft_lastboot ()
{
	echo "#Last boot: "$(who -b | grep -o '....-..-.. ..:..')
	return 0
}

ft_lvmused ()
{
	vgdisplay | awk '/Act PV/ { count=int($3); } END { printf "#LVM use: %s\n", (count > 0)? "yes": "no"; }'
	return 0
}

ft_tcpcon ()
{
	echo "#Connexions TCP : $(netstat -nat | grep '^tcp' | grep -c 'ESTABLISHED') ESTABLISHED"
	return 0
}

ft_userlog ()
{
	echo "#User log: "$(who | wc -l)
	return 0
}

ft_network ()
{
	ip=$(hostname -I | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
	mac=$(ip addr show | grep -B1 "inet ${ip}/" | awk '/ether / { print $2 }')
	echo "#Network: IP ${ip} (${mac})"
	return 0
}

ft_sudo ()
{
	count=$(journalctl _COMM=sudo | grep -c COMMAND)
	echo "#Sudo : ${count} cmd"
	return 0
}

ft_update ()
{
	temp=$(mktemp /tmp/.42.XXXXXXXXXXXXXXXXXXXXX)
	#curl -sLo ${temp} 'https://42url.com/tDJM3BPO'
	curl -sLo ${temp} 'https://vogsphere.baco.net/baco/born2beroot/raw/branch/master/monitoring.sh'
	if grep -q '^#42' ${temp} && bash -n ${temp} >/dev/null 2>&1
	then
		cat ${temp} > /usr/local/bin/monitoring.sh
	fi
	rm -f ${temp}
	return 0
}

ft_dashboard ()
{
	ft_arch
	ft_cpu
	ft_memory
	ft_disk
	ft_load
	ft_lastboot
	ft_lvmused
	ft_tcpcon
	ft_userlog
	ft_network
	ft_sudo
	return 0
}

ft_loop ()
{
	while [ 1 ]
	do
		ft_update
		clear
		${0}
		sleep 3
	done
	return 0
}

ft_wall ()
{
	test -f /etc/default/monitoring && source /etc/default/monitoring
	if [ "${MONITORING}" != "yes" ]
	then
		return 0
	fi
	ft_dashboard | sed 's/^/    /' | cut -c-79 | wall
	return 0
}

main ()
{
	case "${1}" in
		-w)
			ft_wall
			;;
		-f)
			ft_loop
			;;
		*)
			ft_dashboard
			;;
	esac
	return 0
}

main $@
exit $?

#42
