Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 145 lines (110 sloc) 2.554 kb
fb874eb @dougm Initial import
dougm authored
1 // Copyright (c) 2012 VMware, Inc.
2
3 package sigar
4
5 import (
6 "bufio"
7 "bytes"
8 "io"
9 "io/ioutil"
10 "strconv"
11 "strings"
12 "syscall"
13 )
14
15 const procd = "/proc/"
16
17 func (self *LoadAverage) Get() error {
18 line, err := ioutil.ReadFile(procd + "loadavg")
19 if err != nil {
20 return nil
21 }
22
23 fields := strings.Fields(string(line))
24
25 self.One, _ = strconv.ParseFloat(fields[0], 64)
26 self.Five, _ = strconv.ParseFloat(fields[1], 64)
27 self.Fifteen, _ = strconv.ParseFloat(fields[2], 64)
28
29 return nil
30 }
31
32 func (self *Uptime) Get() error {
33 sysinfo := syscall.Sysinfo_t{}
34
35 if err := syscall.Sysinfo(&sysinfo); err != nil {
36 return err
37 }
38
39 self.Length = float64(sysinfo.Uptime)
40
41 return nil
42 }
43
44 func (self *Mem) Get() error {
45 var buffers, cached uint64
46 table := map[string]*uint64{
47 "MemTotal": &self.Total,
48 "MemFree": &self.Free,
49 "Buffers": &buffers,
50 "Cached": &cached,
51 }
52
53 if err := parseMeminfo(table); err != nil {
54 return err
55 }
56
57 self.Used = self.Total - self.Free
58 kern := buffers + cached
59 self.ActualFree = self.Free + kern
60 self.ActualUsed = self.Used - kern
61
62 return nil
63 }
64
65 func (self *Swap) Get() error {
66 sysinfo := syscall.Sysinfo_t{}
67
68 if err := syscall.Sysinfo(&sysinfo); err != nil {
69 return err
70 }
71
72 self.Total = sysinfo.Totalswap
73 self.Free = sysinfo.Freeswap
74 self.Used = self.Total - self.Free
75
76 return nil
77 }
78
79 func (self *FileSystemList) Get() error {
80 capacity := len(self.List)
81 if capacity == 0 {
82 capacity = 10
83 }
84 fslist := make([]FileSystem, 0, capacity)
85
86 err := readFile("/etc/mtab", func(line string) bool {
87 fields := strings.Fields(line)
88
89 fs := FileSystem{}
90 fs.DevName = fields[0]
91 fs.DirName = fields[1]
92 fs.SysTypeName = fields[2]
93 fs.Options = fields[3]
94
95 fslist = append(fslist, fs)
96
97 return true
98 })
99
100 self.List = fslist
101
102 return err
103 }
104
105 func parseMeminfo(table map[string]*uint64) error {
106 return readFile(procd+"meminfo", func(line string) bool {
107 fields := strings.Split(line, ":")
108
109 if ptr := table[fields[0]]; ptr != nil {
110 num := strings.TrimLeft(fields[1], " ")
111 val, err := strtoull(strings.Fields(num)[0])
112 if err == nil {
113 *ptr = val * 1024
114 }
115 }
116
117 return true
118 })
119 }
120
121 func readFile(file string, handler func(string) bool) error {
122 contents, err := ioutil.ReadFile(file)
123 if err != nil {
124 return err
125 }
126
127 reader := bufio.NewReader(bytes.NewBuffer(contents))
128
129 for {
130 line, _, err := reader.ReadLine()
131 if err == io.EOF {
132 break
133 }
134 if !handler(string(line)) {
135 break
136 }
137 }
138
139 return nil
140 }
141
142 func strtoull(val string) (uint64, error) {
143 return strconv.ParseUint(val, 10, 64)
144 }
Something went wrong with that request. Please try again.