cc-metric-collector/collectors/memavg/read_memavg.go

49 lines
1.3 KiB
Go
Raw Normal View History

2021-03-18 18:01:59 +01:00
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
}