github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/influxdb/influxdb.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package influxdb 16 17 import ( 18 "errors" 19 "fmt" 20 "net/url" 21 "strconv" 22 "time" 23 24 "k8s.io/heapster/version" 25 26 influxdb "github.com/influxdata/influxdb/client" 27 ) 28 29 type InfluxdbClient interface { 30 Write(influxdb.BatchPoints) (*influxdb.Response, error) 31 Query(influxdb.Query) (*influxdb.Response, error) 32 Ping() (time.Duration, string, error) 33 } 34 35 type InfluxdbConfig struct { 36 User string 37 Password string 38 Secure bool 39 Host string 40 DbName string 41 WithFields bool 42 InsecureSsl bool 43 RetentionPolicy string 44 ClusterName string 45 DisableCounterMetrics bool 46 Concurrency int 47 } 48 49 func NewClient(c InfluxdbConfig) (InfluxdbClient, error) { 50 url := &url.URL{ 51 Scheme: "http", 52 Host: c.Host, 53 } 54 if c.Secure { 55 url.Scheme = "https" 56 } 57 58 iConfig := &influxdb.Config{ 59 URL: *url, 60 Username: c.User, 61 Password: c.Password, 62 UserAgent: fmt.Sprintf("%v/%v", "heapster", version.HeapsterVersion), 63 UnsafeSsl: c.InsecureSsl, 64 } 65 client, err := influxdb.NewClient(*iConfig) 66 67 if err != nil { 68 return nil, err 69 } 70 if _, _, err := client.Ping(); err != nil { 71 return nil, fmt.Errorf("failed to ping InfluxDB server at %q - %v", c.Host, err) 72 } 73 return client, nil 74 } 75 76 func BuildConfig(uri *url.URL) (*InfluxdbConfig, error) { 77 config := InfluxdbConfig{ 78 User: "root", 79 Password: "root", 80 Host: "localhost:8086", 81 DbName: "k8s", 82 Secure: false, 83 WithFields: false, 84 InsecureSsl: false, 85 RetentionPolicy: "0", 86 ClusterName: "default", 87 DisableCounterMetrics: false, 88 Concurrency: 1, 89 } 90 91 if len(uri.Host) > 0 { 92 config.Host = uri.Host 93 } 94 opts := uri.Query() 95 if len(opts["user"]) >= 1 { 96 config.User = opts["user"][0] 97 } 98 // TODO: use more secure way to pass the password. 99 if len(opts["pw"]) >= 1 { 100 config.Password = opts["pw"][0] 101 } 102 if len(opts["db"]) >= 1 { 103 config.DbName = opts["db"][0] 104 } 105 if len(opts["retention"]) >= 1 { 106 config.RetentionPolicy = opts["retention"][0] 107 } 108 if len(opts["withfields"]) >= 1 { 109 val, err := strconv.ParseBool(opts["withfields"][0]) 110 if err != nil { 111 return nil, fmt.Errorf("failed to parse `withfields` flag - %v", err) 112 } 113 config.WithFields = val 114 } 115 if len(opts["secure"]) >= 1 { 116 val, err := strconv.ParseBool(opts["secure"][0]) 117 if err != nil { 118 return nil, fmt.Errorf("failed to parse `secure` flag - %v", err) 119 } 120 config.Secure = val 121 } 122 123 if len(opts["insecuressl"]) >= 1 { 124 val, err := strconv.ParseBool(opts["insecuressl"][0]) 125 if err != nil { 126 return nil, fmt.Errorf("failed to parse `insecuressl` flag - %v", err) 127 } 128 config.InsecureSsl = val 129 } 130 131 if len(opts["cluster_name"]) >= 1 { 132 config.ClusterName = opts["cluster_name"][0] 133 } 134 135 if len(opts["disable_counter_metrics"]) >= 1 { 136 val, err := strconv.ParseBool(opts["disable_counter_metrics"][0]) 137 if err != nil { 138 return nil, fmt.Errorf("failed to parse `disable_counter_metrics` flag - %v", err) 139 } 140 config.DisableCounterMetrics = val 141 } 142 143 if len(opts["concurrency"]) >= 1 { 144 concurrency, err := strconv.Atoi(opts["concurrency"][0]) 145 if err != nil { 146 return nil, fmt.Errorf("failed to parse `concurrency` flag - %v", err) 147 } 148 149 if concurrency <= 0 { 150 return nil, errors.New("`concurrency` flag can only be positive") 151 } 152 153 config.Concurrency = concurrency 154 } 155 156 return &config, nil 157 }