Title: Short monitoring script for pool Post by: legkodymov on June 25, 2011, 12:52:22 AM I wrote very short monitoring script that will send email (SMS in my case), if hash rate is lower than threshold. I run it every 10 minutes using cron:
/etc/crontab: */10 * * * * user /home/user/guild.pl It has been written for btcguild.com, but can be changed easily to other pools. Maybe this is exactly what you are looking for. It has following dependencies: #sudo apt-get install libjson-perl sendemail Here it is: Code: #!/usr/bin/perl # This program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. # sudo aptitude install libjson-perl sendemail use strict; use Data::Dumper; use JSON; my $request = 'http://www.btcguild.com/api.php?api_key=xxxxxxxxxxxxxx'; my $email = 'legkoXXX@mailsms.mobicomk.ru'; my $from_email = 'XXXXXXXX@mm.st'; my $smtp_server = 'smtp.fastmail.fm'; my $smtp_user = 'XXXXXX@fastmail.fm'; my $smtp_pass = 'XXXXXXXX'; my $sendemail = "sendemail -f $from_email -t $email -s $smtp_server -xu $smtp_user -o message-format=raw -xp $smtp_pass -m"; my $worker_prefix = 'xxx'; my $filename = '/tmp/.guild_timeouts'; my $dead_thres = 150.0; my $h_timeout = {}; sub main { my @lines = (); read_timeouts(); sysexec('localhost', "wget --output-document=- $request 2>/dev/null", \@lines); return 1 if ($lines[0] =~ /Failed to connect to/); my $json = JSON->new->allow_nonref; my $guild = $json->decode($lines[0]); print_out("\$guild: $guild"); my $workers = $guild->{'workers'}; my $total = 0.0; my @dead; my @choped_dead; for my $name (sort keys (%$workers)) { my $worker_name = $workers->{$name}->{worker_name}; my $hash_rate = $workers->{$name}->{hash_rate}; print "$workers->{$name}->{worker_name}: $workers->{$name}->{hash_rate}\n"; if ($hash_rate < $dead_thres) { push (@dead, $worker_name); } else { update_flag($worker_name, 1); } $total += $hash_rate; } print "Total: $total\n"; my $must_send = 0; for my $name (@dead) { my $choped = $name; $choped =~ s/$worker_prefix//g; push @choped_dead, $choped; if (down_flag($name)) { $must_send = 1; update_flag($name, 0); } } if ($must_send) { @lines = (); print_log("Will send: @dead"); sysexec('localhost', $sendemail . " '@choped_dead'", \@lines); } write_timeouts(); print "\n"; } sub update_flag { my $worker = shift; my $flag = shift; $h_timeout->{$worker} = $flag; } sub down_flag { my $worker = shift; my $t = exists $h_timeout->{$worker} ? $h_timeout->{$worker} : 1; return $t; } sub read_timeouts { my $f; local $/=undef; open($f, '<', $filename) or return; my $txt = <$f>; eval $txt; close $f; } sub write_timeouts { my $f; open($f, '>', $filename) or return; my $txt = Data::Dumper->Dump([$h_timeout], ['$h_timeout']); print $f $txt; close $f; } my $local_hostname = 'localhost'; sub sysexec { my ($host, $cmd, $lines) = @_; my $aaa; my $rc = 1; local *KID; return 1 if (!defined $host || $host eq ''); if ($host ne $local_hostname) { $cmd = "ssh -o PreferredAuthentications=publickey $host -t '$cmd' 2>/dev/null"; } my $pid = open(KID, "$cmd |"); if ($pid) { # parent while (defined($aaa = <KID>)) { push(@$lines, $aaa); } close(KID); $rc = ($?>> 8); } else { print_log("FATAL: Could not execute command '$cmd'"); return 1; } return $rc; } sub print_log { my ($txt) = @_; print $txt . "\n"; } &main(); 1; |