github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/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 "fmt" 19 "net/url" 20 "strconv" 21 "time" 22 23 "k8s.io/heapster/version" 24 25 influxdb "github.com/influxdata/influxdb/client" 26 ) 27 28 type InfluxdbClient interface { 29 Write(influxdb.BatchPoints) (*influxdb.Response, error) 30 Query(influxdb.Query) (*influxdb.Response, error) 31 Ping() (time.Duration, string, error) 32 } 33 34 type InfluxdbConfig struct { 35 User string 36 Password string 37 Secure bool 38 Host string 39 DbName string 40 WithFields bool 41 InsecureSsl bool 42 RetentionPolicy string 43 } 44 45 func NewClient(c InfluxdbConfig) (InfluxdbClient, error) { 46 url := &url.URL{ 47 Scheme: "http", 48 Host: c.Host, 49 } 50 if c.Secure { 51 url.Scheme = "https" 52 } 53 54 iConfig := &influxdb.Config{ 55 URL: *url, 56 Username: c.User, 57 Password: c.Password, 58 UserAgent: fmt.Sprintf("%v/%v", "heapster", version.HeapsterVersion), 59 UnsafeSsl: c.InsecureSsl, 60 } 61 client, err := influxdb.NewClient(*iConfig) 62 63 if err != nil { 64 return nil, err 65 } 66 if _, _, err := client.Ping(); err != nil { 67 return nil, fmt.Errorf("failed to ping InfluxDB server at %q - %v", c.Host, err) 68 } 69 return client, nil 70 } 71 72 func BuildConfig(uri *url.URL) (*InfluxdbConfig, error) { 73 config := InfluxdbConfig{ 74 User: "root", 75 Password: "root", 76 Host: "localhost:8086", 77 DbName: "k8s", 78 Secure: false, 79 WithFields: false, 80 InsecureSsl: false, 81 RetentionPolicy: "0", 82 } 83 84 if len(uri.Host) > 0 { 85 config.Host = uri.Host 86 } 87 opts := uri.Query() 88 if len(opts["user"]) >= 1 { 89 config.User = opts["user"][0] 90 } 91 // TODO: use more secure way to pass the password. 92 if len(opts["pw"]) >= 1 { 93 config.Password = opts["pw"][0] 94 } 95 if len(opts["db"]) >= 1 { 96 config.DbName = opts["db"][0] 97 } 98 if len(opts["retention"]) >= 1 { 99 config.RetentionPolicy = opts["retention"][0] 100 } 101 if len(opts["withfields"]) >= 1 { 102 val, err := strconv.ParseBool(opts["withfields"][0]) 103 if err != nil { 104 return nil, fmt.Errorf("failed to parse `withfields` flag - %v", err) 105 } 106 config.WithFields = val 107 } 108 if len(opts["secure"]) >= 1 { 109 val, err := strconv.ParseBool(opts["secure"][0]) 110 if err != nil { 111 return nil, fmt.Errorf("failed to parse `secure` flag - %v", err) 112 } 113 config.Secure = val 114 } 115 116 if len(opts["insecuressl"]) >= 1 { 117 val, err := strconv.ParseBool(opts["insecuressl"][0]) 118 if err != nil { 119 return nil, fmt.Errorf("failed to parse `insecuressl` flag - %v", err) 120 } 121 config.InsecureSsl = val 122 } 123 124 return &config, nil 125 }