|
| 1 | +package fping |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fkd13.github.com/pingtui/state" |
| 6 | + "log" |
| 7 | + "os/exec" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | +) |
| 11 | + |
| 12 | +func RunFPing(appState *state.AppState) { |
| 13 | + var hosts []string |
| 14 | + |
| 15 | + appState.Lock() |
| 16 | + for _, target := range appState.GetTargets() { |
| 17 | + hosts = append(hosts, target.Host) |
| 18 | + } |
| 19 | + appState.Unlock() |
| 20 | + |
| 21 | + cmd := exec.Command("fping", append([]string{"-l"}, hosts...)...) |
| 22 | + stdout, err := cmd.StdoutPipe() |
| 23 | + if err != nil { |
| 24 | + panic(err) |
| 25 | + } |
| 26 | + |
| 27 | + if err := cmd.Start(); err != nil { |
| 28 | + panic(err) |
| 29 | + } |
| 30 | + |
| 31 | + // Failed pings contain "timed out" |
| 32 | + reFail := regexp.MustCompile(`^([^ ]+) +: \[([0-9]+)], timed out \(([0-9]+(\.[0-9]+)?|NaN) avg, ([0-9]+)% loss\)$`) |
| 33 | + // Successful pings contain "bytes" |
| 34 | + reSuccess := regexp.MustCompile(`^([^ ]+) +: \[([0-9]+)], [0-9]+ bytes, ([0-9]+(\.[0-9]+)?) ms \(([0-9]+(\.[0-9]+)?) avg, ([0-9]+)% loss\)$`) |
| 35 | + |
| 36 | + scanner := bufio.NewScanner(stdout) |
| 37 | + for scanner.Scan() { |
| 38 | + line := scanner.Text() |
| 39 | + go handleLine(line, appState, reSuccess, reFail) |
| 40 | + } |
| 41 | + |
| 42 | + if err := cmd.Wait(); err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func handleLine(line string, appState *state.AppState, reSuccess *regexp.Regexp, reFail *regexp.Regexp) { |
| 48 | + if reSuccess.MatchString(line) { |
| 49 | + handleSuccess(line, appState, reSuccess) |
| 50 | + } else if reFail.MatchString(line) { |
| 51 | + handleFail(line, appState, reFail) |
| 52 | + } else { |
| 53 | + // TODO: decide what to do |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func handleSuccess(line string, appState *state.AppState, reSuccess *regexp.Regexp) { |
| 58 | + data := reSuccess.FindAllStringSubmatch(line, -1) |
| 59 | + |
| 60 | + host := data[0][1] |
| 61 | + sequenceID, _ := strconv.Atoi(data[0][2]) |
| 62 | + duration, _ := strconv.ParseFloat(data[0][3], 64) |
| 63 | + avgDuration, _ := strconv.ParseFloat(data[0][5], 64) |
| 64 | + loss, _ := strconv.ParseFloat(data[0][7], 64) |
| 65 | + |
| 66 | + appState.Lock() |
| 67 | + appState.GetTarget(host).UpdateProbe(&state.Probe{ |
| 68 | + State: state.Success, |
| 69 | + SequenceID: sequenceID, |
| 70 | + Latency: duration, |
| 71 | + AverageLatency: avgDuration, |
| 72 | + Loss: loss, |
| 73 | + }) |
| 74 | + appState.Unlock() |
| 75 | +} |
| 76 | + |
| 77 | +func handleFail(line string, appState *state.AppState, reFail *regexp.Regexp) { |
| 78 | + data := reFail.FindAllStringSubmatch(line, -1) |
| 79 | + |
| 80 | + host := data[0][1] |
| 81 | + sequenceID, _ := strconv.Atoi(data[0][2]) |
| 82 | + avgDuration, _ := strconv.ParseFloat(data[0][3], 64) |
| 83 | + loss, _ := strconv.ParseFloat(data[0][5], 64) |
| 84 | + |
| 85 | + appState.Lock() |
| 86 | + appState.GetTarget(host).UpdateProbe(&state.Probe{ |
| 87 | + State: state.Failure, |
| 88 | + SequenceID: sequenceID, |
| 89 | + AverageLatency: avgDuration, |
| 90 | + Loss: loss, |
| 91 | + }) |
| 92 | + appState.Unlock() |
| 93 | +} |
0 commit comments