refactory to extra api
This commit is contained in:
parent
d1605ee841
commit
250ca37781
|
@ -0,0 +1,23 @@
|
|||
package api
|
||||
|
||||
import "encoding/base64"
|
||||
|
||||
// A Client for the API
|
||||
type Client struct {
|
||||
Token string `toml:"token"`
|
||||
Host string `toml:"host"`
|
||||
}
|
||||
|
||||
// New Client from host and token
|
||||
func New(host, token string) *Client {
|
||||
c := &Client{
|
||||
Host: host,
|
||||
}
|
||||
c.SetToken(token)
|
||||
return c
|
||||
}
|
||||
|
||||
// SetToken by using base64encoding
|
||||
func (c *Client) SetToken(token string) {
|
||||
c.Token = base64.StdEncoding.EncodeToString([]byte(token))
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -7,7 +7,8 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func (c *configData) Request(url string, value interface{}) error {
|
||||
// Request to API and unmarshal result
|
||||
func (c *Client) Request(url string, value interface{}) error {
|
||||
netClient := &http.Client{
|
||||
Timeout: time.Second * 20,
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -16,7 +16,7 @@ type ResponseList struct {
|
|||
Data []string `json:"response,omitempty"`
|
||||
}
|
||||
|
||||
func (c *configData) RequestListVHosts() (*ResponseList, error) {
|
||||
func (c *Client) RequestListVHosts() (*ResponseList, error) {
|
||||
req := ResponseList{}
|
||||
url := fmt.Sprintf(URLRequestListVHost)
|
||||
if err := c.Request(url, &req); err != nil {
|
||||
|
@ -25,7 +25,7 @@ func (c *configData) RequestListVHosts() (*ResponseList, error) {
|
|||
return &req, nil
|
||||
}
|
||||
|
||||
func (c *configData) RequestListApps(vhost string) (*ResponseList, error) {
|
||||
func (c *Client) RequestListApps(vhost string) (*ResponseList, error) {
|
||||
req := ResponseList{}
|
||||
url := fmt.Sprintf(URLRequestListApp, vhost)
|
||||
if err := c.Request(url, &req); err != nil {
|
||||
|
@ -34,7 +34,7 @@ func (c *configData) RequestListApps(vhost string) (*ResponseList, error) {
|
|||
return &req, nil
|
||||
}
|
||||
|
||||
func (c *configData) RequestListStreams(vhost, app string) (*ResponseList, error) {
|
||||
func (c *Client) RequestListStreams(vhost, app string) (*ResponseList, error) {
|
||||
req := ResponseList{}
|
||||
url := fmt.Sprintf(URLRequestListStream, vhost, app)
|
||||
if err := c.Request(url, &req); err != nil {
|
|
@ -1,9 +1,7 @@
|
|||
package main
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bdlm/log"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -33,17 +31,7 @@ type ResponseStatsData struct {
|
|||
TotalBytesOut uint64 `json:"totalBytesOut" example:"117022184"`
|
||||
}
|
||||
|
||||
func (resp *ResponseStats) Log(log *log.Entry) {
|
||||
logger := log
|
||||
if resp.Data != nil {
|
||||
logger.WithFields(map[string]interface{}{
|
||||
"max_clients": resp.Data.MaxTotalConnections,
|
||||
"clients": resp.Data.TotalConnections,
|
||||
})
|
||||
}
|
||||
logger.Info(resp.Message)
|
||||
}
|
||||
func (c *configData) RequestStatsVHost(vhost string) (*ResponseStats, error) {
|
||||
func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) {
|
||||
req := ResponseStats{}
|
||||
url := fmt.Sprintf(URLRequestStatsVHost, vhost)
|
||||
if err := c.Request(url, &req); err != nil {
|
||||
|
@ -52,7 +40,7 @@ func (c *configData) RequestStatsVHost(vhost string) (*ResponseStats, error) {
|
|||
return &req, nil
|
||||
}
|
||||
|
||||
func (c *configData) RequestStatsApp(vhost, app string) (*ResponseStats, error) {
|
||||
func (c *Client) RequestStatsApp(vhost, app string) (*ResponseStats, error) {
|
||||
req := ResponseStats{}
|
||||
url := fmt.Sprintf(URLRequestStatsApp, vhost, app)
|
||||
if err := c.Request(url, &req); err != nil {
|
||||
|
@ -61,7 +49,7 @@ func (c *configData) RequestStatsApp(vhost, app string) (*ResponseStats, error)
|
|||
return &req, nil
|
||||
}
|
||||
|
||||
func (c *configData) RequestStatsStream(vhost, app, stream string) (*ResponseStats, error) {
|
||||
func (c *Client) RequestStatsStream(vhost, app, stream string) (*ResponseStats, error) {
|
||||
req := ResponseStats{}
|
||||
url := fmt.Sprintf(URLRequestStatsStream, vhost, app, stream)
|
||||
if err := c.Request(url, &req); err != nil {
|
32
fetch.go
32
fetch.go
|
@ -1,48 +1,60 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"dev.sum7.eu/genofire/oven-exporter/api"
|
||||
"github.com/bdlm/log"
|
||||
)
|
||||
|
||||
func (config *configData) fetch() {
|
||||
respList, err := config.RequestListVHosts()
|
||||
func statsLog(resp *api.ResponseStats, log *log.Entry) {
|
||||
logger := log
|
||||
if resp.Data != nil {
|
||||
logger = logger.WithFields(map[string]interface{}{
|
||||
"max_clients": resp.Data.MaxTotalConnections,
|
||||
"clients": resp.Data.TotalConnections,
|
||||
})
|
||||
}
|
||||
logger.Info(resp.Message)
|
||||
}
|
||||
|
||||
func fetch(client *api.Client) {
|
||||
respList, err := client.RequestListVHosts()
|
||||
if err != nil {
|
||||
log.Panicf("unable to fetch vhosts: %s", err)
|
||||
}
|
||||
for _, vhost := range respList.Data {
|
||||
logVhost := log.WithField("vhost", vhost)
|
||||
resp, err := config.RequestStatsVHost(vhost)
|
||||
resp, err := client.RequestStatsVHost(vhost)
|
||||
if err != nil {
|
||||
logVhost.Errorf("error on request: %s", err)
|
||||
} else {
|
||||
resp.Log(logVhost)
|
||||
statsLog(resp, logVhost)
|
||||
}
|
||||
respList, err = config.RequestListApps(vhost)
|
||||
respList, err = client.RequestListApps(vhost)
|
||||
if err != nil {
|
||||
logVhost.Errorf("unable to fetch apps: %s", err)
|
||||
continue
|
||||
}
|
||||
for _, app := range respList.Data {
|
||||
logApp := logVhost.WithField("app", app)
|
||||
resp, err = config.RequestStatsApp(vhost, app)
|
||||
resp, err = client.RequestStatsApp(vhost, app)
|
||||
if err != nil {
|
||||
logApp.Errorf("error on request: %s", err)
|
||||
} else {
|
||||
resp.Log(logApp)
|
||||
statsLog(resp, logApp)
|
||||
}
|
||||
respList, err = config.RequestListStreams(vhost, app)
|
||||
respList, err = client.RequestListStreams(vhost, app)
|
||||
if err != nil {
|
||||
logApp.Errorf("unable to fetch stream: %s", err)
|
||||
continue
|
||||
}
|
||||
for _, stream := range respList.Data {
|
||||
logStream := logApp.WithField("stream", stream)
|
||||
req, err := config.RequestStatsStream(vhost, app, stream)
|
||||
req, err := client.RequestStatsStream(vhost, app, stream)
|
||||
if err != nil {
|
||||
logStream.Errorf("error on request: %s", err)
|
||||
continue
|
||||
}
|
||||
req.Log(logStream)
|
||||
statsLog(req, logStream)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
10
main.go
10
main.go
|
@ -1,16 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"flag"
|
||||
|
||||
"dev.sum7.eu/genofire/golang-lib/file"
|
||||
"github.com/bdlm/log"
|
||||
|
||||
"dev.sum7.eu/genofire/oven-exporter/api"
|
||||
)
|
||||
|
||||
type configData struct {
|
||||
Token string `toml:"token"`
|
||||
Host string `toml:"host"`
|
||||
API *api.Client `toml:"api"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -24,6 +24,6 @@ func main() {
|
|||
if err := file.ReadTOML(configPath, config); err != nil {
|
||||
log.Panicf("open config file: %s", err)
|
||||
}
|
||||
config.Token = base64.StdEncoding.EncodeToString([]byte(config.Token))
|
||||
config.fetch()
|
||||
config.API.SetToken(config.API.Token)
|
||||
fetch(config.API)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue