memavg collector in Go

This commit is contained in:
Thomas Roehl 2021-03-18 18:01:59 +01:00
parent d49fda0820
commit 9597798ebc

View 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
}