Skip to content

Commit

Permalink
chore: create a temporary workdir for process files
Browse files Browse the repository at this point in the history
That way we are in line with the new kernel (>4.19 permissions management
Cf https://docs.kernel.org/admin-guide/sysctl/fs.html#protected-regular
  • Loading branch information
lconsuegra committed May 29, 2024
1 parent 3147f89 commit 0a5a513
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ipahealthcheck_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type scrapedCheck struct {
}

type ipahealthcheckCollector struct {
exporterWorkdir string
ipahealthcheckPath string
ipahealthcheckLogPath string
}
Expand All @@ -115,10 +116,12 @@ func (ic ipahealthcheckCollector) Collect(ch chan<- prometheus.Metric) {
log.Infof("Scraping metrics from %v", ic.ipahealthcheckPath)

var checks []ipaCheck
tmpFile, err := os.CreateTemp("/dev/shm", "ipa-healthcheck.out")

tmpFile, err := os.CreateTemp(ic.exporterWorkdir, "ipa-healthcheck.out")
if err != nil {
log.Fatal("Cannot create tempfile: ", err)
}
defer os.Remove(tmpFile.Name())

healthCheckCmd := []string{ic.ipahealthcheckPath, "--source", "ipahealthcheck.meta.services", "--output-file", tmpFile.Name()}
if sudo {
Expand Down Expand Up @@ -227,14 +230,20 @@ func (ic ipahealthcheckCollector) Collect(ch chan<- prometheus.Metric) {
}
}
}

defer os.Remove(tmpFile.Name())
}

func main() {

flag.Parse()

workDir, err := os.MkdirTemp("/dev/shm", "ipa-healthcheck")
if err != nil {
log.Fatal("Cannot create: ", err)
}
if verbose {
log.Infof("Created temporary working directory: %v", workDir)
}

go func() {
intChan := make(chan os.Signal, 1)
termChan := make(chan os.Signal, 1)
Expand All @@ -245,14 +254,17 @@ func main() {
select {
case <-intChan:
log.Infof("Received SIGINT, exiting")
os.RemoveAll(workDir)
os.Exit(0)
case <-termChan:
log.Infof("Received SIGTERM, exiting")
os.RemoveAll(workDir)
os.Exit(0)
}
}()

collector := ipahealthcheckCollector{
exporterWorkdir: workDir,
ipahealthcheckPath: ipahealthcheckPath,
ipahealthcheckLogPath: ipahealthcheckLogPath,
}
Expand Down

0 comments on commit 0a5a513

Please sign in to comment.