This commit is contained in:
crusader 2017-11-15 21:33:46 +09:00
parent e07d0717a2
commit 570a0241a0
5 changed files with 141 additions and 34 deletions

View File

@ -25,15 +25,15 @@ func DiscoverZone(dz *model.DiscoveryZone) {
} }
func DiscoverHost(dh *model.DiscoveryHost) { func DiscoverHost(dh *model.DiscoveryHost) {
discoverer.discoverHost(dh) discoverer.discoverHost(dh.Zone, dh)
} }
func DiscoverPort(dp *model.DiscoveryPort) { func DiscoverPort(dp *model.DiscoveryPort) {
discoverer.discoverPort(dp) discoverer.discoverPort(dp.Host, dp)
} }
func DiscoverService(ds *model.DiscoveryService) { func DiscoverService(ds *model.DiscoveryService) {
discoverer.discoverService(ds) discoverer.discoverService(ds.Port, ds)
} }
func Stop() { func Stop() {
@ -57,9 +57,6 @@ func (d *discovery) start() {
d.sendChan = make(chan interface{}) d.sendChan = make(chan interface{})
d.errChan = make(chan error) d.errChan = make(chan error)
logging.Logger().Info(fmt.Sprintf("Discovery: discovery is started")) logging.Logger().Info(fmt.Sprintf("Discovery: discovery is started"))
d.stopWg.Add(1)
go handle(d)
} }
func (d *discovery) stop() { func (d *discovery) stop() {
@ -73,47 +70,141 @@ func (d *discovery) stop() {
logging.Logger().Info(fmt.Sprintf("Discovery: discovery is stopped")) logging.Logger().Info(fmt.Sprintf("Discovery: discovery is stopped"))
} }
func handle(d *discovery) {
defer d.stopWg.Done()
for {
select {
case <-d.stopChan:
return
case data := <-d.sendChan:
logging.Logger().Info(fmt.Sprintf("chan: %v", data))
}
}
}
func (d *discovery) discoverZone(dz *model.DiscoveryZone) { func (d *discovery) discoverZone(dz *model.DiscoveryZone) {
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is start")) logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is start"))
d.stopWg.Add(1)
resultChan := make(chan *model.Zone)
errChan := make(chan error)
doneChan := make(chan struct{}) doneChan := make(chan struct{})
d.stopWg.Add(1) defer func() {
defer d.stopWg.Done() close(resultChan)
close(errChan)
close(doneChan)
d.stopWg.Done()
}()
go scanZone(d, dz, doneChan) go scanZone(dz, resultChan, errChan, doneChan)
for {
select { select {
case z := <-resultChan:
logging.Logger().Info(fmt.Sprintf("zone: %v", z))
if nil != dz.DiscoveryHost {
d.discoverHost(z, dz.DiscoveryHost)
}
case err := <-errChan:
logging.Logger().Info(fmt.Sprintf("zone err: %v", err))
case <-d.stopChan: case <-d.stopChan:
<-doneChan return
case <-doneChan: case <-doneChan:
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is complete")) logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is complete"))
return
}
} }
} }
func (d *discovery) discoverHost(dh *model.DiscoveryHost) { func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) {
logging.Logger().Debug(fmt.Sprintf("DiscoverHost")) logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverHost is start"))
d.stopWg.Add(1)
resultChan := make(chan *model.Host)
errChan := make(chan error)
doneChan := make(chan struct{})
defer func() {
close(resultChan)
close(errChan)
close(doneChan)
d.stopWg.Done()
}()
go scanHost(zone, dh, resultChan, errChan, doneChan)
for {
select {
case h := <-resultChan:
logging.Logger().Info(fmt.Sprintf("host: %v", h))
if nil != dh.DiscoveryPort {
d.discoverPort(h, dh.DiscoveryPort)
}
case err := <-errChan:
logging.Logger().Info(fmt.Sprintf("host err: %v", err))
case <-d.stopChan:
return
case <-doneChan:
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverHost is complete"))
return
}
}
} }
func (d *discovery) discoverPort(dp *model.DiscoveryPort) { func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) {
logging.Logger().Debug(fmt.Sprintf("DiscoverPort")) logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverPort is start"))
d.stopWg.Add(1)
resultChan := make(chan *model.Port)
errChan := make(chan error)
doneChan := make(chan struct{})
defer func() {
close(resultChan)
close(errChan)
close(doneChan)
d.stopWg.Done()
}()
go scanPort(host, dp, resultChan, errChan, doneChan)
for {
select {
case p := <-resultChan:
logging.Logger().Info(fmt.Sprintf("port: %v", p))
if nil != dp.DiscoveryService {
d.discoverService(p, dp.DiscoveryService)
}
case err := <-errChan:
logging.Logger().Info(fmt.Sprintf("port err: %v", err))
case <-d.stopChan:
return
case <-doneChan:
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverPort is complete"))
return
}
}
} }
func (d *discovery) discoverService(ds *model.DiscoveryService) { func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService) {
logging.Logger().Debug(fmt.Sprintf("DiscoverService")) logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverService is start"))
d.stopWg.Add(1)
resultChan := make(chan *model.Service)
errChan := make(chan error)
doneChan := make(chan struct{})
defer func() {
close(resultChan)
close(errChan)
close(doneChan)
d.stopWg.Done()
}()
go scanService(port, ds, resultChan, errChan, doneChan)
for {
select {
case s := <-resultChan:
logging.Logger().Info(fmt.Sprintf("service: %v", s))
case err := <-errChan:
logging.Logger().Info(fmt.Sprintf("service err: %v", err))
case <-d.stopChan:
return
case <-doneChan:
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverService is complete"))
return
}
}
} }
func (d *discovery) sendResult() { func (d *discovery) sendResult() {

View File

@ -2,5 +2,7 @@ package discovery
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func scanHost(d *discovery, dh *model.DiscoveryHost, doneChan chan<- struct{}) { func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan *model.Host, errChan chan error, doneChan chan<- struct{}) {
doneChan <- struct{}{}
} }

7
discovery/port.go Normal file
View File

@ -0,0 +1,7 @@
package discovery
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func scanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan *model.Port, errChan chan error, doneChan chan<- struct{}) {
doneChan <- struct{}{}
}

7
discovery/service.go Normal file
View File

@ -0,0 +1,7 @@
package discovery
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func scanService(port *model.Port, ds *model.DiscoveryService, resultChan chan *model.Service, errChan chan error, doneChan chan<- struct{}) {
doneChan <- struct{}{}
}

View File

@ -8,7 +8,7 @@ import (
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
) )
func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) { func scanZone(dz *model.DiscoveryZone, resultChan chan *model.Zone, errChan chan error, doneChan chan<- struct{}) {
var err error var err error
var ifaces []net.Interface var ifaces []net.Interface
var addrs []net.Addr var addrs []net.Addr
@ -23,7 +23,7 @@ func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) {
// } // }
if ifaces, err = net.Interfaces(); nil != err { if ifaces, err = net.Interfaces(); nil != err {
d.errChan <- err errChan <- err
return return
} }
@ -32,14 +32,14 @@ func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) {
for _, i := range ifaces { for _, i := range ifaces {
if addrs, err = i.Addrs(); nil != err { if addrs, err = i.Addrs(); nil != err {
d.errChan <- err errChan <- err
continue continue
} }
for _, addr := range addrs { for _, addr := range addrs {
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err {
d.errChan <- err errChan <- err
continue continue
} }
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) { if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) {
@ -55,7 +55,7 @@ func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) {
IP: strings.Split(addr.String(), "/")[0], IP: strings.Split(addr.String(), "/")[0],
} }
d.sendChan <- z resultChan <- z
} }
} }