diff --git a/.talismanrc b/.talismanrc index f125017..6695a32 100644 --- a/.talismanrc +++ b/.talismanrc @@ -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 + diff --git a/cmd/root.go b/cmd/root.go index e39535e..de10a5b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) @@ -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() { diff --git a/pkg/client/sftp_client.go b/pkg/client/sftp_client.go index 35ac3e7..8c60b73 100644 --- a/pkg/client/sftp_client.go +++ b/pkg/client/sftp_client.go @@ -16,7 +16,7 @@ import ( type ( SFTPClient interface { Close() - FSStat() (*model.FSStat, error) + FSStat() (model.FSStats, error) } defaultSFTPClient struct { @@ -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) { diff --git a/pkg/collector/sftp_collector.go b/pkg/collector/sftp_collector.go index 9e8de6d..0134df3 100644 --- a/pkg/collector/sftp_collector.go +++ b/pkg/collector/sftp_collector.go @@ -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, ) ) @@ -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 { diff --git a/pkg/config/config.go b/pkg/config/config.go index 42405cd..506189c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,7 @@ type sftpConfig struct { Key []byte KeyFile string KeyPassphrase string + Paths []string } type Config interface { @@ -31,6 +32,7 @@ type Config interface { GetSFTPKey() []byte GetSFTPKeyFile() string GetSFTPKeyPassphrase() []byte + GetSFTPPaths() []string } type sftpExporterConfig struct { @@ -100,6 +102,7 @@ func MustLoadConfig(fs afero.Fs) Config { Key: key, KeyFile: keyFile, KeyPassphrase: viper.GetString(c.ViperKeySFTPKeyPassphrase), + Paths: viper.GetStringSlice(c.ViperKeySFTPPaths), }, } } @@ -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 +} diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 747bdba..f3cb90f 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -11,6 +11,7 @@ const ( ViperKeySFTPKey = "sftp_key" ViperKeySFTPKeyFile = "sftp_key_file" ViperKeySFTPKeyPassphrase = "sftp_key_passphrase" + ViperKeySFTPPaths = "sftp_paths" ) const ( @@ -24,6 +25,7 @@ const ( FlagSFTPKey = "sftp-key" FlagSFTPKeyFile = "sftp-key-file" FlagSFTPKeyPassphrase = "sftp-key-passphrase" + FlagSFTPPaths = "sftp-paths" ) const Namespace = "sftp" diff --git a/pkg/model/model.go b/pkg/model/model.go index 5b148ac..b324860 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -1,6 +1,9 @@ package model type FSStat struct { + Path string FreeSpace float64 TotalSpace float64 } + +type FSStats []FSStat diff --git a/sftp/docker-compose.yml b/sftp/docker-compose.yml index 4c614d8..f4eb06d 100644 --- a/sftp/docker-compose.yml +++ b/sftp/docker-compose.yml @@ -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" @@ -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"