From f464ae9c1c22b3c2748651b91f8407be66395874 Mon Sep 17 00:00:00 2001 From: Jean-Louis Huynen Date: Fri, 7 Feb 2020 15:55:30 +0100 Subject: [PATCH] chg: [template] TODO remove current madness --- logparser/sshd.go | 182 ++++++++++++++++-------------- load.js => logparser/sshd/load.js | 3 +- logparser/sshd/statistics.gohtml | 74 ++++++++++++ 3 files changed, 173 insertions(+), 86 deletions(-) rename load.js => logparser/sshd/load.js (98%) create mode 100644 logparser/sshd/statistics.gohtml diff --git a/logparser/sshd.go b/logparser/sshd.go index ac62677..f884f3d 100644 --- a/logparser/sshd.go +++ b/logparser/sshd.go @@ -3,6 +3,7 @@ package logparser import ( "fmt" "html/template" + "io/ioutil" "log" "math" "os" @@ -303,73 +304,6 @@ func (s *SshdParser) Compile() error { } } - // Write html file for navigating plots - const tpl = ` - - - - - {{.Title}} - - - - - - - - - - - - - - - - - - - - - - - -
- - ` - // Get oldest / newest entries var newest string var oldest string @@ -444,36 +378,116 @@ func (s *SshdParser) Compile() error { } } - t, err := template.New("webpage").Parse(tpl) + // Parse Template + t, err := template.ParseFiles(filepath.Join("logparser", "sshd", "statistics.gohtml")) if err != nil { r.Close() return err } - data := struct { - Title string - Current string - MinDate string - MaxDate string - YearList []string - MonthList map[string][]string + daily := struct { + Title string + Current string + MinDate string + MaxDate string + CurrentTime string }{ - Title: "sshd failed logins statistics", - MinDate: parsedOldestStr, - MaxDate: parsedNewestStr, - Current: newest, - YearList: years, - MonthList: months, + Title: "sshd failed logins - daily statistics", + MinDate: parsedOldestStr, + MaxDate: parsedNewestStr, + Current: newest, + CurrentTime: parsedNewestStr, } - _ = os.Remove("statistics.html") - f, err := os.OpenFile("statistics.html", os.O_RDWR|os.O_CREATE, 0666) + + monthly := struct { + Title string + MonthList map[string][]string + CurrentTime string + Current string + }{ + Title: "sshd failed logins - monthly statistics", + MonthList: months, + CurrentTime: years[0] + months[years[0]][0], + Current: years[0] + months[years[0]][0], + } + + yearly := struct { + Title string + YearList []string + Current string + CurrentTime string + }{ + Title: "sshd failed logins - yearly statistics", + YearList: years, + Current: years[0], + CurrentTime: years[0], + } + + // Create folder to store resulting files + if _, err := os.Stat("data"); os.IsNotExist(err) { + err := os.Mkdir("data", 0700) + if err != nil { + r.Close() + return err + } + } + + if _, err := os.Stat(filepath.Join("data", "sshd")); os.IsNotExist(err) { + err := os.Mkdir(filepath.Join("data", "sshd"), 0700) + if err != nil { + r.Close() + return err + } + } + + _ = os.Remove(filepath.Join("data", "sshd", "dailystatistics.html")) + _ = os.Remove(filepath.Join("data", "sshd", "monthlystatistics.html")) + _ = os.Remove(filepath.Join("data", "sshd", "yearlystatistics.html")) + + f, err := os.OpenFile(filepath.Join("data", "sshd", "dailystatistics.html"), os.O_RDWR|os.O_CREATE, 0666) defer f.Close() - err = t.Execute(f, data) + // err = t.Execute(f, daily) + err = t.ExecuteTemplate(f, "headertpl", daily) + err = t.ExecuteTemplate(f, "dailytpl", daily) + err = t.ExecuteTemplate(f, "footertpl", daily) if err != nil { r.Close() return err } + f, err = os.OpenFile(filepath.Join("data", "sshd", "monthlystatistics.html"), os.O_RDWR|os.O_CREATE, 0666) + defer f.Close() + // err = t.Execute(f, monthly) + err = t.ExecuteTemplate(f, "headertpl", monthly) + err = t.ExecuteTemplate(f, "monthlytpl", monthly) + err = t.ExecuteTemplate(f, "footertpl", monthly) + if err != nil { + r.Close() + return err + } + + f, err = os.OpenFile(filepath.Join("data", "sshd", "yearlystatistics.html"), os.O_RDWR|os.O_CREATE, 0666) + defer f.Close() + // err = t.Execute(f, yearly) + err = t.ExecuteTemplate(f, "headertpl", yearly) + err = t.ExecuteTemplate(f, "yearlytpl", yearly) + err = t.ExecuteTemplate(f, "footertpl", yearly) + if err != nil { + r.Close() + return err + } + + // Copy js asset file + input, err := ioutil.ReadFile(filepath.Join("logparser", "sshd", "load.js")) + if err != nil { + log.Println(err) + } + + err = ioutil.WriteFile(filepath.Join("data", "sshd", "load.js"), input, 0644) + if err != nil { + log.Println(err) + } + return nil } diff --git a/load.js b/logparser/sshd/load.js similarity index 98% rename from load.js rename to logparser/sshd/load.js index 446c226..022e010 100644 --- a/load.js +++ b/logparser/sshd/load.js @@ -37,8 +37,7 @@ function loadImage(date, type) { console.log(type); // Get a reference to the body element, and create a new image object var holder = document.querySelector('#imageholder'), - myImage = new Image(); - + myImage = new Image(); myImage.crossOrigin = ""; // or "anonymous" // Call the function with the URL we want to load, but then chain the diff --git a/logparser/sshd/statistics.gohtml b/logparser/sshd/statistics.gohtml new file mode 100644 index 0000000..9127859 --- /dev/null +++ b/logparser/sshd/statistics.gohtml @@ -0,0 +1,74 @@ +{{ define "headertpl"}} + + + + + + {{.Title}} + + + + + +{{end}} + + +{{ define "footertpl"}} + + + + +
+ + +{{end}} + +{{ define "dailytpl"}} + + + + +{{end}} + +{{ define "yearlytpl"}} + + + +{{end}} + +{{ define "monthlytpl"}} + + + +{{end}}