Skip to content

Commit 62d6ed2

Browse files
committed
chg: [sshd] wip - not functional
1 parent 3818bda commit 62d6ed2

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

logparser/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type (
66
// Parse to parse a line of log
77
// GetAttributes to get list of attributes (map keys)
88
Parser interface {
9-
Parse() error
9+
Parse(string) error
1010
Push() error
1111
Pop() map[string]string
1212
}

logparser/sshd.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package logparser
22

3-
import "github.com/gomodule/redigo/redis"
3+
import (
4+
"fmt"
5+
"log"
6+
"regexp"
7+
"strconv"
8+
"time"
9+
10+
"github.com/gomodule/redigo/redis"
11+
)
412

513
// Sshd is a struct that corresponds to a line
614
type Sshd struct {
@@ -26,8 +34,52 @@ func New(rconn *redis.Conn) *SshdParser {
2634
}
2735

2836
// Parse parses a line of sshd log
29-
func (s *SshdParser) Parse() error {
30-
//TODO
37+
func (s *SshdParser) Parse(logline string) error {
38+
r := *s.r
39+
re := regexp.MustCompile(`^(?P<date>[[:alpha:]]{3}\s\d{2}\s\d{2}:\d{2}:\d{2}) (?P<host>[^ ]+) sshd\[[[:alnum:]]+\]: Invalid user (?P<username>[^ ]+) from (?P<src>.*$)`)
40+
n1 := re.SubexpNames()
41+
r2 := re.FindAllStringSubmatch(logline, -1)[0]
42+
43+
// Build the group map for the line
44+
md := map[string]string{}
45+
for i, n := range r2 {
46+
// fmt.Printf("%d. match='%s'\tname='%s'\n", i, n, n1[i])
47+
md[n1[i]] = n
48+
}
49+
50+
// Assumes the system parses logs recorded during the current year
51+
md["date"] = fmt.Sprintf("%v %v", md["date"], time.Now().Year())
52+
// Make this automatic or a config parameter
53+
loc, _ := time.LoadLocation("Europe/Luxembourg")
54+
parsedTime, _ := time.ParseInLocation("Jan 02 15:04:05 2006", md["date"], loc)
55+
md["date"] = string(strconv.FormatInt(parsedTime.Unix(), 10))
56+
57+
// Pushing logline in redis
58+
redislog := fmt.Sprintf("HMSET %v:%v username \"%v\" src \"%v\"", md["date"], md["host"], md["username"], md["src"])
59+
a, err := r.Do(redislog)
60+
fmt.Println(a)
61+
if err != nil {
62+
log.Fatal("Could connect to the Redis database")
63+
}
64+
today := time.Now()
65+
// Statistics
66+
dailysrc := fmt.Sprintf("ZINCBY %v%v%v:statssrc 1 %v", today.Year(), int(today.Month()), today.Day(), md["src"])
67+
_, err = r.Do(dailysrc)
68+
if err != nil {
69+
log.Fatal("Could connect to the Redis database")
70+
}
71+
dailyusername := fmt.Sprintf("ZINCBY %v%v%v:statsusername 1 %v", today.Year(), int(today.Month()), today.Day(), md["username"])
72+
fmt.Println(dailyusername)
73+
_, err = r.Do(dailyusername)
74+
if err != nil {
75+
log.Fatal("Could connect to the Redis database")
76+
}
77+
dailyhost := fmt.Sprintf("ZINCBY %v%v%v:statshost 1 %v", today.Year(), int(today.Month()), today.Day(), md["host"])
78+
_, err = r.Do(dailyhost)
79+
if err != nil {
80+
log.Fatal("Could connect to the Redis database")
81+
}
82+
3183
return nil
3284
}
3385

main.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"time"
1212

13+
"bufio"
14+
1315
"github.com/D4-project/analyzer-d4-log/logparser"
1416
config "github.com/D4-project/d4-golang-utils/config"
1517
"github.com/gomodule/redigo/redis"
@@ -122,7 +124,7 @@ func main() {
122124
log.Fatal("Missing Database Count in Redis config: should be host:port/max number of DB")
123125
}
124126
rp.redisDBCount, _ = strconv.Atoi(ss[1])
125-
ret, ss[0] = config.IsNet(string(tmp))
127+
ret, ss[0] = config.IsNet(ss[0])
126128
if !ret {
127129
sss := strings.Split(string(ss[0]), ":")
128130
rp.redisHost = sss[0]
@@ -132,17 +134,21 @@ func main() {
132134
// Create a connection Pool
133135
redisParsers = newPool(rp.redisHost+":"+rp.redisPort, rp.redisDBCount)
134136

137+
var torun = []logparser.Parser{}
135138
// Init parser depending on the parser flags:
136139
if *all {
137140
// Init all parsers
138-
var torun = []logparser.Parser{}
139141
for _, v := range parsers {
140142
switch v {
141143
case "sshd":
142144
var sshdrcon, err = redisParsers.Dial()
143145
if err != nil {
144146
log.Fatal("Could not connect to Parser Redis")
145147
}
148+
_, err = sshdrcon.Do("PING")
149+
if err != nil {
150+
log.Fatal("Could connect to the Redis database")
151+
}
146152
sshd := logparser.New(&sshdrcon)
147153
torun = append(torun, sshd)
148154
}
@@ -151,8 +157,31 @@ func main() {
151157
log.Println("TODO should run specific parser here")
152158
}
153159

154-
// Run the parsers
155-
log.Println("TODO should run the parsers here")
160+
f, err = os.Open("./test_seed.log")
161+
if err != nil {
162+
log.Fatalf("Error opening test file: %v", err)
163+
}
164+
defer f.Close()
165+
scanner := bufio.NewScanner(f)
166+
for scanner.Scan() {
167+
168+
// Pop D4 redis queue
169+
//for {
170+
171+
// err := errors.New("")
172+
// logline, err := redis.String(redisD4.Do("LPOP", "analyzer:3:"+rd4.redisQueue))
173+
logline := scanner.Text()
174+
// if err != nil {
175+
// log.Fatal(err)
176+
// }
177+
// fmt.Println(logline)
178+
179+
// Run the parsers
180+
for _, v := range torun {
181+
v.Parse(logline)
182+
}
183+
184+
}
156185

157186
log.Println("Exit")
158187
}

0 commit comments

Comments
 (0)