Skip to content

Instantly share code, notes, and snippets.

@Conobi
Last active December 8, 2021 14:28
Show Gist options
  • Save Conobi/ab787231f07c4aa0fff26053b61a86c5 to your computer and use it in GitHub Desktop.
Save Conobi/ab787231f07c4aa0fff26053b61a86c5 to your computer and use it in GitHub Desktop.
42 projects - born2beroot monitoring.sh

Monitoring.sh - born2beroot (Debian flavour)

This script has only been tested on Debian environement. It uses jc and jq to parse the commands to JSON, and then select the proper data to output. You must install them before trying the script. Warning: ifconfig has been configured to use the Debian 5.10 path. It would not work on Ubuntu or others distributions.

#!/bin/bash
# LANG=C is needed to works when Debian is set up with other languages
LANG=C
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# All the commands used to show informations
arch=$(uname -a)
pcpu=$(cat /proc/cpuinfo | grep 'physical id' | uniq | wc -l)
vcpu=$(cat /proc/cpuinfo | grep '^processor' | uniq | wc -l)
memtotal=$(free --mega | jc --free | jq '.[0].total')
memfree=$(free --mega | jc --free | jq '.[0].free')
memperc=$(echo "scale=2; 100*$memfree/$memtotal" | bc -l)
dskused=$(echo "scale=1; $(df --type ext4 | jc --df | jq '[.[]."used"] | add')/1000000" | bc -l)
dsktotal=$(echo "scale=1; $(df --type ext4 | jc --df | jq '[.[]."1k_blocks"] | add')/1000000" | bc -l)
dskperc=$(echo "scale=2; 100*$dskused/$dsktotal" | bc -l)
cpul=$(top -bn1 | grep '^%Cpu' | cut -c 9- | xargs | awk '{printf("%.1f%%"), $1 + $3}')
lastboot=$(date -d "`cut -f1 -d. /proc/uptime` seconds ago")
tcpcon=$(netstat -an | grep ESTABLISHED | wc -l)
logged=$(LANG=C who | jc --who| jq '[.[].user] | length')
loggeduniq=$(LANG=C who | jc --who | jq '[.[].user] | unique | length')
ifconfigpars=$(LANG=C /usr/sbin/ifconfig | jc --ifconfig)
ipv4=$(echo $ifconfigpars | jq -r '[.[] | select(.type=="Ethernet")][].ipv4_addr')
macadr=$(echo $ifconfigpars | jq -r '[.[] | select(.type=="Ethernet")][].mac_addr')
nbsudo=$(journalctl -q _COMM=sudo | grep COMMAND | wc -l)
# A way to show if LVM is used or not
if grep -q '^/dev/mapper' /etc/fstab; then
lvmused="\e[32myes\e[39m"
else
lvmused="\e[31mno\e[39m"
fi
# The colors to pimp the output
cs='\e[1m\e[92m#'
ce=':\e[39m\e[0m'
output="
${cs}Architecture$ce|$arch
${cs}CPU physical${ce}|$pcpu
${cs}vCPU${ce}|$vcpu
${cs}Memory Usage${ce}|$memfree/$memtotal MB ($memperc%)
${cs}Disk Usage${ce}|$dskused/$dsktotal GB ($dskperc%)
${cs}CPU load${ce}|$cpul
${cs}Last boot${ce}|$lastboot
${cs}LVM used${ce}|$lvmused
${cs}TCP connections${ce}|$tcpcon ESTABLISHED
${cs}Users logged in${ce}|$loggeduniq ($logged sessions)
${cs}Network${ce}|IP $ipv4 ($macadr)
${cs}Sudo${ce}|$nbsudo commands executed
"
# Columns aligns the the different parts of your output
echo -e "$output" | column -t -s "|"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment