Browse Source

init

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

+ 6
- 0
helpers/apt View File

@@ -0,0 +1,6 @@
1
+#/bin/sh
2
+
3
+get_updates() {
4
+	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
+}
6
+

+ 5
- 0
helpers/date View File

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

+ 5
- 0
helpers/df View File

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

+ 5
- 0
helpers/hostname View File

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

+ 5
- 0
helpers/mount View File

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

+ 6
- 0
helpers/smart View File

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

+ 5
- 0
helpers/template View File

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

+ 5
- 0
helpers/uptime View File

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

+ 1
- 0
hooks/on-finish View File

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

+ 11
- 0
hooks/on-start View File

@@ -0,0 +1,11 @@
1
+#!/bin/sh
2
+
3
+if ! apt-get update >/dev/null 2>/dev/null; then
4
+	printf "La mise à jour de la liste des paquets à échoué.\n"
5
+fi
6
+
7
+if ! which envsubst 2>/dev/null >/dev/null; then
8
+	printf "Le programme \`envsubst\` est nécessaire. Installer \`gettext-base\`.\n"
9
+
10
+	exit 1
11
+fi

+ 45
- 0
report View File

@@ -0,0 +1,45 @@
1
+#!/bin/sh
2
+
3
+PATH=/usr/sbin:/usr/bin:/sbin:/bin
4
+
5
+export _SCRIPT_NAME="$(realpath "$0")"
6
+export _SCRIPT_PATH="$(dirname "$_SCRIPT_NAME")"
7
+
8
+# export EMAIL_RECIPIENT=you@example.com
9
+# export EMAIL_SENDER=system@example.com
10
+# export EMAIL_SUBJECT="[$(hostname)] Rapport journalier"
11
+
12
+# export HTML_OUTPUT=/tmp/report.html
13
+# export MUNIN_DIRECTORY=/var/cache/munin/www/localhost/localhost
14
+# export SMART_DEVICES="/dev/sda /dev/sdb /dev/sdc"
15
+
16
+if [ -z "$TEMPLATE" ]; then
17
+	export TEMPLATE="$_SCRIPT_PATH/templates/deblan"
18
+fi
19
+
20
+cd "$_SCRIPT_PATH"
21
+
22
+./hooks/on-start
23
+
24
+REPORT="$(
25
+	./scripts/header
26
+	./scripts/system
27
+	./scripts/mount
28
+	./scripts/df
29
+	./scripts/smart
30
+	./scripts/footer
31
+)"
32
+
33
+if [ -n "$HTML_OUTPUT" ]; then
34
+	export REPORT
35
+
36
+	./scripts/html
37
+fi
38
+
39
+if [ -n "$EMAIL_RECIPIENT" ]; then
40
+	export REPORT
41
+
42
+	./scripts/email
43
+fi
44
+
45
+./hooks/on-finish

+ 8
- 0
scripts/df View File

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

+ 22
- 0
scripts/email View File

@@ -0,0 +1,22 @@
1
+#!/bin/sh
2
+
3
+. ./helpers/template
4
+
5
+export BOUNDARY="-$(date | md5sum | cut -d ' ' -f 1)"
6
+
7
+(
8
+	render_template mail
9
+
10
+	if [ -n "$MUNIN_DIRECTORY" -a -d "$MUNIN_DIRECTORY" ]; then
11
+		cd "$MUNIN_DIRECTORY"
12
+
13
+		for file in *day.png; do
14
+			export file
15
+			export base64="$(base64 "$file")"
16
+
17
+			render_template mail-png-attachment
18
+		done
19
+	fi
20
+
21
+	printf "\n--%s--\n" "$BOUNDARY"
22
+)| sendmail "$EMAIL_RECIPIENT"

+ 5
- 0
scripts/footer View File

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

+ 10
- 0
scripts/header View File

@@ -0,0 +1,10 @@
1
+#!/bin/sh
2
+
3
+. ./helpers/template
4
+. ./helpers/hostname
5
+. ./helpers/date
6
+
7
+export hostname="$(get_hostname)"
8
+export date="$(get_date)"
9
+
10
+render_template header

+ 7
- 0
scripts/html View File

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

+ 8
- 0
scripts/mount View File

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

+ 12
- 0
scripts/smart View File

@@ -0,0 +1,12 @@
1
+#!/bin/sh
2
+
3
+. ./helpers/template
4
+. ./helpers/smart
5
+
6
+for device in $SMART_DEVICES; do
7
+	export device
8
+	export result="$(get_smart "$device")"
9
+
10
+	render_template smart
11
+done
12
+

+ 22
- 0
scripts/system View File

@@ -0,0 +1,22 @@
1
+#!/bin/sh
2
+
3
+. ./helpers/template
4
+. ./helpers/uptime
5
+. ./helpers/apt
6
+
7
+updates=$(get_updates)
8
+
9
+export uptime="$(get_uptime)"
10
+
11
+if [ $updates -gt 1 ]; then
12
+	export apt_color="#ff5e1e"
13
+	export apt_text="$(printf "%d paquets à mettre à jour" $updates)"
14
+elif [ $updates -eq 1 ]; then
15
+	export apt_color="#ff5e1e"
16
+	export apt_text="$(printf "%d paquet à mettre à jour" $updates)"
17
+else
18
+	export apt_color="#77c81b"
19
+	export apt_text="Système à jour"
20
+fi
21
+
22
+render_template system

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

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

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

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

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

@@ -0,0 +1,62 @@
1
+<!DOCTYPE html>
2
+<html>
3
+	<head>
4
+		<meta charset="UTF-8" />
5
+		<style>
6
+			html {
7
+				margin: 0;
8
+				padding: 0;
9
+				background: #191e2b;
10
+			}
11
+
12
+			body {
13
+				margin: 0;
14
+				padding: 0;
15
+				font-family: Calibri;
16
+				background: #191e2b;
17
+			}
18
+
19
+			ul {
20
+				padding: 10px;
21
+				margin: 0;
22
+			}
23
+
24
+			li {
25
+				padding: 0;
26
+				margin: 0 0 0 15px;
27
+				list-style: none;
28
+				color: #fff;
29
+			}
30
+
31
+			.title {
32
+				background: #283044;
33
+				color: #fff;
34
+				padding: 15px;
35
+				margin: 0;
36
+				font-size: 24px;
37
+			}
38
+
39
+			.content {
40
+				padding: 10px;
41
+				background: #191e2b;
42
+				color: #fff;
43
+			}
44
+		</style>
45
+	</head>	
46
+	<body>
47
+		<div style="padding: 10px; background: #3b4664;">
48
+			<table width="100%">
49
+				<tr>
50
+					<td valign="middle" width="245">
51
+						<img src="https://upload.deblan.org/u/2019-05/5cefbc0a.png">
52
+					</td>
53
+					<td width="10"></td>
54
+					<td valign="middle" style="font-size: 22px">
55
+						<ul>
56
+							<li><strong>${hostname}</strong></li>
57
+							<li>${date}</li>
58
+						</ul>
59
+					</td>
60
+				</tr>
61
+			</table>
62
+		</div>

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

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

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

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

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

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

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

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

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

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

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

@@ -0,0 +1,9 @@
1
+<div class="title">
2
+	Système
3
+</div>
4
+
5
+<div class="content">
6
+	<span style="color: ${apt_color}; font-weight:bold">${apt_text}</span>
7
+
8
+	<pre>${uptime}</pre>
9
+</div>

Loading…
Cancel
Save