This commit is contained in:
insanity 2017-10-24 18:30:22 +09:00
parent 3134350172
commit 87ae90959a
3 changed files with 50 additions and 1 deletions

View File

@ -140,3 +140,18 @@ func (c *Crawler) ProcessStat(ch chan interface{}) {
ch <- res
}()
}
func (c *Crawler) Uptime(ch chan interface{}) {
go func() {
upt := &stat.Uptime{}
b, err := c.sshCli.RunCommand(upt.Command())
if err != nil {
ch <- err
}
res, err := upt.Read(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
}

View File

@ -125,8 +125,12 @@ func main() {
cr.LoadAvgStat(loadCh)
print(<-loadCh)
//Uptime
uptCh := make(chan interface{})
cr.Uptime(uptCh)
print(<-uptCh)
//Process
//Process (It doesn't to be a Map. will be displayed by a list)
psCh := make(chan interface{})
cr.ProcessStat(psCh)
print(<-psCh)

View File

@ -1 +1,31 @@
package stat
import (
"io"
"bufio"
"strings"
)
type Uptime struct {
}
func (upt Uptime) Command() string {
return "uptime -s"
}
func (upt Uptime) Read(r io.Reader) (*map[string]string, error) {
var (
scanner = bufio.NewScanner(r)
resMap = make(map[string]string)
)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
resMap["system.uptime.date"] = parts[0]
resMap["system.uptime.time"]= parts[1]
}
return &resMap, scanner.Err()
}