-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmonitor_resources.go
More file actions
40 lines (37 loc) · 1.45 KB
/
monitor_resources.go
File metadata and controls
40 lines (37 loc) · 1.45 KB
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
package main
import (
"log"
"os/exec"
"strconv"
"strings"
"time"
)
func monitorResourceAvailability(resourceName string, checkCommand string, checkInterval time.Duration, resourceManager *ResourceManager) {
for {
if config.LogLevel == LogLevelDebug {
log.Printf("[Resource Monitor][%s] Running check command \"%s\"", resourceName, checkCommand)
}
cmd := exec.Command("sh", "-c", checkCommand)
output, err := cmd.Output()
if err != nil {
log.Printf("[Resource Monitor][%s] Failed to execute check command \"%s\": %v", resourceName, checkCommand, err)
} else {
outputString := string(output)
outputString = strings.TrimSuffix(outputString, "\n")
resourceIntValue, err := strconv.Atoi(outputString)
if err == nil {
if *resourceManager.resourcesAvailable[resourceName] != resourceIntValue { //Don't need a lock for reading since this is the only place which writes
if config.LogLevel == LogLevelDebug {
log.Printf("[Resource Monitor][%s] Setting available resource amount to %d", resourceName, resourceIntValue)
}
resourceManager.serviceMutex.Lock() //Avoid other places reading while we write
*resourceManager.resourcesAvailable[resourceName] = resourceIntValue
resourceManager.serviceMutex.Unlock()
}
} else {
log.Printf("[Resource Monitor][%s] Failed to parse check command \"%s\" output: %v. Output:\n%s", resourceName, checkCommand, err, string(output))
}
}
time.Sleep(checkInterval)
}
}