diff --git a/cmd/root.go b/cmd/root.go index cc494a4..e0a08e3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,6 +34,7 @@ var rootCmd = &cobra.Command{ logrus.Panic(err) } pools.CoinMineAPIKey = config.CoinMineAPIKey + pools.MiningDutchAPIKey = config.MiningDutchAPIKey daemon.Start() }, } diff --git a/daemon/daemon.go b/daemon/daemon.go index 97ac1ff..9c0c52f 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -5,6 +5,8 @@ import ( "os/signal" "syscall" + "github.com/lbryio/sentinel/nicehash" + "github.com/lbryio/lbry.go/v2/extras/stop" "github.com/lbryio/sentinel/pools" @@ -15,7 +17,8 @@ var stopper = stop.New(nil) // Start starts the daemon that runs collecting information and watching the blockchain func Start() { //Start daemon jobs - go pools.MonitorPools(stopper) + go pools.Monitor(stopper) + go nicehash.Monitor(stopper) //Wait for shutdown signal, then shutdown api server. This will wait for all connections to finish. interruptChan := make(chan os.Signal, 1) diff --git a/env/env.go b/env/env.go index 7350fef..8370de5 100644 --- a/env/env.go +++ b/env/env.go @@ -8,10 +8,11 @@ import ( // Config holds the environment configuration used by lighthouse. type Config struct { - CoinMineAPIKey string `env:"COINMINE_API_KEY"` - LbrycrdURL string `env:"LBRYCRD_CONNECT" envDefault:""` - SlackHookURL string `env:"SLACKHOOKURL"` - SlackChannel string `env:"SLACKCHANNEL"` + CoinMineAPIKey string `env:"COINMINE_API_KEY"` + MiningDutchAPIKey string `env:"MININGDUTCH_API_KEY"` + LbrycrdURL string `env:"LBRYCRD_CONNECT" envDefault:""` + SlackHookURL string `env:"SLACKHOOKURL"` + SlackChannel string `env:"SLACKCHANNEL"` } // NewWithEnvVars creates an Config from environment variables diff --git a/nicehash/nicehash.go b/nicehash/nicehash.go new file mode 100644 index 0000000..946d4ca --- /dev/null +++ b/nicehash/nicehash.go @@ -0,0 +1,35 @@ +package nicehash + +import ( + "time" + + "github.com/lbryio/lbry.go/v2/extras/errors" + "github.com/lbryio/lbry.go/v2/extras/stop" + + "github.com/sirupsen/logrus" +) + +// CheckPeriod time between checking on nicehash +var CheckPeriod = 60 +var checkPeriod = time.Duration(CheckPeriod) * time.Second + +// Monitor kicks off the monitoring of nice hash apis +func Monitor(parent *stop.Group) { + stopper := stop.New(parent) + ticker := time.NewTicker(checkPeriod) + for { + select { + case <-stopper.Ch(): + return + case <-ticker.C: + err := checkNiceHash() + if err != nil { + logrus.Error(errors.FullTrace(err)) + } + } + } +} + +func checkNiceHash() error { + return nil +} diff --git a/pools/coinmine.go b/pools/coinmine.go index 1c31aa1..7012209 100644 --- a/pools/coinmine.go +++ b/pools/coinmine.go @@ -18,6 +18,7 @@ const url = "https://www2.coinmine.pl/lbc/index.php?page=api&action=getpoolstatu // CoinMineAPIKey is the api key to use to access coinmine metrics var CoinMineAPIKey string +var lastCoinMineResult *CoinMineResult func monitorCoinmine(parent *stop.Group) { stopper := stop.New(parent) @@ -29,7 +30,7 @@ func monitorCoinmine(parent *stop.Group) { case <-ticker.C: err := checkCoinMine() if err != nil { - logrus.Error(err) + logrus.Error(errors.FullTrace(err)) } } } @@ -55,6 +56,7 @@ func checkCoinMine() error { if err != nil { return errors.Err(err) } + lastCoinMineResult = result return nil } diff --git a/pools/miningdutch.go b/pools/miningdutch.go new file mode 100644 index 0000000..f77c76e --- /dev/null +++ b/pools/miningdutch.go @@ -0,0 +1,69 @@ +package pools + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" + + "github.com/lbryio/lbry.go/v2/extras/errors" + "github.com/lbryio/lbry.go/v2/extras/stop" + "github.com/sirupsen/logrus" +) + +// MiningDutchAPIKey api key for polling the mining dutch mining pool +var MiningDutchAPIKey string +var lastMiningDutchResult *MiningDutchResponse + +var mdurl = "https://www.mining-dutch.nl/pools/lbrycredits.php?page=api&id=66009&action=getpoolhashrate&api_key=" + +func monitorMiningDutch(parent *stop.Group) { + stopper := stop.New(parent) + ticker := time.NewTicker(checkPeriod) + for { + select { + case <-stopper.Ch(): + return + case <-ticker.C: + err := checkMiningDutch() + if err != nil { + logrus.Error(errors.FullTrace(err)) + } + } + } +} + +func checkMiningDutch() error { + logrus.Debug("Checking Mining Dutch") + apiURL := fmt.Sprintf("%s%s", mdurl, MiningDutchAPIKey) + req, err := http.NewRequest(http.MethodGet, apiURL, bytes.NewReader(nil)) + if err != nil { + return errors.Err(err) + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return errors.Err(err) + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return errors.Err(err) + } + result := &MiningDutchResponse{} + err = json.Unmarshal(body, result) + if err != nil { + return errors.Err(err) + } + lastMiningDutchResult = result + return nil +} + +// MiningDutchResponse holds the data returned from the Mining Dutch API +type MiningDutchResponse struct { + Getpoolhashrate struct { + Version string `json:"version"` + Runtime float64 `json:"runtime"` + Data float64 `json:"data"` + } `json:"getpoolhashrate"` +} diff --git a/pools/pool.go b/pools/pool.go index 5a4c3a1..9ddb00b 100644 --- a/pools/pool.go +++ b/pools/pool.go @@ -10,10 +10,9 @@ import ( var CheckPeriod = 60 var checkPeriod = time.Duration(CheckPeriod) * time.Second -var stopper *stop.Group - -// MonitorPools kicks off the monitors for the different pools for mining LBRY -func MonitorPools(parent *stop.Group) { +// Monitor kicks off the monitors for the different pools for mining LBRY +func Monitor(parent *stop.Group) { stopper := stop.New(parent) go monitorCoinmine(stopper) + go monitorMiningDutch(stopper) }