[minor] update netstat module to new interfaces

This commit is contained in:
Julien Vehent 2015-04-29 12:52:55 -04:00
Родитель 5817c1c53d
Коммит 7d3ffb474f
1 изменённых файлов: 36 добавлений и 41 удалений

Просмотреть файл

@ -12,7 +12,7 @@ package netstat
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"mig" "mig/modules"
"net" "net"
"regexp" "regexp"
"strconv" "strconv"
@ -21,14 +21,14 @@ import (
) )
func init() { func init() {
mig.RegisterModule("netstat", func() interface{} { modules.Register("netstat", func() interface{} {
return new(Runner) return new(Runner)
}, false) })
} }
type Runner struct { type Runner struct {
Parameters params Parameters params
Results mig.ModuleResult Results modules.Result
} }
type params struct { type params struct {
@ -147,7 +147,7 @@ func validatePort(val string) error {
return nil return nil
} }
func (r Runner) Run(args []byte) (resStr string) { func (r Runner) Run() (resStr string) {
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
// return error in json // return error in json
@ -160,7 +160,8 @@ func (r Runner) Run(args []byte) (resStr string) {
}() }()
t0 := time.Now() t0 := time.Now()
err := json.Unmarshal(args, &r.Parameters) // read module parameters from stdin
err := modules.ReadInputParameters(&r.Parameters)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -312,28 +313,23 @@ func HasLocalIP(ipStr string) (found bool, elements []element, err error) {
return return
} }
func (r Runner) PrintResults(rawResults []byte, foundOnly bool) (prints []string, err error) { func (r Runner) PrintResults(result modules.Result, matchOnly bool) (prints []string, err error) {
var (
el elements
stats statistics
)
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
err = fmt.Errorf("PrintResults() -> %v", e) err = fmt.Errorf("PrintResults() -> %v", e)
} }
}() }()
var modres mig.ModuleResult el = *newElements()
err = json.Unmarshal(rawResults, &modres) err = result.GetElements(&el)
if err != nil { if err != nil {
panic(err) panic(err)
} }
buf, err := json.Marshal(modres.Elements) for val, res := range el.LocalMAC {
if err != nil { if matchOnly && len(res) < 1 {
panic(err)
}
els := *newElements()
err = json.Unmarshal(buf, &els)
if err != nil {
panic(err)
}
for val, res := range els.LocalMAC {
if foundOnly && len(res) < 1 {
continue continue
} }
for _, el := range res { for _, el := range res {
@ -341,8 +337,8 @@ func (r Runner) PrintResults(rawResults []byte, foundOnly bool) (prints []string
prints = append(prints, resStr) prints = append(prints, resStr)
} }
} }
for val, res := range els.NeighborMAC { for val, res := range el.NeighborMAC {
if foundOnly && len(res) < 1 { if matchOnly && len(res) < 1 {
continue continue
} }
for _, el := range res { for _, el := range res {
@ -355,8 +351,8 @@ func (r Runner) PrintResults(rawResults []byte, foundOnly bool) (prints []string
prints = append(prints, resStr) prints = append(prints, resStr)
} }
} }
for val, res := range els.LocalIP { for val, res := range el.LocalIP {
if foundOnly && len(res) < 1 { if matchOnly && len(res) < 1 {
continue continue
} }
for _, el := range res { for _, el := range res {
@ -368,8 +364,8 @@ func (r Runner) PrintResults(rawResults []byte, foundOnly bool) (prints []string
prints = append(prints, resStr) prints = append(prints, resStr)
} }
} }
for val, res := range els.ConnectedIP { for val, res := range el.ConnectedIP {
if foundOnly && len(res) < 1 { if matchOnly && len(res) < 1 {
continue continue
} }
for _, el := range res { for _, el := range res {
@ -382,8 +378,8 @@ func (r Runner) PrintResults(rawResults []byte, foundOnly bool) (prints []string
prints = append(prints, resStr) prints = append(prints, resStr)
} }
} }
for val, res := range els.ListeningPort { for val, res := range el.ListeningPort {
if foundOnly && len(res) < 1 { if matchOnly && len(res) < 1 {
continue continue
} }
for _, el := range res { for _, el := range res {
@ -395,19 +391,18 @@ func (r Runner) PrintResults(rawResults []byte, foundOnly bool) (prints []string
prints = append(prints, resStr) prints = append(prints, resStr)
} }
} }
if !foundOnly { if matchOnly {
buf, err := json.Marshal(modres.Statistics) return
if err != nil {
panic(err)
}
var stats statistics
err = json.Unmarshal(buf, &stats)
if err != nil {
panic(err)
}
resStr := fmt.Sprintf("Statistics: total hits %.0f examined %.0f items exectime %s",
stats.Totalhits, stats.Examined, stats.Exectime)
prints = append(prints, resStr)
} }
for _, e := range result.Errors {
prints = append(prints, fmt.Sprintf("error: %v", e))
}
err = result.GetStatistics(&stats)
if err != nil {
panic(err)
}
resStr := fmt.Sprintf("Statistics: total hits %.0f examined %.0f items exectime %s",
stats.Totalhits, stats.Examined, stats.Exectime)
prints = append(prints, resStr)
return return
} }