Skip to content

Commit

Permalink
statsdaemon: add health monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarLuu committed Mar 6, 2020
1 parent 262a9ab commit 88e29df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ Usage of ./statsdaemon:
-receive-counter="": Metric name for total metrics received per interval
-tcpaddr="": TCP service address, if set
-version=false: print version string
-heartbeat-file="": heartbeat file to update after a successful write to graphite
```
24 changes: 24 additions & 0 deletions statsdaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ var (
percentThreshold = Percentiles{}
prefix = flag.String("prefix", "", "Prefix for all stats")
postfix = flag.String("postfix", "", "Postfix for all stats")
heartbeatFilePath = flag.String("heartbeat-file", "", "heartbeat file to update after a successful write to graphite.")
)

func init() {
Expand Down Expand Up @@ -245,6 +246,9 @@ func submit(deadline time.Time) error {
}

log.Printf("sent %d stats to %s", num, *graphiteAddress)
if *heartbeatFilePath != "" {
heartbeat()
}

return nil
}
Expand Down Expand Up @@ -572,6 +576,26 @@ func tcpListener() {
}
}

func heartbeat() {
_, err := os.Stat(*heartbeatFilePath)
if os.IsNotExist(err) {
file, err := os.Create(*heartbeatFilePath)
if err != nil {
log.Fatalf("ERROR: Creating heartbeat file - %s", err)
}
defer file.Close()
} else {
currentTime := time.Now()
err = os.Chtimes(*heartbeatFilePath, currentTime, currentTime)
if err != nil {
log.Fatalf("ERROR: Touching %s", err)
}
}
if err != nil {
log.Fatalf("ERROR: %s", err)
}
}

func main() {
flag.Parse()

Expand Down

0 comments on commit 88e29df

Please sign in to comment.