2015-04-04 00:22:31 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
common "github.com/influxdb/telegraf/plugins/system/ps/common"
|
2015-04-04 00:22:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
|
|
|
filename := "/proc/meminfo"
|
|
|
|
lines, _ := common.ReadLines(filename)
|
|
|
|
|
|
|
|
ret := &VirtualMemoryStat{}
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, ":")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(fields[0])
|
|
|
|
value := strings.TrimSpace(fields[1])
|
|
|
|
value = strings.Replace(value, " kB", "", -1)
|
|
|
|
|
|
|
|
t, err := strconv.ParseUint(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "MemTotal":
|
2015-08-21 23:08:54 +00:00
|
|
|
ret.Total = t * 1024
|
2015-04-04 00:22:31 +00:00
|
|
|
case "MemFree":
|
2015-08-21 23:08:54 +00:00
|
|
|
ret.Free = t * 1024
|
2015-04-04 00:22:31 +00:00
|
|
|
case "Buffers":
|
2015-08-21 23:08:54 +00:00
|
|
|
ret.Buffers = t * 1024
|
2015-04-04 00:22:31 +00:00
|
|
|
case "Cached":
|
2015-08-21 23:08:54 +00:00
|
|
|
ret.Cached = t * 1024
|
2015-04-04 00:22:31 +00:00
|
|
|
case "Active":
|
2015-08-21 23:08:54 +00:00
|
|
|
ret.Active = t * 1024
|
2015-04-04 00:22:31 +00:00
|
|
|
case "Inactive":
|
2015-08-21 23:08:54 +00:00
|
|
|
ret.Inactive = t * 1024
|
2015-04-04 00:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.Available = ret.Free + ret.Buffers + ret.Cached
|
|
|
|
ret.Used = ret.Total - ret.Free
|
2015-09-12 17:11:40 +00:00
|
|
|
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
2015-04-04 00:22:31 +00:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
|
|
|
sysinfo := &syscall.Sysinfo_t{}
|
|
|
|
|
|
|
|
if err := syscall.Sysinfo(sysinfo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &SwapMemoryStat{
|
|
|
|
Total: uint64(sysinfo.Totalswap),
|
|
|
|
Free: uint64(sysinfo.Freeswap),
|
|
|
|
}
|
|
|
|
ret.Used = ret.Total - ret.Free
|
|
|
|
//check Infinity
|
|
|
|
if ret.Total != 0 {
|
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
|
|
|
} else {
|
|
|
|
ret.UsedPercent = 0
|
|
|
|
}
|
|
|
|
lines, _ := common.ReadLines("/proc/vmstat")
|
|
|
|
for _, l := range lines {
|
|
|
|
fields := strings.Fields(l)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch fields[0] {
|
|
|
|
case "pswpin":
|
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Sin = value * 4 * 1024
|
|
|
|
case "pswpout":
|
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Sout = value * 4 * 1024
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|