nextcloudpi/bin/ncp-diag
thecalcaholic cdce1c24de
ncp-diag: Fix datadir incorrectly report if running as user www-data
Signed-off-by: thecalcaholic <6317548+theCalcaholic@users.noreply.github.com>
2022-09-22 12:28:16 +02:00

165 lines
6.0 KiB
Bash

#!/bin/bash
# NextCloudPi diagnostics report
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# Usage:
# sudo ncp-diag
#
# More at https://ownyourbits.com
#
source /usr/local/etc/library.sh
# Distro, NCP version and tag
echo "NextCloudPi version|$( cat /usr/local/etc/ncp-version )"
[[ -f /usr/local/etc/ncp-baseimage ]] && echo "NextCloudPi image|$( cat /usr/local/etc/ncp-baseimage )"
echo "OS|$(cat /etc/issue | sed 's| \\n \\l||'). $(uname -r) ($(uname -m))"
# Data
DATADIR="$( grep datadirectory /var/www/nextcloud/config/config.php |
awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )"
test -d "$DATADIR" || DIRINFO=" (doesn't exist)"
USBDEVS="$( lsblk -S -o NAME,TRAN | awk '{ if ( $2 == "usb" ) print $1; }' | tr '\n' ' ' )"
[[ "$USBDEVS" == "" ]] && USBDEVS="none"
am_cfg="/usr/local/etc/ncp-config.d/nc-automount.cfg"
[[ -f "$am_cfg" ]] && [[ "$(jq -r ".params[0].value" "$am_cfg")" == "yes" ]] && echo "automount|yes" || echo "automount|no"
echo "USB devices|$USBDEVS"
echo "datadir|$DATADIR$DIRINFO"
[[ "$DIRINFO" == "" ]] && {
echo "data in SD|$( [[ $( stat -fc%d / ) == $( stat -fc%d "$DATADIR" ) ]] && echo yes || echo no )"
echo "data filesystem|$( stat -fc%T $DATADIR )"
echo "data disk usage|$( df -h "$DATADIR" | tail -1 | awk '{ print $3"/"$2 }')"
}
echo "rootfs usage|$( df -h / | tail -1 | awk '{ print $3"/"$2 }')"
SWP="$( swapon | tail -1 | awk '{ print $1 }' )"
[[ "$SWP" == "" ]] && SWP="none"
echo "swapfile|$SWP"
# Database
DBDIR=$( grep datadir /etc/mysql/mariadb.conf.d/90-ncp.cnf | awk -F "= " '{ print $2 }' )
test -d "$DBDIR" || DBDIRINFO=" (doesn't exist)"
echo "dbdir|$DBDIR$DBDIRINFO"
# Nextcloud
VERSION="$( ncc status | grep "version:" | awk '{ print $3 }' )"
if [[ "$VERSION" != "" ]]; then
echo "Nextcloud check|ok"
echo "Nextcloud version|$VERSION"
else
echo "Nextcloud check|error"
fi
# Services
echo "HTTPD service|$( pgrep -c apache2 &>/dev/null && echo up || echo down )"
echo "PHP service|$( pgrep -c php-fpm &>/dev/null && echo up || echo down )"
echo "MariaDB service|$( (pgrep -c mysqld || pgrep -c mariadb) &>/dev/null && echo up || echo down )"
echo "Redis service|$( pgrep -c redis-server &>/dev/null && echo up || echo down )"
echo "HPB service|$( ncc notify_push:self-test &>/dev/null && echo up || echo down )"
echo "Postfix service|$( pgrep -fc postfix &>/dev/null && echo up || echo down )"
# WAN
echo "Internet check|$( ping -W 2 -w 1 -q github.com &>/dev/null && echo ok || echo no )"
function is_port_open()
{
local port_url tmp_file token ipv4_portcheck_args ipv6_portcheck_args
tmp_file=$(mktemp)
trap 'rm -rf ${tmp_file}' EXIT ERR SIGINT SIGQUIT SIGABRT SIGTERM SIGHUP
local port="${1?}"
local publicIPv4="${2}"
local publicIPv6="${3}"
readonly port_url="https://portchecker.co"
if [[ -z "$publicIPv4" ]] && [[ -z "$publicIPv6" ]]
then
echo -n "Error - IPv4 & IPv6: [N/A] Couldn't get public IP."
return 1
fi
token=$(wget -T2 -t1 -qO- --keep-session-cookies --save-cookies "${tmp_file}" "${port_url}" | grep -oP "_csrf\" value=\"\K.*\"" )
readonly ipv4_portcheck_args=(-T2 -t1 -qO- --load-cookies "${tmp_file}" "${port_url}/check" --post-data "target_ip=${publicIPv4}&port=${port}&_csrf=${token::-1}")
readonly ipv6_portcheck_args=(-T2 -t1 -qO- --load-cookies "${tmp_file}" "${port_url}/check" --post-data "target_ip=${publicIPv6}&port=${port}&_csrf=${token::-1}")
[[ -n "${token}" ]] || {
echo -n "Error - Couldn't obtain a token for port check"
return 1
}
local ipv4_port_access=False
local ipv6_port_access=False
[[ -n "$publicIPv4" ]] && \
grep -q '<span class="green">open</span>' <(wget "${ipv4_portcheck_args[@]}") && \
ipv4_port_access=True
[[ -n "$publicIPv6" ]] && \
grep -q '<span class="green">open</span>' <(wget "${ipv6_portcheck_args[@]}") && \
ipv6_port_access=True
local result=""
if [[ "${ipv4_port_access}" == True ]] || [[ "${ipv6_port_access}" == True ]]
then
result="open ("
else
result="closed"
fi
[[ "${ipv4_port_access}" == True ]] && result="${result}ipv4)"
[[ "${ipv6_port_access}" == True ]] && result="${result/)/ \& }ipv6)"
echo -n "$result"
}
publicIPv4=$(curl -s -m4 -4 "https://icanhazip.com" 2>/dev/null) || unset publicIPv4
echo "public IPv4|${publicIPv4:-"not found"}"
publicIPv6=$(curl -s -m4 -6 "https://icanhazip.com" 2>/dev/null) || unset publicIPv6
echo "public IPv6|${publicIPv6:-"not found"}"
echo "Port check 80|$( is_port_open 80 "$publicIPv4" "$publicIPv6" )"
echo "Port check 443|$( is_port_open 443 "$publicIPv4" "$publicIPv6" )"
# LAN
IFACE=$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )
GW=$( ip r | grep "default via" | awk '{ print $3 }' | head -1 )
IP="$(get_ip)"
echo "IP|$IP"
echo "Gateway|$GW"
echo "Interface|$IFACE"
# Certificates
CERTS="$( grep "SSLCertificateFile */etc/letsencrypt/live/" /etc/apache2/sites-available/nextcloud.conf \
| sed 's|.*SSLCertificateFile */etc/letsencrypt/live/||;s|/fullchain.pem||' )"
[[ "$CERTS" == "" ]] && CERTS=none
echo "certificates|$CERTS"
RESOLV="$( ping -c1 -w1 "$CERTS" 2>/dev/null | head -1 | grep -oP '\d{1,3}(.\d{1,3}){3}' )"
echo "NAT loopback|$( [[ "$RESOLV" == "$IP" ]] && echo yes || echo no )"
# Other
echo "Uptime|$( uptime | cut -f1 -d',' | awk '{ $1=""; $2=""; print }' | tr -d " " )"
# Get kernel version
# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA