Skip to content

Commit

Permalink
Extract sftp paths as config and expose fsStats by path using labels
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvelsriram committed Aug 12, 2020
1 parent be7530d commit 0df7347
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
13 changes: 3 additions & 10 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
fileignoreconfig:
- filename: sftp/ssh/key_with_passphrase.pub
checksum: f54d3e10f89dcec5c69bba33c5bbadff2b0241306af8ce37f7b5695080ccdea2
- filename: sftp/ssh/key_without_passphrase.pub
checksum: 2ec64dcc1725d82c1b1bce8a9769dd6effcb785ceb6682471d9f30d932abf64f
- filename: sftp/ssh/key_without_passphrase
checksum: 2e5bc6583ffed56355367c75bc533e57ceb19e31d1ecc44516189074e55a5142
- filename: sftp/ssh/key_with_passphrase
checksum: f59431b0b023ff7e9b85830121e8869da1fc5780d809ecfb0a28843239cac578
- filename: pkg/client/sftp_client.go
checksum: 0100aeebda7fff385ed63faf81eebbcc958433d0474e299dc31bf11eb66a1905
- filename: sftp/docker-compose.yml
checksum: 63ab40efe2b198ebe8891ab80bf6374ca80b00afbee45c9344e493a801cffecd

4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var rootCmd = &cobra.Command{
Short: "",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cfg.GetSFTPPaths())
log.Debugf("config dump: %+v\n", cfg)
if err := server.Start(cfg); err != nil {
log.WithFields(log.Fields{"event": "starting server"}).Fatal(err)
Expand Down Expand Up @@ -75,6 +76,9 @@ func init() {

rootCmd.PersistentFlags().String(c.FlagSFTPKeyPassphrase, "", "sftp key passphrase")
_ = viper.BindPFlag(c.ViperKeySFTPKeyPassphrase, rootCmd.PersistentFlags().Lookup(c.FlagSFTPKeyPassphrase))

rootCmd.PersistentFlags().StringSlice(c.FlagSFTPPaths, []string{"/"}, "sftp paths")
_ = viper.BindPFlag(c.ViperKeySFTPPaths, rootCmd.PersistentFlags().Lookup(c.FlagSFTPPaths))
}

func initConfig() {
Expand Down
25 changes: 15 additions & 10 deletions pkg/client/sftp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
type (
SFTPClient interface {
Close()
FSStat() (*model.FSStat, error)
FSStat() (model.FSStats, error)
}

defaultSFTPClient struct {
Expand All @@ -39,16 +39,21 @@ func (d defaultSFTPClient) Close() {
}
}

func (d defaultSFTPClient) FSStat() (*model.FSStat, error) {
statVFS, err := d.sftpClient.StatVFS("/upload")
if err != nil {
return nil, err
func (d defaultSFTPClient) FSStat() (model.FSStats, error) {
paths := d.config.GetSFTPPaths()
fsStats := make([]model.FSStat, len(paths))
for i, path := range paths {
statVFS, err := d.sftpClient.StatVFS(path)
if err != nil {
return nil, err
}
fsStats[i] = model.FSStat{
Path: path,
TotalSpace: float64(statVFS.TotalSpace()),
FreeSpace: float64(statVFS.FreeSpace()),
}
}

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

func ParsePrivateKey(key, keyPassphrase []byte) (parsedKey ssh.Signer, err error) {
Expand Down
14 changes: 8 additions & 6 deletions pkg/collector/sftp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ var (
fsTotalSpace = prometheus.NewDesc(
prometheus.BuildFQName(c.Namespace, "", "filesystem_total_space_bytes"),
"Total space in the filesystem containing the path",
[]string{},
[]string{"path"},
nil,
)

fsFreeSpace = prometheus.NewDesc(
prometheus.BuildFQName(c.Namespace, "", "filesystem_free_space_bytes"),
"Free space in the filesystem containing the path",
[]string{},
[]string{"path"},
nil,
)
)
Expand All @@ -52,13 +52,15 @@ func (s SFTPCollector) Collect(ch chan<- prometheus.Metric) {
defer sftpClient.Close()
ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 1)

fsStat, err := sftpClient.FSStat()
stats, err := sftpClient.FSStat()
if err != nil {
log.WithFields(log.Fields{"event": "getting FS stat"}).Error(err)
log.WithFields(log.Fields{"event": "getting FS stats"}).Error(err)
return
}
ch <- prometheus.MustNewConstMetric(fsTotalSpace, prometheus.GaugeValue, fsStat.TotalSpace)
ch <- prometheus.MustNewConstMetric(fsFreeSpace, prometheus.GaugeValue, fsStat.FreeSpace)
for _, stat := range stats {
ch <- prometheus.MustNewConstMetric(fsTotalSpace, prometheus.GaugeValue, stat.TotalSpace, stat.Path)
ch <- prometheus.MustNewConstMetric(fsFreeSpace, prometheus.GaugeValue, stat.FreeSpace, stat.Path)
}
}

func NewSFTPCollector(cfg config.Config) prometheus.Collector {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type sftpConfig struct {
Key []byte
KeyFile string
KeyPassphrase string
Paths []string
}

type Config interface {
Expand All @@ -31,6 +32,7 @@ type Config interface {
GetSFTPKey() []byte
GetSFTPKeyFile() string
GetSFTPKeyPassphrase() []byte
GetSFTPPaths() []string
}

type sftpExporterConfig struct {
Expand Down Expand Up @@ -100,6 +102,7 @@ func MustLoadConfig(fs afero.Fs) Config {
Key: key,
KeyFile: keyFile,
KeyPassphrase: viper.GetString(c.ViperKeySFTPKeyPassphrase),
Paths: viper.GetStringSlice(c.ViperKeySFTPPaths),
},
}
}
Expand Down Expand Up @@ -143,3 +146,7 @@ func (c sftpExporterConfig) GetSFTPKeyFile() string {
func (c sftpExporterConfig) GetSFTPKeyPassphrase() []byte {
return []byte(c.SFTPConfig.KeyPassphrase)
}

func (c sftpExporterConfig) GetSFTPPaths() []string {
return c.SFTPConfig.Paths
}
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
ViperKeySFTPKey = "sftp_key"
ViperKeySFTPKeyFile = "sftp_key_file"
ViperKeySFTPKeyPassphrase = "sftp_key_passphrase"
ViperKeySFTPPaths = "sftp_paths"
)

const (
Expand All @@ -24,6 +25,7 @@ const (
FlagSFTPKey = "sftp-key"
FlagSFTPKeyFile = "sftp-key-file"
FlagSFTPKeyPassphrase = "sftp-key-passphrase"
FlagSFTPPaths = "sftp-paths"
)

const Namespace = "sftp"
3 changes: 3 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package model

type FSStat struct {
Path string
FreeSpace float64
TotalSpace float64
}

type FSStats []FSStat
6 changes: 3 additions & 3 deletions sftp/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ services:
image: atmoz/sftp
ports:
- "2220:22"
command: foo:pass:::upload
command: foo:pass:::upload1,upload2
sftp-key:
container_name: sftp-key
hostname: sftp-key
image: atmoz/sftp
ports:
- "2221:22"
command: foo::::upload
command: foo::::upload1,upload2
volumes:
- "$PWD/ssh/key_without_passphrase.pub:/home/foo/.ssh/keys/key_without_passphrase.pub"
- "$PWD/ssh/key_with_passphrase.pub:/home/foo/.ssh/keys/key_with_passphrase.pub"
Expand All @@ -24,7 +24,7 @@ services:
image: atmoz/sftp
ports:
- "2222:22"
command: foo:pass:::upload
command: foo:pass:::upload1,upload2
volumes:
- "$PWD/ssh/key_without_passphrase.pub:/home/foo/.ssh/keys/key_without_passphrase.pub"
- "$PWD/ssh/key_with_passphrase.pub:/home/foo/.ssh/keys/key_with_passphrase.pub"
Expand Down

0 comments on commit 0df7347

Please sign in to comment.