main.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "os"
  7. "time"
  8. "github.com/cloudfoundry/gosigar"
  9. "github.com/codegangsta/cli"
  10. "github.com/prometheus/client_golang/prometheus"
  11. )
  12. func main() {
  13. app := cli.NewApp()
  14. app.Name = "promUsage"
  15. app.Usage = "exposes statistics to prometheus"
  16. app.Action = run
  17. dfDefault := cli.StringSlice([]string{"/"})
  18. app.Flags = []cli.Flag{
  19. cli.StringFlag{Name: "host", Value: "127.0.0.1:8090", Usage: "our host to listen on for prometheus"},
  20. cli.StringFlag{Name: "name, n", Value: "undefined", Usage: "the namespace to use"},
  21. cli.DurationFlag{Name: "interval,i", Value: time.Minute},
  22. cli.BoolFlag{Name: "verbose,vv", Usage: "print gathered stats to stderr"},
  23. cli.StringSliceFlag{Name: "df", Value: &dfDefault},
  24. }
  25. app.Run(os.Args)
  26. }
  27. func run(ctx *cli.Context) {
  28. log.SetOutput(ioutil.Discard)
  29. if ctx.Bool("verbose") {
  30. log.SetOutput(os.Stderr)
  31. }
  32. n := ctx.String("name")
  33. i := ctx.Duration("interval")
  34. go CollectRam(i, n)
  35. go CollectSwap(i, n)
  36. go CollectCPULoad(i, n)
  37. for _, p := range ctx.StringSlice("df") {
  38. go CollectUsagePercent(n, p)
  39. go CollectUsed(i, n, p)
  40. }
  41. http.Handle("/metrics", prometheus.Handler())
  42. checkFatal(http.ListenAndServe(ctx.String("host"), nil))
  43. }
  44. // utilities
  45. func checkFatal(err error) {
  46. if err != nil {
  47. log.SetOutput(os.Stderr) // might be ioutil.Discard
  48. log.Fatal(err)
  49. }
  50. }
  51. func formatSize(size uint64) string {
  52. return sigar.FormatSize(size * 1024)
  53. }