|
| 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 reHop = `(\d+)\s+(\S+),\[(\S+)\].+\s(\S+)\s+(\S+)\s+(\S+)` |
| 19 | +var reINTF = `(?i:ifid=)(\d+)` |
| 20 | + |
| 21 | +// ExtractTracerouteRespData will parse cmd line output from scmp traceroute for adding TracerouteItem fields. |
| 22 | +func ExtractTracerouteRespData(resp string, d *model.TracerouteItem, start time.Time) { |
| 23 | + // store duration in ms |
| 24 | + diff := time.Now().Sub(start) |
| 25 | + d.ActualDuration = int(diff.Nanoseconds() / 1e6) |
| 26 | + |
| 27 | + // store current epoch in ms |
| 28 | + d.Inserted = time.Now().UnixNano() / 1e6 |
| 29 | + |
| 30 | + //log.Info("resp response", "content", resp) |
| 31 | + |
| 32 | + var path, err string |
| 33 | + pathNext := false |
| 34 | + r := strings.Split(resp, "\n") |
| 35 | + for i := range r { |
| 36 | + // save used path (default or interactive) for later user display |
| 37 | + if pathNext { |
| 38 | + path = strings.TrimSpace(r[i]) |
| 39 | + } |
| 40 | + match, _ := regexp.MatchString(reUPath, r[i]) |
| 41 | + pathNext = match |
| 42 | + |
| 43 | + // evaluate error message potential |
| 44 | + match1, _ := regexp.MatchString(reErr1, r[i]) |
| 45 | + match2, _ := regexp.MatchString(reErr2, r[i]) |
| 46 | + match3, _ := regexp.MatchString(reErr3, r[i]) |
| 47 | + match4, _ := regexp.MatchString(reErr4, r[i]) |
| 48 | + match5, _ := regexp.MatchString(reErr5, r[i]) |
| 49 | + |
| 50 | + if match1 { |
| 51 | + re := regexp.MustCompile(reErr1) |
| 52 | + err = re.FindStringSubmatch(r[i])[1] |
| 53 | + } else if match2 { |
| 54 | + re := regexp.MustCompile(reErr2) |
| 55 | + err = re.FindStringSubmatch(r[i])[1] |
| 56 | + } else if match3 { |
| 57 | + re := regexp.MustCompile(reErr3) |
| 58 | + err = re.FindStringSubmatch(r[i])[1] |
| 59 | + } else if match4 { |
| 60 | + re := regexp.MustCompile(reErr4) |
| 61 | + err = re.FindStringSubmatch(r[i])[1] |
| 62 | + } else if match5 { |
| 63 | + re := regexp.MustCompile(reErr5) |
| 64 | + err = re.FindStringSubmatch(r[i])[1] |
| 65 | + } |
| 66 | + |
| 67 | + handleHopData(r[i], d.Inserted) |
| 68 | + } |
| 69 | + log.Debug("***Path: " + path) |
| 70 | + |
| 71 | + d.Error = err |
| 72 | + d.CmdOutput = resp // pipe log output to render in display later |
| 73 | + d.Path = path |
| 74 | +} |
| 75 | + |
| 76 | +// Extract the hop info from traceroute response and store them in the db related to hop |
| 77 | +func handleHopData(line string, runTimeKey int64) { |
| 78 | + d := model.TrHopItem{} |
| 79 | + d.RunTimeKey = runTimeKey |
| 80 | + // store current epoch in ms |
| 81 | + d.Inserted = time.Now().UnixNano() / 1e6 |
| 82 | + |
| 83 | + match, _ := regexp.MatchString(reHop, line) |
| 84 | + if match { |
| 85 | + re := regexp.MustCompile(reHop) |
| 86 | + order, _ := strconv.Atoi(re.FindStringSubmatch(line)[1]) |
| 87 | + d.Ord = int(order) |
| 88 | + d.HopIa = re.FindStringSubmatch(line)[2] |
| 89 | + d.HopAddr = re.FindStringSubmatch(line)[3] |
| 90 | + |
| 91 | + matchIntf, _ := regexp.MatchString(reINTF, line) |
| 92 | + if matchIntf { |
| 93 | + reIF := regexp.MustCompile(reINTF) |
| 94 | + intfID, _ := strconv.Atoi(reIF.FindStringSubmatch(line)[1]) |
| 95 | + d.IntfID = int(intfID) |
| 96 | + } else { |
| 97 | + d.IntfID = -1 |
| 98 | + } |
| 99 | + |
| 100 | + var RespTime [3]float32 |
| 101 | + for i := 0; i < 3; i++ { |
| 102 | + resp := re.FindStringSubmatch(line)[4+i] |
| 103 | + t, err := time.ParseDuration(resp) |
| 104 | + if err == nil { |
| 105 | + RespTime[i] = float32(t.Nanoseconds()) / 1e6 |
| 106 | + } else { |
| 107 | + RespTime[i] = -1 |
| 108 | + } |
| 109 | + } |
| 110 | + d.RespTime1 = RespTime[0] |
| 111 | + d.RespTime2 = RespTime[1] |
| 112 | + d.RespTime3 = RespTime[2] |
| 113 | + |
| 114 | + //store hop information in db |
| 115 | + err := model.StoreTrHopItem(&d) |
| 116 | + if err != nil { |
| 117 | + log.Error(fmt.Sprintf("Error storing hop items: %v", err)) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// GetTracerouteByTimeHandler request the traceroute results stored since provided time. |
| 123 | +func GetTracerouteByTimeHandler(w http.ResponseWriter, r *http.Request, active bool, srcpath string) { |
| 124 | + r.ParseForm() |
| 125 | + since := r.PostFormValue("since") |
| 126 | + log.Info("Requesting traceroute data since", "timestamp", since) |
| 127 | + // find undisplayed test results |
| 128 | + tracerouteResults, err := model.ReadTracerouteItemsSince(since) |
| 129 | + if CheckError(err) { |
| 130 | + returnError(w, err) |
| 131 | + return |
| 132 | + } |
| 133 | + // log.Debug("Requested data:", "tracerouteResults", tracerouteResults) |
| 134 | + |
| 135 | + tracerouteJSON, err := json.Marshal(tracerouteResults) |
| 136 | + if CheckError(err) { |
| 137 | + returnError(w, err) |
| 138 | + return |
| 139 | + } |
| 140 | + jsonBuf := []byte(`{ "graph": ` + string(tracerouteJSON)) |
| 141 | + json := []byte(`, "active": ` + strconv.FormatBool(active)) |
| 142 | + jsonBuf = append(jsonBuf, json...) |
| 143 | + jsonBuf = append(jsonBuf, []byte(`}`)...) |
| 144 | + |
| 145 | + //log.Debug(string(jsonBuf)) |
| 146 | + // ensure % if any, is escaped correctly before writing to printf formatter |
| 147 | + fmt.Fprintf(w, strings.Replace(string(jsonBuf), "%", "%%", -1)) |
| 148 | +} |
0 commit comments