@@ -3,6 +3,7 @@ package logparser
3
3
import (
4
4
"fmt"
5
5
"html/template"
6
+ "io/ioutil"
6
7
"log"
7
8
"math"
8
9
"os"
@@ -303,73 +304,6 @@ func (s *SshdParser) Compile() error {
303
304
}
304
305
}
305
306
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
-
373
307
// Get oldest / newest entries
374
308
var newest string
375
309
var oldest string
@@ -444,36 +378,116 @@ func (s *SshdParser) Compile() error {
444
378
}
445
379
}
446
380
447
- t , err := template .New ("webpage" ).Parse (tpl )
381
+ // Parse Template
382
+ t , err := template .ParseFiles (filepath .Join ("logparser" , "sshd" , "statistics.gohtml" ))
448
383
if err != nil {
449
384
r .Close ()
450
385
return err
451
386
}
452
387
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
460
419
}{
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 )
470
470
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 )
472
475
if err != nil {
473
476
r .Close ()
474
477
return err
475
478
}
476
479
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
+
477
491
return nil
478
492
}
479
493
0 commit comments