MOTDfetch/motdfetch.d/02-services

144 lines
4.6 KiB
Plaintext
Raw Normal View History

2023-02-07 10:10:46 +01:00
#!/usr/bin/env bash
#
# check status of a list of systemd service units
# + accept service name with or without extension (.service)
# + default to system wide services but accept --user services with the :uid
# + alarm by bullet coloration and blinking
#
# requirements: systemd
# GENERAL ###########################################################
# locale env
unset LC_ALL
export LC_MESSAGES=C
# check if module was disabled
module_disable=${module_services_disable:=0}
if (($module_disable == 1)); then
exit 1
fi
# colors
c_txt=${c_txt:="39"}
c_txt_emphase=${c_txt_emphase:="35"}
c_txt_deco=${c_txt_deco:="97"}
c_txt_invert=${c_txt_invert:="30"}
c_bg=${c_bg_sec:="47"}
c_danger=${c_danger:="31"}
c_warning=${c_warning:="33"}
c_success=${c_success:="32"}
c_title=${c_title:="${c_bg};1;${c_txt_invert}m"}
# OPTIONS ###########################################################
# array of services
if [[ $module_services ]]; then
services=(${module_services[@]})
else
services=("cron.service" "dbus.service:1000" "user2service:1002")
fi
IFS=$'\n' services=($(sort <<<"${services[*]}"))
unset IFS
# column max-width
width=${module_services_width:="80"}
# PREPARATIONS ######################################################
# get status
# + dim status if the service does not exist or if the user is not root and the service has another uid
serviceStatus=()
for service in "${services[@]}"; do
uid=${service/*:/}
# script executed as root and the service to check is from a user
if [[ $service = *:* ]] && [[ "$(id -u)" == "0" ]]; then
service=${service/.service/}
service=${service/:[0-9]*/}
serviceexist=$(machinectl -q shell --uid="$uid" .host /usr/bin/systemctl list-units --user -q --plain --no-pager --full --all -t service 2>/dev/null \
| awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' \
| /usr/bin/grep "$service")
if [[ "$serviceexist" ]] ;then
status=$(machinectl -q shell --uid="$uid" .host /usr/bin/systemctl is-active --user "$service")
if [[ $status = inactive* ]]; then
serviceStatus+=("inactive")
elif [[ $status = active* ]]; then
serviceStatus+=("active")
else
serviceStatus+=("hidden")
fi
else
serviceStatus+=("hidden")
fi
# script executed by the same user as the service user
elif [[ $service = *:* ]] && [[ "$(id -u)" == "$uid" ]]; then
service=${service/.service/}
service=${service/:[0-9]*/}
serviceexist=$(/usr/bin/systemctl list-units --user -q --plain --no-pager --full --all -t service 2>/dev/null \
| awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' \
| /usr/bin/grep "$service")
if [[ "$serviceexist" ]] ;then
serviceStatus+=($(/usr/bin/systemctl --user is-active "$service"))
else
serviceStatus+=("hidden")
fi
# script executed by another user than root and than the service user
elif [[ $service = *:* ]]; then
serviceStatus+=("hidden")
# system service
else
service=${service/.service/}
service=${service/:[0-9]*/}
serviceexist=$(/usr/bin/systemctl list-units -q --plain --no-pager --full --all -t service 2>/dev/null \
| awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' \
| /usr/bin/grep "$service")
if [[ "$serviceexist" ]] ;then
serviceStatus+=($(/usr/bin/systemctl is-active "$service"))
else
serviceStatus+=("hidden")
fi
fi
done
# OUTPUT ############################################################
# output header
echo -e "\n\e[${c_title} Services checks \e[0m\n"
# output checklist
line=" "
lineLen=1
for i in "${!serviceStatus[@]}"
do
service=${services[$i]/.service/}
service=${service^}
# Next line and next line length
next=" $service "
nextLen=$((1+${#next}))
# If the current line will exceed the max column with then echo and start a new line
if [[ $((lineLen+nextLen)) -gt $width ]]; then
echo -e "$line"
lineLen=1
line=" "
fi
lineLen=$((lineLen+nextLen))
# Color the next line green if it's active, red if inactive, else orange
if [[ "${serviceStatus[$i]}" == "active" ]]
then
line+=" \e[${c_success}m●\e[0m \e[${c_txt}m$service\e[0m "
elif [[ "${serviceStatus[$i]}" == "inactive" ]]
then
line+=" \e[1;5;${c_danger}m▲\e[0m \e[1;${c_txt}m$service\e[0m "
else
line+=" \e[1;2;${c_warning}m▲\e[0m \e[1;2;${c_txt}m$service\e[0m "
fi
done
# echo what is left
echo -e "$line"