-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathh-stats.sh
executable file
·206 lines (175 loc) · 7.09 KB
/
h-stats.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
# Set the log file path
log_basename="/var/log/miner/custom/custom"
log_name="$log_basename.log"
log_head_name="${log_basename}_head.log"
# Update the configuration file path
conf_name="/hive/miners/custom/qubminer/appsettings.json"
# Function to calculate the miner version
get_miner_version() {
local ver="${custom_version}"
[[ -n "${epoh_runner}" ]] && ver="${ver}, ${epoh_runner}"
[[ -n "${gpu_runner}" ]] && ver="${ver}, ${gpu_runner}"
[[ -n "${cpu_runner}" ]] && ver="${ver}, ${cpu_runner}"
echo "$ver"
}
# Updated function to calculate miner uptime
get_miner_uptime() {
local uptime=0
local log_time=$(stat --format='%Y' "$log_name")
if [ -e "$conf_name" ]; then
local conf_time=$(stat --format='%Y' "$conf_name")
let uptime=log_time-conf_time
fi
echo $uptime
}
# Function to get log time difference
get_log_time_diff() {
local a=0
let a=$(date +%s)-$(stat --format='%Y' "$log_name")
echo $a
}
# Function to extract and validate hashrate
extract_and_validate_hashrate() {
local line="$1"
local hashrate=$(echo "$line" | grep -oP '\d+ avg it/s' | awk '{print $1}')
if [[ $hashrate =~ ^[0-9]+(\.[0-9]+)?$ ]] && (( $(echo "$hashrate > 0" | bc -l) )); then
echo "$hashrate"
else
echo "0"
fi
}
# Function to get last valid hashrate
get_last_valid_hashrate() {
local device_type="$1"
local last_lines=$(grep -E "\[${device_type}]" "$log_name" | tail -n 20)
local last_hashrate=0
while read -r line; do
local current_hashrate=$(extract_and_validate_hashrate "$line")
if (( $(echo "$current_hashrate > 0" | bc -l) )); then
last_hashrate=$current_hashrate
break
fi
done <<< "$last_lines"
echo "$last_hashrate"
}
# Updated function to extract shares from a log line
extract_shares() {
local line="$1"
local shares=$(echo "$line" | grep -oP "(SHARES|SOLS): \K\d+/\d+ \(R:\d+\)")
local accepted=$(echo "$shares" | cut -d'/' -f2 | cut -d' ' -f1)
local rejected=$(echo "$shares" | grep -oP "R:\K\d+")
echo "$accepted $rejected"
}
# Extract version and runner information
custom_version=$(grep -Po "(?<=Version ).*" "$log_name" | tail -n1)
gpu_runner=$(tac "$log_name" | grep -m1 -Po "(?<=Trainer: ).*?cuda.*?(?:\d+(?:\.\d+)*)(?= is (starting|running))")
cpu_runner=$(tac "$log_name" | grep -m1 -Po "(?<=Trainer: ).*?cpu.*?(?:\d+(?:\.\d+)*)(?= is (starting|running))")
epoh_runner=$(grep -Po "E:\d+" "$log_name" | tail -n1)
# Check if the log file is recent enough
diffTime=$(get_log_time_diff)
maxDelay=300
if [ "$diffTime" -lt "$maxDelay" ]; then
ver=$(get_miner_version)
hs_units="hs"
algo="qubic"
uptime=$(get_miner_uptime)
[[ $uptime -lt 60 ]] && head -n 150 $log_name > $log_head_name
# Update GPU count detection
gpu_count=$(grep -oP "\[GPU\] Trainer: \K\d+" "$log_name" | tail -n 1)
[[ -z $gpu_count ]] && gpu_count=0
# New CPU detection method
cpu_count=$(grep -cE "\[(AVX512|AVX2|GENERIC)\]" "$log_name")
[[ $cpu_count -gt 0 ]] && cpu_count=1 || cpu_count=0
# Get CPU temperature if CPU is used
[[ $cpu_count -eq 1 ]] && cpu_temp=$(cpu-temp) || cpu_temp=null
# Initialize arrays and counters
declare -a hs temp fan bus_numbers
let ac=0 rj=0
# Extract CPU hashrate (avg it/s)
cpu_hs=$(get_last_valid_hashrate "(AVX512|AVX2|GENERIC)")
# Extract individual GPU hashrates from the log format
gpu_hashrates=$(grep "\[GPU\] Trainer:" "$log_name" | grep -v "Switching ID" | grep -v "Found a share" | grep -v "Fine-tuning completed" | tail -n $gpu_count | grep -oP "GPU #\d+: \K\d+ it/s")
# Process GPU data
if [[ $gpu_count -gt 0 ]]; then
# Extract GPU shares information
gpu_shares=$(grep "\[CUDA\]" "$log_name" | grep -E "(SHARES|SOLS):" | tail -n 1)
if [[ -n "$gpu_shares" ]]; then
read gpu_accepted gpu_rejected <<< $(extract_shares "$gpu_shares")
[[ -z "$gpu_accepted" ]] && gpu_accepted=0
[[ -z "$gpu_rejected" ]] && gpu_rejected=0
let ac=$ac+$gpu_accepted
let rj=$rj+$gpu_rejected
fi
# Extract GPU temperature, fan, and bus information
gpu_temp=$(jq '.temp' <<< "$gpu_stats")
gpu_fan=$(jq '.fan' <<< "$gpu_stats")
gpu_bus=$(jq '.busids' <<< "$gpu_stats")
if [[ $cpu_indexes_array != '[]' ]]; then
gpu_temp=$(jq -c "del(.$cpu_indexes_array)" <<< "$gpu_temp") &&
gpu_fan=$(jq -c "del(.$cpu_indexes_array)" <<< "$gpu_fan") &&
gpu_bus=$(jq -c "del(.$cpu_indexes_array)" <<< "$gpu_bus")
fi
# Process individual GPU data
for (( i=0; i < ${gpu_count}; i++ )); do
hs[$i]=$(echo "$gpu_hashrates" | sed -n "$((i+1))p" | awk '{print $1}')
[[ -z ${hs[$i]} ]] && hs[$i]=0
temp[$i]=$(jq .[$i] <<< "$gpu_temp")
fan[$i]=$(jq .[$i] <<< "$gpu_fan")
busid=$(jq .[$i] <<< "$gpu_bus")
bus_numbers[$i]=$(echo $busid | cut -d ":" -f1 | cut -c2- | awk -F: '{ printf "%d\n",("0x"$1) }')
done
fi
# Process CPU stats
if [[ $cpu_count -eq 1 ]]; then
# Extract CPU shares information
cpu_shares=$(grep -E "\[(AVX512|AVX2|GENERIC)\]" "$log_name" | grep -E "(SHARES|SOLS):" | tail -n 1)
if [[ -n "$cpu_shares" ]]; then
read cpu_accepted cpu_rejected <<< $(extract_shares "$cpu_shares")
[[ -z "$cpu_accepted" ]] && cpu_accepted=0
[[ -z "$cpu_rejected" ]] && cpu_rejected=0
let ac=$ac+$cpu_accepted
let rj=$rj+$cpu_rejected
fi
# Add CPU data to arrays
hs+=($cpu_hs)
temp+=($cpu_temp)
fan+=("")
bus_numbers+=("null")
fi
# Adjust shares if both GPU and CPU are in use
if [[ $gpu_count -gt 0 && $cpu_count -eq 1 ]]; then
ac=$((ac / 2))
rj=$((rj / 2))
fi
# Calculate total GPU hashrate
gpu_total_hs=$(tail -n 20 "$log_name" | grep -oP '\[CUDA\].*?(\d+) avg it/s' | tail -n 1 | grep -oP '\d+(?= avg it/s)')
gpu_total_hs=${gpu_total_hs:-0}
# Calculate total hashrate (GPU + CPU)
total_hs=$(echo "$gpu_total_hs + $cpu_hs" | bc)
# Calculate total hashrate in khs
if (( $(echo "$total_hs > 0" | bc -l) )); then
khs=$(echo "scale=6; $total_hs / 1000" | bc)
else
khs=0
fi
# Prepare stats JSON
stats=$(jq -nc \
--arg khs "$khs" \
--arg hs_units "$hs_units" \
--argjson hs "$(printf '%s\n' "${hs[@]}" | jq -cs '.')" \
--argjson temp "$(printf '%s\n' "${temp[@]}" | jq -cs '.')" \
--argjson fan "$(printf '%s\n' "${fan[@]}" | jq -cs '.')" \
--arg uptime "$uptime" \
--arg ver "$ver" \
--arg ac "$ac" --arg rj "$rj" \
--arg algo "$algo" \
--argjson bus_numbers "$(printf '%s\n' "${bus_numbers[@]}" | jq -cs '.')" \
'{$hs, $hs_units, $temp, $fan, $uptime, $ver, ar: [$ac, $rj], $algo, $bus_numbers}')
else
stats=""
khs=0
fi
# Output results
echo $khs
echo $stats