This commit is contained in:
crusader 2017-12-14 16:10:11 +09:00
parent cda77e4ad6
commit cda6a02f65
4 changed files with 91 additions and 17 deletions

View File

@ -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

View File

@ -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() {

View File

@ -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{}
)

View File

@ -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
}