Skip to content

Commit 9be5c60

Browse files
committed
add: [init] parser logic - not functional
1 parent 4c95d22 commit 9be5c60

File tree

3 files changed

+138
-22
lines changed

3 files changed

+138
-22
lines changed

logparser/parser.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
package logparser
2+
3+
type (
4+
// Parser provides the interface for a Parser
5+
// It should provide:
6+
// Parse to parse a line of log
7+
// GetAttributes to get list of attributes (map keys)
8+
Parser interface {
9+
Parse() error
10+
Push() error
11+
Pop() map[string]string
12+
}
13+
)

logparser/sshd.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package logparser
2+
3+
import "github.com/gomodule/redigo/redis"
4+
5+
// Sshd is a struct that corresponds to a line
6+
type Sshd struct {
7+
Date string
8+
Host string
9+
User string
10+
Src string
11+
}
12+
13+
// SshdParser Holds a struct that corresponds to a sshd log line
14+
// and the redis connection
15+
type SshdParser struct {
16+
logs Sshd
17+
r *redis.Conn
18+
}
19+
20+
// New Creates a new sshd parser
21+
func New(rconn *redis.Conn) *SshdParser {
22+
return &SshdParser{
23+
logs: Sshd{},
24+
r: rconn,
25+
}
26+
}
27+
28+
// Parse parses a line of sshd log
29+
func (s *SshdParser) Parse() error {
30+
//TODO
31+
return nil
32+
}
33+
34+
// Push pushed the parsed line into redis
35+
func (s *SshdParser) Push() error {
36+
//TODO
37+
return nil
38+
}
39+
40+
// Pop returns the list of attributes
41+
func (s *SshdParser) Pop() map[string]string {
42+
//TODO
43+
return nil
44+
}

main.go

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
package main
22

33
import (
4-
"errors"
54
"flag"
65
"fmt"
76
"log"
87
"os"
98
"os/signal"
109
"strconv"
1110
"strings"
11+
"time"
1212

13+
"github.com/D4-project/analyzer-d4-log/logparser"
1314
config "github.com/D4-project/d4-golang-utils/config"
1415
"github.com/gomodule/redigo/redis"
1516
)
1617

1718
type (
18-
conf struct {
19+
redisconfD4 struct {
1920
redisHost string
2021
redisPort string
2122
redisDB int
2223
redisQueue string
23-
httpHost string
24-
httpPort string
24+
}
25+
redisconfParsers struct {
26+
redisHost string
27+
redisPort string
28+
redisDBCount int
29+
}
30+
conf struct {
31+
httpHost string
32+
httpPort string
2533
}
2634
)
2735

2836
// Setting up flags
2937
var (
30-
confdir = flag.String("c", "conf.sample", "configuration directory")
31-
cr redis.Conn
38+
confdir = flag.String("c", "conf.sample", "configuration directory")
39+
all = flag.Bool("a", true, "run all parsers when set. Set by default")
40+
specific = flag.String("o", "", "run only a specific parser [sshd]")
41+
redisD4 redis.Conn
42+
redisParsers *redis.Pool
43+
parsers = [1]string{"sshd"}
3244
)
3345

3446
func main() {
@@ -61,14 +73,17 @@ func main() {
6173
fmt.Printf("\n")
6274
fmt.Printf("The configuration directory should hold the following files\n")
6375
fmt.Printf("to specify the settings to use:\n\n")
64-
fmt.Printf(" mandatory: redis - host:port/db\n")
76+
fmt.Printf(" mandatory: redis_d4 - host:port/db\n")
6577
fmt.Printf(" mandatory: redis_queue - uuid\n")
78+
fmt.Printf(" mandatory: redis_parsers - host:port/maxdb\n")
6679
fmt.Printf(" optional: http_server - host:port\n\n")
6780
fmt.Printf("See conf.sample for an example.\n")
6881
}
6982

7083
// Config
71-
c := conf{}
84+
// c := conf{}
85+
rd4 := redisconfD4{}
86+
rp := redisconfParsers{}
7287
flag.Parse()
7388
if flag.NFlag() == 0 || *confdir == "" {
7489
flag.Usage()
@@ -78,31 +93,76 @@ func main() {
7893
*confdir = strings.TrimSuffix(*confdir, "\\")
7994
}
8095

81-
// Parse Redis Config
82-
tmp := config.ReadConfigFile(*confdir, "redis")
96+
// Parse Redis D4 Config
97+
tmp := config.ReadConfigFile(*confdir, "redis_d4")
8398
ss := strings.Split(string(tmp), "/")
8499
if len(ss) <= 1 {
85-
log.Fatal("Missing Database in Redis config: should be host:port/database_name")
100+
log.Fatal("Missing Database in Redis D4 config: should be host:port/database_name")
86101
}
87-
c.redisDB, _ = strconv.Atoi(ss[1])
102+
rd4.redisDB, _ = strconv.Atoi(ss[1])
88103
var ret bool
89104
ret, ss[0] = config.IsNet(ss[0])
90105
if !ret {
91106
sss := strings.Split(string(ss[0]), ":")
92-
c.redisHost = sss[0]
93-
c.redisPort = sss[1]
107+
rd4.redisHost = sss[0]
108+
rd4.redisPort = sss[1]
109+
}
110+
rd4.redisQueue = string(config.ReadConfigFile(*confdir, "redis_queue"))
111+
// Connect to D4 Redis
112+
redisD4, err = redis.Dial("tcp", rd4.redisHost+":"+rd4.redisPort, redis.DialDatabase(rd4.redisDB))
113+
if err != nil {
114+
log.Fatal(err)
94115
}
95-
c.redisQueue = string(config.ReadConfigFile(*confdir, "redis_queue"))
96-
initRedis(c.redisHost, c.redisPort, c.redisDB)
97-
defer cr.Close()
116+
defer redisD4.Close()
117+
118+
// Parse Redis Parsers Config
119+
tmp = config.ReadConfigFile(*confdir, "redis_parsers")
120+
ss = strings.Split(string(tmp), "/")
121+
if len(ss) <= 1 {
122+
log.Fatal("Missing Database Count in Redis config: should be host:port/max number of DB")
123+
}
124+
rp.redisDBCount, _ = strconv.Atoi(ss[1])
125+
ret, ss[0] = config.IsNet(string(tmp))
126+
if !ret {
127+
sss := strings.Split(string(ss[0]), ":")
128+
rp.redisHost = sss[0]
129+
rp.redisPort = sss[1]
130+
}
131+
132+
// Create a connection Pool
133+
redisParsers = newPool(rp.redisHost+":"+rp.redisPort, rp.redisDBCount)
134+
135+
// Init parser depending on the parser flags:
136+
if *all {
137+
// Init all parsers
138+
var torun = []logparser.Parser{}
139+
for _, v := range parsers {
140+
switch v {
141+
case "sshd":
142+
var sshdrcon, err = redisParsers.Dial()
143+
if err != nil {
144+
log.Fatal("Could not connect to Parser Redis")
145+
}
146+
sshd := logparser.New(&sshdrcon)
147+
torun = append(torun, sshd)
148+
}
149+
}
150+
} else if *specific != "" {
151+
log.Println("TODO should run specific parser here")
152+
}
153+
154+
// Run the parsers
155+
log.Println("TODO should run the parsers here")
98156

99157
log.Println("Exit")
100158
}
101159

102-
func initRedis(host string, port string, d int) {
103-
err := errors.New("")
104-
cr, err = redis.Dial("tcp", host+":"+port, redis.DialDatabase(d))
105-
if err != nil {
106-
log.Fatal(err)
160+
func newPool(addr string, maxconn int) *redis.Pool {
161+
return &redis.Pool{
162+
MaxActive: maxconn,
163+
MaxIdle: 3,
164+
IdleTimeout: 240 * time.Second,
165+
// Dial or DialContext must be set. When both are set, DialContext takes precedence over Dial.
166+
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr) },
107167
}
108168
}

0 commit comments

Comments
 (0)