metrics.sh: Install and manage ncp-metrics-exporter

Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com>
This commit is contained in:
Tobias K 2022-03-29 00:45:57 +02:00
parent 9409c115e9
commit 0e694329d2
No known key found for this signature in database
GPG Key ID: 44FD368932E645C1
7 changed files with 213 additions and 6 deletions

View File

@ -7,6 +7,17 @@
# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/
#
tmpl_get_destination() {
(
. /usr/local/etc/library.sh
find_app_param nc-backup-auto DESTDIR
)
}
is_active() {
[[ $ACTIVE == "yes" ]]
}
configure()
{
[[ $ACTIVE != "yes" ]] && {
@ -44,6 +55,11 @@ EOF
chmod 644 /etc/cron.d/ncp-backup-auto
service cron restart
(
. "${BINDIR}/SYSTEM/metrics.sh"
reload_metrics_config
)
echo "automatic backups enabled"
}

View File

@ -7,6 +7,13 @@
# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/
#
tmpl_get_destination() {
(
. /usr/local/etc/library.sh
find_app_param nc-backup DESTDIR
)
}
install()
{
apt-get update
@ -123,6 +130,10 @@ EOF
configure()
{
(
. "${BINDIR}/SYSTEM/metrics.sh"
reload_metrics_config
)
ncp-backup "$DESTDIR" "$INCLUDEDATA" "$COMPRESS" "$BACKUPLIMIT"
}

View File

@ -9,6 +9,25 @@
#
tmpl_get_destination() {
(
. /usr/local/etc/library.sh
find_app_param nc-snapshot-sync DESTINATION
)
}
tmpl_is_destination_local() {
(
. /usr/local/etc/library.sh
is_active_app nc-snapshot-sync || exit 1
! [[ "$(find_app_param nc-snapshot-sync DESTINATION)" =~ .*"@".*":".* ]]
)
}
is_active() {
[[ $ACTIVE == "yes" ]]
}
install()
{
apt-get update
@ -46,6 +65,12 @@ configure()
echo "30 4 */${SYNCDAYS} * * root /usr/local/bin/btrfs-sync -qd $ZIP \"$SNAPDIR\" \"$DESTINATION\"" > /etc/cron.d/ncp-snapsync-auto
chmod 644 /etc/cron.d/ncp-snapsync-auto
service cron restart
(
. "${BINDIR}/SYSTEM/metrics.sh"
reload_metrics_config
)
echo "snapshot sync enabled"
}

View File

@ -113,6 +113,11 @@ configure()
sed -i "s|logpath =.*nextcloud.log|logpath = ${DATADIR}/nextcloud.log|" /etc/fail2ban/jail.local
restore_maintenance_mode
(
. "${BINDIR}/SYSTEM/metrics.sh"
reload_metrics_config
)
}
# License

View File

@ -28,9 +28,47 @@ EOF
systemctl disable prometheus-node-exporter
service prometheus-node-exporter stop
[[ "$(uname -m)" =~ ("arm"|"aarch").* ]] && arch="armv7" || arch="i686"
[[ "$arch" == "i686" ]] && apt_install lib32gcc-s1 libc6-i386
wget -O "/usr/local/bin/ncp-metrics-exporter" \
"https://github.com/theCalcaholic/ncp-metrics-exporter/releases/download/v1.0.0/${arch}-ncp-metrics-exporter"
chmod +x /usr/local/bin/ncp-metrics-exporter
cat <<EOF > /etc/systemd/system/ncp-metrics-exporter.service
[Unit]
Description=NCP Metrics Exporter
[Service]
Environment=NCP_CONFIG_DIR=/usr/local/etc
ExecStart=/usr/local/bin/ncp-metrics-exporter
SyslogIdentifier=ncp-metrics
Restart=on-failure
RestartSec=30
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
)
}
reload_metrics_config() {
install_template ncp-metrics.cfg.sh "/usr/local/etc/ncp-metrics.cfg" || {
echo "ERROR while generating ncp-metrics.conf!"
return 1
}
service ncp-metrics-exporter status > /dev/null && {
service ncp-metrics-exporter restart
service ncp-metrics-exporter status > /dev/null 2>&1 || {
rc=$?
echo -e "WARNING: An error ncp-metrics exporter failed to start (exit-code $rc)!"
return 1
}
}
}
configure() {
if [[ "$ACTIVE" != yes ]]
@ -39,6 +77,9 @@ configure() {
systemctl disable prometheus-node-exporter
service prometheus-node-exporter stop
systemctl disable ncp-metrics-exporter
service ncp-metrics-exporter stop
else
[[ -n "$USER" ]] || {
echo "ERROR: User can not be empty!" >&2
@ -63,15 +104,26 @@ configure() {
echo "ERROR while generating nextcloud.conf! Exiting..."
return 1
}
echo "Generate config..."
reload_metrics_config
echo "done."
echo "Starting prometheus node exporter..."
systemctl enable prometheus-node-exporter
service prometheus-node-exporter start
service prometheus-node-exporter status
echo "done."
echo "Metric endpoint enabled. You can test it at https://nextcloudpi.local/metrics/system (or under your NC domain under the same path)"
echo "Starting ncp metrics exporter..."
systemctl enable ncp-metrics-exporter
service ncp-metrics-exporter start
service ncp-metrics-exporter status
echo "done."
echo "Metrics endpoint enabled. You can test it at https://nextcloudpi.local/metrics/system (or under your NC domain under the same path)"
fi
echo "Apache Test:"
apache2ctl -t
bash -c "sleep 2 && service apache2 reload" &>/dev/null &
}

View File

@ -0,0 +1,84 @@
#!/usr/bin/env bash
set -e
source /usr/local/etc/library.sh
if [[ "$1" == "--defaults" ]]
then
echo "INFO: Restoring template to default settings" >&2
cat <<EOF
{
"backups": []
}
EOF
exit 0
fi
cat <<EOF
{
"backups": [
EOF
declare -a BKP_DIRS
DATADIR=$( ncc config:system:get datadirectory ) || {
echo -e "ERROR: Could not get data directory. Is NextCloud running?";
return 1;
}
NC_SNAPSHOTS_DIR="$(dirname "$DATADIR")/ncp-snapshots"
NC_SNAPSHOTS_SYNC_DIR="$(
source "${BINDIR}/BACKUPS/nc-snapshot-sync.sh"
if tmpl_is_destination_local
then
tmpl_get_destination
fi
)"
SNAP_PATTERN=".*_(?P<year>\\\\d+)-(?P<month>\\\\d+)-(?P<day>\\\\d+)_(?P<hour>\\\\d{2})(?P<minute>\\\\d{2})(?P<second>\\\\d{2})"
cat <<EOF
{
"path": "${NC_SNAPSHOTS_DIR}",
"pattern": "${SNAP_PATTERN}"
}
EOF
[[ -z "$NC_SNAPSHOTS_SYNC_DIR" ]] || {
cat <<EOF
,{
"path": "${NC_SNAPSHOTS_SYNC_DIR}",
"pattern": "${SNAP_PATTERN}"
}
EOF
}
NC_BACKUP_DIR="$(
source "${BINDIR}/BACKUPS/nc-backup.sh"
tmpl_get_destination
)"
NC_BACKUP_AUTO_DIR="$(
source "${BINDIR}/BACKUPS/nc-backup-auto.sh"
tmpl_get_destination
)"
if [[ "$NC_BACKUP_DIR" == "$NC_BACKUP_AUTO_DIR" ]]
then
NC_BACKUP_AUTO_DIR=""
fi
for BKP_DIR in "$NC_BACKUP_DIR" "$NC_BACKUP_AUTO_DIR"
do
[[ -n "$BKP_DIR" ]] || continue
cat <<EOF
,{
"path": "$BKP_DIR",
"pattern": "nextcloud-bkp_(?P<year>\\\\d{4})(?P<month>\\\\d{2})(?P<day>\\\\d{2})_.*\\\\.tar(\\\\.gz)?"
}
EOF
done
cat <<EOF
]
}
EOF

View File

@ -86,14 +86,13 @@ if [[ "$1" != "--defaults" ]] && [[ "$METRICS_IS_ENABLED" == yes ]]
then
cat <<EOF
<Location /metrics/system>
ProxyPass http://localhost:9100/metrics
Order deny,allow
Allow from all
AuthType Basic
AuthName "Metrics"
AuthName "System Metrics"
AuthUserFile /usr/local/etc/metrics.htpasswd
<RequireAll>
<RequireAny>
@ -101,7 +100,22 @@ then
Require valid-user
</RequireAny>
</RequireAll>
</Location>
<Location /metrics/ncp>
ProxyPass http://localhost:9000/metrics
Order deny,allow
Allow from all
AuthType Basic
AuthName "NCP Metrics"
AuthUserFile /usr/local/etc/metrics.htpasswd
<RequireAll>
<RequireAny>
Require host localhost
Require valid-user
</RequireAny>
</RequireAll>
</Location>
EOF
fi
@ -125,6 +139,6 @@ cat <<EOF
EOF
if ! [[ -f /.ncp-image ]]; then
echo "Apache self check:" >> /var/log/ncp.log
apache2ctl -t >> /var/log/ncp.log 2>&1
echo "Apache self check:" | tee /var/log/ncp.log >&2
apache2ctl -t 2>&1 | tee /var/log/ncp.log >&2
fi