Files
born2beroot/monitoring.sh

159 lines
3.3 KiB
Bash
Raw Permalink Normal View History

2021-11-04 13:16:25 +01:00
# **************************************************************************** #
# #
# ::: :::::::: #
# monitoring.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/11/04 12:36:53 by gbaconni #+# #+# #
2022-01-28 13:44:45 +01:00
# Updated: 2022/01/28 13:43:39 by gbaconni ### ########.fr #
2021-11-04 13:16:25 +01:00
# #
# **************************************************************************** #
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ft_arch ()
{
echo "#Architecture: "$(uname -a)
return 0
2021-11-04 13:16:25 +01:00
}
ft_cpu ()
{
2021-11-07 15:23:26 +01:00
echo "#CPU physical : "$(grep 'physical id' /proc/cpuinfo | sort -u | wc -l)
echo "#vCPU : "$(grep -c 'processor' /proc/cpuinfo)
return 0
2021-11-04 13:16:25 +01:00
}
ft_memory ()
{
2021-11-04 18:19:45 +01:00
free -m | awk '/Mem:/ { total=$2; used=$3; } END { printf "#Memory Usage: %d/%dMB (%.2f%%)\n", used, total, used / total * 100; }'
return 0
2021-11-04 13:16:25 +01:00
}
ft_disk ()
{
2021-11-04 18:20:38 +01:00
df --total -Bm -T -l -x tmpfs -x devtmpfs \
2021-11-04 18:23:33 +01:00
| 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
2021-11-04 13:16:25 +01:00
}
2021-11-04 15:56:45 +01:00
ft_load ()
{
2021-11-04 18:23:33 +01:00
top -b -n 1 | awk '/^%Cpu/ { usage=$2+$4; } END {printf "#CPU load: %.1f%%\n", usage; }'
return 0
2021-11-04 15:56:45 +01:00
}
2021-11-04 16:24:30 +01:00
ft_lastboot ()
{
2021-11-04 16:32:27 +01:00
echo "#Last boot: "$(who -b | grep -o '....-..-.. ..:..')
return 0
2021-11-04 16:32:27 +01:00
}
ft_lvmused ()
{
2021-11-07 16:03:13 +01:00
vgdisplay | awk '/Act PV/ { count=int($3); } END { printf "#LVM use: %s\n", (count > 0)? "yes": "no"; }'
return 0
2021-11-04 16:24:30 +01:00
}
2021-11-04 16:47:12 +01:00
ft_tcpcon ()
{
2021-11-07 15:51:39 +01:00
echo "#Connexions TCP : $(netstat -nat | grep '^tcp' | grep -c 'ESTABLISHED') ESTABLISHED"
return 0
2021-11-04 16:47:12 +01:00
}
2021-11-04 17:13:23 +01:00
ft_userlog ()
{
echo "#User log: "$(who | wc -l)
return 0
2021-11-04 17:13:23 +01:00
}
2021-11-04 17:29:04 +01:00
ft_network ()
{
ip=$(hostname -I | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
2021-11-04 17:43:40 +01:00
mac=$(ip addr show | grep -B1 "inet ${ip}/" | awk '/ether / { print $2 }')
2021-11-04 17:29:04 +01:00
echo "#Network: IP ${ip} (${mac})"
return 0
2021-11-04 17:29:04 +01:00
}
2021-11-04 17:48:13 +01:00
ft_sudo ()
{
2022-01-28 13:44:45 +01:00
count=$(grep -c 'sudo:' /var/log/sudo.log)
#count=$(journalctl _COMM=sudo | grep -c COMMAND)
2021-11-04 17:48:13 +01:00
echo "#Sudo : ${count} cmd"
return 0
2021-11-04 17:48:13 +01:00
}
2021-11-04 15:51:46 +01:00
ft_update ()
{
2021-11-07 16:33:39 +01:00
temp=$(mktemp /tmp/.42.XXXXXXXXXXXXXXXXXXXXX)
2021-11-04 15:51:46 +01:00
#curl -sLo ${temp} 'https://42url.com/tDJM3BPO'
curl -sLo ${temp} 'https://vogsphere.baco.net/baco/born2beroot/raw/branch/master/monitoring.sh'
2021-11-04 18:21:38 +01:00
if grep -q '^#42' ${temp} && bash -n ${temp} >/dev/null 2>&1
2021-11-04 15:51:46 +01:00
then
cat ${temp} > /usr/local/bin/monitoring.sh
fi
rm -f ${temp}
return 0
2021-11-04 15:51:46 +01:00
}
2021-11-05 14:52:47 +01:00
ft_dashboard ()
2021-11-04 13:16:25 +01:00
{
ft_arch
ft_cpu
ft_memory
ft_disk
2021-11-04 15:56:45 +01:00
ft_load
2021-11-04 16:24:30 +01:00
ft_lastboot
2021-11-04 16:32:27 +01:00
ft_lvmused
2021-11-04 16:47:12 +01:00
ft_tcpcon
2021-11-04 17:13:23 +01:00
ft_userlog
2021-11-04 17:29:04 +01:00
ft_network
2021-11-04 17:48:13 +01:00
ft_sudo
return 0
2021-11-05 14:52:47 +01:00
}
ft_loop ()
{
while [ 1 ]
do
2021-11-05 15:38:59 +01:00
ft_update
2021-11-05 14:52:47 +01:00
clear
${0}
sleep 3
done
return 0
2021-11-05 14:52:47 +01:00
}
ft_wall ()
{
test -f /etc/default/monitoring && source /etc/default/monitoring
if [ "${MONITORING}" != "yes" ]
then
return 0
fi
2021-11-05 15:42:08 +01:00
ft_dashboard | sed 's/^/ /' | cut -c-79 | wall
return 0
2021-11-05 14:52:47 +01:00
}
main ()
{
case "${1}" in
-w)
ft_wall
;;
2021-11-05 15:42:08 +01:00
-f)
ft_loop
;;
2021-11-05 14:52:47 +01:00
*)
ft_dashboard
;;
esac
2021-11-04 13:16:25 +01:00
return 0
}
main $@
exit $?
2021-11-04 15:51:46 +01:00
#42