Skip to content

Commit

Permalink
feat: added option to follow symlinks
Browse files Browse the repository at this point in the history
closes #11
  • Loading branch information
dundee committed Apr 8, 2023
1 parent 89e8097 commit 4940697
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ Flags:
-b, --bind-address string Address to bind to (default "0.0.0.0:9995")
-c, --config string config file (default is $HOME/.disk_usage_exporter.yaml)
-l, --dir-level int Directory nesting level to show (0 = only selected dir) (default 2)
-L, --follow-symlinks Follow symlinks for files, i.e. show the size of the file to which symlink points to (symlinks to directories are not followed)
-h, --help help for disk_usage_exporter
-i, --ignore-dirs strings Absolute paths to ignore (separated by comma) (default [/proc,/dev,/sys,/run,/var/cache/rsnapshot])
-m, --mode string Exposition method - either 'file' or 'http' (default "http")
-f, --output-file string Target file to store metrics in (default "./disk-usage-exporter.prom")
-i, --ignore-dirs strings Absolute paths to ignore (separated by comma) (default [/proc,/dev,/sys,/run,/var/cache/rsnapshot])
```

## Example output
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and reporting which directories consume what space.`,
e := exporter.NewExporter(
viper.GetInt("dir-level"),
viper.GetString("analyzed-path"),
viper.GetBool("follow-symlinks"),
)
e.SetIgnoreDirPaths(viper.GetStringSlice("ignore-dirs"))

Expand Down Expand Up @@ -59,6 +60,10 @@ func init() {
flags.StringP("analyzed-path", "p", "/", "Path where to analyze disk usage")
flags.IntP("dir-level", "l", 2, "Directory nesting level to show (0 = only selected dir)")
flags.StringSliceP("ignore-dirs", "i", []string{"/proc", "/dev", "/sys", "/run", "/var/cache/rsnapshot"}, "Absolute paths to ignore (separated by comma)")
flags.BoolP(
"follow-symlinks", "L", false,
"Follow symlinks for files, i.e. show the size of the file to which symlink points to (symlinks to directories are not followed)",
)

viper.BindPFlags(flags)
}
Expand Down
9 changes: 6 additions & 3 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ type Exporter struct {
ignoreDirPaths map[string]struct{}
maxLevel int
path string
followSymlinks bool
}

// NewExporter creates new Exporter
func NewExporter(maxLevel int, path string) *Exporter {
func NewExporter(maxLevel int, path string, followSymlinks bool) *Exporter {
return &Exporter{
maxLevel: maxLevel,
path: filepath.Clean(path),
maxLevel: maxLevel,
path: filepath.Clean(path),
followSymlinks: followSymlinks,
}
}

func (e *Exporter) runAnalysis() {
defer debug.FreeOSMemory()
analyzer := analyze.CreateAnalyzer()
analyzer.SetFollowSymlinks(e.followSymlinks)
dir := analyzer.AnalyzeDir(e.path, e.shouldDirBeIgnored, false)
dir.UpdateStats(fs.HardLinkedItems{})
e.reportItem(dir, 0)
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRunAnalysis(t *testing.T) {

w := &mockedResponseWriter{}

e := exporter.NewExporter(2, "test_dir")
e := exporter.NewExporter(2, "test_dir", false)
e.SetIgnoreDirPaths([]string{"/proc"})
e.ServeHTTP(w, &http.Request{
Header: make(http.Header),
Expand Down

0 comments on commit 4940697

Please sign in to comment.