-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonitor.sh
More file actions
361 lines (314 loc) · 11.8 KB
/
monitor.sh
File metadata and controls
361 lines (314 loc) · 11.8 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
# Version number
VERSION="1.2.9"
# Function to check if API URL is reachable with SSL
check_ssl_support() {
local url="https://api.rg3d.eu:8443/api.php"
local rig_conf_path=~/rig.conf
# Initialize ssl_supported with the current value from rig.conf or default to "false"
ssl_supported=$(grep -E "^ssl_supported=" "$rig_conf_path" | cut -d '=' -f 2)
if [ -z "$ssl_supported" ]; then
ssl_supported="false"
fi
# Check if SSL is supported
if curl --output /dev/null --silent --head --fail --connect-timeout 5 --max-time 10 "$url"; then
if [ "$ssl_supported" != "true" ]; then
echo "SSL is supported. Updating rig.conf."
ssl_supported="true"
update_rig_conf "$miner_id" "$miner_token"
fi
else
if [ "$ssl_supported" != "false" ]; then
echo "SSL is not supported. Updating rig.conf."
ssl_supported="false"
update_rig_conf "$miner_id" "$miner_token"
fi
fi
}
# Function to authenticate and get miner_token
authenticate() {
echo "Authenticating with the server..."
local url="https://api.rg3d.eu:8443/api.php"
local data="password=$rig_pw"
if [ -n "$miner_id" ]; then
data+="&miner_id=$miner_id"
fi
response=$(curl -s -X POST -d "$data" "$url")
local status=$(echo "$response" | jq -r '.status')
miner_token=$(echo "$response" | jq -r '.miner_token')
new_miner_id=$(echo "$response" | jq -r '.miner_id')
if [ "$status" == "success" ] && [ -n "$miner_token" ] && [ "$miner_token" != "null" ]; then
# Save miner_token and miner_id to rig.conf
update_rig_conf "$new_miner_id" "$miner_token"
echo "Authentication successful. Miner Token obtained."
else
echo "Authentication failed. Response: $response"
echo "Resetting credentials in rig.conf due to authentication failure..."
update_rig_conf "" "" # Clear credentials
exit 1
fi
}
# Function to send data to PHP script or echo if dryrun
send_data() {
local url="https://api.rg3d.eu:8443/api.php"
# Load miner_token and miner_id from rig.conf
miner_token=$(grep -E "^miner_token=" ~/rig.conf | cut -d '=' -f 2)
miner_id=$(grep -E "^miner_id=" ~/rig.conf | cut -d '=' -f 2)
# Build data payload
local data="hw_brand=$hw_brand&hw_model=$hw_model&ip=$ip&summary=$summary_json&pool=$pool_json&battery=$battery&cpu_temp=$cpu_temp_json&cpu_max=$cpu_count&monitor_version=$VERSION&scheduler_version=$scheduler_version"
if [ -n "$miner_id" ]; then
data+="&miner_id=$miner_id"
fi
# Handle authentication
if [ -n "$miner_token" ]; then
auth_header="Authorization: Bearer $miner_token"
elif [ -n "$rig_pw" ]; then
data+="&password=$rig_pw"
fi
# Send data
if [ -n "$auth_header" ]; then
response=$(curl -s -X POST -H "$auth_header" -d "$data" "$url")
else
response=$(curl -s -X POST -d "$data" "$url")
fi
echo "Response from server: $response"
# Check if the response indicates an error
local status=$(echo "$response" | jq -r '.status')
if [ "$status" == "error" ]; then
echo "Error received from server: $response"
echo "Resetting credentials in rig.conf due to server error..."
update_rig_conf "" "" # Clear credentials
authenticate # Re-authenticate
fi
# Extract miner_token and miner_id from the response if provided
new_miner_token=$(echo "$response" | jq -r '.miner_token')
new_miner_id=$(echo "$response" | jq -r '.miner_id')
if [ -n "$new_miner_token" ] && [ "$new_miner_token" != "null" ]; then
update_rig_conf "$new_miner_id" "$new_miner_token"
fi
}
# Function to update rig.conf with miner_id, miner_token, and ssl_supported
update_rig_conf() {
local miner_id=$1
local miner_token=$2
local rig_conf_path=~/rig.conf
if [ -f "$rig_conf_path" ]; then
# Update miner_id
if grep -q "^miner_id=" "$rig_conf_path"; then
sed -i "s/^miner_id=.*/miner_id=$(printf '%q' "$miner_id")/" "$rig_conf_path"
else
echo "miner_id=$miner_id" >> "$rig_conf_path"
fi
# Update miner_token
if [ -n "$miner_token" ]; then
if grep -q "^miner_token=" "$rig_conf_path"; then
sed -i "s/^miner_token=.*/miner_token=$(printf '%q' "$miner_token")/" "$rig_conf_path"
else
echo "miner_token=$miner_token" >> "$rig_conf_path"
fi
fi
# Update ssl_supported
if grep -q "^ssl_supported=" "$rig_conf_path"; then
sed -i "s/^ssl_supported=.*/ssl_supported=$(printf '%q' "$ssl_supported")/" "$rig_conf_path"
else
echo "ssl_supported=$ssl_supported" >> "$rig_conf_path"
fi
else
echo "rig.conf file not found. Creating a new one."
echo "miner_id=$miner_id" > "$rig_conf_path"
[ -n "$miner_token" ] && echo "miner_token=$miner_token" >> "$rig_conf_path"
echo "ssl_supported=$ssl_supported" >> "$rig_conf_path"
fi
}
# Ensure jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is required but not installed. Installing jq..."
if [ "$(uname -o)" == "Android" ]; then
pkg install jq -y
else
sudo apt-get install jq -y
fi
fi
# Determine SSL support and update rig.conf only if not already set
ssl_supported="false"
if [ -f ~/rig.conf ]; then
ssl_supported=$(grep -E "^ssl_supported=" ~/rig.conf | cut -d '=' -f 2)
fi
if [ -z "$ssl_supported" ]; then
if check_ssl_support; then
ssl_supported="true"
else
ssl_supported="false"
fi
# Update rig.conf with the SSL support status
update_rig_conf "$miner_id"
fi
# Get the number of CPUs
cpu_count=$(lscpu | grep -E '^CPU\(s\):' | awk '{print $2}')
# Check if connectivity to Internet is given
x=$(ping -c1 google.com 2>&1 | grep unknown)
if [ ! "$x" = "" ]; then
# For Android if connection is down try to restart Wifi network
if su -c true 2>/dev/null; then
# SU rights are available
echo "Connection to Internet broken. Restarting Network!"
su -c input keyevent 26
su -c svc wifi disable
su -c svc wifi enable
sleep 10
fi
fi
# Parse arguments
dryrun=false
if [ "$1" == "--dryrun" ]; then
dryrun=true
fi
# 1. Check if ~/rig.conf exists and necessary credentials are set
if [ -f ~/rig.conf ]; then
rig_pw=$(grep -E "^rig_pw=" ~/rig.conf | cut -d '=' -f 2)
miner_id=$(grep -E "^miner_id=" ~/rig.conf | cut -d '=' -f 2)
miner_token=$(grep -E "^miner_token=" ~/rig.conf | cut -d '=' -f 2)
if [ -z "$rig_pw" ] && [ -z "$miner_token" ]; then
echo "Neither rig_pw, password, nor miner_token is set in ~/rig.conf. Exiting."
exit 1
fi
else
echo "~/rig.conf file not found. Exiting."
exit 1
fi
# Authenticate if miner_token is missing
if [ -z "$miner_token" ]; then
if [ -n "$rig_pw" ] || [ -n "$password" ]; then
authenticate
else
echo "Cannot authenticate because neither miner_token nor rig_pw/password is available."
exit 1
fi
fi
# 2. Check hardware brand and format to uppercase
if [ -f /sys/firmware/devicetree/base/model ]; then
hw_brand=$(cat /sys/firmware/devicetree/base/model | awk '{print $1}' | tr '[:lower:]' '[:upper:]')
elif [ -n "$(uname -o | grep Android)" ]; then
hw_brand=$(getprop ro.product.brand | tr '[:lower:]' '[:upper:]')
elif [ "$(uname -s)" == "Linux" ]; then
# For GNU/Linux systems, fetch PRETTY_NAME from lsb_release -a
hw_brand=$(lsb_release -a 2>/dev/null | grep "Description:" | cut -d ':' -f 2- | sed 's/^[ \t]*//;s/[ \t]*$//')
else
hw_brand=$(uname -o | tr '[:lower:]' '[:upper:]')
fi
# 3. Check hardware model and format to uppercase
if [ -f /sys/firmware/devicetree/base/model ]; then
hw_model=$(cat /sys/firmware/devicetree/base/model | awk '{print $2 $3}')
elif [ -n "$(uname -o | grep Android)" ]; then
hw_model=$(getprop ro.product.model)
else
hw_model=$(uname -m)
fi
hw_model=$(echo "$hw_model" | tr '[:lower:]' '[:upper:]')
# 4. Get local IP address (prefer ethernet over wlan, IPv4 only)
if [ -n "$(uname -o | grep Android)" ]; then
# For Android
ip=$(termux-wifi-connectioninfo | grep -oP '(?<="ip": ")[^"]*')
# Check if IP is empty or 0.0.0.0
if [ -z "$ip" ] || [ "$ip" = "0.0.0.0" ]; then
# Fallback to previous method if no valid IP is found
ip=$(ifconfig 2> /dev/null | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '[0-9.]*' | grep -v 127.0.0.1)
# Check again if IP is empty or 0.0.0.0
if [ -z "$ip" ] || [ "$ip" = "0.0.0.0" ]; then
# If no IP address was found, try with 'su' rights
if su -c true 2>/dev/null; then
# SU rights are available
ip=$(su -c ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1)
# Final check for IP validity
if [ -z "$ip" ] || [ "$ip" = "0.0.0.0" ]; then
echo "Failed to retrieve a valid IP address. Please check your network."
fi
else
echo "SU rights are not available. Unable to retrieve a valid IP address."
fi
fi
fi
else
# For other Unix systems
ip=$(ip -4 -o addr show | awk '$2 !~ /lo|docker/ {print $4}' | cut -d "/" -f 1 | head -n 1)
# Check if IP is empty or 0.0.0.0
if [ -z "$ip" ] || [ "$ip" = "0.0.0.0" ]; then
echo "Failed to retrieve a valid IP address. Please check your network."
fi
fi
# 5. Check if ccminer is running, exit if not
if ! screen -list | grep -q "\.CCminer"; then
echo "ccminer not running. Exiting."
exit 1
fi
# 6. Get summary output of ccminer API socket (default port)
summary_raw=$(echo 'summary' | nc 127.0.0.1 4068 | tr -d '\0')
summary_raw=${summary_raw%|} # Remove trailing '|'
summary_json=$(echo "$summary_raw" | jq -R 'split(";") | map(split("=")) | map({(.[0]): .[1]}) | add')
# 7. Get pool output of ccminer API socket (default port)
pool_raw=$(echo 'pool' | nc 127.0.0.1 4068 | tr -d '\0')
pool_raw=${pool_raw%|} # Remove trailing '|'
pool_json=$(echo "$pool_raw" | jq -R 'split(";") | map(split("=")) | map({(.[0]): .[1]}) | add')
# 8. Check battery status if OS is Termux and termux-battery-status is available
if [ "$(uname -o)" == "Android" ]; then
if ! command -v termux-battery-status &> /dev/null; then
battery='{}'
else
battery=$(timeout 2s termux-battery-status | jq -c '.')
if [ -z "$battery" ]; then
battery='{"health":"0","percentage":"0","plugged":"0","status":"0","temperature":"0.0","current":"0"}'
fi
fi
else
battery="{}"
fi
# 9. Check CPU temperature
cpu_temp=0
# Check for Raspberry Pi or other Linux systems
if [ -f /sys/class/thermal/thermal_zone0/temp ]; then
cpu_temp=$(awk '{printf "%.1f", $1 / 1000}' /sys/class/thermal/thermal_zone0/temp)
fi
# If still zero, check for Android devices
if [ "$cpu_temp" == "0" ] || [ -z "$cpu_temp" ]; then
if [ -n "$(uname -o | grep Android)" ]; then
# Attempt to get temperature without SU first
cpu_temp_raw=$("~/vcgencmd measure_temp" 2>/dev/null)
cpu_temp=$(echo "$cpu_temp_raw" | grep -oP 'temp=\K\d+\.\d+')
# If no valid temperature was obtained, try with SU
if ! [[ "$cpu_temp" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
cpu_temp_raw=$(su -c ~/vcgencmd measure_temp 2>/dev/null)
cpu_temp=$(echo "$cpu_temp_raw" | grep -oP 'temp=\K\d+\.\d+')
fi
# Check if the temperature is still not valid or if the command simply failed
if ! [[ "$cpu_temp" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
cpu_temp=0
fi
fi
fi
# Additional check if cpu_temp is still 0
if [ "$cpu_temp" == "0" ] || [ -z "$cpu_temp" ]; then
for cpuseq in $(seq 1 60); do
v1=$(cat /sys/devices/virtual/thermal/thermal_zone$cpuseq/type 2>/dev/null)
v2="back_temp"
if [[ "$v1" == "$v2" ]]; then
cpu_temp_raw=$(cat /sys/devices/virtual/thermal/thermal_zone$cpuseq/temp 2>/dev/null)
cpu_temp=$((cpu_temp_raw / 1000))
break
fi
done
fi
# Format cpu_temp as JSON
cpu_temp_json="{\"temp\":\"$cpu_temp\"}"
# Get the scheduler version from the jobscheduler.sh file
scheduler_version=$(grep -E "^VERSION=" ~/jobscheduler.sh | cut -d '=' -f 2 | tr -d '"')
# Authenticate if miner_token is missing and rig_pw is available
if [ -z "$miner_token" ]; then
if [ -n "$rig_pw" ]; then
authenticate
else
echo "Cannot authenticate because neither miner_token nor rig_pw is available."
exit 1
fi
fi
# Send data to PHP script or echo if dryrun
send_data