From cda6a02f65024cce59763c9fa9e0f0ee62efd78a Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 14 Dec 2017 16:10:11 +0900 Subject: [PATCH] ing --- crawler/impl/ssh/SSHCrawler.go | 18 ++--- crawler/impl/ssh/parser/cpu_parser.go | 3 +- crawler/impl/ssh/parser/memory_parser.go | 3 +- crawler/impl/ssh/parser/parser.go | 84 +++++++++++++++++++++++- 4 files changed, 91 insertions(+), 17 deletions(-) diff --git a/crawler/impl/ssh/SSHCrawler.go b/crawler/impl/ssh/SSHCrawler.go index de9d730..10e05a8 100644 --- a/crawler/impl/ssh/SSHCrawler.go +++ b/crawler/impl/ssh/SSHCrawler.go @@ -48,7 +48,9 @@ func (c *SSHCrawler) InternalGet(config *configM.Config) (map[string]string, err r := bytes.NewReader(buf) scanner := bufio.NewScanner(r) -ParseLoop: + pScanner := parser.NewParserScanner(scanner, boundary) + + for i := 0; i < itemCount; i++ { item := config.Items[i] mode := item.QueryInfo.Extend["mode"].(string) @@ -57,7 +59,7 @@ ParseLoop: return nil, fmt.Errorf("Container: Parser[%s] is not exist", mode) } - rm, err := p.Parse(scanner) + rm, err := p.Parse(pScanner) if nil != err { return nil, err } @@ -68,17 +70,9 @@ ParseLoop: } } - for scanner.Scan() { - line := scanner.Text() - if line == "--"+boundary+"--" { - break ParseLoop - } - - if line == "--"+boundary { - break - } + if !pScanner.Clean() { + break } - } return results, nil diff --git a/crawler/impl/ssh/parser/cpu_parser.go b/crawler/impl/ssh/parser/cpu_parser.go index d42e3af..1e69600 100644 --- a/crawler/impl/ssh/parser/cpu_parser.go +++ b/crawler/impl/ssh/parser/cpu_parser.go @@ -1,7 +1,6 @@ package parser import ( - "bufio" "strconv" "strings" ) @@ -13,7 +12,7 @@ func (p *CPUParser) Name() string { return "cpu" } -func (p *CPUParser) Parse(scanner *bufio.Scanner) (map[string]string, error) { +func (p *CPUParser) Parse(scanner *ParserScanner) (map[string]string, error) { resMap := make(map[string]string, 0) for scanner.Scan() { diff --git a/crawler/impl/ssh/parser/memory_parser.go b/crawler/impl/ssh/parser/memory_parser.go index afdc21f..6f21b9e 100644 --- a/crawler/impl/ssh/parser/memory_parser.go +++ b/crawler/impl/ssh/parser/memory_parser.go @@ -1,7 +1,6 @@ package parser import ( - "bufio" "strings" ) @@ -12,7 +11,7 @@ func (p *MemoryParser) Name() string { return "mem" } -func (p *MemoryParser) Parse(scanner *bufio.Scanner) (map[string]string, error) { +func (p *MemoryParser) Parse(scanner *ParserScanner) (map[string]string, error) { var ( stats = map[string]string{} ) diff --git a/crawler/impl/ssh/parser/parser.go b/crawler/impl/ssh/parser/parser.go index 3733c9d..540acb7 100644 --- a/crawler/impl/ssh/parser/parser.go +++ b/crawler/impl/ssh/parser/parser.go @@ -25,5 +25,87 @@ func GetParser(name string) Parser { type Parser interface { Name() string - Parse(scanner *bufio.Scanner) (map[string]string, error) + Parse(scanner *ParserScanner) (map[string]string, error) +} + +type ParserScanner struct { + scanner *bufio.Scanner + boundary string + + text string + + ended bool + eof bool +} + +func (ps *ParserScanner) Clean() bool { + if ps.eof { + return false + } + + if ps.ended { + ps.ended = false + return true + } + + for ps.scanner.Scan() { + line := ps.scanner.Text() + if line == "--"+ps.boundary+"--" { + ps.eof = true + return false + } + + if line == "--"+ps.boundary { + break + } + } + return true +} + +func (ps *ParserScanner) EOF() bool { + return ps.eof +} + +func (ps *ParserScanner) Scan() bool { + if ps.eof || ps.ended { + return false + } + + if !ps.scanner.Scan() { + return false + } + + line := ps.scanner.Text() + if line == "--"+ps.boundary+"--" { + ps.eof = true + return false + } + + if line == "--"+ps.boundary { + ps.ended = true + return false + } + + ps.text = line + return true +} + +func (ps *ParserScanner) Text() string { + if ps.eof || ps.ended { + return "" + } + return ps.text +} + +func (ps *ParserScanner) Err() error { + return ps.scanner.Err() +} + +func NewParserScanner(scanner *bufio.Scanner, boundary string) *ParserScanner { + ps := &ParserScanner{ + scanner: scanner, + boundary: boundary, + } + + return ps }