Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bderenzo committed Aug 30, 2020
0 parents commit 617ec09
Show file tree
Hide file tree
Showing 16 changed files with 471 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Baptiste DE RENZO

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Dynamic MOTD

## Setup

To install this, with root rights:

* Clone the repo
* Install: `sudo ./install -u <user>`
* Add, configure or remove widgets: `ls ./widgets`
* Run interactively: `sudo ./generate`

## Profit

```
. o ..
o . o o..o. __ __
. ..o.. . | | |_ _ ___ ___ ___
o o | | | | . | -_| _|
__|=|__|=|__ |__|__|_ | _|___|_|
___|____________|____ |___|_|
| o o o o o o o o o /
~ ~ ~~~~'~~~~~~~~~~~~~~~~~~~'~~~~ ~ ~
system info: network:
system load: 0.00 process: 99 public ip: 10.20.30.40
cpu usage: 0% uptime: 10d private ip: 192.168.0.10
memory usage: 10% users: 1
certificates:
services: google.fr valid 01/01/2020
apparmor: active cron: active debian.org valid 01/01/2020
logrotate: inactive lxc: active wikipedia.org valid 01/01/2020
networking: active sshd: active domain.org valid 01/01/2020
containers: disk usage:
git: stopped mailer: stopped Size Used Free Use%
dlna: running plex: stopped / 10.0G 1.0G 9.0G 10%
```
39 changes: 39 additions & 0 deletions generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

# system info: network:
# system load: 0.00 process: 99 public ip: 10.20.30.40
# cpu usage: 0% uptime: 10d private ip: 192.168.1.2
# memory usage: 10% users: 1
# certificates:
# services: google.fr valid 01/01/2020
# apparmor: active cron: active debian.org valid 01/01/2020
# logrotate: inactive lxc: active wikipedia.org valid 01/01/2020
# networking: active sshd: active
# disk usage:
# lxc status: Size Used Free Use%
# git: stopped mailer: stopped / 10.0G 1.0G 9.0G 10%
# dlna: running plex: stopped

# go to script dir
cd "$(dirname "$(readlink -f "$0")")"

# remove old values
if [[ $1 == '-f' ]]; then
rm -f /tmp/.motd.*
fi

# print header
cat headers/*

# get widgets
cd widgets
for m in L*.sh; do
LEFT+="$(./$m)\n"
done
for m in R*.sh; do
RIGHT+="$(./$m)\n"
done

# print widgets
paste <(echo -e "$LEFT") <(echo -e "$RIGHT") -d '@' | sed -e 's,@, @ ,' | column -ts '@'

9 changes: 9 additions & 0 deletions headers/header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

. o ..
o . o o..o.  __ __ 
. ..o.. . | | |_ _ ___ ___ ___
o o | | | | . | -_| _|
__|=|__|=|__ |__|__|_ | _|___|_|
___|____________|____ |___|_|
| o o o o o o o o o /
~ ~ ~~~~'~~~~~~~~~~~~~~~~~~~'~~~~ ~ ~
36 changes: 36 additions & 0 deletions install
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

# go to script dir
cd "$(dirname "$(readlink -f "$0")")"

# parse args
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case "$1" in
-u|--user) shift; user="$1";;
esac
shift
done

# get homedir
home="$(getent passwd "${user}" | cut -d: -f6)"

# motd file
motd='/tmp/.motd'

# add cron rule
cron='/etc/cron.hourly/motd'
cat <<EOF > "${cron}"
#!/usr/bin/env bash
$(pwd)/generate > "${motd}"
EOF
chmod +x "${cron}"

# add print
bashrc="${home}/.bashrc"
cat <<EOF >> "${bashrc}"
# motd supervision
if [[ -f "${motd}" ]]; then
clear && cat "${motd}"
fi
EOF

10 changes: 10 additions & 0 deletions preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# go to local dir
cd "$(dirname "$(readlink -f "$0")")"

# view widgets
echo
cd widgets
paste <(ls L[0-9]*) <(ls R[0-9]*) | column -t | sed 's,^, ,'
echo
58 changes: 58 additions & 0 deletions tools/colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!!/bin/bash

# Reset
reset="\e[0m" # Text Reset

# regular colors
black="\e[0;30m" # black
red="\e[0;31m" # Red
green="\e[0;32m" # Green
yellow="\e[0;33m" # Yellow
blue="\e[0;34m" # blue
purple="\e[0;35m" # Purple
cyan="\e[0;36m" # Cyan
white="\e[0;37m" # White

# bold
bblack="\e[1;30m" # Black
bred="\e[1;31m" # Red
bgreen="\e[1;32m" # Green
byellow="\e[1;33m" # Yellow
bblue="\e[1;34m" # Blue
bpurple="\e[1;35m" # Purple
bcyan="\e[1;36m" # Cyan
bwhite="\e[1;37m" # White

# c_if '2' '<' '5' '%'
c_if(){
if (( ${1%%.*} $2 ${3%%.*} )); then
echo "${bgreen}$1${@:4}${reset}"
else
echo "${bred}$1${@:4}${reset}"
fi
}
# c_if '2' '<' '5' 'r1' 'r2'
c_if_r(){
if (( ${1%%.*} $2 ${3%%.*} )); then
echo "${bgreen}${4}${reset}"
else
echo "${bred}${5}${reset}"
fi
}
# c_match 'abc' 'def' 'pkgs'
c_match(){
if [[ ${1%%.*} = ${2%%.*} ]]; then
echo "${bgreen}$1${@:3}${reset}"
else
echo "${bred}$1${@:3}${reset}"
fi
}

# c_match_r 'abc' 'def' 'r1' 'r2'
c_match_r(){
if [[ ${1%%.*} = ${2%%.*} ]]; then
echo "${bgreen}${3}${reset}"
else
echo "${bred}${4}${reset}"
fi
}
32 changes: 32 additions & 0 deletions tools/cpu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
renice -n 20 -p "$$" &>/dev/null

iter=2
delay=0.5

# 0 1 2 3 4 5 6 7 8 9 10
# user nice system idle iowait irq softirq steal guest guest_nice
# cpu 180288 509 316177 443927540 53229 0 5395 0 0 0

# init
previous_idle=0
previous_total=0
for i in $(seq "${iter}"); do
cpu=( $(grep '^cpu ' /proc/stat) )
unset cpu[0]
# compute idle cpu time
idle=$(( ${cpu[4]} + ${cpu[5]} ))
# compute total cpu time
total=$(( ${cpu[@]/%/+}0 ))
# compute cpu usage since last check
idle_d=$(( idle - previous_idle ))
total_d=$(( total - previous_total ))
cpu_usage=$(( 100*(total_d-idle_d)/total_d ))
# remember cpu total and idle for next check
previous_total="${total}"
previous_idle="${idle}"
# wait before next check
sleep "${delay}"
done
echo "${cpu_usage}" # %

40 changes: 40 additions & 0 deletions widgets/L01-sysinfo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

# system info:
# system load: 0.01 process: 120
# cpu usage: 0% uptime: 30d
# memory usage: 5% users: 1
# temperature: 45°c

mem_warn=50 # %
cpu_warn=20 # %
temp_warn=60 # °c

ps="$(ps -haux | wc -l)"
load="$(cat /proc/loadavg | cut -d' ' -f3)"
users="$(w -h | wc -l)"
up="$(( $(cat /proc/uptime | cut -d'.' -f1) / 3600 / 24 ))"
mem="$(free -b | awk 'FNR == 2 {p=100*$3/$2} END{printf("%0.f",p)}')"
cpunb="$(grep -i '^processor' /proc/cpuinfo | wc -l)"
cpu="$(../tools/cpu.sh)"

source ../tools/colors.sh
#ps=$(c_if $ps '<' '150')
mem=$(c_if "${mem}" '<' "${mem_warn}" '%')
load=$(c_if "${load}" '<' "$cpunb")
cpu=$(c_if "${cpu}" '<' "${cpu_warn}" '%')

out+="system load:|${load}|process:|${ps}\n"
out+="cpu usage:|${cpu}|uptime:|${up}d\n"
out+="memory usage:|${mem}|users:|${users}\n"

if [[ -f '/sys/class/thermal/thermal_zone0/temp' ]]; then
temp="$(( $(cat /sys/class/thermal/thermal_zone0/temp) / 1000 ))"
temp=$(c_if "${temp}" '<' "${temp_warn}" '°c')
out+="|temperature:|${temp}"
fi

echo
echo 'system info:'
echo -e "${out}" | column -ts'|' | sed 's,^, ,'

25 changes: 25 additions & 0 deletions widgets/L02-services.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# services:
# apparmor: active cron: active
# logrotate: inactive lxc: active
# network: active sshd: active

columns=2
services=( 'apparmor' 'cron' 'logrotate.timer' 'sshd' 'networking' 'lxc' 'netfilter-persistent' 'lxcfs')
svc_names=('apparmor' 'cron' 'logrotate' 'sshd' 'network' 'lxc' 'iptables' 'lxcfs')

out=''
source ../tools/colors.sh
for i in "${!services[@]}"; do
svc_status=$(systemctl is-active "${services[${i}]}")
svc_status=$(c_match "${svc_status}" 'active')
out+="${svc_names[$i]}:|${svc_status}|"
(( $(( (i+1) % columns )) == 0 )) && out+='\n'
done
out+='\n'

echo
echo 'services:'
echo -e "${out}" | column -ts '|' | sed -e 's/^/ /'

25 changes: 25 additions & 0 deletions widgets/L03-lxc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# lxc status:
# git: stopped mailer: stopped
# dlna: running plex: stopped

columns=2

# Get containers
mapfile -t containers < <(lxc-ls -f | awk '{ print $1,$2 }' | sed '/^\s*$/d' | tail -n +2)

out=''
source ../tools/colors.sh
for i in "${!containers[@]}"; do
IFS=" " read name status <<< "${containers[$i]}"
status=$(c_match "${status}" 'RUNNING')
out+="${name}:|${status,,}|"
(( $(( (i+1) % columns )) == 0 )) && out+='\n'
done
out+='\n'

echo
echo 'containers:'
echo -e "${out}" | column -ts '|' | sed -e 's/^/ /'

22 changes: 22 additions & 0 deletions widgets/L04-pkgs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# packages:
# 3 new packages avalaible...

date=$(date +%Y%m%d)
tmp=/tmp/.motd.pkgs

[[ -r "${tmp}" ]] && source "${tmp}"
if [[ "${lastupdate}" != "${date}" ]]; then
source ../tools/colors.sh
apt-get -qq update
pkgs=$(apt -qq list --upgradable 2>/dev/null | wc -l)
pkgs=$(c_if_r "${pkgs}" '<' '1' 'no new package' "${pkgs} new packages")
echo "lastupdate=${date}" > "${tmp}"
echo "pkgs='${pkgs}'" >> "${tmp}"
fi

echo
echo 'packages:'
echo -e " ${pkgs} avalaible..."

27 changes: 27 additions & 0 deletions widgets/R01-network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# network:
# public ip; 215.105.10.10
# private ip: 192.168.1.10

date=$(date +%Y%m%d)
tmp=/tmp/.motd.network
domain=bdr.ddns.net

[[ -r "${tmp}" ]] && source "${tmp}"
if [[ "${lastupdate}" != "${date}" ]]; then
privateip=$(hostname -I | cut -d' ' -f1)
publicip=$(dig +short "${domain}")
echo "lastupdate=${date}" > "${tmp}"
echo "publicip=${publicip}" >> "${tmp}"
echo "privateip=${privateip}" >> "${tmp}"
fi

out=''
out+="public ip:|${publicip}\n"
out+="private ip:|${privateip}\n"

echo
echo 'network:'
echo -e "${out}" | column -ts'|' | sed 's,^, ,'

Loading

0 comments on commit 617ec09

Please sign in to comment.