Skip to content

Commit f464ae9

Browse files
committed
chg: [template] TODO remove current madness
1 parent 695290e commit f464ae9

File tree

3 files changed

+174
-87
lines changed

3 files changed

+174
-87
lines changed

logparser/sshd.go

Lines changed: 99 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package logparser
33
import (
44
"fmt"
55
"html/template"
6+
"io/ioutil"
67
"log"
78
"math"
89
"os"
@@ -303,73 +304,6 @@ func (s *SshdParser) Compile() error {
303304
}
304305
}
305306

306-
// Write html file for navigating plots
307-
const tpl = `
308-
<!DOCTYPE html>
309-
<html>
310-
<head>
311-
<meta charset="UTF-8">
312-
<title>{{.Title}}</title>
313-
<script>
314-
var currentType = "statsusername";
315-
var currentDay = {{.MaxDate}};
316-
</script>
317-
<script src="load.js"></script>
318-
<style>
319-
body {
320-
background: white
321-
}
322-
#imageholder {
323-
background: black;
324-
margin: auto;
325-
width: 50%;
326-
padding: 10px;
327-
}
328-
span {
329-
float: left;
330-
clear: left;
331-
}
332-
</style>
333-
</head>
334-
<body onload="loadImage({{.Current}}, currentType)">
335-
336-
<span>
337-
<label for="statsday">Statistics for: </label>
338-
<input id="statsday" type="date" value="{{.MaxDate}}" min="{{.MinDate}}" max="{{.MaxDate}}" onchange="currentDay = this.value.replace(/-/g, ''); loadImage(currentDay, currentType)"/>
339-
</span>
340-
341-
<span>
342-
<select>
343-
<option selected value>year</option>
344-
{{range $val := .YearList}}
345-
<option value="{{$val}}">{{$val}}</option>
346-
{{end}}
347-
</select>
348-
</span>
349-
350-
<span>
351-
<select>
352-
<option selected value>month</option>
353-
{{range $key, $val := .MonthList}}
354-
{{range $month := index $val}}
355-
<option value="{{$month}}">{{$month}}</option>
356-
{{end}}
357-
{{end}}
358-
</select>
359-
</span>
360-
361-
<span>
362-
<label for="statstype">Type: </label>
363-
<select selected="statsusername" onchange="currentType = this.value; loadImage(currentDay.replace(/-/g, ''), currentType)">
364-
<option value="statsusername">Usernames</option>
365-
<option value="statssrc">Sources</option>
366-
<option value="statshost">Hosts</option>
367-
</select>
368-
</span>
369-
<div id="imageholder"></div>
370-
</body>
371-
</html>`
372-
373307
// Get oldest / newest entries
374308
var newest string
375309
var oldest string
@@ -444,36 +378,116 @@ func (s *SshdParser) Compile() error {
444378
}
445379
}
446380

447-
t, err := template.New("webpage").Parse(tpl)
381+
// Parse Template
382+
t, err := template.ParseFiles(filepath.Join("logparser", "sshd", "statistics.gohtml"))
448383
if err != nil {
449384
r.Close()
450385
return err
451386
}
452387

453-
data := struct {
454-
Title string
455-
Current string
456-
MinDate string
457-
MaxDate string
458-
YearList []string
459-
MonthList map[string][]string
388+
daily := struct {
389+
Title string
390+
Current string
391+
MinDate string
392+
MaxDate string
393+
CurrentTime string
394+
}{
395+
Title: "sshd failed logins - daily statistics",
396+
MinDate: parsedOldestStr,
397+
MaxDate: parsedNewestStr,
398+
Current: newest,
399+
CurrentTime: parsedNewestStr,
400+
}
401+
402+
monthly := struct {
403+
Title string
404+
MonthList map[string][]string
405+
CurrentTime string
406+
Current string
407+
}{
408+
Title: "sshd failed logins - monthly statistics",
409+
MonthList: months,
410+
CurrentTime: years[0] + months[years[0]][0],
411+
Current: years[0] + months[years[0]][0],
412+
}
413+
414+
yearly := struct {
415+
Title string
416+
YearList []string
417+
Current string
418+
CurrentTime string
460419
}{
461-
Title: "sshd failed logins statistics",
462-
MinDate: parsedOldestStr,
463-
MaxDate: parsedNewestStr,
464-
Current: newest,
465-
YearList: years,
466-
MonthList: months,
467-
}
468-
_ = os.Remove("statistics.html")
469-
f, err := os.OpenFile("statistics.html", os.O_RDWR|os.O_CREATE, 0666)
420+
Title: "sshd failed logins - yearly statistics",
421+
YearList: years,
422+
Current: years[0],
423+
CurrentTime: years[0],
424+
}
425+
426+
// Create folder to store resulting files
427+
if _, err := os.Stat("data"); os.IsNotExist(err) {
428+
err := os.Mkdir("data", 0700)
429+
if err != nil {
430+
r.Close()
431+
return err
432+
}
433+
}
434+
435+
if _, err := os.Stat(filepath.Join("data", "sshd")); os.IsNotExist(err) {
436+
err := os.Mkdir(filepath.Join("data", "sshd"), 0700)
437+
if err != nil {
438+
r.Close()
439+
return err
440+
}
441+
}
442+
443+
_ = os.Remove(filepath.Join("data", "sshd", "dailystatistics.html"))
444+
_ = os.Remove(filepath.Join("data", "sshd", "monthlystatistics.html"))
445+
_ = os.Remove(filepath.Join("data", "sshd", "yearlystatistics.html"))
446+
447+
f, err := os.OpenFile(filepath.Join("data", "sshd", "dailystatistics.html"), os.O_RDWR|os.O_CREATE, 0666)
448+
defer f.Close()
449+
// err = t.Execute(f, daily)
450+
err = t.ExecuteTemplate(f, "headertpl", daily)
451+
err = t.ExecuteTemplate(f, "dailytpl", daily)
452+
err = t.ExecuteTemplate(f, "footertpl", daily)
453+
if err != nil {
454+
r.Close()
455+
return err
456+
}
457+
458+
f, err = os.OpenFile(filepath.Join("data", "sshd", "monthlystatistics.html"), os.O_RDWR|os.O_CREATE, 0666)
459+
defer f.Close()
460+
// err = t.Execute(f, monthly)
461+
err = t.ExecuteTemplate(f, "headertpl", monthly)
462+
err = t.ExecuteTemplate(f, "monthlytpl", monthly)
463+
err = t.ExecuteTemplate(f, "footertpl", monthly)
464+
if err != nil {
465+
r.Close()
466+
return err
467+
}
468+
469+
f, err = os.OpenFile(filepath.Join("data", "sshd", "yearlystatistics.html"), os.O_RDWR|os.O_CREATE, 0666)
470470
defer f.Close()
471-
err = t.Execute(f, data)
471+
// err = t.Execute(f, yearly)
472+
err = t.ExecuteTemplate(f, "headertpl", yearly)
473+
err = t.ExecuteTemplate(f, "yearlytpl", yearly)
474+
err = t.ExecuteTemplate(f, "footertpl", yearly)
472475
if err != nil {
473476
r.Close()
474477
return err
475478
}
476479

480+
// Copy js asset file
481+
input, err := ioutil.ReadFile(filepath.Join("logparser", "sshd", "load.js"))
482+
if err != nil {
483+
log.Println(err)
484+
}
485+
486+
err = ioutil.WriteFile(filepath.Join("data", "sshd", "load.js"), input, 0644)
487+
if err != nil {
488+
log.Println(err)
489+
}
490+
477491
return nil
478492
}
479493

load.js renamed to logparser/sshd/load.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ function loadImage(date, type) {
3737
console.log(type);
3838
// Get a reference to the body element, and create a new image object
3939
var holder = document.querySelector('#imageholder'),
40-
myImage = new Image();
41-
40+
myImage = new Image();
4241
myImage.crossOrigin = ""; // or "anonymous"
4342

4443
// Call the function with the URL we want to load, but then chain the

logparser/sshd/statistics.gohtml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{{ define "headertpl"}}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
6+
<meta charset="UTF-8">
7+
<title>{{.Title}}</title>
8+
<script>
9+
var currentType = "statsusername";
10+
var current = {{.CurrentTime}};
11+
</script>
12+
<script src="load.js"></script>
13+
<style>
14+
body {
15+
background: white
16+
}
17+
#imageholder {
18+
background: black;
19+
margin: auto;
20+
width: 50%;
21+
padding: 10px;
22+
}
23+
span {
24+
float: left;
25+
clear: left;
26+
}
27+
</style>
28+
</head>
29+
<body onload="loadImage({{.Current}}, currentType)">
30+
{{end}}
31+
32+
33+
{{ define "footertpl"}}
34+
<span>
35+
<label for="statstype">Type: </label>
36+
<select selected="statsusername" onchange="currentType = this.value; loadImage(current.replace(/-/g, ''), currentType)">
37+
<option value="statsusername">Usernames</option>
38+
<option value="statssrc">Sources</option>
39+
<option value="statshost">Hosts</option>
40+
</select>
41+
</span>
42+
<div id="imageholder"></div>
43+
</body>
44+
</html>
45+
{{end}}
46+
47+
{{ define "dailytpl"}}
48+
<span>
49+
<label for="statsday">Statistics for: </label>
50+
<input id="statsday" type="date" value="{{.CurrentTime}}" min="{{.MinDate}}" max="{{.MaxDate}}" onchange="current = this.value.replace(/-/g, ''); loadImage(current, currentType)"/>
51+
</span>
52+
{{end}}
53+
54+
{{ define "yearlytpl"}}
55+
<span>
56+
<select onchange="current = this.value; loadImage(current, currentType)">
57+
{{range $val := .YearList}}
58+
<option value="{{$val}}">{{$val}}</option>
59+
{{end}}
60+
</select>
61+
</span>
62+
{{end}}
63+
64+
{{ define "monthlytpl"}}
65+
<span>
66+
<select onchange="current = this.value.replace(/-/g, ''); loadImage(current, currentType)">
67+
{{range $key, $val := .MonthList}}
68+
{{range $month := index $val}}
69+
<option value="{{$month}}">{{$month}}</option>
70+
{{end}}
71+
{{end}}
72+
</select>
73+
</span>
74+
{{end}}

0 commit comments

Comments
 (0)