Browse Source

init

develop
Simon Vieille 5 months ago
commit
95c1295818
Signed by: Simon Vieille <simon@deblan.fr> GPG Key ID: 03383D15A1D31745

+ 6
- 0
helpers/apt View File

@@ -0,0 +1,6 @@
#/bin/sh

get_updates() {
LANG=en apt-get --just-print upgrade | grep "not upgraded" | grep -Eo '[0-9]{1,4}' | tr ' ' '\n' | awk '{sum+=$0}END{print sum}'
}


+ 5
- 0
helpers/date View File

@@ -0,0 +1,5 @@
#!/bin/sh

get_date() {
LANG=fr_FR.UTF-8 date
}

+ 5
- 0
helpers/df View File

@@ -0,0 +1,5 @@
#!/bin/sh

get_df() {
df -h
}

+ 5
- 0
helpers/hostname View File

@@ -0,0 +1,5 @@
#!/bin/sh

get_hostname() {
hostname
}

+ 5
- 0
helpers/mount View File

@@ -0,0 +1,5 @@
#!/bin/sh

get_mount() {
mount
}

+ 6
- 0
helpers/smart View File

@@ -0,0 +1,6 @@
#!/bin/sh

get_smart() {
smartctl -H "$1"
}


+ 5
- 0
helpers/template View File

@@ -0,0 +1,5 @@
#!/bin/sh

render_template() {
envsubst < "$TEMPLATE/$1.tpl"
}

+ 5
- 0
helpers/uptime View File

@@ -0,0 +1,5 @@
#!/bin/sh

get_uptime() {
uptime | sed 's/^\s//'
}

+ 1
- 0
hooks/on-finish View File

@@ -0,0 +1 @@
#!/bin/sh

+ 11
- 0
hooks/on-start View File

@@ -0,0 +1,11 @@
#!/bin/sh

if ! apt-get update >/dev/null 2>/dev/null; then
printf "La mise à jour de la liste des paquets à échoué.\n"
fi

if ! which envsubst 2>/dev/null >/dev/null; then
printf "Le programme \`envsubst\` est nécessaire. Installer \`gettext-base\`.\n"

exit 1
fi

+ 45
- 0
report View File

@@ -0,0 +1,45 @@
#!/bin/sh

PATH=/usr/sbin:/usr/bin:/sbin:/bin

export _SCRIPT_NAME="$(realpath "$0")"
export _SCRIPT_PATH="$(dirname "$_SCRIPT_NAME")"

# export EMAIL_RECIPIENT=you@example.com
# export EMAIL_SENDER=system@example.com
# export EMAIL_SUBJECT="[$(hostname)] Rapport journalier"

# export HTML_OUTPUT=/tmp/report.html
# export MUNIN_DIRECTORY=/var/cache/munin/www/localhost/localhost
# export SMART_DEVICES="/dev/sda /dev/sdb /dev/sdc"

if [ -z "$TEMPLATE" ]; then
export TEMPLATE="$_SCRIPT_PATH/templates/deblan"
fi

cd "$_SCRIPT_PATH"

./hooks/on-start

REPORT="$(
./scripts/header
./scripts/system
./scripts/mount
./scripts/df
./scripts/smart
./scripts/footer
)"

if [ -n "$HTML_OUTPUT" ]; then
export REPORT

./scripts/html
fi

if [ -n "$EMAIL_RECIPIENT" ]; then
export REPORT

./scripts/email
fi

./hooks/on-finish

+ 8
- 0
scripts/df View File

@@ -0,0 +1,8 @@
#!/bin/sh

. ./helpers/template
. ./helpers/df

export df="$(get_df)"

render_template df

+ 22
- 0
scripts/email View File

@@ -0,0 +1,22 @@
#!/bin/sh

. ./helpers/template

export BOUNDARY="-$(date | md5sum | cut -d ' ' -f 1)"

(
render_template mail

if [ -n "$MUNIN_DIRECTORY" -a -d "$MUNIN_DIRECTORY" ]; then
cd "$MUNIN_DIRECTORY"

for file in *day.png; do
export file
export base64="$(base64 "$file")"

render_template mail-png-attachment
done
fi

printf "\n--%s--\n" "$BOUNDARY"
)| sendmail "$EMAIL_RECIPIENT"

+ 5
- 0
scripts/footer View File

@@ -0,0 +1,5 @@
#!/bin/sh

. ./helpers/template

render_template footer

+ 10
- 0
scripts/header View File

@@ -0,0 +1,10 @@
#!/bin/sh

. ./helpers/template
. ./helpers/hostname
. ./helpers/date

export hostname="$(get_hostname)"
export date="$(get_date)"

render_template header

+ 7
- 0
scripts/html View File

@@ -0,0 +1,7 @@
#!/bin/sh

. ./helpers/template

export content="$REPORT"

render_template plain > "$HTML_OUTPUT"

+ 8
- 0
scripts/mount View File

@@ -0,0 +1,8 @@
#!/bin/sh

. ./helpers/template
. ./helpers/mount

export mount="$(get_mount)"

render_template mount

+ 12
- 0
scripts/smart View File

@@ -0,0 +1,12 @@
#!/bin/sh

. ./helpers/template
. ./helpers/smart

for device in $SMART_DEVICES; do
export device
export result="$(get_smart "$device")"

render_template smart
done


+ 22
- 0
scripts/system View File

@@ -0,0 +1,22 @@
#!/bin/sh

. ./helpers/template
. ./helpers/uptime
. ./helpers/apt

updates=$(get_updates)

export uptime="$(get_uptime)"

if [ $updates -gt 1 ]; then
export apt_color="#ff5e1e"
export apt_text="$(printf "%d paquets à mettre à jour" $updates)"
elif [ $updates -eq 1 ]; then
export apt_color="#ff5e1e"
export apt_text="$(printf "%d paquet à mettre à jour" $updates)"
else
export apt_color="#77c81b"
export apt_text="Système à jour"
fi

render_template system

+ 7
- 0
templates/deblan/df.tpl View File

@@ -0,0 +1,7 @@
<div class="title">
Espace(s) disque(s)
</div>

<div class="content">
<pre>${df}</pre>
</div>

+ 3
- 0
templates/deblan/footer.tpl View File

@@ -0,0 +1,3 @@
</body>
</html>


+ 62
- 0
templates/deblan/header.tpl View File

@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
html {
margin: 0;
padding: 0;
background: #191e2b;
}

body {
margin: 0;
padding: 0;
font-family: Calibri;
background: #191e2b;
}

ul {
padding: 10px;
margin: 0;
}

li {
padding: 0;
margin: 0 0 0 15px;
list-style: none;
color: #fff;
}

.title {
background: #283044;
color: #fff;
padding: 15px;
margin: 0;
font-size: 24px;
}

.content {
padding: 10px;
background: #191e2b;
color: #fff;
}
</style>
</head>
<body>
<div style="padding: 10px; background: #3b4664;">
<table width="100%">
<tr>
<td valign="middle" width="245">
<img src="https://upload.deblan.org/u/2019-05/5cefbc0a.png">
</td>
<td width="10"></td>
<td valign="middle" style="font-size: 22px">
<ul>
<li><strong>${hostname}</strong></li>
<li>${date}</li>
</ul>
</td>
</tr>
</table>
</div>

+ 9
- 0
templates/deblan/mail-png-attachment.tpl View File

@@ -0,0 +1,9 @@
--${BOUNDARY}
Content-type: image/png;
name="${file}"
Content-Transfer-Encoding: base64
Content-ID: <${file}>
Content-Disposition: attachement;
filename="${file}"

${base64}

+ 10
- 0
templates/deblan/mail.tpl View File

@@ -0,0 +1,10 @@
From: ${EMAIL_SENDER}
To: ${EMAIL_RECIPIENT}
Subject: ${EMAIL_RECIPIENT}
Content-type: multipart/mixed;
boundary="${BOUNDARY}"
--$BOUNDARY
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

${REPORT}

+ 7
- 0
templates/deblan/mount.tpl View File

@@ -0,0 +1,7 @@
<div class="title">
Point(s) de montage(s)
</div>

<div class="content">
<pre>${mount}</pre>
</div>

+ 1
- 0
templates/deblan/plain.tpl View File

@@ -0,0 +1 @@
${content}

+ 7
- 0
templates/deblan/smart.tpl View File

@@ -0,0 +1,7 @@
<div class="title">
Smart ${device}
</div>

<div class="content">
<pre>${result}</pre>
</div>

+ 9
- 0
templates/deblan/system.tpl View File

@@ -0,0 +1,9 @@
<div class="title">
Système
</div>

<div class="content">
<span style="color: ${apt_color}; font-weight:bold">${apt_text}</span>

<pre>${uptime}</pre>
</div>

Loading…
Cancel
Save