Skip to content

Commit

Permalink
Added some probe count boundaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
icemarkom committed Nov 26, 2021
1 parent 822c747 commit 618f876
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
10 changes: 8 additions & 2 deletions cmd/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ func PollFan(cfg *mffp.Config) (*mffp.FanData, error) {

// ProbeFan runs continuous fan poller.
func ProbeFan(cfg *mffp.Config) {
for probeCount := 1; ; probeCount++ {
var probeCount, failCount int

for probeCount = 1; ; probeCount++ {
fd, err := PollFan(cfg)
switch err != nil {
case true:
{
failCount++
log.SetOutput(os.Stderr)
log.Printf("Probe #%d: error reading fan information from %q: %v.", probeCount, cfg.Host, err)
if cfg.ExitOnError {
if cfg.ExitOnError && failCount > cfg.MaxFail {
os.Exit(42)
}
log.SetOutput(os.Stdout)
Expand All @@ -66,6 +69,9 @@ func ProbeFan(cfg *mffp.Config) {
log.Printf("Probe #%d: %s", probeCount, fd)
}
}
if cfg.MaxCount > 0 && probeCount >= cfg.MaxCount {
continue
}
time.Sleep(cfg.Interval)
}
}
Expand Down
10 changes: 10 additions & 0 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func init() {
flag.BoolVar(&cfg.ExitOnError, "exit-on-error", true, "Exit polling loop on error")
flag.BoolVar(&cfg.Quiet, "quiet", false, "Log only polling errors")
flag.DurationVar(&cfg.Timeout, "timeout", 1*time.Second, "Polling probe timeout in seconds")
flag.IntVar(&cfg.MaxCount, "maxcount", 0, "Maximum number of probes (0 means unlimited)")
flag.IntVar(&cfg.MaxFail, "maxfail", 0, "Maximum number of failed probes")
flag.BoolVar(&v, "version", false, "Show version")
flag.Parse()

Expand All @@ -36,6 +38,14 @@ func init() {
os.Exit(42)
}

if cfg.MaxCount < 0 {
log.Fatalf("Maximum probe count must be greater or equal to 0.")
}

if cfg.MaxFail < 0 {
log.Fatalf("Maximum probe number of failed probes be greater or equal to 0.")
}

if cfg.Host == "" {
log.Fatalf("Target host must be specified.")
}
Expand Down
13 changes: 8 additions & 5 deletions mffprober.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const (

// Config holds the configuration parameters used throughout the prober.
type Config struct {
Host string
Interval time.Duration
ExitOnError bool
Quiet bool
Timeout time.Duration
Host string
Interval time.Duration
ExitOnError bool
Quiet bool
Timeout time.Duration
MaxCount, MaxFail int
}

func (cfg Config) String() string {
Expand All @@ -32,6 +33,8 @@ func (cfg Config) String() string {
s.WriteString(fmt.Sprintf("Timeout: %s; ", cfg.Timeout))
s.WriteString(fmt.Sprintf("Exit on error: %v; ", cfg.ExitOnError))
s.WriteString(fmt.Sprintf("Quiet mode: %v", cfg.Quiet))
s.WriteString(fmt.Sprintf("Max probes: %v", cfg.MaxCount))
s.WriteString(fmt.Sprintf("Max failed probes: %v", cfg.MaxFail))

return s.String()
}
Expand Down

0 comments on commit 618f876

Please sign in to comment.