Skip to content

Use worker pool for smartctl #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (i *SMARTctlManagerCollector) Describe(ch chan<- *prometheus.Desc) {
func (i *SMARTctlManagerCollector) Collect(ch chan<- prometheus.Metric) {
info := NewSMARTctlInfo(ch)
i.mutex.Lock()
refreshAllDevices(i.logger, i.Devices)
for _, device := range i.Devices {
json := readData(i.logger, device)
if json.Exists() {
Expand Down
39 changes: 25 additions & 14 deletions readjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func readFakeSMARTctl(logger *slog.Logger, device Device) gjson.Result {
}

// Get json from smartctl and parse it
func readSMARTctl(logger *slog.Logger, device Device) (gjson.Result, bool) {
func readSMARTctl(logger *slog.Logger, device Device, wg *sync.WaitGroup) {
defer wg.Done()
start := time.Now()
var smartctlArgs = []string{"--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", "--log=error", "--device=" + device.Type, device.Name}

Expand All @@ -76,7 +77,9 @@ func readSMARTctl(logger *slog.Logger, device Device) (gjson.Result, bool) {
rcOk := resultCodeIsOk(logger, device, json.Get("smartctl.exit_status").Int())
jsonOk := jsonIsOk(logger, json)
logger.Debug("Collected S.M.A.R.T. json data", "device", device, "duration", time.Since(start))
return json, rcOk && jsonOk
if rcOk && jsonOk {
jsonCache.Store(device, JSONCache{JSON: json, LastCollect: time.Now()})
}
}

func readSMARTctlDevices(logger *slog.Logger) gjson.Result {
Expand All @@ -100,23 +103,31 @@ func readSMARTctlDevices(logger *slog.Logger) gjson.Result {
return parseJSON(string(out))
}

// Select json source and parse
// Refresh all devices' json
func refreshAllDevices(logger *slog.Logger, devices []Device) {
if *smartctlFakeData {
return
}

var wg sync.WaitGroup
for _, device := range devices {
cacheValue, cacheOk := jsonCache.Load(device)
if !cacheOk || time.Now().After(cacheValue.(JSONCache).LastCollect.Add(*smartctlInterval)) {
wg.Add(1)
go readSMARTctl(logger, device, &wg)
}
}
wg.Wait()
}

func readData(logger *slog.Logger, device Device) gjson.Result {
if *smartctlFakeData {
return readFakeSMARTctl(logger, device)
}

cacheValue, cacheOk := jsonCache.Load(device)
if !cacheOk || time.Now().After(cacheValue.(JSONCache).LastCollect.Add(*smartctlInterval)) {
json, ok := readSMARTctl(logger, device)
if ok {
jsonCache.Store(device, JSONCache{JSON: json, LastCollect: time.Now()})
j, found := jsonCache.Load(device)
if !found {
logger.Warn("device not found", "device", device)
}
return j.(JSONCache).JSON
}
cacheValue, found := jsonCache.Load(device)
if !found {
logger.Warn("device not found", "device", device)
return gjson.Result{}
}
return cacheValue.(JSONCache).JSON
Expand Down