diff --git a/crawler/crawler.go b/crawler/crawler.go index 9daba60..ee1e41f 100644 --- a/crawler/crawler.go +++ b/crawler/crawler.go @@ -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 + }() +} diff --git a/main.go b/main.go index 51e0cb7..1c90fda 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/stat/uptime.go b/stat/uptime.go index a5ffb48..1358345 100644 --- a/stat/uptime.go +++ b/stat/uptime.go @@ -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() +}