github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/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/influxdb/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  }
    42  
    43  func NewClient(c InfluxdbConfig) (InfluxdbClient, error) {
    44  	url := &url.URL{
    45  		Scheme: "http",
    46  		Host:   c.Host,
    47  	}
    48  	if c.Secure {
    49  		url.Scheme = "https"
    50  	}
    51  
    52  	iConfig := &influxdb.Config{
    53  		URL:       *url,
    54  		Username:  c.User,
    55  		Password:  c.Password,
    56  		UserAgent: fmt.Sprintf("%v/%v", "heapster", version.HeapsterVersion),
    57  	}
    58  	client, err := influxdb.NewClient(*iConfig)
    59  
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	if _, _, err := client.Ping(); err != nil {
    64  		return nil, fmt.Errorf("failed to ping InfluxDB server at %q - %v", c.Host, err)
    65  	}
    66  	return client, nil
    67  }
    68  
    69  func BuildConfig(uri *url.URL) (*InfluxdbConfig, error) {
    70  	config := InfluxdbConfig{
    71  		User:       "root",
    72  		Password:   "root",
    73  		Host:       "localhost:8086",
    74  		DbName:     "k8s",
    75  		Secure:     false,
    76  		WithFields: false,
    77  	}
    78  
    79  	if len(uri.Host) > 0 {
    80  		config.Host = uri.Host
    81  	}
    82  	opts := uri.Query()
    83  	if len(opts["user"]) >= 1 {
    84  		config.User = opts["user"][0]
    85  	}
    86  	// TODO: use more secure way to pass the password.
    87  	if len(opts["pw"]) >= 1 {
    88  		config.Password = opts["pw"][0]
    89  	}
    90  	if len(opts["db"]) >= 1 {
    91  		config.DbName = opts["db"][0]
    92  	}
    93  	if len(opts["withfields"]) >= 1 {
    94  		val, err := strconv.ParseBool(opts["withfields"][0])
    95  		if err != nil {
    96  			return nil, fmt.Errorf("failed to parse `withfields` flag - %v", err)
    97  		}
    98  		config.WithFields = val
    99  	}
   100  	if len(opts["secure"]) >= 1 {
   101  		val, err := strconv.ParseBool(opts["secure"][0])
   102  		if err != nil {
   103  			return nil, fmt.Errorf("failed to parse `secure` flag - %v", err)
   104  		}
   105  		config.Secure = val
   106  	}
   107  
   108  	return &config, nil
   109  }