3
3
# A more advanced version of the chronometer provided with Pihole
4
4
5
5
# SETS LOCALE
6
- # Addresses Issue 5: https://github.com/jpmck/chronometer2/issues/5 ... I think...
7
- export LC_ALL=C.UTF-8
6
+ # Issue 5: https://github.com/jpmck/chronometer2/issues/5
7
+ # Updated to en_US to support
8
+ export LC_ALL=en_US.UTF-8
8
9
9
- chronometer2Version=" 1.2.2 "
10
+ chronometer2Version=" 1.3 "
10
11
11
12
# COLORS
12
13
blueColor=$( tput setaf 6)
@@ -45,16 +46,23 @@ GetFTLData() {
45
46
}
46
47
47
48
GetSummaryInformation () {
49
+ # From pihole -c
48
50
local summary
49
51
summary=$( GetFTLData " stats" )
52
+
50
53
domains_being_blocked_raw=$( grep " domains_being_blocked" <<< " ${summary}" | grep -Eo " [0-9]+$" )
51
54
domains_being_blocked=$( printf " %'.f" ${domains_being_blocked_raw} )
55
+
52
56
dns_queries_today_raw=$( grep " dns_queries_today" <<< " $summary" | grep -Eo " [0-9]+$" )
53
57
dns_queries_today=$( printf " %'.f" ${dns_queries_today_raw} )
58
+
54
59
ads_blocked_today_raw=$( grep " ads_blocked_today" <<< " $summary" | grep -Eo " [0-9]+$" )
55
60
ads_blocked_today=$( printf " %'.f" ${ads_blocked_today_raw} )
61
+
56
62
ads_percentage_today_raw=$( grep " ads_percentage_today" <<< " $summary" | grep -Eo " [0-9.]+$" )
57
- LC_NUMERIC=C ads_percentage_today=$( printf " %'.f" ${ads_percentage_today_raw} )
63
+ LC_NUMERIC=C ads_percentage_today=$( printf " %'.1f" ${ads_percentage_today_raw} )
64
+
65
+ adsBlockedBar=$( BarGenerator $ads_percentage_today 40 " color" )
58
66
}
59
67
60
68
GetSystemInformation () {
@@ -74,35 +82,43 @@ GetSystemInformation() {
74
82
temperature=$( printf %.1f " $( echo " scale=4; ${cpu} /1000" | bc) " ) °C
75
83
fi
76
84
77
- # CPU load and heatmap calculations
85
+ # CPU load, heatmap and bar
78
86
cpuLoad1=$( cat /proc/loadavg | awk ' {print $1}' )
79
- cpuLoad1Heatmap=$( CPUHeatmapGenerator ${cpuLoad1} ${numProc} )
80
-
87
+ cpuLoad1Heatmap=$( HeatmapGenerator ${cpuLoad1} ${numProc} )
81
88
cpuLoad5=$( cat /proc/loadavg | awk ' {print $2}' )
82
- cpuLoad5Heatmap=$( CPUHeatmapGenerator ${cpuLoad5} ${numProc} )
83
-
89
+ cpuLoad5Heatmap=$( HeatmapGenerator ${cpuLoad5} ${numProc} )
84
90
cpuLoad15=$( cat /proc/loadavg | awk ' {print $3}' )
85
- cpuLoad15Heatmap=$( CPUHeatmapGenerator ${cpuLoad15} ${numProc} )
86
-
91
+ cpuLoad15Heatmap=$( HeatmapGenerator ${cpuLoad15} ${numProc} )
87
92
cpuPercent=$( printf %.1f " $( echo " scale=4; (${cpuLoad1} /${numProc} )*100" | bc) " )
88
-
89
- # Memory use
90
- # Corrects Issue #9 https://github.com/jpmck/chronometer2/issues/9
91
- # Thanks, Twiggy159 and WaLLy3K!
92
- memoryUsedPercent=$( awk ' /MemTotal:/{total=$2} /MemFree:/{free=$2} /Buffers:/{buffers=$2} /^Cached:/{cached=$2} END {printf "%.1f", (total-free-buffers-cached)*100/total}' ' /proc/meminfo' )
93
+ cpuBar=$( BarGenerator ${cpuPercent} 10)
93
94
94
95
# CPU temperature heatmap
95
96
if [ ${cpu} -gt 60000 ]; then
96
97
tempHeatMap=${redColor}
97
98
else
98
99
tempHeatMap=${blueColor}
99
100
fi
101
+
102
+ # Memory use, heatmap and bar
103
+ memoryUsedPercent=$( awk ' /MemTotal:/{total=$2} /MemFree:/{free=$2} /Buffers:/{buffers=$2} /^Cached:/{cached=$2} END {printf "%.1f", (total-free-buffers-cached)*100/total}' ' /proc/meminfo' )
104
+ memoryHeatmap=$( HeatmapGenerator ${memoryUsedPercent} )
105
+ memoryBar=$( BarGenerator ${memoryUsedPercent} 10)
100
106
}
101
107
102
- CPUHeatmapGenerator () {
103
- x=$( echo " scale=2; ($1 /$2 )*100" | bc)
104
- load=$( printf " %.0f" " ${x} " )
108
+ # Provides a color based on a provided percentage
109
+ # takes in one or two parameters
110
+ HeatmapGenerator () {
111
+ # if one number is provided, just use that percentage to figure out the colors
112
+ if [ -z " $2 " ]; then
113
+ load=$( printf " %.0f" " $1 " )
114
+ # if two numbers are provided, do some math to make a percentage to figure out the colors
115
+ else
116
+ load=$( printf " %.0f" " $( echo " scale=2; ($1 /$2 )*100" | bc) " )
117
+ fi
105
118
119
+ # Color logic
120
+ # | green | yellow | red ->
121
+ # 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
106
122
if [ ${load} -lt 75 ]; then
107
123
out=${greenColor}
108
124
elif [ ${load} -lt 90 ]; then
@@ -114,6 +130,40 @@ CPUHeatmapGenerator () {
114
130
echo $out
115
131
}
116
132
133
+ # Provides a "bar graph"
134
+ # takes in two or three parameters
135
+ # $1: percentage filled
136
+ # $2: max length of the bar
137
+ # $3: colored flag, if "color" backfill with color
138
+ BarGenerator () {
139
+ # number of filled in cells in the bar
140
+ barNumber=$( printf %.f " $( echo " scale=1; (($1 /100)*$2 )" | bc) " )
141
+ frontFill=$( for i in $( seq $barNumber ) ; do echo -n ' ■' ; done)
142
+
143
+ # remaining "unfilled" cells in the bar
144
+ backfillNumber=$(( $2 - ${barNumber} ))
145
+
146
+ # if the filled in cells is less than the max length of the bar, fill it
147
+ if [ " $barNumber " -lt " $2 " ]; then
148
+ # if the bar should be colored
149
+ if [ " $3 " = " color" ]; then
150
+ # fill the rest in color
151
+ backFill=$( for i in $( seq $backfillNumber ) ; do echo -n ' ■' ; done)
152
+ out=" ${redColor}${frontFill}${greenColor}${backFill}${resetColor} "
153
+ # else, it shouldn't be colored in
154
+ else
155
+ # fill the rest with "space"
156
+ backFill=$( for i in $( seq $backfillNumber ) ; do echo -n ' ·' ; done)
157
+ out=" ${frontFill}${backFill} "
158
+ fi
159
+ # else, fill it all the way
160
+ else
161
+ out=$( for i in $( seq $2 ) ; do echo -n ' ■' ; done)
162
+ fi
163
+
164
+ echo $out
165
+ }
166
+
117
167
GetNetworkInformation () {
118
168
# Is Pi-Hole acting as the DHCP server?
119
169
if [[ " ${DHCP_ACTIVE} " == " true" ]]; then
@@ -133,7 +183,7 @@ GetNetworkInformation() {
133
183
dhcpStatus=" Disabled"
134
184
135
185
# if the DHCP Router variable isn't set
136
- # Addresses Issue 3: https://github.com/jpmck/chronometer2/issues/3
186
+ # Issue 3: https://github.com/jpmck/chronometer2/issues/3
137
187
if [ -z ${DHCP_ROUTER+x} ]; then
138
188
DHCP_ROUTER=$( /sbin/ip route | awk ' /default/ { print $3 }' )
139
189
fi
@@ -289,12 +339,6 @@ GetVersionInformation() {
289
339
fi
290
340
}
291
341
292
- outputDHCPInformation () {
293
- echo " DHCP SERVER ================================================"
294
- printf " %-10s${dhcpHeatmap} %-19s${resetColor} %-10s${dhcpIPv6Heatmap} %-19s${resetColor} \n" " Status:" " ${dhcpStatus} " " IPv6:" ${dhcpIPv6Status}
295
- printf " ${dhcpInfo} \n"
296
- }
297
-
298
342
outputJSON () {
299
343
GetSummaryInformation
300
344
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} }"
@@ -307,47 +351,64 @@ outputLogo() {
307
351
308
352
echo -e " ${greenColor} ' ${redColor} ' ${magentaColor} ' ${yellowColor} '${greenColor} -${blueColor} \` ${magentaColor} -${redColor} '${yellowColor} '${greenColor} -${blueColor} \` ${magentaColor} '${redColor} - ${resetColor} Chronometer2 ${chronometer2VersionHeatmap} v${chronometer2Version}${resetColor} "
309
353
310
-
311
- # echo " [0;1;35;95m_[0;1;31;91m__[0m [0;1;33;93m_[0m [0;1;34;94m_[0m [0;1;36;96m_[0m"
312
- # echo -e "[0;1;31;91m|[0m [0;1;33;93m_[0m [0;1;32;92m(_[0;1;36;96m)_[0;1;34;94m__[0;1;35;95m|[0m [0;1;31;91m|_[0m [0;1;32;92m__[0;1;36;96m_|[0m [0;1;34;94m|[0;1;35;95m__[0;1;31;91m_[0m ${versionHeatmap}${versionStatus}"
313
- # echo "[0;1;33;93m|[0m [0;1;32;92m_[0;1;36;96m/[0m [0;1;34;94m|_[0;1;35;95m__[0;1;31;91m|[0m [0;1;33;93m'[0m [0;1;32;92m\/[0m [0;1;36;96m_[0m [0;1;34;94m\[0m [0;1;35;95m/[0m [0;1;31;91m-[0;1;33;93m_)[0m${resetColor} Pi-hole Core ${piholeVersionHeatmap}v${piholeVersion}${resetColor}"
314
- # echo "[0;1;32;92m|_[0;1;36;96m|[0m [0;1;34;94m|_[0;1;35;95m|[0m [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${resetColor} (Web ${webVersionHeatmap}v${webVersion}${resetColor}, FTL ${ftlVersionHeatmap}v${ftlVersion}${resetColor})"
315
354
echo " "
316
355
}
317
356
318
357
outputNetworkInformation () {
319
358
echo " NETWORK ===================================================="
320
- printf " %-10s%-19s%-10s%-19s\n" " Hostname:" " $( hostname) " " Domain:" ${PIHOLE_DOMAIN}
359
+ printf " %-10s%-19s %-10s%-19s\n" " Hostname:" " $( hostname) " " Domain:" ${PIHOLE_DOMAIN}
321
360
printf " %-10s%-19s\n" " IPv4:" " ${IPV4_ADDRESS} "
322
361
printf " %-10s%-19s\n" " IPv6:" " ${IPV6_ADDRESS} "
362
+
363
+ if [[ " ${DHCP_ACTIVE} " == " true" ]]; then
364
+ printf " %-10s${dhcpHeatmap} %-19s${resetColor} %-10s${dhcpIPv6Heatmap} %-19s${resetColor} \n" " DHCP:" " ${dhcpStatus} " " IPv6:" ${dhcpIPv6Status}
365
+ printf " ${dhcpInfo} \n"
366
+ fi
323
367
}
324
368
325
369
outputPiholeInformation () {
326
370
echo " PI-HOLE ===================================================="
327
- printf " %-10s${piHoleHeatmap} %-19s${resetColor} %-10s${ftlHeatmap} %-19s${resetColor} \n" " Status:" " ${piHoleStatus} " " FTL:" " ${ftlStatus} "
371
+ printf " %-10s${piHoleHeatmap} %-19s${resetColor} %-10s${ftlHeatmap} %-19s${resetColor} \n" " Status:" " ${piHoleStatus} " " FTL:" " ${ftlStatus} "
328
372
}
329
373
330
374
outputPiholeStats () {
331
375
# Pi-Hole Information
332
376
echo " STATS ======================================================"
333
- printf " %-10s%0.0f\n" " Blocking:" " ${domains_being_blocked} "
334
- printf " %-10s%-19s%-10s%-19s\n" " Queries:" " ${dns_queries_today} " " Pi-holed:" " ${ads_blocked_today} (${ads_percentage_today} %)"
335
- printf " %-10s%-39s\n" " Latest:" " ${domain} "
377
+ printf " %-10s%-49s\n" " Blocking:" " ${domains_being_blocked} domains"
378
+ printf " %-10s[%-40s] %-5s\n" " Pi-holed:" " ${adsBlockedBar} " " ${ads_percentage_today} %"
379
+ printf " %-10s%-49s\n" " Pi-holed:" " ${ads_blocked_today} out of ${dns_queries_today} queries"
380
+ printf " %-10s%-39s\n" " Latest:" " ${latestBlocked} "
381
+ printf " %-10s%-39s\n" " Top Ad:" " ${topBlocked} "
382
+ if [[ " ${DHCP_ACTIVE} " != " true" ]]; then
383
+ printf " %-10s%-39s\n" " Top Dmn:" " ${topDomain} "
384
+ printf " %-10s%-39s\n" " Top Clnt:" " ${topClient} "
385
+ fi
336
386
}
337
387
338
388
outputSystemInformation () {
339
389
# System Information
340
390
echo " SYSTEM ====================================================="
341
- printf " %-10s%-19s\n" " Uptime:" " ${systemUptime} "
391
+ # Uptime
392
+ printf " %-10s%-39s\n" " Uptime:" " ${systemUptime} "
393
+
394
+ # Temp and Loads
342
395
printf " %-10s${tempHeatMap} %-19s${resetColor} " " CPU Temp:" " ${temperature} "
343
396
printf " %-10s${cpuLoad1Heatmap} %-4s${resetColor} , ${cpuLoad5Heatmap} %-4s${resetColor} , ${cpuLoad15Heatmap} %-4s${resetColor} \n" " CPU Load:" " ${cpuLoad1} " " ${cpuLoad5} " " ${cpuLoad15} "
344
- printf " %-10s%-19s%-10s${cpuLoad1Heatmap} %-19s${resetColor} " " Memory:" " ${memoryUsedPercent} %" " CPU Load:" " ${cpuPercent} %"
397
+
398
+ # Memory and CPU bar
399
+ printf " %-10s[${memoryHeatmap} %-10s${resetColor} ] %-5s %-10s[${cpuLoad1Heatmap} %-10s${resetColor} ] %-5s" " Memory:" " ${memoryBar} " " ${memoryUsedPercent} %" " CPU Load:" " ${cpuBar} " " ${cpuPercent} %"
345
400
}
346
401
347
402
normalChrono () {
348
403
for (( ; ; )) ; do
349
404
GetSummaryInformation
350
- domain=$( GetFTLData recentBlocked)
405
+
406
+ latestBlocked=$( GetFTLData recentBlocked)
407
+ topBlocked=$( GetFTLData " top-ads (1)" | awk ' {print $3}' )
408
+
409
+ topDomain=$( GetFTLData " top-domains (1)" | awk ' {print $3}' )
410
+ topClient=$( GetFTLData " top-clients (1)" | awk ' {print $3}' )
411
+
351
412
clear
352
413
353
414
# Get Config variables
@@ -358,7 +419,6 @@ normalChrono() {
358
419
outputPiholeInformation
359
420
outputPiholeStats
360
421
outputNetworkInformation
361
- outputDHCPInformation
362
422
outputSystemInformation
363
423
364
424
# Get our information
@@ -439,7 +499,7 @@ if [[ $# = 0 ]]; then
439
499
echo " "
440
500
printf " Chronometer2 will start in"
441
501
442
- for i in 5 4 3 2 1
502
+ for i in 3 2 1
443
503
do
444
504
printf " $i ..."
445
505
sleep 1
0 commit comments