Skip to content

Commit

Permalink
read from config file instead of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Mar 10, 2020
1 parent 938558e commit 5177128
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ conoha_exporter
gin-bin
vendor
.idea
conoha_exporter_config.yaml
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ cd conoha_exporter
go mod download

go build
./conoha_exporter --help
./conoha_exporter
```

## usage

```
Usage of ./conoha_exporter:
-password string
ConoHa API user password
-port string
Port number to listen on
-region string
ConoHa region (default "tyo1")
-tenant-id string
ConoHa tenant ID
-username string
ConoHa API user name
Put `conoha_exporter_config.yaml` in the same directory

```yaml
# Port to listen on
port: 3030
# Conoha region
region: tyo1
tenant_id: your-tenant-id
username: conoha-api-username
password: conoha-api-password
```
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/traPtitech/conoha_exporter

go 1.14

require github.com/prometheus/client_golang v1.5.0
require (
github.com/prometheus/client_golang v1.5.0
gopkg.in/yaml.v2 v2.2.5
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -82,8 +84,10 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
39 changes: 23 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package main

import (
"flag"
"log"
"net/http"
"os"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
)

type Config struct {
Port string `yaml:"port"`
Region string `yaml:"region"`
TenantId string `yaml:"tenant_id"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}

// インデックスページ用 (Prometheusは別にココを触らないので、お好みで……)
func indexPage(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`
Expand All @@ -29,20 +36,20 @@ func indexPage(w http.ResponseWriter, _ *http.Request) {
func main() {
log.Println("ConoHa exporter started.")

// コマンドライン引数
port := flag.String("port", os.Getenv("PORT"), "Port number to listen on")
region := flag.String("region", "tyo1", "ConoHa region")
tenantId := flag.String("tenant-id", "", "ConoHa tenant ID")
username := flag.String("username", "", "ConoHa API user name")
password := flag.String("password", "", "ConoHa API user password")
flag.Parse()
// config.ymlを読み取る
buf, err := ioutil.ReadFile("./conoha_exporter_config.yaml")
if err != nil {
log.Fatal(err)
return
}

if *port == "" {
*port = "3000"
var config Config
if err := yaml.Unmarshal(buf, &config); err != nil {
log.Fatal(err)
}

// ConoHa APIクライアントを作成
client, err := NewClient(*region, *tenantId, *username, *password)
client, err := NewClient(config.Region, config.TenantId, config.Username, config.Password)
if err != nil {
log.Fatal(err)
}
Expand All @@ -64,5 +71,5 @@ func main() {
// HTTPでメトリクスを出力
http.HandleFunc("/", indexPage)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":"+*port, nil))
log.Fatal(http.ListenAndServe(":"+config.Port, nil))
}

0 comments on commit 5177128

Please sign in to comment.