-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsmnotify.pl
executable file
·242 lines (230 loc) · 7.54 KB
/
smnotify.pl
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
#!/usr/bin/perl
# This file is part of IFMI SeedManager.
#
# SeedManager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
use strict;
use warnings;
use YAML qw( DumpFile LoadFile );
use Email::Simple::Creator;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP;
use Email::Sender::Transport::SMTP::TLS;
use Try::Tiny;
require '/opt/ifmi/sm-common.pl';
my $conf = &getConfig;
my %conf = %{$conf};
my $conffile = "/opt/ifmi/seedmanager.conf";
my $miner_name = `hostname`;
chomp $miner_name;
my $nicget = `/sbin/ifconfig`;
my $iptxt = "";
while ($nicget =~ m/(\w\w\w\w?\d)\s.+\n\s+inet addr:(\d+\.\d+\.\d+\.\d+)\s/g) {
$iptxt = $2;
}
my $now = POSIX::strftime("%m/%d at %H:%M", localtime());
sub doEmail {
my $emaildo = $conf{monitoring}{do_email};
if ($emaildo == 1) {
my $msg = "";
my $ispriv = &CGMinerIsPriv;
if ($ispriv ne "S") {
$msg .= "No miner data available - mining process may be stopped or hung!\n";
} else {
my $hashlo = $conf{monitoring}{monitor_hash_lo};
my $rejhi = $conf{monitoring}{monitor_reject_hi};
my @ascs = &getFreshASCData(1);
for (my $i=0;$i<@ascs;$i++) {
my $ascid = "ASC$i";
# stuff with settings
my $rr = "0";
my $gsha = $ascs[$i]{'shares_accepted'};
my $gshr = $ascs[$i]{'shares_invalid'}; $gshr = 0 if ($gshr eq "");
if ($gshr > 0) {
$rr = sprintf("%.2f", $gshr / ($gsha + $gshr)*100);
}
if (($gsha > 0) && ($rr > ${$conf}{monitoring}{monitor_reject_hi})) {
if (!(defined($conf{monitoring}{alert}{$ascid}{rejhi}))) {
$msg .= "$ascid reject rate is: $rr%. ";
$msg .= "Alert level is: $conf{monitoring}{monitor_reject_hi}%\n";
$conf{monitoring}{alert}{$ascid}{rejhi} = 1;
}
} else {
if (defined($conf{monitoring}{alert}{$ascid}{rejhi})) {
$msg .= "$ascid reject rate is: $rr%. ";
$msg .= "Alert has cleared.\n";
delete $conf{monitoring}{alert}{$ascid}{rejhi};
}
}
my $ghashrate = $ascs[$i]{'hashrate'};
$ghashrate = $ascs[$i]{'hashavg'} if ($ghashrate eq "");
if ($ghashrate < $conf{monitoring}{monitor_hash_lo}) {
if (!(defined($conf{monitoring}{alert}{$ascid}{hashlo}))) {
$msg .= "$ascid hashrate is: $ghashrate Kh/s. ";
$msg .= "Alert level is: $conf{monitoring}{monitor_hash_lo}Kh/s\n";
$conf{monitoring}{alert}{$ascid}{hashlo} = 1;
}
} else {
if (defined($conf{monitoring}{alert}{$ascid}{hashlo})) {
$msg .= "$ascid hashrate is: $ghashrate Kh/s. ";
$msg .= "Alert has cleared.\n";
delete $conf{monitoring}{alert}{$ascid}{hashlo};
}
}
# stuff without settings
my $ghealth = $ascs[$i]{'status'};
if ($ghealth ne "Alive") {
if (!(defined($conf{monitoring}{alert}{$ascid}{health}))) {
$msg .= "$ascid health is: $ghealth.\n";
$conf{monitoring}{alert}{$ascid}{health} = 1;
}
} else {
if (defined($conf{monitoring}{alert}{$ascid}{health})) {
$msg .= "$ascid health is: $ghealth. ";
$msg .= "Alert has cleared.\n";
delete $conf{monitoring}{alert}{$ascid}{health};
}
}
my $ghwe = $ascs[$i]{'hardware_errors'};
if ($ghwe > 0) {
if (!(defined($conf{monitoring}{alert}{$ascid}{health}))) {
$msg .= "$ascid has $ghwe Hardware Errors.\n";
$conf{monitoring}{alert}{$ascid}{health} = 1;
}
} else {
if (defined($conf{monitoring}{alert}{$ascid}{health})) {
$msg .= "$ascid has $ghwe Hardware Errors. ";
$msg .= "Alert has cleared.\n";
delete $conf{monitoring}{alert}{$ascid}{health};
}
}
}
my @pools = &getCGMinerPools(1);
if (@pools) {
for (my $i=0;$i<@pools;$i++) {
my $phealth = ${$pools[$i]}{'status'};
my $pname = ${$pools[$i]}{'url'};
my $shorturl = "";
if ($pname =~ m|://(\w+-?\w+\.)?(\w+-?\w+\.\w+:\d+)|) {
$shorturl = $2;
}
my $pactive = 0;
for (my $g=0;$g<@ascs;$g++) {
if ($pname eq $ascs[$g]{'pool_url'}) {
$pactive++;
}
}
my $poola; my $poolnum = 0;
for (keys %{$conf{pools}}) {
if ($pname eq ${$conf}{pools}{$_}{url}) {
$poola = ${$conf}{pools}{$_}{alias};
$poolnum = $_;
}
}
my $poolid = "pool$poolnum";
my $prr = "0";
my $pacc = ${$pools[$i]}{'accepted'};
my $prej = ${$pools[$i]}{'rejected'};
if (defined $prej && $prej > 0) {
$prr = sprintf("%.2f", $prej / ($pacc + $prej)*100);
}
my $prhl = ${$conf}{pools}{$poolnum}{pool_reject_hi};
if ((defined $prej) && (defined $prhl) && ($prr > $prhl)) {
if (!(defined($conf{monitoring}{alert}{$poolid}{rejhi}))) {
$msg .= "Pool $i ($shorturl) reject rate is: $prr%. ";
$msg .= "Alert level is: $prhl%\n";
$conf{monitoring}{alert}{$poolid}{rejhi} = 1;
}
} else {
if (defined($conf{monitoring}{alert}{$poolid}{rejhi})) {
$msg .= "Pool $i ($shorturl) reject rate is: $prr%. ";
$msg .= "Alert has cleared.\n";
delete $conf{monitoring}{alert}{$poolid}{rejhi};
}
}
my $pnotify = $conf{pools}{$poolnum}{pnotify};
if (($phealth ne "Alive") && ($pnotify == 1)) {
if (!(defined($conf{monitoring}{alert}{$poolid}{phealth}))) {
$msg .= "Pool $i ($shorturl) health is $phealth.";
$conf{monitoring}{alert}{$poolid}{phealth} = 1;
}
} else {
if (($phealth eq "Alive") && (defined($conf{monitoring}{alert}{$poolid}{phealth}))) {
$msg .= "Pool $i ($shorturl) health is $phealth. ";
$msg .= "Alert has cleared.\n";
delete $conf{monitoring}{alert}{$poolid}{phealth};
}
}
}
}
DumpFile($conffile, $conf);
}
if ($msg ne "") {
my $email = "Alerts for $miner_name ($iptxt) - $now\n";
$email .= $msg;
my $subject = "Alerts for $miner_name ($iptxt) - $now";
&sendAnEmail($subject, $email);
}
}
}
sub sendAnEmail {
my ($subject,$message) = @_;
my $to = $conf{email}{smtp_to};
my $host = $conf{email}{smtp_host};
my $from = $conf{email}{smtp_from};
my $port = $conf{email}{smtp_port};
my $tls = $conf{email}{smtp_tls};
my $ssl = $conf{email}{smtp_ssl};
if ($subject eq "TEST") {
$subject = "Test Email from $miner_name ($iptxt) - $now";
}
if ($to ne "") {
my $helo = $miner_name . 'nohost.net';
#domain name picked entirely at random, and apparently perfect for the cause.
my $email = Email::Simple->create(
header => [
To => $to,
From => $from,
Subject => $subject,],
body => $message,
);
my $transport = "";
if ($tls == 1) {
$transport = Email::Sender::Transport::SMTP::TLS->new(
host => $host,
port => $port,
helo => $helo,
username => $conf{email}{smtp_auth_user},
password => $conf{email}{smtp_auth_pass},
);
} elsif ($ssl == 1) {
$transport = Email::Sender::Transport::SMTP->new(
host => $host,
port => $port,
helo => $helo,
ssl => $ssl,
sasl_username => $conf{email}{smtp_auth_user},
sasl_password => $conf{email}{smtp_auth_pass},
);
} else {
$transport = Email::Sender::Transport::SMTP->new(
host => $host,
port => $port,
helo => $helo,
);
}
try {
sendmail($email, { transport => $transport });
} finally {
`/usr/bin/touch /tmp/smnotify.lastsent`;
} catch {
return "Email Error: $_";
};
} else {
die "No recipient!\n";
}
}
1;