Skip to content

Commit fa0fbff

Browse files
committed
metrics.sh: Add docker support
Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com>
1 parent 0e69432 commit fa0fbff

3 files changed

Lines changed: 222 additions & 91 deletions

File tree

bin/ncp/SYSTEM/metrics.sh

Lines changed: 181 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#!/bin/bash
22

3+
is_supported() {
4+
[[ "$DOCKERBUILD" == 1 ]] && [[ "$(lsb_release -r)" =~ .*10 ]] && return 1
5+
return 0
6+
}
7+
38
is_active() {
4-
systemctl is-active -q prometheus-node-exporter || return 1
9+
is_supported || return 1
10+
11+
metrics_services status > /dev/null 2>&1 || return 1
12+
# systemctl is-active -q prometheus-node-exporter || return 1
513
return 0
614
}
715

@@ -13,28 +21,153 @@ tmpl_metrics_enabled() {
1321
)
1422
}
1523

24+
reload_metrics_config() {
25+
is_supported || return 0
26+
27+
install_template ncp-metrics.cfg.sh "/usr/local/etc/ncp-metrics.cfg" || {
28+
echo "ERROR while generating ncp-metrics.conf!"
29+
return 1
30+
}
31+
service ncp-metrics-exporter status > /dev/null && {
32+
service ncp-metrics-exporter restart
33+
service ncp-metrics-exporter status > /dev/null 2>&1 || {
34+
rc=$?
35+
echo -e "WARNING: An error ncp-metrics exporter failed to start (exit-code $rc)!"
36+
return 1
37+
}
38+
}
39+
}
40+
41+
metrics_services() {
42+
cmd="${1?}"
43+
44+
if [[ "$cmd" =~ (start|stop|restart|reload|status) ]]
45+
then
46+
rc1=0
47+
rc2=0
48+
service prometheus-node-exporter "$cmd" || rc1=$?
49+
service ncp-metrics-exporter "$cmd" || rc2=$?
50+
[[ $rc1 > $rc2 ]] && return $rc1
51+
return $rc2
52+
fi
53+
54+
if ! [[ "$cmd" =~ (en|dis)able ]]
55+
then
56+
echo -e "ERROR: Invalid command: metrics_services ${cmd}!"
57+
exit 1
58+
fi
59+
60+
if [[ "$DOCKERBUILD" == 1 ]]
61+
then
62+
update-rc.d ncp-metrics "$cmd"
63+
return $?
64+
else
65+
systemctl "$cmd" prometheus-node-exporter ncp-metrics-exporter
66+
return $?
67+
fi
68+
}
69+
1670
install() {
1771

1872
# Subshell to return on failure instead of exiting (due to set -e)
1973
(
2074

2175
set -e
76+
77+
is_supported || {
78+
echo -e "Metrics app is not supported in debian 10 docker containers. Installation will be skipped."
79+
return 0
80+
}
81+
2282
cat > /etc/default/prometheus-node-exporter <<'EOF'
2383
ARGS="--collector.filesystem.ignored-mount-points=\"^/(dev|proc|run|sys|mnt|var/log|var/lib/docker)($|/)\""
2484
EOF
25-
apt_install prometheus-node-exporter
2685

27-
# TODO: Docker support?
28-
systemctl disable prometheus-node-exporter
29-
service prometheus-node-exporter stop
30-
31-
[[ "$(uname -m)" =~ ("arm"|"aarch").* ]] && arch="armv7" || arch="i686"
32-
[[ "$arch" == "i686" ]] && apt_install lib32gcc-s1 libc6-i386
86+
arch="$(uname -m)"
87+
[[ "$arch" =~ ("arm"|"aarch").* ]] && bin_arch="armv7" || bin_arch="i686"
88+
[[ "$(lsb_release -r)" =~ .*10 ]] && gcclib="lib32gcc1" || gcclib="lib32gcc-s1"
89+
[[ "$arch" == "x86_64" ]] && apt_install "$gcclib" libc6-i386
3390

3491
wget -O "/usr/local/bin/ncp-metrics-exporter" \
35-
"https://github.com/theCalcaholic/ncp-metrics-exporter/releases/download/v1.0.0/${arch}-ncp-metrics-exporter"
92+
"https://github.com/theCalcaholic/ncp-metrics-exporter/releases/download/v1.0.0/${bin_arch}-ncp-metrics-exporter"
3693
chmod +x /usr/local/bin/ncp-metrics-exporter
37-
cat <<EOF > /etc/systemd/system/ncp-metrics-exporter.service
94+
95+
# Apply fix to init-d-script (https://salsa.debian.org/debian/sysvinit/-/commit/aa40516c)
96+
# Otherwise the init.d scripts of prometheus-node-exporter won't work
97+
# shellcheck disable=SC2016
98+
sed -i 's|status_of_proc "$DAEMON" "$NAME" ${PIDFILE:="-p ${PIDFILE}"}|status_of_proc ${PIDFILE:+-p "$PIDFILE"} "$DAEMON" "$NAME"|' /lib/init/init-d-script
99+
100+
if [[ "$DOCKERBUILD" == 1 ]]
101+
then
102+
# during installation of prometheus-node-exporter `useradd` is used to create a user.
103+
# However, `useradd` doesn't the symlink in /etc/shadow, so we need to temporarily move it back
104+
trap "mv /etc/shadow /data/etc/shadow; ln -s /data/etc/shadow /etc/shadow" EXIT
105+
rm /etc/shadow
106+
cp /data/etc/shadow /etc/shadow
107+
apt_install prometheus-node-exporter prometheus-node-exporter-collectors
108+
mv /etc/shadow /data/etc/shadow
109+
ln -s /data/etc/shadow /etc/shadow
110+
trap - EXIT
111+
else
112+
apt_install prometheus-node-exporter prometheus-node-exporter-collectors
113+
fi
114+
115+
if [[ "$DOCKERBUILD" == 1 ]]
116+
then
117+
cat > /etc/init.d/ncp-metrics-exporter <<'EOF'
118+
#!/bin/sh
119+
# Generated by sysd2v v0.3 -- http://www.trek.eu.org/devel/sysd2v
120+
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
121+
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
122+
set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
123+
fi
124+
### BEGIN INIT INFO
125+
# Provides: ncp-metrics-exporter
126+
# Required-Start: $remote_fs
127+
# Required-Stop: $remote_fs
128+
# Default-Start: 2 3 4 5
129+
# Default-Stop: 0 1 6
130+
# Description: NCP Metrics Exporter
131+
### END INIT INFO
132+
set -a
133+
NCP_CONFIG_DIR=/usr/local/etc
134+
set +a
135+
136+
NAME=ncp-exporter
137+
DAEMON=/usr/local/bin/ncp-metrics-exporter
138+
PIDFILE=/var/run/ncp-metrics-exporter.pid
139+
LOGFILE=/var/log/ncp-metrics.log
140+
START_ARGS="--background --make-pidfile"
141+
EOF
142+
chmod +x /etc/init.d/ncp-metrics-exporter
143+
144+
cat > /etc/services-available.d/101ncp-metrics <<EOF
145+
#!/bin/bash
146+
147+
source /usr/local/etc/library.sh
148+
[[ "\$1" == "stop" ]] && {
149+
echo "stopping prometheus-node-exporter..."
150+
service prometheus-node-exporter stop
151+
echo "done."
152+
echo "stopping ncp-metrics-exporter"
153+
service ncp-metrics-exporter stop
154+
echo "done."
155+
exit 0
156+
}
157+
158+
persistent_cfg /etc/default/prometheus-node-exporter
159+
160+
echo "starting prometheus-node-exporter..."
161+
service prometheus-node-exporter start
162+
[[ -n "\$(pgrep prometheus)" ]] || echo -e "ERROR: prometheus-node-exporter failed to start!"
163+
echo "starting ncp-metrics-exporter
164+
service ncp-metrics-exporter start
165+
EOF
166+
chmod +x /etc/services-available.d/101ncp-metrics
167+
168+
else
169+
170+
cat <<EOF > /etc/systemd/system/ncp-metrics-exporter.service
38171
[Unit]
39172
Description=NCP Metrics Exporter
40173
@@ -49,81 +182,79 @@ RestartSec=30
49182
WantedBy=multi-user.target
50183
EOF
51184

52-
systemctl daemon-reload
185+
systemctl daemon-reload
53186

54-
)
55-
}
187+
fi
56188

57-
reload_metrics_config() {
58-
install_template ncp-metrics.cfg.sh "/usr/local/etc/ncp-metrics.cfg" || {
59-
echo "ERROR while generating ncp-metrics.conf!"
60-
return 1
61-
}
62-
service ncp-metrics-exporter status > /dev/null && {
63-
service ncp-metrics-exporter restart
64-
service ncp-metrics-exporter status > /dev/null 2>&1 || {
65-
rc=$?
66-
echo -e "WARNING: An error ncp-metrics exporter failed to start (exit-code $rc)!"
67-
return 1
68-
}
69-
}
189+
metrics_services stop
190+
metrics_services disable
191+
192+
)
70193
}
71194

72195
configure() {
73196

74197
if [[ "$ACTIVE" != yes ]]
75198
then
76-
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf
77-
78-
systemctl disable prometheus-node-exporter
79-
service prometheus-node-exporter stop
80199

81-
systemctl disable ncp-metrics-exporter
82-
service ncp-metrics-exporter stop
200+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf || {
201+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf --allow-fallback
202+
echo -e "ERROR while generating nextcloud.conf! Exiting..."
203+
return 1
204+
}
205+
echo "Disabling and stopping services..."
206+
metrics_services disable
207+
metrics_services stop
208+
echo "done."
83209
else
210+
211+
is_supported || {
212+
echo -e "Metrics app is not supported in debian 10 docker containers. Terminating..."
213+
return 0
214+
}
215+
84216
[[ -n "$USER" ]] || {
85-
echo "ERROR: User can not be empty!" >&2
217+
echo -e "ERROR: User can not be empty!" >&2
86218
return 1
87219
}
88220

89221
[[ -n "$PASSWORD" ]] || {
90-
echo "ERROR: Password can not be empty!" >&2
222+
echo -e "ERROR: Password can not be empty!" >&2
91223
return 1
92224
}
93225

94226
[[ ${#PASSWORD} -ge 10 ]] || {
95-
echo "ERROR: Password must be at least 10 characters long!" >&2
227+
echo -e "ERROR: Password must be at least 10 characters long!" >&2
96228
return 1
97229
}
98230

99231
local htpasswd_file="/usr/local/etc/metrics.htpasswd"
100232
rm -f "${htpasswd_file}"
101233
echo "$PASSWORD" | htpasswd -ciB "${htpasswd_file}" "$USER"
102234

103-
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf || {
104-
echo "ERROR while generating nextcloud.conf! Exiting..."
105-
return 1
106-
}
107235
echo "Generate config..."
108236
reload_metrics_config
109237
echo "done."
110238

111-
echo "Starting prometheus node exporter..."
112-
systemctl enable prometheus-node-exporter
113-
service prometheus-node-exporter start
114-
service prometheus-node-exporter status
239+
echo "Enabling and starting services..."
240+
metrics_services enable
241+
metrics_services start
242+
metrics_services status || {
243+
echo -e "ERROR: Metrics services not running!"
244+
return 1
245+
}
115246
echo "done."
116247

117-
echo "Starting ncp metrics exporter..."
118-
systemctl enable ncp-metrics-exporter
119-
service ncp-metrics-exporter start
120-
service ncp-metrics-exporter status
121-
echo "done."
248+
249+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf || {
250+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf --allow-fallback
251+
echo -e "ERROR while generating nextcloud.conf! Exiting..."
252+
return 1
253+
}
122254

123255
echo "Metrics endpoint enabled. You can test it at https://nextcloudpi.local/metrics/system (or under your NC domain under the same path)"
124256
fi
125-
echo "Apache Test:"
126-
apache2ctl -t
257+
127258
bash -c "sleep 2 && service apache2 reload" &>/dev/null &
128259

129260
}

etc/ncp-templates/ncp-metrics.cfg.sh

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,6 @@ cat <<EOF
1919
"backups": [
2020
EOF
2121

22-
declare -a BKP_DIRS
23-
24-
25-
DATADIR=$( ncc config:system:get datadirectory ) || {
26-
echo -e "ERROR: Could not get data directory. Is NextCloud running?";
27-
return 1;
28-
}
29-
NC_SNAPSHOTS_DIR="$(dirname "$DATADIR")/ncp-snapshots"
30-
31-
NC_SNAPSHOTS_SYNC_DIR="$(
32-
source "${BINDIR}/BACKUPS/nc-snapshot-sync.sh"
33-
if tmpl_is_destination_local
34-
then
35-
tmpl_get_destination
36-
fi
37-
)"
38-
39-
SNAP_PATTERN=".*_(?P<year>\\\\d+)-(?P<month>\\\\d+)-(?P<day>\\\\d+)_(?P<hour>\\\\d{2})(?P<minute>\\\\d{2})(?P<second>\\\\d{2})"
40-
cat <<EOF
41-
{
42-
"path": "${NC_SNAPSHOTS_DIR}",
43-
"pattern": "${SNAP_PATTERN}"
44-
}
45-
EOF
46-
47-
[[ -z "$NC_SNAPSHOTS_SYNC_DIR" ]] || {
48-
cat <<EOF
49-
,{
50-
"path": "${NC_SNAPSHOTS_SYNC_DIR}",
51-
"pattern": "${SNAP_PATTERN}"
52-
}
53-
EOF
54-
}
55-
5622
NC_BACKUP_DIR="$(
5723
source "${BINDIR}/BACKUPS/nc-backup.sh"
5824
tmpl_get_destination
@@ -67,16 +33,51 @@ then
6733
NC_BACKUP_AUTO_DIR=""
6834
fi
6935

70-
for BKP_DIR in "$NC_BACKUP_DIR" "$NC_BACKUP_AUTO_DIR"
71-
do
72-
[[ -n "$BKP_DIR" ]] || continue
36+
NC_BACKUP_PATTERN="nextcloud-bkp_(?P<year>\\\\d{4})(?P<month>\\\\d{2})(?P<day>\\\\d{2})_.*\\\\.tar(\\\\.gz)?"
37+
38+
cat <<EOF
39+
{
40+
"path": "$NC_BACKUP_DIR",
41+
"pattern": "$NC_BACKUP_PATTERN"
42+
}
43+
EOF
44+
[[ -z "$NC_BACKUP_AUTO_DIR" ]] || {
7345
cat <<EOF
7446
,{
75-
"path": "$BKP_DIR",
76-
"pattern": "nextcloud-bkp_(?P<year>\\\\d{4})(?P<month>\\\\d{2})(?P<day>\\\\d{2})_.*\\\\.tar(\\\\.gz)?"
47+
"path": "$NC_BACKUP_AUTO_DIR",
48+
"pattern": "$NC_BACKUP_PATTERN"
49+
}
50+
EOF
51+
}
52+
53+
[[ "$DOCKERBUILD" == 1 ]] || {
54+
55+
DATADIR=$( ncc config:system:get datadirectory ) || {
56+
echo -e "ERROR: Could not get data directory. Is NextCloud running?";
57+
return 1;
58+
}
59+
NC_SNAPSHOTS_DIR="$(dirname "$DATADIR")/ncp-snapshots"
60+
61+
NC_SNAPSHOTS_SYNC_DIR="$(
62+
source "${BINDIR}/BACKUPS/nc-snapshot-sync.sh"
63+
if tmpl_is_destination_local
64+
then
65+
tmpl_get_destination
66+
fi
67+
)"
68+
69+
for snap_dir in "$NC_SNAPSHOTS_DIR" "$NC_SNAPSHOTS_SYNC_DIR"
70+
do
71+
[[ -n "$snap_dir" ]] || continue
72+
cat <<EOF
73+
,{
74+
"path": "${snap_dir}",
75+
"pattern": ".*_(?P<year>\\\\d+)-(?P<month>\\\\d+)-(?P<day>\\\\d+)_(?P<hour>\\\\d{2})(?P<minute>\\\\d{2})(?P<second>\\\\d{2})"
7776
}
7877
EOF
79-
done
78+
done
79+
80+
}
8081

8182
cat <<EOF
8283
]

0 commit comments

Comments
 (0)