Skip to content

Commit

Permalink
Add client factory
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvelsriram committed Aug 9, 2020
1 parent ee1fd1f commit 4854e02
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
21 changes: 21 additions & 0 deletions pkg/client/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import "github.com/arunvelsriram/sftp-exporter/pkg/config"

type (
Factory interface {
SFTPClient() (SFTPClient, error)
}

factory struct {
config config.Config
}
)

func (p factory) SFTPClient() (SFTPClient, error) {
return newSFTPClient(p.config)
}

func NewFactory(c config.Config) Factory {
return factory{c}
}
68 changes: 35 additions & 33 deletions pkg/client/sftp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,45 @@ import (
"golang.org/x/crypto/ssh"
)

type SFTPClient interface {
Close()
FSStat() (*model.FSStat, error)
type (
SFTPClient interface {
Close()
FSStat() (*model.FSStat, error)
}

defaultSFTPClient struct {
sshClient *ssh.Client
sftpClient *sftp.Client
config config.Config
}
)

func (d defaultSFTPClient) Close() {
if err := d.sftpClient.Close(); err != nil {
log.WithFields(log.Fields{
"event": "closing SFTP connection"},
).Error(err)
}
if err := d.sshClient.Close(); err != nil {
log.WithFields(log.Fields{
"event": "closing SSH connection"},
).Error(err)
}
}

type defaultSFTPClient struct {
sshClient *ssh.Client
sftpClient *sftp.Client
config config.Config
func (d defaultSFTPClient) FSStat() (*model.FSStat, error) {
statVFS, err := d.sftpClient.StatVFS("/upload")
if err != nil {
return nil, err
}

return &model.FSStat{
TotalSpace: float64(statVFS.TotalSpace()),
FreeSpace: float64(statVFS.FreeSpace()),
}, nil
}

func NewSFTPClient(cfg config.Config) (SFTPClient, error) {
func newSFTPClient(cfg config.Config) (SFTPClient, error) {
addr := fmt.Sprintf("%s:%d", cfg.GetSFTPHost(), cfg.GetSFTPPort())
clientConfig := &ssh.ClientConfig{
User: cfg.GetSFTPUser(),
Expand Down Expand Up @@ -52,28 +79,3 @@ func NewSFTPClient(cfg config.Config) (SFTPClient, error) {
config: cfg,
}, nil
}

func (d defaultSFTPClient) Close() {
if err := d.sftpClient.Close(); err != nil {
log.WithFields(log.Fields{
"event": "closing SFTP connection"},
).Error(err)
}
if err := d.sshClient.Close(); err != nil {
log.WithFields(log.Fields{
"event": "closing SSH connection"},
).Error(err)
}
}

func (d defaultSFTPClient) FSStat() (*model.FSStat, error) {
statVFS, err := d.sftpClient.StatVFS("/upload")
if err != nil {
return nil, err
}

return &model.FSStat{
TotalSpace: float64(statVFS.TotalSpace()),
FreeSpace: float64(statVFS.FreeSpace()),
}, nil
}
12 changes: 8 additions & 4 deletions pkg/collector/sftp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ var (
)

type SFTPCollector struct {
config config.Config
config config.Config
clientFactory client.Factory
}

func (s SFTPCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- up
}

func (s SFTPCollector) Collect(ch chan<- prometheus.Metric) {
sftpClient, err := client.NewSFTPClient(s.config)
sftpClient, err := s.clientFactory.SFTPClient()
if err != nil {
ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 0)
log.WithFields(log.Fields{"event": "creating SFTP sftpClient"}).Error(err)
Expand All @@ -59,6 +60,9 @@ func (s SFTPCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(fsFreeSpace, prometheus.GaugeValue, fsStat.FreeSpace)
}

func NewSFTPCollector(cfg config.Config) prometheus.Collector {
return SFTPCollector{cfg}
func NewSFTPCollector(cfg config.Config, f client.Factory) prometheus.Collector {
return SFTPCollector{
config: cfg,
clientFactory: f,
}
}
6 changes: 4 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"github.com/arunvelsriram/sftp-exporter/pkg/client"
"github.com/arunvelsriram/sftp-exporter/pkg/collector"
"github.com/arunvelsriram/sftp-exporter/pkg/config"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -11,7 +12,8 @@ import (
)

func Start(cfg config.Config) error {
sftpCollector := collector.NewSFTPCollector(cfg)
clientFactory := client.NewFactory(cfg)
sftpCollector := collector.NewSFTPCollector(cfg, clientFactory)
prometheus.MustRegister(sftpCollector)

r := http.NewServeMux()
Expand All @@ -28,4 +30,4 @@ func healthzHandler() http.Handler {
_, _ = fmt.Fprintf(w, "healthy")
}
return http.HandlerFunc(fn)
}
}

0 comments on commit 4854e02

Please sign in to comment.