Skip to content
mailboot.in 2.4 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/bin/bash

# This file is part of MAILBOOT.
#
# MAILBOOT is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MAILBOOT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with MAILBOOT.  If not, see <http://www.gnu.org/licenses/>.

VERSION=@VERSION@

# default settings
declare -a targets=('root')
declare -i systemd_analyze_retry=10
declare -i systemd_analyze_interval=5

# source config file if exists
[[ -r @sysconfdir@/mailboot.conf ]] && source @sysconfdir@/mailboot.conf

Seblu's avatar
Seblu committed
usage() {
    echo "usage: ${0##*/} [options]" >&2
    echo 'options:' >&2
    echo "   -s: send mail" >&2
    echo "   -p: print mail to stdout" >&2
Seblu's avatar
Seblu committed
    echo "   -h: this help" >&2
    echo "   -v: print mailboot version" >&2
    exit ${1:-1}
}

print_version() {
    echo $VERSION
}

# systemd-analyse detects if systemd has finished to boot up
# we try during 60s to have a valid answer.
print_systemd-analyze() {
    declare -i count=0
    while (($count <= $systemd_analyze_retry)); do
        if systemd-analyze &>/dev/null; then
            echo
            systemd-analyze
            echo
            return
        fi
        count+=1
        sleep $systemd_analyze_interval
    done
}

Seblu's avatar
Seblu committed
print_mail() {
    source /etc/os-release

    printf 'To: '
    for target in "${targets[@]}";do
        printf "\"$target\" "
    done | sed -e 's/ $/\n/' -e 's/ /, /g'
Seblu's avatar
Seblu committed
    echo "Subject: [MAILBOOT] $(hostname -f)"
    echo
    echo "Operating system is $NAME"
    echo "Kernel is $(uname -s) $(uname -r) on $(uname -m)"
    echo
Seblu's avatar
Seblu committed
    echo "System is up since $(date -d "$(uptime -s)" -R)"
    print_systemd-analyze
Seblu's avatar
Seblu committed
    echo 'Last:'
    last -5 -FRwx|head -n 5
Seblu's avatar
Seblu committed
    echo
    printf 'Generated '
    date -R
Seblu's avatar
Seblu committed
}

send_mail() {
    print_mail | sed 's/$/\r/' | sendmail "${targets[@]}"
Seblu's avatar
Seblu committed
}

while getopts 'psvh' opt; do
    case $opt in
        s) send_mail;;
        p) print_mail;;
        v) print_version;;
        h) usage 0;;
        *) usage;;
    esac
done

(( $OPTIND != 1 )) || usage

Seblu's avatar
Seblu committed
# vim:set ts=4 sw=4 ft=sh ai et: