Browse Source

init

develop
Simon Vieille 2 years ago
commit
95c1295818
Signed by: deblan GPG Key ID: 03383D15A1D31745
  1. 6
      helpers/apt
  2. 5
      helpers/date
  3. 5
      helpers/df
  4. 5
      helpers/hostname
  5. 5
      helpers/mount
  6. 6
      helpers/smart
  7. 5
      helpers/template
  8. 5
      helpers/uptime
  9. 1
      hooks/on-finish
  10. 11
      hooks/on-start
  11. 45
      report
  12. 8
      scripts/df
  13. 22
      scripts/email
  14. 5
      scripts/footer
  15. 10
      scripts/header
  16. 7
      scripts/html
  17. 8
      scripts/mount
  18. 12
      scripts/smart
  19. 22
      scripts/system
  20. 7
      templates/deblan/df.tpl
  21. 3
      templates/deblan/footer.tpl
  22. 62
      templates/deblan/header.tpl
  23. 9
      templates/deblan/mail-png-attachment.tpl
  24. 10
      templates/deblan/mail.tpl
  25. 7
      templates/deblan/mount.tpl
  26. 1
      templates/deblan/plain.tpl
  27. 7
      templates/deblan/smart.tpl
  28. 9
      templates/deblan/system.tpl

6
helpers/apt

@ -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
helpers/date

@ -0,0 +1,5 @@
#!/bin/sh
get_date() {
LANG=fr_FR.UTF-8 date
}

5
helpers/df

@ -0,0 +1,5 @@
#!/bin/sh
get_df() {
df -h
}

5
helpers/hostname

@ -0,0 +1,5 @@
#!/bin/sh
get_hostname() {
hostname
}

5
helpers/mount

@ -0,0 +1,5 @@
#!/bin/sh
get_mount() {
mount
}

6
helpers/smart

@ -0,0 +1,6 @@
#!/bin/sh
get_smart() {
smartctl -H "$1"
}

5
helpers/template

@ -0,0 +1,5 @@
#!/bin/sh
render_template() {
envsubst < "$TEMPLATE/$1.tpl"
}

5
helpers/uptime

@ -0,0 +1,5 @@
#!/bin/sh
get_uptime() {
uptime | sed 's/^\s//'
}

1
hooks/on-finish

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

11
hooks/on-start

@ -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
report

@ -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
scripts/df

@ -0,0 +1,8 @@
#!/bin/sh
. ./helpers/template
. ./helpers/df
export df="$(get_df)"
render_template df

22
scripts/email

@ -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
scripts/footer

@ -0,0 +1,5 @@
#!/bin/sh
. ./helpers/template
render_template footer

10
scripts/header

@ -0,0 +1,10 @@
#!/bin/sh
. ./helpers/template
. ./helpers/hostname
. ./helpers/date
export hostname="$(get_hostname)"
export date="$(get_date)"
render_template header

7
scripts/html

@ -0,0 +1,7 @@
#!/bin/sh
. ./helpers/template
export content="$REPORT"
render_template plain > "$HTML_OUTPUT"

8
scripts/mount

@ -0,0 +1,8 @@
#!/bin/sh
. ./helpers/template
. ./helpers/mount
export mount="$(get_mount)"
render_template mount

12
scripts/smart

@ -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
scripts/system

@ -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
templates/deblan/df.tpl

@ -0,0 +1,7 @@
<div class="title">
Espace(s) disque(s)
</div>
<div class="content">
<pre>${df}</pre>
</div>

3
templates/deblan/footer.tpl

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

62
templates/deblan/header.tpl

@ -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
templates/deblan/mail-png-attachment.tpl

@ -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
templates/deblan/mail.tpl

@ -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
templates/deblan/mount.tpl

@ -0,0 +1,7 @@
<div class="title">
Point(s) de montage(s)
</div>
<div class="content">
<pre>${mount}</pre>
</div>

1
templates/deblan/plain.tpl

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

7
templates/deblan/smart.tpl

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

9
templates/deblan/system.tpl

@ -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