· 6 years ago · Jan 31, 2020, 11:04 PM
1#!/bin/bash
2
3# Antminer S9 logic for automated database
4# Developed by Tim Brown <tim@timbrown.me>
5# Virginia Beach, Virginia, United States
6# Bend, Oregon, United States
7# Arlington, Texas, United States
8# 2018-09-22 through 2020-02-03
9
10# Licensed under CC BY-NC-ND 4.0 International
11
12mysql --login-path=local temple -s --skip-column-names -e "SELECT ip FROM antminer_s9" > minersKnown.txt
13nmap -n -sn 192.168.1-6.2-254 -oG - | awk '/Up$/{print $2}' > minersOn.txt
14cat minersKnown.txt minersOn.txt | sort | uniq > miners.txt
15
16while read -r line || [[ -n "$line" ]]; do
17 ip=${line};
18 pingable="N";
19 hashrate=0;
20 temperature=0;
21 position=0;
22 if grep -Fxq "$ip" minersOn.txt
23 then
24 pingable='Y';
25
26
27 { wisdom=`echo -n 'stats+pools' | nc -w2 $ip 4028 | tr ',' '\n'`; } 2>/dev/null
28 api="N";
29 if [[ "$wisdom" = *"miner_count"* ]]; then
30 api="Y";
31 position=`echo "${wisdom#*User=}" | head -1`;
32 position=${position##*.}; #get workerID from username.workerID
33 hashrate=`echo "${wisdom##*GHS 5s=}" | head -1`; #gigahashes to 2 decimal places
34 tempR=`echo "${wisdom##*temp2_6=}" | head -1`;
35 tempC=`echo "${wisdom##*temp2_7=}" | head -1`;
36 tempL=`echo "${wisdom##*temp2_8=}" | head -1`;
37 temperature=$tempR
38 if [ $tempC -gt $temperature ]
39 then
40 temperature=$tempC
41 fi
42 if [ $tempL -gt $temperature ]
43 then
44 temperature=$tempL
45 fi
46 hashrate=${hashrate%.*}; #gigahashes as truncated integer
47 terahash=$((hashrate/1000)); #terahashes as truncated integer
48 hashrate=`echo "$hashrate" | awk '{print $0/1000}'`; #terahashes to 3 decimal places
49 fi
50 confidence=55;
51 if [[ "$terahash" -lt "3" || "$api" = "N" ]]
52 then
53 issue="Fatal Error";
54 elif [ "$terahash" -lt "7" ]
55 then
56 right=`echo "${wisdom##*chain_rate6=}" | head -1`;
57 centre=`echo "${wisdom##*chain_rate7=}" | head -1`;
58 left=`echo "${wisdom##*chain_rate8=}" | head -1`;
59 rate=${right%%.*}
60 centre=${centre%%.*}
61 left=${left%%.*}
62 board=6
63 if [ $centre -gt $rate ]
64 then
65 rate=$centre
66 board=7
67 fi
68 if [ $left -gt $rate ]
69 then
70 board=8
71 fi
72 J=$board
73 if [ "$J" = "6" ]
74 then
75 issue="Left and middle boards dead";
76 fi
77 if [ "$J" = "7" ]
78 then
79 issue="Left and right boards dead";
80 fi
81 if [ "$J" = "8" ]
82 then
83 issue="Middle and right boards dead";
84 fi
85 elif [ "$terahash" -lt "11" ]
86 then
87 right=`echo "${wisdom##*chain_rate6=}" | head -1`;
88 centre=`echo "${wisdom##*chain_rate7=}" | head -1`;
89 left=`echo "${wisdom##*chain_rate8=}" | head -1`;
90 rate=${right%%.*}
91 centre=${centre%%.*}
92 left=${left%%.*}
93 board=6
94 if [ $centre -lt $rate ]
95 then
96 rate=$centre
97 board=7
98 fi
99 if [ $left -lt $rate ]
100 then
101 board=8
102 fi
103 J=$board
104 if [ "$J" = "6" ]
105 then
106 issue="Right board dead";
107 fi
108 if [ "$J" = "7" ]
109 then
110 issue="Middle board dead";
111 fi
112 if [ "$J" = "8" ]
113 then
114 issue="Left board dead";
115 fi
116 else
117 issue="None";
118 fi
119echo "INFO: IP $ip SLOT $position PINGABLE $pingable API $api HASHRATE $hashrate TEMP $temperature TH $terahash ISSUE $issue";
120
121
122 fi
123 localrow=$(mysql --login-path=local bend -s --skip-column-names -e "SELECT * FROM antminer_s9 WHERE ip='$ip'");
124 IFS=$'\t' read -r -a miner_old <<< "${localrow}";
125 issue_old=${miner_old[6]};
126 confidence_old=${miner_old[7]};
127 checked=${miner_old[8]};
128 if [[ "$issue_old" = "$issue"* ]]
129 then
130 issue="$issue_old";
131 fi
132 if [ "$issue" = "$issue_old" ]
133 then
134 confidence=$((confidence_old + 1));
135 if [ "$confidence" -gt 99 ]
136 then
137 confidence=99;
138 fi
139 fi
140 if [[ "$pingable" = "Y" ]]
141 then
142 echo "INSERT INTO antminer_s9 VALUES ('$ip', '$position', '$pingable', '$api', $hashrate, $temperature, NOW(), '$issue', $confidence, NULL) ON DUPLICATE KEY UPDATE position='$position', pingable='$pingable', api='$api', hashrate=$hashrate, temperature=$temperature, seen=NOW(), issue='$issue', confidence=$confidence;" | mysql --login-path=local bend;
143 else
144 echo "UPDATE antminer_s9 SET position='$position', pingable='N', api='N', hashrate=0, issue='$issue', confidence='$confidence' WHERE ip='$ip';" | mysql --login-path=local bend;
145 fi
146
147done < miners.txt