Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Commit

Permalink
new feature: a separate count of files, directories or all at once (f…
Browse files Browse the repository at this point in the history
…lag -count)
  • Loading branch information
Кошелев Евгений Игоревич committed Mar 20, 2019
1 parent 53987e9 commit bad2f38
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
FROM alpine:3.8

COPY numfiles_exporter /opt/numfiles_exporter/
COPY numfiles_exporter /bin/
COPY targets.yaml /opt/numfiles_exporter/

WORKDIR /opt/numfiles_exporter/

CMD ["./numfiles_exporter"] # specify the required flags!
CMD ["numfiles_exporter"] # specify the required flags!
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
var (
directory = flag.String("directory", "", "directory with target subdirectories")
targetsFile = flag.String("targets", "", "file with separate target directories")
timeout = flag.Int64("timeout", 10, "interval (seconds) of checking the number of files in the target directory")
timeout = flag.Int64("timeout", 15, "interval (seconds) of checking the number of files in the target directory")
port = flag.String("port", ":9095", "port of return of metrics")
handler = flag.String("handler", "/metrics", "handler for which metrics will be available")
count = flag.String("count", "all", "counting what to do: only files, only folders, everything (can take: files || dirs || all)")
)

type targetList struct {
Expand Down
2 changes: 1 addition & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var (
numberOfFiles = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "number_of_files",
Help: "Number of files in the target directory",
}, []string{"directory"})
}, []string{"directory", "counting"})
)

func init() {
Expand Down
Binary file added numfiles_exporter
Binary file not shown.
61 changes: 50 additions & 11 deletions work.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ func openDirectory(directories []string) {
log.Printf("Target directories:\n")

for _, dir := range directories {
log.Printf("\"%s\"\n", dir)
go checkNumFiles(dir)
go checkNumber(dir)
}

}
Expand All @@ -29,20 +28,60 @@ func openSubdirectories(directory string) {

for _, item := range dir {
if item.IsDir() {
log.Printf("\"%s\"\n", item.Name())
go checkNumFiles(path.Join(directory, item.Name()))
go checkNumber(path.Join(directory, item.Name()))
}
}
}

func checkNumFiles(directory string) {
func checkNumber(directory string) {

for {
dir, err := ioutil.ReadDir(directory)
if err != nil {
log.Fatalf("Failed read directory: %s\n", err)
switch *count {
case "all":

log.Printf("\"%s\"\n", directory)
for {
dir, err := ioutil.ReadDir(directory)
if err != nil {
log.Fatalf("Failed read directory: %s\n", err)
}
numberOfFiles.WithLabelValues(directory, *count).Set(float64(len(dir)))
time.Sleep(time.Duration(*timeout) * time.Second)
}
case "files":

log.Printf("\"%s\"\n", directory)
for {
dir, err := ioutil.ReadDir(directory)
if err != nil {
log.Fatalf("Failed read directory: %s\n", err)
}
filesCount := 0
for _, item := range dir {
if !item.IsDir() {
filesCount++
}
}
numberOfFiles.WithLabelValues(directory, *count).Set(float64(filesCount))
time.Sleep(time.Duration(*timeout) * time.Second)
}
case "dirs":

log.Printf("\"%s\"\n", directory)
for {
dir, err := ioutil.ReadDir(directory)
if err != nil {
log.Fatalf("Failed read directory: %s\n", err)
}
dirsCount := 0
for _, item := range dir {
if item.IsDir() {
dirsCount++
}
}
numberOfFiles.WithLabelValues(directory, *count).Set(float64(dirsCount))
time.Sleep(time.Duration(*timeout) * time.Second)
}
numberOfFiles.WithLabelValues(directory).Set(float64(len(dir)))
time.Sleep(time.Duration(*timeout) * time.Second)
default:
log.Fatalf("Not valid value of the \"-count\" flag - \"%s\": should be \"files\", \"dirs\" or \"all\" - default.", *count)
}
}

0 comments on commit bad2f38

Please sign in to comment.