diff --git a/cmd/container.plugin/container.plugin.go b/cmd/container.plugin/container.plugin.go index 3433156..a8449df 100644 --- a/cmd/container.plugin/container.plugin.go +++ b/cmd/container.plugin/container.plugin.go @@ -18,12 +18,13 @@ package main import ( "flag" - "log" "github.com/open-io/openio-netdata-plugins/collector" "github.com/open-io/openio-netdata-plugins/container" "github.com/open-io/openio-netdata-plugins/netdata" + "log" "os" "strings" + "time" "github.com/go-redis/redis" ) @@ -39,15 +40,15 @@ func main() { var limit int64 var threshold int64 var fast bool - var bucketdb string + var timeout time.Duration fs := flag.NewFlagSet("", flag.ExitOnError) fs.StringVar(&ns, "ns", "OPENIO", "List of namespaces delimited by semicolons (:)") fs.StringVar(&conf, "conf", "/etc/oio/sds/", "Path to SDS config directory") fs.StringVar(&addr, "addr", "", "Force redis IP:PORT for each namespace") - fs.StringVar(&bucketdb, "bucketdb", "", "BucketDB redis IP:PORT for each namespace") fs.Int64Var(&limit, "limit", -1, "Amount of processed containers in a single request, -1 for unlimited") fs.Int64Var(&threshold, "threshold", 3e5, "Minimal number of objects in container to report it") + fs.DurationVar(&timeout, "timeout", time.Duration(30*time.Second), "Read/Write timeout on redis requests") fs.BoolVar(&fast, "fast", false, "Use fast account listing") err := fs.Parse(os.Args[2:]) if err != nil { @@ -57,10 +58,10 @@ func main() { namespaces := strings.Split(ns, ":") redisAddr := strings.Split(addr, ",") - collector.Run(intervalSeconds, makeCollect(conf, redisAddr, strings.Split(bucketdb, ","), namespaces, limit, threshold, fast)) + collector.Run(intervalSeconds, makeCollect(conf, redisAddr, namespaces, limit, threshold, fast, timeout)) } -func makeCollect(basePath string, addr, bucketdb, namespaces []string, l int64, t int64, f bool) (collect collector.Collect) { +func makeCollect(basePath string, addr, namespaces []string, l int64, t int64, f bool, timeout time.Duration) (collect collector.Collect) { return func(c chan netdata.Metric) error { errors := make(map[string]error) @@ -76,16 +77,13 @@ func makeCollect(basePath string, addr, bucketdb, namespaces []string, l int64, return err } } - client := redis.NewClient(&redis.Options{Addr: redisAddr}) - - var bucketDBClient *redis.Client - if i < len(bucketdb) && bucketdb[i] != "" { - bucketDBClient = redis.NewClient(&redis.Options{Addr: bucketdb[i]}) - } else { - bucketDBClient = client - } + client := redis.NewClient(&redis.Options{ + Addr: redisAddr, + ReadTimeout: timeout, + WriteTimeout: timeout, + }) - errors[ns] = container.Collect(client, bucketDBClient, ns, l, t, f, c) + errors[ns] = container.Collect(client, ns, l, t, f, c) } for _, err := range errors { if err != nil { diff --git a/container/container.go b/container/container.go index 9206305..b04ea16 100644 --- a/container/container.go +++ b/container/container.go @@ -19,13 +19,13 @@ package container import ( "encoding/json" "fmt" + "github.com/go-redis/redis" "github.com/open-io/openio-netdata-plugins/netdata" "github.com/open-io/openio-netdata-plugins/util" "path" "path/filepath" "strconv" "strings" - "github.com/go-redis/redis" ) var scriptGetAccounts = redis.NewScript(` @@ -110,21 +110,19 @@ type bucketInfoStruct struct { } // Collect -- collect container metrics -func Collect(client, bucketdb *redis.Client, ns string, l int64, t int64, f bool, c chan netdata.Metric) error { - if bucketdb != nil { - bucketInfoStr, err := scriptBucketInfo.Run(bucketdb, []string{}, 0).Result() - if err != nil { - return err - } - bucketInfo := map[string]bucketInfoStruct{} - if err := json.Unmarshal([]byte(bucketInfoStr.(string)), &bucketInfo); err != nil { - return err - } - for name, info := range bucketInfo { - bucket := info.Account + "." + strings.Split(name, ":")[1] - netdata.Update("account_bucket_kilobytes", bucket, fmt.Sprintf("%d", info.Bytes/1000), c) - netdata.Update("account_bucket_objects", bucket, fmt.Sprintf("%d", info.Objects), c) - } +func Collect(client *redis.Client, ns string, l int64, t int64, f bool, c chan netdata.Metric) error { + bucketInfoStr, err := scriptBucketInfo.Run(client, []string{}, 0).Result() + if err != nil { + return err + } + bucketInfo := map[string]bucketInfoStruct{} + if err := json.Unmarshal([]byte(bucketInfoStr.(string)), &bucketInfo); err != nil { + return err + } + for name, info := range bucketInfo { + bucket := info.Account + "." + strings.Split(name, ":")[1] + netdata.Update("account_bucket_kilobytes", bucket, fmt.Sprintf("%d", info.Bytes/1000), c) + netdata.Update("account_bucket_objects", bucket, fmt.Sprintf("%d", info.Objects), c) } accounts, err := scriptGetAccounts.Run(client, []string{}, 0).Result()