mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 18:53:56 +00:00
ecde222512
* Fix handling of wildcard help text * Rewrite help text for better handling of params * Replace misleading letter variable * stash changes on branch switch, else it fails if any changes have been made. * Make changes according to comment in #1384 * Update queryFunc() * Allow scanList() to search files using a wildcard by removing quotes wrapped around `${list}` * scanList() will not provide a domain ouput on each string if exact is specified (`grep -l`) * Remove unused processWildcards() function * Return a message if no domain is specified * IDN domains are converted to punycode when running a `pihole -q` search if the `python` package is available, otherwise will revert to current behaviour * Scan Blacklist & Wildcards first, exiting from search if a match is found (Fixes #1330) * Use one `grep` subshell to search for all "*.domains" lists at once (opposed to looping to get every matching file name, and then spawning a `grep` instance for every matching file) * queryFunc() will not return "(0 results)" output from files where no match is found * Sort results based off list number * Return a message if no results are found * Update basic-install.sh * Update block page. Allow for setupVars setting of CUSTOMBLOCKPAGE (bool) to prevent it being overwritten * simplify * further simplify * fix inteliJ IDEA complaints * even further simplify * tidy up output * revert line, looks tidyer * clarify * Revert "Ensure any changes to blocking page are updated." * We test for dpkg lock on line 830 directly, no need for the check also in the template section. Signed-off-by: Dan Schaper <dan.schaper@pi-hole.net> * Display FTL version & version.sh rewrite While testing to make sure `pihole -v` would output `pihole-FTL version`, I noticed some options didn't work how I expected them to. For example, if I use `pihole -v -p`, I would expect to see the version output of Pi-hole Core. Instead, I'm informed that it's an invalid option. I've had the following things in mind while rewriting this: * I'm operating under the assumption that FTL is only installed if the Admin Console is (Line 113 exit 0) * I have modified the help text to only output with `pihole -v --help` * I have modified all output to be more similar to the output style of `grep` and `curl` (Ditching ":::") Testing output: ``` w3k@MCT:~$ pihole -v Pi-hole version is v3.0.1-14-ga928cd3 (Latest: v3.0.1) Admin Console version is v3.0-9-g3760482 (Latest: v3.0.1) FTL version is v2.6.2 (Latest: v2.6.2) w3k@MCT:~$ pihole -v -c Current Pi-hole version is v3.0.1-14-ga928cd3 Current Admin Console version is v3.0-9-g3760482 Current FTL version is v2.6.2 w3k@MCT:~$ pihole -v -l Latest Pi-hole version is v3.0.1 Latest Admin Console version is v3.0.1 Latest FTL version is v2.6.2 w3k@MCT:~$ pihole -v -p --hash Current Pi-hole hash isa928cd3
w3k@MCT:~$ pihole -v -a --hash Current Admin Console hash is 3760482 w3k@MCT:~$ pihole -v --help Usage: pihole -v [REPO | OPTION] [OPTION] Show Pi-hole, Web Admin & FTL versions <Shows all Repositories and Options> w3k@MCT:~$ pihole -v -foo Invalid Option! ``` * Update -h to work as --hash Also provide error output as per https://github.com/pi-hole/pi-hole/pull/1447#issuecomment-300600093 * Perform EXACT searches on HOSTS lists correctly `\s` on the end may be overkill, but it is the existing scanList() behaviour. * Fixed indentation * Minimise string duplication & other minor changes Instead of duplicating output strings, rewrite core/web/ftlOutput() into one neat versionOutput(). * Modified syntax to be valid for Shellcheck * Log and echo gateway responses * Update queryFunc() to search Whitelist If there is a match in Whitelist/Blacklist/Wildcards, `[ ! -t 1 ]` will cause the search to end if the terminal is closed when the script is called. This has the intended effect of allowing a user to search for a W/B/W domain (as well as all the adlists it's found in) using `pihole -q` via Terminal, but the script will stop searching after a W/B/W match when called by the block page. * Wrap in double brackets * Provide remote hashes for version.sh * Provide remote hashes for comparison * Use double braces for all conditions (for consistency) * Suppress potential "cd" error output * Provide "not applicable" output upon any hash request for FTL * whitelist on website blocked doesnt work (#1452) Since Pi-hole redirects ad domains to itself, accessing the script via de.ign.com is the same as pi.hole in this case. The fix should be as simple as adding a / before admin on this line. * Solve piholeLogFlush.sh having to be issued 2 x to clear logs (#1460) Simplified the command -v syntax, and added a sleep 3 timer to the first execution of the log rotation. The second execution was being issued while the first was still running, thus it would fail and you would have to issue the "Flush Logs" command a second time. * Use `echo "ABC" | pihole tricorder` to upload to Pi-hole's medical tricorder. Uses SSL if available. * Update list.sh I believe this has feature parity with `sed /foo/ Id` but also supports busybox, and my alpine docker ;) * Document `sed` substitution for user readability Comment the oneliner with explanations of what each step does. * Update Help Output (#1467) * File consistency * Tabs to 2 spaces * Corrected indenting * Double braced conditionals * Quoted variables within conditionals * Standardise core help text * Added help text for disable command * Added help text for logging command * Clean up * Fixed certain new lines and spaces * Sync with development branch * Formatting consistency * Tabs to 2 spaces * Corrected indenting * Double braced conditionals * Quoted variables within conditionals * Fixed certain newlines and spaces * Admin help text * Added help text for interface command * Sync with development branch * Formatting consistency * Tabs to 2 spaces * Fixed some wording * Fixed certain spaces * Formatting consistency * Minor wording changes * Tabs to 2 spaces * Corrected indenting * Double braced conditionals * Quoted variables within conditionals * Fixed certain newlines and spaces * Blacklist help text * Formatting consistency * Tabs to 2 spaces * Corrected indenting * Cronometer help text * Formatting consistency * Fixed certain newlines and spaces * Corrected indenting * Checkout warning alteration * Add checkout help text * Corrected help output * Show help for "pihole -a -i --help" * Fix "pihole disable --help" and "pihole -l --help" * Show help for "pihole -v -h" * Indent output text * Minor help text change * Show help for "pihole checkout --help" * Tricorder: Insecure Opt-out * Check to see if Tricorder is being called directly * Provide opt-out for insecure transmission of debug log * Remove mention of internal function from help menu * 🌮 is the new :shipit: squirrel * Wording changes and bug fix * Fix wildcard help text * -wild is not a valid option since we're already using -wild * Fix logrotation: manual flushing should be done twice, but automated rotation at midnight should only be done *once*! * Print echos only when manual flushing is requested * Add "quiet" mode + update comments in the cron file * Confirm Tricorder is online * Scan port 9998 to confirm the availability of "tricorder.pi-hole.net" * Exit codes for upload process * Formatting consistency * Add link to Windows DNS Swapper See #1400 * Install loopback firewall rules for FTL (#1419) * Install loopback firewall rules for FTL * FirewallD FTL ports Signed-off-by: Dan Schaper <dan.schaper@pi-hole.net> * Remove firewallD FTL local rules. Local rules should not be blocked in firewallD, not requred for internal service FTD> * Reinstate https rules, and delete FTL rules Fixes earlier commit. * Retrieve local repos on repair (#1481) * Retrieve local repos on repair * Change conditional to check for repair * Change wording of Update/Reconfigure message * Fixed indenting * Perform "git reset --hard" on reconfigure * Change directory before trying to reset repository. Fixes #1489 * No need to `cd $PWD` as it doesn't affect flow of caller script. Signed-off-by: Dan Schaper <dan.schaper@pi-hole.net> * Refine output of password status in basic-install.sh:displayFinalMessage(). Fixes #1488 (#1490) * Rewrite Chronometer to output more stats * Fix output IPv4 addr when removing CIDR notation (#1498) * Move wildcards file if blocking is disabled (#1495) * Move wildcards file if blocking is diabled * Delete newline * Roll back merge #1417 (#1494) * Update ISSUE_TEMPLATE.md * Remove Question option * Prefer ULA over GUA addresses [IPv6] (#1508) * On installs with GUA and ULA's we should prefer ULA's as it's been demonstrated that GUA's can and often are rotated by ISPs. Fixes #1473 * Add test for link-local address detection * Add ULA-only and GUA-only tests * Add test_IPv6_GUA_ULA_test and test_IPv6_ULA_GUA_test * Add "" * Add mock_command_2 command that can mock a command with more than one argument (as "ip -6 address") and result multiple lines of results * Make mock_command_2 more similar to the original mock_command * Correct comments * Fixed remaining comments * Fixed one last comment... * Fixed a comment... * Add weekly logrotation of FTL's log (#1509) * Update LICENSE of the project to EUPL v1.2 * Make clear that NO is the default if the user just hits return (#1514) * Add tricorderFunc back as usable function (#1515) As per #1464 * Don't update FTL when there is a core update (as this will update FTL a second time). Fixes #1516 * Add FTL tests to the test suite (#1510) * Add first version of FTL tests * Wait one second to allow FTL to start up and analyze our mock log * Add test_FTL_telnet_statistics * Added test_FTL_telnet_top_clients * Add test_FTL_telnet_top_domains * Revert "Add FTL tests to the test suite (#1510)" (#1519) This reverts commitcf6a1ac9ad
. * Trim version output when update is successful (#1527) * Change ownership of /etc/pihole to user/group pihole. Fixes #1529 (#1530) * Delete temporary files after installing the FTL binary. Fixes #1525 * Change from admin to approvers teams * Introduce new file black.list for blacklist content * Add "pihole -g -b" to *only* update black.list (saves a bunch of time when adding/changing only blacklisted files - won'tdownload lal lists, but only processes the blacklist and restars dnsmasq) * Remove useless cat * Improve displayed messages and overall logic * Disable black.list on "pihole disable" * cp + rm === mv (well, almost)
435 lines
15 KiB
Bash
Executable file
435 lines
15 KiB
Bash
Executable file
#!/usr/bin/env bash
|
||
# Pi-hole: A black hole for Internet advertisements
|
||
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
||
# Network-wide ad blocking via your own hardware.
|
||
#
|
||
# Calculates stats and displays to an LCD
|
||
#
|
||
# This file is copyright under the latest version of the EUPL.
|
||
# Please see LICENSE file for your rights under this license.
|
||
|
||
# Retrieve stats from FTL engine
|
||
pihole-FTL() {
|
||
ftl_port=$(cat /var/run/pihole-FTL.port 2> /dev/null)
|
||
if [[ -n "$ftl_port" ]]; then
|
||
# Open connection to FTL
|
||
exec 3<>"/dev/tcp/localhost/$ftl_port"
|
||
|
||
# Test if connection is open
|
||
if { "true" >&3; } 2> /dev/null; then
|
||
# Send command to FTL
|
||
echo -e ">$1" >&3
|
||
|
||
# Read input
|
||
read -r -t 1 LINE <&3
|
||
until [[ ! $? ]] || [[ "$LINE" == *"EOM"* ]]; do
|
||
echo "$LINE" >&1
|
||
read -r -t 1 LINE <&3
|
||
done
|
||
|
||
# Close connection
|
||
exec 3>&-
|
||
exec 3<&-
|
||
fi
|
||
else
|
||
echo -e "${COL_LIGHT_RED}FTL offline${COL_NC}"
|
||
fi
|
||
}
|
||
|
||
# Print spaces to align right-side content
|
||
printFunc() {
|
||
txt_len="${#2}"
|
||
|
||
# Reduce string length when using colour code
|
||
[ "${2:0:1}" == "" ] && txt_len=$((txt_len-7))
|
||
|
||
if [[ "$3" == "last" ]]; then
|
||
# Prevent final line from printing trailing newline
|
||
scr_size=( $(stty size 2>/dev/null || echo 24 80) )
|
||
scr_width="${scr_size[1]}"
|
||
|
||
title_len="${#1}"
|
||
spc_num=$(( (scr_width - title_len) - txt_len ))
|
||
[[ "$spc_num" -lt 0 ]] && spc_num="0"
|
||
spc=$(printf "%${spc_num}s")
|
||
|
||
printf "%s%s$spc" "$1" "$2"
|
||
else
|
||
# Determine number of spaces for padding
|
||
spc_num=$(( 20 - txt_len ))
|
||
[[ "$spc_num" -lt 0 ]] && spc_num="0"
|
||
spc=$(printf "%${spc_num}s")
|
||
|
||
# Print string (Max 20 characters, prevents overflow)
|
||
printf "%s%s$spc" "$1" "${2:0:20}"
|
||
fi
|
||
}
|
||
|
||
# Perform on first Chrono run (not for JSON formatted string)
|
||
get_init_stats() {
|
||
LC_NUMERIC=C
|
||
calcFunc(){ awk "BEGIN {print $*}"; }
|
||
|
||
# Convert bytes to human-readable format
|
||
hrBytes() {
|
||
awk '{
|
||
num=$1;
|
||
if(num==0) {
|
||
print "0 B"
|
||
} else {
|
||
xxx=(num<0?-num:num)
|
||
sss=(num<0?-1:1)
|
||
split("B KB MB GB TB PB",type)
|
||
for(i=5;yyy < 1;i--) {
|
||
yyy=xxx / (2^(10*i))
|
||
}
|
||
printf "%.0f " type[i+2], yyy*sss
|
||
}
|
||
}' <<< "$1";
|
||
}
|
||
|
||
# Convert seconds to human-readable format
|
||
hrSecs() {
|
||
day=$(( $1/60/60/24 )); hrs=$(( $1/3600%24 )); mins=$(( ($1%3600)/60 )); secs=$(( $1%60 ))
|
||
[[ "$day" -ge "2" ]] && plu="s"
|
||
[[ "$day" -ge "1" ]] && days="$day day${plu}, " || days=""
|
||
printf "%s%02d:%02d:%02d\n" "$days" "$hrs" "$mins" "$secs"
|
||
}
|
||
|
||
# Set Colour Codes
|
||
coltable="/opt/pihole/COL_TABLE"
|
||
if [[ -f "${coltable}" ]]; then
|
||
source ${coltable}
|
||
else
|
||
COL_NC='[0m'
|
||
COL_DARK_GRAY='[1;30m'
|
||
COL_LIGHT_GREEN='[1;32m'
|
||
COL_LIGHT_BLUE='[1;34m'
|
||
COL_LIGHT_RED='[1;31m'
|
||
COL_YELLOW='[1;33m'
|
||
COL_LIGHT_RED='[1;31m'
|
||
COL_URG_RED='[39;41m'
|
||
fi
|
||
|
||
# Get RPi model number, or OS distro info
|
||
if command -v vcgencmd &> /dev/null; then
|
||
sys_rev=$(awk '/Revision/ {print $3}' < /proc/cpuinfo)
|
||
case "$sys_rev" in
|
||
000[2-6]) sys_model=" 1, Model B";; # 256MB
|
||
000[7-9]) sys_model=" 1, Model A" ;; # 256MB
|
||
000d|000e|000f) sys_model=" 1, Model B";; # 512MB
|
||
0010|0013) sys_model=" 1, Model B+";; # 512MB
|
||
0012|0015) sys_model=" 1, Model A+";; # 256MB
|
||
a0104[0-1]|a21041|a22042) sys_model=" 2, Model B";; # 1GB
|
||
900021) sys_model=" 1, Model A+";; # 512MB
|
||
900032) sys_model=" 1, Model B+";; # 512MB
|
||
90009[2-3]|920093) sys_model=" Zero";; # 512MB
|
||
9000c1) sys_model=" Zero W";; # 512MB
|
||
a02082|a[2-3]2082) sys_model=" 3, Model B";; # 1GB
|
||
*) sys_model="" ;;
|
||
esac
|
||
sys_type="Raspberry Pi$sys_model"
|
||
else
|
||
source "/etc/os-release"
|
||
CODENAME=$(sed 's/[()]//g' <<< "${VERSION/* /}")
|
||
sys_type="${NAME/ */} ${CODENAME^} $VERSION_ID"
|
||
fi
|
||
|
||
# Get core count
|
||
sys_cores=$(grep -c "^processor" /proc/cpuinfo)
|
||
[[ "$sys_cores" -ne 1 ]] && sys_cores_plu="cores" || sys_cores_plu="core"
|
||
|
||
# Test existence of clock speed file for ARM CPU
|
||
if [[ -f "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" ]]; then
|
||
scaling_freq_file="/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
|
||
fi
|
||
|
||
# Test existence of temperature file
|
||
if [[ -f "/sys/class/thermal/thermal_zone0/temp" ]]; then
|
||
temp_file="/sys/class/thermal/thermal_zone0/temp"
|
||
elif [[ -f "/sys/class/hwmon/hwmon0/temp1_input" ]]; then
|
||
temp_file="/sys/class/hwmon/hwmon0/temp1_input"
|
||
else
|
||
temp_file=""
|
||
fi
|
||
|
||
# Test existence of setupVars config
|
||
if [[ -f "/etc/pihole/setupVars.conf" ]]; then
|
||
setupVars="/etc/pihole/setupVars.conf"
|
||
fi
|
||
}
|
||
|
||
get_sys_stats() {
|
||
local ph_ver_raw
|
||
local cpu_raw
|
||
local ram_raw
|
||
local disk_raw
|
||
|
||
# Update every 12 refreshes (Def: every 60s)
|
||
count=$((count+1))
|
||
if [[ "$count" == "1" ]] || (( "$count" % 12 == 0 )); then
|
||
[[ -n "$setupVars" ]] && source "$setupVars"
|
||
|
||
|
||
ph_ver_raw=($(pihole -v -c 2> /dev/null | sed -n 's/^.* v/v/p'))
|
||
if [[ -n "${ph_ver_raw[0]}" ]]; then
|
||
ph_core_ver="${ph_ver_raw[0]}"
|
||
ph_lte_ver="${ph_ver_raw[1]}"
|
||
ph_ftl_ver="${ph_ver_raw[2]}"
|
||
else
|
||
ph_core_ver="${COL_LIGHT_RED}API unavailable${COL_NC}"
|
||
fi
|
||
|
||
sys_name=$(hostname)
|
||
|
||
[[ -n "$TEMPERATUREUNIT" ]] && temp_unit="$TEMPERATUREUNIT" || temp_unit="c"
|
||
|
||
# Get storage stats for partition mounted on /
|
||
disk_raw=($(df -B1 / 2> /dev/null | awk 'END{ print $3,$2,$5 }'))
|
||
disk_used="${disk_raw[0]}"
|
||
disk_total="${disk_raw[1]}"
|
||
disk_perc="${disk_raw[2]}"
|
||
|
||
net_gateway=$(route -n | awk '$4 == "UG" {print $2;exit}')
|
||
|
||
# Get DHCP stats, if feature is enabled
|
||
if [[ "$DHCP_ACTIVE" == "true" ]]; then
|
||
ph_dhcp_eip="${DHCP_END##*.}"
|
||
ph_dhcp_max=$(( ${DHCP_END##*.} - ${DHCP_START##*.} + 1 ))
|
||
fi
|
||
|
||
# Get alt DNS server, or print total count of alt DNS servers
|
||
if [[ -z "${PIHOLE_DNS_3}" ]]; then
|
||
ph_alts="${PIHOLE_DNS_2}"
|
||
else
|
||
dns_count="0"
|
||
[[ -n "${PIHOLE_DNS_2}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_3}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_4}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_5}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_6}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_7}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_8}" ]] && dns_count=$((dns_count+1))
|
||
[[ -n "${PIHOLE_DNS_9}" ]] && dns_count="$dns_count+"
|
||
ph_alts="${dns_count} others"
|
||
fi
|
||
fi
|
||
|
||
sys_uptime=$(hrSecs "$(cut -d. -f1 /proc/uptime)")
|
||
sys_loadavg=$(cut -d " " -f1,2,3 /proc/loadavg)
|
||
|
||
# Get CPU usage, only counting processes over 1% CPU as active
|
||
cpu_raw=$(ps -eo pcpu,rss --no-headers | grep -E -v " 0")
|
||
cpu_tasks=$(wc -l <<< "$cpu_raw")
|
||
cpu_taskact=$(sed -r "/(^ 0.)/d" <<< "$cpu_raw" | wc -l)
|
||
cpu_perc=$(awk '{sum+=$1} END {printf "%.0f\n", sum/'"$sys_cores"'}' <<< "$cpu_raw")
|
||
|
||
# Get CPU clock speed
|
||
if [[ -n "$scaling_freq_file" ]]; then
|
||
cpu_mhz=$(( $(< /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) / 1000 ))
|
||
else
|
||
cpu_mhz=$(lscpu | awk -F "[ .]+" '/MHz/ {print $4;exit}')
|
||
fi
|
||
|
||
# Determine correct string format for CPU clock speed
|
||
if [[ -n "$cpu_mhz" ]]; then
|
||
[[ "$cpu_mhz" -le "999" ]] && cpu_freq="$cpu_mhz MHz" || cpu_freq="$(calcFunc "$cpu_mhz"/1000) Ghz"
|
||
[[ -n "$cpu_freq" ]] && cpu_freq_str=" @ $cpu_freq" || cpu_freq_str=""
|
||
fi
|
||
|
||
# Determine colour for temperature
|
||
if [[ -n "$temp_file" ]]; then
|
||
if [[ "$temp_unit" == "C" ]]; then
|
||
cpu_temp=$(printf "%'.0fc\n" "$(calcFunc "$(< $temp_file) / 1000")")
|
||
|
||
case "${cpu_temp::-1}" in
|
||
-*|[0-9]|[1-3][0-9]) cpu_col="$COL_LIGHT_BLUE";;
|
||
4[0-9]) cpu_col="";;
|
||
5[0-9]) cpu_col="$COL_YELLOW";;
|
||
6[0-9]) cpu_col="$COL_LIGHT_RED";;
|
||
*) cpu_col="$COL_URG_RED";;
|
||
esac
|
||
|
||
# $COL_NC$COL_DARK_GRAY is needed for $COL_URG_RED
|
||
cpu_temp_str=", $cpu_col$cpu_temp$COL_NC$COL_DARK_GRAY"
|
||
|
||
elif [[ "$temp_unit" == "F" ]]; then
|
||
cpu_temp=$(printf "%'.0ff\n" "$(calcFunc "($(< $temp_file) / 1000) * 9 / 5 + 32")")
|
||
|
||
case "${cpu_temp::-1}" in
|
||
-*|[0-9]|[0-9][0-9]) cpu_col="$COL_LIGHT_BLUE";;
|
||
1[0-1][0-9]) cpu_col="";;
|
||
1[2-3][0-9]) cpu_col="$COL_YELLOW";;
|
||
1[4-5][0-9]) cpu_col="$COL_LIGHT_RED";;
|
||
*) cpu_col="$COL_URG_RED";;
|
||
esac
|
||
|
||
cpu_temp_str=", $cpu_col$cpu_temp$COL_NC$COL_DARK_GRAY"
|
||
|
||
else
|
||
cpu_temp_str=$(printf ", %'.0fk\n" "$(calcFunc "($(< $temp_file) / 1000) + 273.15")")
|
||
fi
|
||
else
|
||
cpu_temp_str=""
|
||
fi
|
||
|
||
ram_raw=($(awk '/MemTotal:/{total=$2} /MemFree:/{free=$2} /Buffers:/{buffers=$2} /^Cached:/{cached=$2} END {printf "%.0f %.0f %.0f", (total-free-buffers-cached)*100/total, (total-free-buffers-cached)*1024, total*1024}' /proc/meminfo))
|
||
ram_perc="${ram_raw[0]}"
|
||
ram_used="${ram_raw[1]}"
|
||
ram_total="${ram_raw[2]}"
|
||
|
||
if [[ "$(pihole status web 2> /dev/null)" == "1" ]]; then
|
||
ph_status="${COL_LIGHT_GREEN}Active"
|
||
else
|
||
ph_status="${COL_LIGHT_RED}Inactive"
|
||
fi
|
||
|
||
if [[ "$DHCP_ACTIVE" == "true" ]]; then
|
||
ph_dhcp_num=$(wc -l 2> /dev/null < "/etc/pihole/dhcp.leases")
|
||
fi
|
||
}
|
||
|
||
get_ftl_stats() {
|
||
local stats_raw
|
||
|
||
stats_raw=($(pihole-FTL "stats"))
|
||
domains_being_blocked_raw="${stats_raw[1]}"
|
||
dns_queries_today_raw="${stats_raw[3]}"
|
||
ads_blocked_today_raw="${stats_raw[5]}"
|
||
ads_percentage_today_raw="${stats_raw[7]}"
|
||
|
||
# Only retrieve these stats when not called from jsonFunc
|
||
if [[ -z "$1" ]]; then
|
||
local recent_blocked_raw
|
||
local top_ad_raw
|
||
local top_domain_raw
|
||
local top_client_raw
|
||
|
||
domains_being_blocked=$(printf "%'.0f\n" "${domains_being_blocked_raw}")
|
||
dns_queries_today=$(printf "%'.0f\n" "${dns_queries_today_raw}")
|
||
ads_blocked_today=$(printf "%'.0f\n" "${ads_blocked_today_raw}")
|
||
ads_percentage_today=$(printf "%'.0f\n" "${ads_percentage_today_raw}")
|
||
|
||
recent_blocked_raw=$(pihole-FTL recentBlocked)
|
||
top_ad_raw=($(pihole-FTL "top-ads (1)"))
|
||
top_domain_raw=($(pihole-FTL "top-domains (1)"))
|
||
top_client_raw=($(pihole-FTL "top-clients (1)"))
|
||
|
||
# Limit strings to 40 characters to prevent overflow
|
||
recent_blocked="${recent_blocked_raw:0:40}"
|
||
top_ad="${top_ad_raw[2]:0:40}"
|
||
top_domain="${top_domain_raw[2]:0:40}"
|
||
[[ "${top_client_raw[3]}" ]] && top_client="${top_client_raw[3]:0:40}" || top_client="${top_client_raw[2]:0:40}"
|
||
fi
|
||
}
|
||
|
||
chronoFunc() {
|
||
get_init_stats
|
||
|
||
for (( ; ; )); do
|
||
get_sys_stats
|
||
get_ftl_stats
|
||
|
||
# Do not print LTE/FTL strings if API is unavailable
|
||
ph_core_str=" ${COL_DARK_GRAY}Pi-hole: $ph_core_ver${COL_NC}"
|
||
if [[ -n "$ph_lte_ver" ]]; then
|
||
ph_lte_str=" ${COL_DARK_GRAY}AdminLTE: $ph_lte_ver${COL_NC}"
|
||
ph_ftl_str=" ${COL_DARK_GRAY}FTL: $ph_ftl_ver${COL_NC}"
|
||
fi
|
||
|
||
clear
|
||
|
||
echo -e "[0;1;31;91m|¯[0;1;33;93m¯[0;1;32;92m¯[0;1;32;92m(¯[0;1;36;96m)_[0;1;34;94m_[0;1;35;95m|[0;1;33;93m¯[0;1;31;91m|_ [0;1;32;92m__[0;1;36;96m_|[0;1;31;91m¯[0;1;34;94m|[0;1;35;95m__[0;1;31;91m_[0m$ph_core_str
|
||
[0;1;33;93m| ¯[0;1;32;92m_[0;1;36;96m/¯[0;1;34;94m|_[0;1;35;95m_[0;1;31;91m| [0;1;33;93m' [0;1;32;92m\/ [0;1;36;96m_ [0;1;34;94m\ [0;1;35;95m/ [0;1;31;91m-[0;1;33;93m_)[0m$ph_lte_str
|
||
[0;1;32;92m|_[0;1;36;96m| [0;1;34;94m|_[0;1;35;95m| [0;1;33;93m|_[0;1;32;92m||[0;1;36;96m_\[0;1;34;94m__[0;1;35;95m_/[0;1;31;91m_\[0;1;33;93m__[0;1;32;92m_|[0m$ph_ftl_str
|
||
${COL_DARK_GRAY}——————————————————————————————————————————————————————————${COL_NC}"
|
||
|
||
printFunc " Hostname: " "$sys_name"
|
||
[ -n "$sys_type" ] && printf "%s(%s)%s\n" "$COL_DARK_GRAY" "$sys_type" "$COL_NC" || printf "\n"
|
||
|
||
printf "%s\n" " Uptime: $sys_uptime"
|
||
|
||
printFunc " Task Load: " "$sys_loadavg"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Active: $cpu_taskact of $cpu_tasks tasks" "$COL_NC"
|
||
|
||
printFunc " CPU usage: " "$cpu_perc%"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "$sys_cores $sys_cores_plu$cpu_freq_str$cpu_temp_str" "$COL_NC"
|
||
|
||
printFunc " RAM usage: " "$ram_perc%"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Used: $(hrBytes "$ram_used") of $(hrBytes "$ram_total")" "$COL_NC"
|
||
|
||
printFunc " HDD usage: " "$disk_perc"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Used: $(hrBytes "$disk_used") of $(hrBytes "$disk_total")" "$COL_NC"
|
||
|
||
printFunc " LAN addr: " "${IPV4_ADDRESS/\/*/}"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Gateway: $net_gateway" "$COL_NC"
|
||
|
||
if [[ "$DHCP_ACTIVE" == "true" ]]; then
|
||
printFunc " DHCP: " "$DHCP_START to $ph_dhcp_eip"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Leased: $ph_dhcp_num of $ph_dhcp_max" "$COL_NC"
|
||
fi
|
||
|
||
printFunc " Pi-hole: " "$ph_status"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Blocking: $domains_being_blocked sites" "$COL_NC"
|
||
|
||
printFunc " Ads Today: " "$ads_percentage_today%"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "$ads_blocked_today of $dns_queries_today queries" "$COL_NC"
|
||
|
||
printFunc " Fwd DNS: " "$PIHOLE_DNS_1"
|
||
printf "%s(%s)%s\n" "$COL_DARK_GRAY" "Alt DNS: $ph_alts" "$COL_NC"
|
||
|
||
echo -e " ${COL_DARK_GRAY}——————————————————————————————————————————————————————————${COL_NC}"
|
||
echo " Recently blocked: $recent_blocked"
|
||
echo " Top Advertiser: $top_ad"
|
||
echo " Top Domain: $top_domain"
|
||
printFunc " Top Client: " "$top_client" "last"
|
||
|
||
if [[ "$1" == "exit" ]]; then
|
||
exit 0
|
||
else
|
||
if [[ -n "$1" ]]; then
|
||
sleep "${1}"
|
||
else
|
||
sleep 5
|
||
fi
|
||
fi
|
||
|
||
done
|
||
}
|
||
|
||
jsonFunc() {
|
||
get_ftl_stats "json"
|
||
echo "{\"domains_being_blocked\":${domains_being_blocked_raw},\"dns_queries_today\":${dns_queries_today_raw},\"ads_blocked_today\":${ads_blocked_today_raw},\"ads_percentage_today\":${ads_percentage_today_raw}}"
|
||
}
|
||
|
||
helpFunc() {
|
||
if [[ "$1" == "?" ]]; then
|
||
echo "Unknown option. Please view 'pihole -c --help' for more information"
|
||
else
|
||
echo "Usage: pihole -c [options]
|
||
Example: 'pihole -c -j'
|
||
Calculates stats and displays to an LCD
|
||
|
||
Options:
|
||
-j, --json Output stats as JSON formatted string
|
||
-r, --refresh Set update frequency (in seconds)
|
||
-e, --exit Output stats and exit witout refreshing
|
||
-h, --help Display this help text"
|
||
fi
|
||
|
||
exit 0
|
||
}
|
||
|
||
if [[ $# = 0 ]]; then
|
||
chronoFunc
|
||
fi
|
||
|
||
for var in "$@"; do
|
||
case "$var" in
|
||
"-j" | "--json" ) jsonFunc;;
|
||
"-h" | "--help" ) helpFunc;;
|
||
"-r" | "--refresh" ) chronoFunc "$2";;
|
||
"-e" | "--exit" ) chronoFunc "exit";;
|
||
* ) helpFunc "?";;
|
||
esac
|
||
done
|