Skip to content

Commit 93ed1f4

Browse files
ZilinIBmwfarb
authored andcommitted
Webapp backend for traceroute finished. (#83)
* Webapp backend for traceroute finished. * Fixed small typo * TrHop struct renamed, json output for tracerouteGraph modified. * Path column in the traceroute db added. Redundant logging deleted. Debug for json response of traceroute. * Reset vendor.json, further reduced logging. * Merge branch 'master' into WebappTraceroute
1 parent eb71dc9 commit 93ed1f4

File tree

10 files changed

+782
-54
lines changed

10 files changed

+782
-54
lines changed

webapp/config/servers_default.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,31 @@
9696
"addr": "203.230.60.98",
9797
"port": 40002
9898
}
99+
],
100+
"traceroute": [
101+
{
102+
"name": "17-ffaa:0:1107 Attachment Point",
103+
"isdas": "17-ffaa:0:1107",
104+
"addr": "192.33.93.195",
105+
"port": 40002
106+
},
107+
{
108+
"name": "18-ffaa:0:1202 Attachment Point",
109+
"isdas": "18-ffaa:0:1202",
110+
"addr": "128.105.21.208",
111+
"port": 40002
112+
},
113+
{
114+
"name": "19-ffaa:0:1303 Attachment Point",
115+
"isdas": "19-ffaa:0:1303",
116+
"addr": "141.44.25.144",
117+
"port": 40002
118+
},
119+
{
120+
"name": "20-ffaa:0:1404 Attachment Point",
121+
"isdas": "20-ffaa:0:1404",
122+
"addr": "203.230.60.98",
123+
"port": 40002
124+
}
99125
]
100126
}

webapp/lib/bwcont.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ func removeOuterQuotes(s string) string {
186186
return s
187187
}
188188

189-
// WriteContCmdCsv appends the continuous cmd data (bwtest or echo) in csv-format to srcpath.
190-
func WriteContCmdCsv(d model.CmdItem, srcpath string, appSel string) {
189+
// WriteCmdCsv appends the cmd data (bwtest or echo) in csv-format to srcpath.
190+
func WriteCmdCsv(d model.CmdItem, srcpath string, appSel string) {
191191
// newfile name for every day
192192
dataFileCmd := "data/" + appSel + "-" + time.Now().Format("2006-01-02") + ".csv"
193193
cmdDataPath := path.Join(srcpath, dataFileCmd)

webapp/lib/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func GenServerNodeDefaults(srcpath string) {
101101
serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefSen + `}], `)
102102
jsonBuf = append(jsonBuf, json...)
103103
json = []byte(`"echo": [{"name":"localhost","isdas":"` +
104+
serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefSen + `}], `)
105+
jsonBuf = append(jsonBuf, json...)
106+
json = []byte(`"traceroute": [{"name":"localhost","isdas":"` +
104107
serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefSen + `}]`)
105108
jsonBuf = append(jsonBuf, json...)
106109

webapp/lib/traceroutecont.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}

webapp/models/db.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func LoadDB() error {
4141
if err != nil {
4242
return err
4343
}
44+
err = createTracerouteTable()
45+
if err != nil {
46+
return err
47+
}
48+
err = createTrHopTable()
49+
if err != nil {
50+
return err
51+
}
4452
version, err := getUserVersion()
4553
if err != nil {
4654
return err
@@ -74,16 +82,28 @@ func MaintainDatabase() {
7482
for {
7583
before := time.Now().Add(-dbExpire)
7684

77-
count1, err1 := DeleteBwTestItemsBefore(strconv.FormatInt(before.UnixNano()/1e6, 10))
78-
CheckError(err1)
79-
if count1 > 0 {
80-
log.Warn(fmt.Sprint("Deleting ", count1, " bwtests db rows older than", dbExpire))
85+
count, err := DeleteBwTestItemsBefore(strconv.FormatInt(before.UnixNano()/1e6, 10))
86+
CheckError(err)
87+
if count > 0 {
88+
log.Warn(fmt.Sprint("Deleting ", count, " bwtests db rows older than", dbExpire))
89+
}
90+
91+
count, err = DeleteEchoItemsBefore(strconv.FormatInt(before.UnixNano()/1e6, 10))
92+
CheckError(err)
93+
if count > 0 {
94+
log.Warn(fmt.Sprint("Deleting ", count, " echo db rows older than", dbExpire))
95+
}
96+
97+
count, err = DeleteTracerouteItemsBefore(strconv.FormatInt(before.UnixNano()/1e6, 10))
98+
CheckError(err)
99+
if count > 0 {
100+
log.Warn(fmt.Sprint("Deleting ", count, " traceroute db rows older than", dbExpire))
81101
}
82102

83-
count2, err2 := DeleteEchoItemsBefore(strconv.FormatInt(before.UnixNano()/1e6, 10))
84-
CheckError(err2)
85-
if count2 > 0 {
86-
log.Warn(fmt.Sprint("Deleting ", count2, " echo db rows older than", dbExpire))
103+
count, err = DeleteTrHopItemsBefore(strconv.FormatInt(before.UnixNano()/1e6, 10))
104+
CheckError(err)
105+
if count > 0 {
106+
log.Warn(fmt.Sprint("Deleting ", count, " trhops db rows older than", dbExpire))
87107
}
88108
time.Sleep(dbExpire)
89109
}

webapp/models/scmpecho.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type EchoItem struct {
2020
SIa string
2121
SAddr string
2222
Count int // Default 1
23-
Timeout int // s Default 2
24-
Interval int // s Default 1
23+
Timeout float32 // s Default 2
24+
Interval float32 // s Default 1
2525
ResponseTime float32 // ms
2626
RunTime float32
2727
PktLoss int // percent Indicating pkt loss rate
@@ -54,8 +54,8 @@ func createEchoTable() error {
5454
SIa TEXT,
5555
SAddr TEXT,
5656
Count INT,
57-
Timeout INT,
58-
Interval INT,
57+
Timeout REAL,
58+
Interval REAL,
5959
ResponseTime REAL,
6060
RunTime REAL,
6161
PktLoss INT,

0 commit comments

Comments
 (0)