mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-10 04:27:25 +01:00
memavg collector in Go
This commit is contained in:
parent
d49fda0820
commit
9597798ebc
48
collectors/memavg/read_memavg.go
Executable file
48
collectors/memavg/read_memavg.go
Executable file
@ -0,0 +1,48 @@
|
||||
package main
|
||||
import (
|
||||
"strings"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"time"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := time.Now()
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
fmt.Println("#", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
hostname = strings.Split(hostname, ".")[0]
|
||||
data, err := ioutil.ReadFile("/proc/meminfo")
|
||||
if err != nil {
|
||||
fmt.Println("#", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
lines := strings.Split(string(data), "\n")
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, "MemTotal") {
|
||||
f := strings.Fields(l)
|
||||
v, err := strconv.ParseInt(f[1], 10, 0)
|
||||
if err == nil {
|
||||
fmt.Printf("mem_total,hostname=%s value=%v %v\n", hostname, v*1024, t.UnixNano())
|
||||
}
|
||||
} else if strings.HasPrefix(l, "MemAvailable") {
|
||||
f := strings.Fields(l)
|
||||
v, err := strconv.ParseInt(f[1], 10, 0)
|
||||
if err == nil {
|
||||
fmt.Printf("mem_avail,hostname=%s value=%v %v\n", hostname, v*1024, t.UnixNano())
|
||||
}
|
||||
} else if strings.HasPrefix(l, "MemFree") {
|
||||
f := strings.Fields(l)
|
||||
v, err := strconv.ParseInt(f[1], 10, 0)
|
||||
if err == nil {
|
||||
fmt.Printf("mem_free,hostname=%s value=%v %v\n", hostname, v*1024, t.UnixNano())
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user