main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "time"
  7. "github.com/cloudfoundry/gosigar"
  8. "github.com/codegangsta/cli"
  9. "github.com/influxdb/influxdb/client"
  10. )
  11. var name string
  12. func main() {
  13. app := cli.NewApp()
  14. app.Name = "influxUsage"
  15. app.Usage = "Sends usage reports to influxdb"
  16. app.Action = run
  17. app.Flags = []cli.Flag{
  18. cli.StringFlag{Name: "host", Value: "127.0.0.1:8086", Usage: "influxdb host to connect to"},
  19. cli.StringFlag{Name: "name, n", Usage: "the name of the system to report as"},
  20. cli.BoolFlag{Name: "verbose,vv", Usage: "print gathered stats to stderr"},
  21. }
  22. app.Run(os.Args)
  23. }
  24. func run(ctx *cli.Context) {
  25. // default is discard
  26. log.SetOutput(ioutil.Discard)
  27. if ctx.Bool("verbose") {
  28. log.SetOutput(os.Stderr)
  29. }
  30. // set default name if flag is empty
  31. name = ctx.String("name")
  32. if name == "" {
  33. name = "undefined"
  34. }
  35. cfg := client.ClientConfig{
  36. Host: ctx.String("host"),
  37. Database: "nethealth",
  38. }
  39. schan, err := NewInfluxCollector(&cfg)
  40. checkFatal(err)
  41. go CollectMemory(schan, 1*time.Second)
  42. go CollectCPULoad(schan, 1*time.Second)
  43. go CollectDiskSpace(schan, 1*time.Minute, "/")
  44. // lazy block..
  45. done := make(chan struct{})
  46. <-done
  47. }
  48. // utilities
  49. func checkFatal(err error) {
  50. if err != nil {
  51. log.SetOutput(os.Stderr) // might be ioutil.Discard
  52. log.Fatal(err)
  53. }
  54. }
  55. func formatSize(size uint64) string {
  56. return sigar.FormatSize(size * 1024)
  57. }