summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/filecheck/collect_files.go
blob: afbc4052ab5d0b8f293f0cb817ec149145727a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-License-Identifier: GPL-3.0-or-later

package filecheck

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
)

func (fc *Filecheck) collectFiles(ms map[string]int64) {
	curTime := time.Now()
	if time.Since(fc.lastDiscoveryFiles) >= fc.DiscoveryEvery.Duration() {
		fc.lastDiscoveryFiles = curTime
		fc.curFiles = fc.discoveryFiles()
		fc.updateFilesCharts(fc.curFiles)
	}

	for _, path := range fc.curFiles {
		fc.collectFile(ms, path, curTime)
	}
	ms["num_of_files"] = int64(len(fc.curFiles))
}

func (fc *Filecheck) collectFile(ms map[string]int64, path string, curTime time.Time) {
	info, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			ms[fileDimID(path, "exists")] = 0
		} else {
			ms[fileDimID(path, "exists")] = 1
		}
		fc.Debug(err)
		return
	}

	if info.IsDir() {
		return
	}

	ms[fileDimID(path, "exists")] = 1
	ms[fileDimID(path, "size_bytes")] = info.Size()
	ms[fileDimID(path, "mtime_ago")] = int64(curTime.Sub(info.ModTime()).Seconds())
}

func (fc *Filecheck) discoveryFiles() (files []string) {
	for _, path := range fc.Files.Include {
		if hasMeta(path) {
			continue
		}
		files = append(files, path)
	}

	for _, path := range fc.Files.Include {
		if !hasMeta(path) {
			continue
		}
		matches, _ := filepath.Glob(path)
		for _, v := range matches {
			fi, err := os.Lstat(v)
			if err == nil && fi.Mode().IsRegular() {
				files = append(files, v)
			}
		}
	}
	return removeDuplicates(files)
}

func (fc *Filecheck) updateFilesCharts(files []string) {
	set := make(map[string]bool, len(files))
	for _, path := range files {
		set[path] = true
		if !fc.collectedFiles[path] {
			fc.collectedFiles[path] = true
			fc.addFileToCharts(path)
		}
	}
	for path := range fc.collectedFiles {
		if !set[path] {
			delete(fc.collectedFiles, path)
			fc.removeFileFromCharts(path)
		}
	}
}

func (fc *Filecheck) addFileToCharts(path string) {
	for _, chart := range *fc.Charts() {
		if !strings.HasPrefix(chart.ID, "file_") {
			continue
		}

		var id string
		switch chart.ID {
		case fileExistenceChart.ID:
			id = fileDimID(path, "exists")
		case fileModTimeAgoChart.ID:
			id = fileDimID(path, "mtime_ago")
		case fileSizeChart.ID:
			id = fileDimID(path, "size_bytes")
		default:
			fc.Warningf("add dimension: couldn't dim id for '%s' chart (file '%s')", chart.ID, path)
			continue
		}

		dim := &module.Dim{ID: id, Name: reSpace.ReplaceAllString(path, "_")}

		if err := chart.AddDim(dim); err != nil {
			fc.Warning(err)
			continue
		}
		chart.MarkNotCreated()
	}
}

func (fc *Filecheck) removeFileFromCharts(path string) {
	for _, chart := range *fc.Charts() {
		if !strings.HasPrefix(chart.ID, "file_") {
			continue
		}

		var id string
		switch chart.ID {
		case fileExistenceChart.ID:
			id = fileDimID(path, "exists")
		case fileModTimeAgoChart.ID:
			id = fileDimID(path, "mtime_ago")
		case fileSizeChart.ID:
			id = fileDimID(path, "size_bytes")
		default:
			fc.Warningf("remove dimension: couldn't dim id for '%s' chart (file '%s')", chart.ID, path)
			continue
		}

		if err := chart.MarkDimRemove(id, true); err != nil {
			fc.Warning(err)
			continue
		}
		chart.MarkNotCreated()
	}
}

func fileDimID(path, metric string) string {
	return fmt.Sprintf("file_%s_%s", reSpace.ReplaceAllString(path, "_"), metric)
}