|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + log "github.com/inconshreveable/log15" |
| 13 | + model "github.com/netsec-ethz/scion-apps/webapp/models" |
| 14 | + . "github.com/netsec-ethz/scion-apps/webapp/util" |
| 15 | +) |
| 16 | + |
| 17 | +// results data extraction regex |
| 18 | +var reRunTime = `(packet loss, time )(\S*)` |
| 19 | +var reRespTime = `(scmp_seq=0 time=)(\S*)` |
| 20 | +var rePktLoss = `(\d+)(% packet loss,)` |
| 21 | + |
| 22 | +// ExtractEchoRespData will parse cmd line output from scmp echo for adding EchoItem fields. |
| 23 | +func ExtractEchoRespData(resp string, d *model.EchoItem, start time.Time) { |
| 24 | + // store duration in ms |
| 25 | + diff := time.Now().Sub(start) |
| 26 | + d.ActualDuration = int(diff.Nanoseconds() / 1e6) |
| 27 | + |
| 28 | + // store current epoch in ms |
| 29 | + d.Inserted = time.Now().UnixNano() / 1e6 |
| 30 | + |
| 31 | + log.Info("resp response", "content", resp) |
| 32 | + |
| 33 | + var data = make(map[string]float32) |
| 34 | + // -1 if no match for response time, indicating response timeout or packets out of order |
| 35 | + data["response_time"] = -1 |
| 36 | + var path, err string |
| 37 | + var match bool |
| 38 | + pathNext := false |
| 39 | + r := strings.Split(resp, "\n") |
| 40 | + for i := range r { |
| 41 | + // match response time in unit ms |
| 42 | + match, _ = regexp.MatchString(reRespTime, r[i]) |
| 43 | + if match { |
| 44 | + re := regexp.MustCompile(reRespTime) |
| 45 | + tStr := re.FindStringSubmatch(r[i])[2] |
| 46 | + t, _ := time.ParseDuration(tStr) |
| 47 | + data["response_time"] = float32(t.Nanoseconds()) / 1e6 |
| 48 | + } |
| 49 | + |
| 50 | + // match run time |
| 51 | + match, _ = regexp.MatchString(reRunTime, r[i]) |
| 52 | + if match { |
| 53 | + re := regexp.MustCompile(reRunTime) |
| 54 | + tStr := re.FindStringSubmatch(r[i])[2] |
| 55 | + t, _ := time.ParseDuration(tStr) |
| 56 | + data["run_time"] = float32(t.Nanoseconds()) / 1e6 |
| 57 | + } |
| 58 | + |
| 59 | + // match packet loss |
| 60 | + match, _ = regexp.MatchString(rePktLoss, r[i]) |
| 61 | + if match { |
| 62 | + re := regexp.MustCompile(rePktLoss) |
| 63 | + loss := re.FindStringSubmatch(r[i])[1] |
| 64 | + l, _ := strconv.ParseFloat(loss, 32) |
| 65 | + data["packet_loss"] = float32(l) |
| 66 | + } |
| 67 | + |
| 68 | + // save used path (default or interactive) for later user display |
| 69 | + if pathNext { |
| 70 | + path = strings.TrimSpace(r[i]) |
| 71 | + } |
| 72 | + match, _ = regexp.MatchString(reUPath, r[i]) |
| 73 | + pathNext = match |
| 74 | + |
| 75 | + // evaluate error message potential |
| 76 | + match1, _ := regexp.MatchString(reErr1, r[i]) |
| 77 | + match2, _ := regexp.MatchString(reErr2, r[i]) |
| 78 | + match3, _ := regexp.MatchString(reErr3, r[i]) |
| 79 | + |
| 80 | + if match1 { |
| 81 | + re := regexp.MustCompile(reErr1) |
| 82 | + err = re.FindStringSubmatch(r[i])[1] |
| 83 | + //log.Info("match1", "err", err) |
| 84 | + } else if match2 { |
| 85 | + re := regexp.MustCompile(reErr2) |
| 86 | + err = re.FindStringSubmatch(r[i])[1] |
| 87 | + } else if match3 { |
| 88 | + re := regexp.MustCompile(reErr3) |
| 89 | + err = re.FindStringSubmatch(r[i])[1] |
| 90 | + } |
| 91 | + } |
| 92 | + log.Info("app response", "data", data) |
| 93 | + |
| 94 | + //log.Info("print parsed result", "error", err) |
| 95 | + //log.Info("print parsed result", "path", path) |
| 96 | + |
| 97 | + d.RunTime, _ = data["run_time"] |
| 98 | + d.ResponseTime, _ = data["response_time"] |
| 99 | + d.PktLoss = int(data["packet_loss"]) |
| 100 | + d.Error = err |
| 101 | + d.Path = path |
| 102 | + d.CmdOutput = resp // pipe log output to render in display later |
| 103 | +} |
| 104 | + |
| 105 | +// GetEchoByTimeHandler request the echo results stored since provided time. |
| 106 | +func GetEchoByTimeHandler(w http.ResponseWriter, r *http.Request, active bool, srcpath string) { |
| 107 | + r.ParseForm() |
| 108 | + since := r.PostFormValue("since") |
| 109 | + log.Info("Requesting echo data since", "timestamp", since) |
| 110 | + // find undisplayed test results |
| 111 | + echoResults, err := model.ReadEchoItemsSince(since) |
| 112 | + if CheckError(err) { |
| 113 | + returnError(w, err) |
| 114 | + return |
| 115 | + } |
| 116 | + log.Debug("Requested data:", "echoResults", echoResults) |
| 117 | + |
| 118 | + echoJSON, err := json.Marshal(echoResults) |
| 119 | + if CheckError(err) { |
| 120 | + returnError(w, err) |
| 121 | + return |
| 122 | + } |
| 123 | + jsonBuf := []byte(`{ "graph": ` + string(echoJSON)) |
| 124 | + json := []byte(`, "active": ` + strconv.FormatBool(active)) |
| 125 | + jsonBuf = append(jsonBuf, json...) |
| 126 | + jsonBuf = append(jsonBuf, []byte(`}`)...) |
| 127 | + |
| 128 | + // ensure % if any, is escaped correctly before writing to printf formatter |
| 129 | + fmt.Fprintf(w, strings.Replace(string(jsonBuf), "%", "%%", -1)) |
| 130 | +} |
0 commit comments