Skip to content

Commit 0e69432

Browse files
committed
metrics.sh: Install and manage ncp-metrics-exporter
Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com>
1 parent 9409c11 commit 0e69432

7 files changed

Lines changed: 213 additions & 6 deletions

File tree

bin/ncp/BACKUPS/nc-backup-auto.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/
88
#
99

10+
tmpl_get_destination() {
11+
(
12+
. /usr/local/etc/library.sh
13+
find_app_param nc-backup-auto DESTDIR
14+
)
15+
}
16+
17+
is_active() {
18+
[[ $ACTIVE == "yes" ]]
19+
}
20+
1021
configure()
1122
{
1223
[[ $ACTIVE != "yes" ]] && {
@@ -44,6 +55,11 @@ EOF
4455
chmod 644 /etc/cron.d/ncp-backup-auto
4556
service cron restart
4657

58+
(
59+
. "${BINDIR}/SYSTEM/metrics.sh"
60+
reload_metrics_config
61+
)
62+
4763
echo "automatic backups enabled"
4864
}
4965

bin/ncp/BACKUPS/nc-backup.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/
88
#
99

10+
tmpl_get_destination() {
11+
(
12+
. /usr/local/etc/library.sh
13+
find_app_param nc-backup DESTDIR
14+
)
15+
}
16+
1017
install()
1118
{
1219
apt-get update
@@ -123,6 +130,10 @@ EOF
123130

124131
configure()
125132
{
133+
(
134+
. "${BINDIR}/SYSTEM/metrics.sh"
135+
reload_metrics_config
136+
)
126137
ncp-backup "$DESTDIR" "$INCLUDEDATA" "$COMPRESS" "$BACKUPLIMIT"
127138
}
128139

bin/ncp/BACKUPS/nc-snapshot-sync.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
#
1010

1111

12+
tmpl_get_destination() {
13+
(
14+
. /usr/local/etc/library.sh
15+
find_app_param nc-snapshot-sync DESTINATION
16+
)
17+
}
18+
19+
tmpl_is_destination_local() {
20+
(
21+
. /usr/local/etc/library.sh
22+
is_active_app nc-snapshot-sync || exit 1
23+
! [[ "$(find_app_param nc-snapshot-sync DESTINATION)" =~ .*"@".*":".* ]]
24+
)
25+
}
26+
27+
is_active() {
28+
[[ $ACTIVE == "yes" ]]
29+
}
30+
1231
install()
1332
{
1433
apt-get update
@@ -46,6 +65,12 @@ configure()
4665
echo "30 4 */${SYNCDAYS} * * root /usr/local/bin/btrfs-sync -qd $ZIP \"$SNAPDIR\" \"$DESTINATION\"" > /etc/cron.d/ncp-snapsync-auto
4766
chmod 644 /etc/cron.d/ncp-snapsync-auto
4867
service cron restart
68+
69+
(
70+
. "${BINDIR}/SYSTEM/metrics.sh"
71+
reload_metrics_config
72+
)
73+
4974
echo "snapshot sync enabled"
5075
}
5176

bin/ncp/CONFIG/nc-datadir.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ configure()
113113
sed -i "s|logpath =.*nextcloud.log|logpath = ${DATADIR}/nextcloud.log|" /etc/fail2ban/jail.local
114114

115115
restore_maintenance_mode
116+
117+
(
118+
. "${BINDIR}/SYSTEM/metrics.sh"
119+
reload_metrics_config
120+
)
116121
}
117122

118123
# License

bin/ncp/SYSTEM/metrics.sh

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,47 @@ EOF
2828
systemctl disable prometheus-node-exporter
2929
service prometheus-node-exporter stop
3030

31+
[[ "$(uname -m)" =~ ("arm"|"aarch").* ]] && arch="armv7" || arch="i686"
32+
[[ "$arch" == "i686" ]] && apt_install lib32gcc-s1 libc6-i386
33+
34+
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"
36+
chmod +x /usr/local/bin/ncp-metrics-exporter
37+
cat <<EOF > /etc/systemd/system/ncp-metrics-exporter.service
38+
[Unit]
39+
Description=NCP Metrics Exporter
40+
41+
[Service]
42+
Environment=NCP_CONFIG_DIR=/usr/local/etc
43+
ExecStart=/usr/local/bin/ncp-metrics-exporter
44+
SyslogIdentifier=ncp-metrics
45+
Restart=on-failure
46+
RestartSec=30
47+
48+
[Install]
49+
WantedBy=multi-user.target
50+
EOF
51+
52+
systemctl daemon-reload
53+
3154
)
3255
}
3356

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+
}
70+
}
71+
3472
configure() {
3573

3674
if [[ "$ACTIVE" != yes ]]
@@ -39,6 +77,9 @@ configure() {
3977

4078
systemctl disable prometheus-node-exporter
4179
service prometheus-node-exporter stop
80+
81+
systemctl disable ncp-metrics-exporter
82+
service ncp-metrics-exporter stop
4283
else
4384
[[ -n "$USER" ]] || {
4485
echo "ERROR: User can not be empty!" >&2
@@ -63,15 +104,26 @@ configure() {
63104
echo "ERROR while generating nextcloud.conf! Exiting..."
64105
return 1
65106
}
107+
echo "Generate config..."
108+
reload_metrics_config
109+
echo "done."
66110

111+
echo "Starting prometheus node exporter..."
67112
systemctl enable prometheus-node-exporter
68113
service prometheus-node-exporter start
114+
service prometheus-node-exporter status
115+
echo "done."
69116

70-
echo "Metric endpoint enabled. You can test it at https://nextcloudpi.local/metrics/system (or under your NC domain under the same path)"
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."
122+
123+
echo "Metrics endpoint enabled. You can test it at https://nextcloudpi.local/metrics/system (or under your NC domain under the same path)"
71124
fi
72125
echo "Apache Test:"
73126
apache2ctl -t
74127
bash -c "sleep 2 && service apache2 reload" &>/dev/null &
75128

76-
77129
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
source /usr/local/etc/library.sh
5+
6+
if [[ "$1" == "--defaults" ]]
7+
then
8+
echo "INFO: Restoring template to default settings" >&2
9+
cat <<EOF
10+
{
11+
"backups": []
12+
}
13+
EOF
14+
exit 0
15+
fi
16+
17+
cat <<EOF
18+
{
19+
"backups": [
20+
EOF
21+
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+
56+
NC_BACKUP_DIR="$(
57+
source "${BINDIR}/BACKUPS/nc-backup.sh"
58+
tmpl_get_destination
59+
)"
60+
61+
NC_BACKUP_AUTO_DIR="$(
62+
source "${BINDIR}/BACKUPS/nc-backup-auto.sh"
63+
tmpl_get_destination
64+
)"
65+
if [[ "$NC_BACKUP_DIR" == "$NC_BACKUP_AUTO_DIR" ]]
66+
then
67+
NC_BACKUP_AUTO_DIR=""
68+
fi
69+
70+
for BKP_DIR in "$NC_BACKUP_DIR" "$NC_BACKUP_AUTO_DIR"
71+
do
72+
[[ -n "$BKP_DIR" ]] || continue
73+
cat <<EOF
74+
,{
75+
"path": "$BKP_DIR",
76+
"pattern": "nextcloud-bkp_(?P<year>\\\\d{4})(?P<month>\\\\d{2})(?P<day>\\\\d{2})_.*\\\\.tar(\\\\.gz)?"
77+
}
78+
EOF
79+
done
80+
81+
cat <<EOF
82+
]
83+
}
84+
EOF

etc/ncp-templates/nextcloud.conf.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,36 @@ if [[ "$1" != "--defaults" ]] && [[ "$METRICS_IS_ENABLED" == yes ]]
8686
then
8787

8888
cat <<EOF
89-
9089
<Location /metrics/system>
9190
ProxyPass http://localhost:9100/metrics
9291
9392
Order deny,allow
9493
Allow from all
9594
AuthType Basic
96-
AuthName "Metrics"
95+
AuthName "System Metrics"
9796
AuthUserFile /usr/local/etc/metrics.htpasswd
9897
<RequireAll>
9998
<RequireAny>
10099
Require host localhost
101100
Require valid-user
102101
</RequireAny>
103102
</RequireAll>
103+
</Location>
104104
105+
<Location /metrics/ncp>
106+
ProxyPass http://localhost:9000/metrics
107+
108+
Order deny,allow
109+
Allow from all
110+
AuthType Basic
111+
AuthName "NCP Metrics"
112+
AuthUserFile /usr/local/etc/metrics.htpasswd
113+
<RequireAll>
114+
<RequireAny>
115+
Require host localhost
116+
Require valid-user
117+
</RequireAny>
118+
</RequireAll>
105119
</Location>
106120
EOF
107121
fi
@@ -125,6 +139,6 @@ cat <<EOF
125139
EOF
126140

127141
if ! [[ -f /.ncp-image ]]; then
128-
echo "Apache self check:" >> /var/log/ncp.log
129-
apache2ctl -t >> /var/log/ncp.log 2>&1
142+
echo "Apache self check:" | tee /var/log/ncp.log >&2
143+
apache2ctl -t 2>&1 | tee /var/log/ncp.log >&2
130144
fi

0 commit comments

Comments
 (0)