1
1
package main
2
2
3
3
import (
4
- "errors"
5
4
"flag"
6
5
"fmt"
7
6
"log"
8
7
"os"
9
8
"os/signal"
10
9
"strconv"
11
10
"strings"
11
+ "time"
12
12
13
+ "github.com/D4-project/analyzer-d4-log/logparser"
13
14
config "github.com/D4-project/d4-golang-utils/config"
14
15
"github.com/gomodule/redigo/redis"
15
16
)
16
17
17
18
type (
18
- conf struct {
19
+ redisconfD4 struct {
19
20
redisHost string
20
21
redisPort string
21
22
redisDB int
22
23
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
25
33
}
26
34
)
27
35
28
36
// Setting up flags
29
37
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" }
32
44
)
33
45
34
46
func main () {
@@ -61,14 +73,17 @@ func main() {
61
73
fmt .Printf ("\n " )
62
74
fmt .Printf ("The configuration directory should hold the following files\n " )
63
75
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 " )
65
77
fmt .Printf (" mandatory: redis_queue - uuid\n " )
78
+ fmt .Printf (" mandatory: redis_parsers - host:port/maxdb\n " )
66
79
fmt .Printf (" optional: http_server - host:port\n \n " )
67
80
fmt .Printf ("See conf.sample for an example.\n " )
68
81
}
69
82
70
83
// Config
71
- c := conf {}
84
+ // c := conf{}
85
+ rd4 := redisconfD4 {}
86
+ rp := redisconfParsers {}
72
87
flag .Parse ()
73
88
if flag .NFlag () == 0 || * confdir == "" {
74
89
flag .Usage ()
@@ -78,31 +93,76 @@ func main() {
78
93
* confdir = strings .TrimSuffix (* confdir , "\\ " )
79
94
}
80
95
81
- // Parse Redis Config
82
- tmp := config .ReadConfigFile (* confdir , "redis " )
96
+ // Parse Redis D4 Config
97
+ tmp := config .ReadConfigFile (* confdir , "redis_d4 " )
83
98
ss := strings .Split (string (tmp ), "/" )
84
99
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" )
86
101
}
87
- c .redisDB , _ = strconv .Atoi (ss [1 ])
102
+ rd4 .redisDB , _ = strconv .Atoi (ss [1 ])
88
103
var ret bool
89
104
ret , ss [0 ] = config .IsNet (ss [0 ])
90
105
if ! ret {
91
106
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 )
94
115
}
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" )
98
156
99
157
log .Println ("Exit" )
100
158
}
101
159
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 ) },
107
167
}
108
168
}
0 commit comments