This commit is contained in:
crusader 2018-09-18 00:11:56 +09:00
parent 7f63063557
commit a2472a88ab

View File

@ -105,15 +105,15 @@ func (s *PingSummary) GetAvgTime() float32 {
} }
type PingResult struct { type PingResult struct {
responses map[int]Response Responses map[int]Response
summary Summary Summary Summary
} }
func (r *PingResult) GetResponses() map[int]Response { func (r *PingResult) GetResponses() map[int]Response {
return r.responses return r.Responses
} }
func (r *PingResult) GetSummary() Summary { func (r *PingResult) GetSummary() Summary {
return r.summary return r.Summary
} }
// $ ping 192.168.1.1 -c 7 -i 1 -w 0.3 // $ ping 192.168.1.1 -c 7 -i 1 -w 0.3
@ -129,8 +129,8 @@ func (r *PingResult) GetSummary() Summary {
// rtt min/avg/max/mdev = 0.163/0.253/0.344/0.082 ms // rtt min/avg/max/mdev = 0.163/0.253/0.344/0.082 ms
func parseLinuxPing(output []byte) (Result, error) { func parseLinuxPing(output []byte) (Result, error) {
result := &PingResult{ result := &PingResult{
responses: make(map[int]Response, 0), Responses: make(map[int]Response, 0),
summary: &PingSummary{}, Summary: &PingSummary{},
} }
lines := strings.Split(string(output), "\n") lines := strings.Split(string(output), "\n")
@ -150,21 +150,21 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).MinTime = float32(minTime) result.Summary.(*PingSummary).MinTime = float32(minTime)
maxTime, err := strconv.ParseFloat(times[2], 32) maxTime, err := strconv.ParseFloat(times[2], 32)
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).MaxTime = float32(maxTime) result.Summary.(*PingSummary).MaxTime = float32(maxTime)
avgTime, err := strconv.ParseFloat(times[1], 32) avgTime, err := strconv.ParseFloat(times[1], 32)
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).AvgTime = float32(avgTime) result.Summary.(*PingSummary).AvgTime = float32(avgTime)
case 8: case 8:
if "bytes" != fields[1] || "from" != fields[2] { if "bytes" != fields[1] || "from" != fields[2] {
@ -190,7 +190,7 @@ LOOP:
continue LOOP continue LOOP
} }
result.responses[seq] = &PingResponse{ result.Responses[seq] = &PingResponse{
TTL: ttl, TTL: ttl,
Time: float32(_time), Time: float32(_time),
} }
@ -201,21 +201,21 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).SendCount = sendCount result.Summary.(*PingSummary).SendCount = sendCount
receiveCount, err := strconv.Atoi(fields[3]) receiveCount, err := strconv.Atoi(fields[3])
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).ReceiveCount = receiveCount result.Summary.(*PingSummary).ReceiveCount = receiveCount
lossPercent, err := strconv.ParseFloat(strings.Replace(fields[5], "%", "", -1), 32) lossPercent, err := strconv.ParseFloat(strings.Replace(fields[5], "%", "", -1), 32)
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).LossPercent = float32(lossPercent) result.Summary.(*PingSummary).LossPercent = float32(lossPercent)
} }
} }
@ -253,8 +253,8 @@ LOOP:
func parseWindowsPing(output []byte) (Result, error) { func parseWindowsPing(output []byte) (Result, error) {
result := &PingResult{ result := &PingResult{
responses: make(map[int]Response, 0), Responses: make(map[int]Response, 0),
summary: &PingSummary{}, Summary: &PingSummary{},
} }
lines := strings.Split(string(output), "\n") lines := strings.Split(string(output), "\n")
@ -291,7 +291,7 @@ LOOP:
continue LOOP continue LOOP
} }
result.responses[seq] = &PingResponse{ result.Responses[seq] = &PingResponse{
TTL: ttl, TTL: ttl,
Time: float32(_time), Time: float32(_time),
} }
@ -308,7 +308,7 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).MinTime = float32(minTime) result.Summary.(*PingSummary).MinTime = float32(minTime)
maxTimes := strings.Replace(fields[5], "ms", "", -1) maxTimes := strings.Replace(fields[5], "ms", "", -1)
maxTimes = strings.Replace(maxTimes, ",", "", -1) maxTimes = strings.Replace(maxTimes, ",", "", -1)
@ -317,7 +317,7 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).MaxTime = float32(maxTime) result.Summary.(*PingSummary).MaxTime = float32(maxTime)
avgTimes := strings.Replace(fields[8], "ms", "", -1) avgTimes := strings.Replace(fields[8], "ms", "", -1)
avgTime, err := strconv.ParseFloat(avgTimes, 32) avgTime, err := strconv.ParseFloat(avgTimes, 32)
@ -325,7 +325,7 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).AvgTime = float32(avgTime) result.Summary.(*PingSummary).AvgTime = float32(avgTime)
case 12: case 12:
if "Packets:" != fields[0] { if "Packets:" != fields[0] {
@ -336,14 +336,14 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).SendCount = sendCount result.Summary.(*PingSummary).SendCount = sendCount
receiveCount, err := strconv.Atoi(strings.Replace(fields[6], ",", "", -1)) receiveCount, err := strconv.Atoi(strings.Replace(fields[6], ",", "", -1))
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).ReceiveCount = receiveCount result.Summary.(*PingSummary).ReceiveCount = receiveCount
lossPercents := strings.Replace(fields[10], "(", "", -1) lossPercents := strings.Replace(fields[10], "(", "", -1)
lossPercents = strings.Replace(lossPercents, "%", "", -1) lossPercents = strings.Replace(lossPercents, "%", "", -1)
@ -352,7 +352,7 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).LossPercent = float32(lossPercent) result.Summary.(*PingSummary).LossPercent = float32(lossPercent)
} }
} }
@ -372,8 +372,8 @@ LOOP:
// round-trip min/avg/max/stddev = 0.971/2.760/3.934/1.204 ms // round-trip min/avg/max/stddev = 0.971/2.760/3.934/1.204 ms
func parseDarwinPing(output []byte) (Result, error) { func parseDarwinPing(output []byte) (Result, error) {
result := &PingResult{ result := &PingResult{
responses: make(map[int]Response, 0), Responses: make(map[int]Response, 0),
summary: &PingSummary{}, Summary: &PingSummary{},
} }
lines := strings.Split(string(output), "\n") lines := strings.Split(string(output), "\n")
@ -393,21 +393,21 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).MinTime = float32(minTime) result.Summary.(*PingSummary).MinTime = float32(minTime)
maxTime, err := strconv.ParseFloat(times[2], 32) maxTime, err := strconv.ParseFloat(times[2], 32)
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).MaxTime = float32(maxTime) result.Summary.(*PingSummary).MaxTime = float32(maxTime)
avgTime, err := strconv.ParseFloat(times[1], 32) avgTime, err := strconv.ParseFloat(times[1], 32)
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).AvgTime = float32(avgTime) result.Summary.(*PingSummary).AvgTime = float32(avgTime)
case 8: case 8:
if "bytes" != fields[1] || "from" != fields[2] { if "bytes" != fields[1] || "from" != fields[2] {
@ -433,7 +433,7 @@ LOOP:
continue LOOP continue LOOP
} }
result.responses[seq] = &PingResponse{ result.Responses[seq] = &PingResponse{
TTL: ttl, TTL: ttl,
Time: float32(_time), Time: float32(_time),
} }
@ -444,21 +444,21 @@ LOOP:
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).SendCount = sendCount result.Summary.(*PingSummary).SendCount = sendCount
receiveCount, err := strconv.Atoi(fields[3]) receiveCount, err := strconv.Atoi(fields[3])
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).ReceiveCount = receiveCount result.Summary.(*PingSummary).ReceiveCount = receiveCount
lossPercent, err := strconv.ParseFloat(strings.Replace(fields[6], "%", "", -1), 32) lossPercent, err := strconv.ParseFloat(strings.Replace(fields[6], "%", "", -1), 32)
if nil != err { if nil != err {
log.Print(err) log.Print(err)
continue LOOP continue LOOP
} }
result.summary.(*PingSummary).LossPercent = float32(lossPercent) result.Summary.(*PingSummary).LossPercent = float32(lossPercent)
} }
} }