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