2021-10-08 13:29:57 +02:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2022-02-21 12:44:26 +01:00
|
|
|
"bufio"
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2022-02-21 12:44:26 +01:00
|
|
|
"os"
|
2021-10-08 13:29:57 +02:00
|
|
|
"strings"
|
2022-02-21 12:44:26 +01:00
|
|
|
"syscall"
|
2021-10-08 13:29:57 +02:00
|
|
|
"time"
|
2022-02-21 12:44:26 +01:00
|
|
|
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-10-08 13:29:57 +02:00
|
|
|
)
|
|
|
|
|
2022-02-21 12:44:26 +01:00
|
|
|
// "log"
|
|
|
|
|
|
|
|
const MOUNTFILE = `/proc/self/mounts`
|
2021-11-25 14:04:03 +01:00
|
|
|
|
|
|
|
type DiskstatCollectorConfig struct {
|
2022-01-20 12:38:52 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-10-08 13:29:57 +02:00
|
|
|
|
|
|
|
type DiskstatCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-02-21 12:44:26 +01:00
|
|
|
//matches map[string]int
|
|
|
|
config IOstatCollectorConfig
|
|
|
|
//devices map[string]IOstatCollectorEntry
|
2021-10-08 13:29:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *DiskstatCollector) Init(config json.RawMessage) error {
|
2021-10-08 13:29:57 +02:00
|
|
|
m.name = "DiskstatCollector"
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "Disk"}
|
2021-10-08 13:29:57 +02:00
|
|
|
m.setup()
|
2021-11-25 14:04:03 +01:00
|
|
|
if len(config) > 0 {
|
2022-02-21 12:44:26 +01:00
|
|
|
err := json.Unmarshal(config, &m.config)
|
2021-11-25 15:11:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
file, err := os.Open(string(MOUNTFILE))
|
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(m.name, err.Error())
|
|
|
|
return err
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
defer file.Close()
|
|
|
|
m.init = true
|
|
|
|
return nil
|
2021-10-08 13:29:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *DiskstatCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2021-10-08 13:29:57 +02:00
|
|
|
|
2022-02-21 12:44:26 +01:00
|
|
|
file, err := os.Open(string(MOUNTFILE))
|
2021-11-25 15:11:39 +01:00
|
|
|
if err != nil {
|
2022-02-21 12:44:26 +01:00
|
|
|
cclog.ComponentError(m.name, err.Error())
|
2021-11-25 15:11:39 +01:00
|
|
|
return
|
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
defer file.Close()
|
2021-10-08 13:29:57 +02:00
|
|
|
|
2022-02-21 12:44:26 +01:00
|
|
|
part_max_used := uint64(0)
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2021-11-25 15:11:39 +01:00
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
if !strings.HasPrefix(line, "/dev") {
|
2021-11-25 15:11:39 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
linefields := strings.Fields(line)
|
|
|
|
if strings.Contains(linefields[0], "loop") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(linefields[1], "boot") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := strings.Replace(linefields[1], `\040`, " ", -1)
|
|
|
|
stat := syscall.Statfs_t{}
|
|
|
|
err := syscall.Statfs(path, &stat)
|
|
|
|
if err != nil {
|
2022-05-04 11:54:34 +02:00
|
|
|
continue
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
tags := map[string]string{"type": "node", "device": linefields[0]}
|
|
|
|
total := (stat.Blocks * uint64(stat.Bsize)) / uint64(1000000000)
|
|
|
|
y, err := lp.New("disk_total", tags, m.meta, map[string]interface{}{"value": total}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
y.AddMeta("unit", "GBytes")
|
|
|
|
output <- y
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-21 12:44:26 +01:00
|
|
|
free := (stat.Bfree * uint64(stat.Bsize)) / uint64(1000000000)
|
|
|
|
y, err = lp.New("disk_free", tags, m.meta, map[string]interface{}{"value": free}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
y.AddMeta("unit", "GBytes")
|
|
|
|
output <- y
|
|
|
|
}
|
|
|
|
perc := (100 * (total - free)) / total
|
|
|
|
if perc > part_max_used {
|
|
|
|
part_max_used = perc
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 15:50:49 +01:00
|
|
|
y, err := lp.New("part_max_used", map[string]string{"type": "node"}, m.meta, map[string]interface{}{"value": int(part_max_used)}, time.Now())
|
2022-02-21 12:44:26 +01:00
|
|
|
if err == nil {
|
|
|
|
y.AddMeta("unit", "percent")
|
|
|
|
output <- y
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2021-10-08 13:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DiskstatCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|