Skip to content

Commit 75a917b

Browse files
committed
chg: [sshd] sshd parses logline and pushed to redis
1 parent 62d6ed2 commit 75a917b

File tree

4 files changed

+39
-53
lines changed

4 files changed

+39
-53
lines changed

conf.sample/redis

Lines changed: 0 additions & 1 deletion
This file was deleted.

logparser/parser.go

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

3+
import "github.com/gomodule/redigo/redis"
4+
35
type (
46
// Parser provides the interface for a Parser
57
// It should provide:
8+
// Set to assign a redis connection to it
69
// Parse to parse a line of log
7-
// GetAttributes to get list of attributes (map keys)
810
Parser interface {
11+
Set(*redis.Conn)
912
Parse(string) error
10-
Push() error
11-
Pop() map[string]string
1213
}
1314
)

logparser/sshd.go

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,22 @@ package logparser
22

33
import (
44
"fmt"
5-
"log"
65
"regexp"
76
"strconv"
87
"time"
98

109
"github.com/gomodule/redigo/redis"
1110
)
1211

13-
// Sshd is a struct that corresponds to a line
14-
type Sshd struct {
15-
Date string
16-
Host string
17-
User string
18-
Src string
19-
}
20-
2112
// SshdParser Holds a struct that corresponds to a sshd log line
2213
// and the redis connection
2314
type SshdParser struct {
24-
logs Sshd
25-
r *redis.Conn
15+
r *redis.Conn
2616
}
2717

28-
// New Creates a new sshd parser
29-
func New(rconn *redis.Conn) *SshdParser {
30-
return &SshdParser{
31-
logs: Sshd{},
32-
r: rconn,
33-
}
18+
// Set set the redic connection to this parser
19+
func (s *SshdParser) Set(rconn *redis.Conn) {
20+
s.r = rconn
3421
}
3522

3623
// Parse parses a line of sshd log
@@ -54,43 +41,37 @@ func (s *SshdParser) Parse(logline string) error {
5441
parsedTime, _ := time.ParseInLocation("Jan 02 15:04:05 2006", md["date"], loc)
5542
md["date"] = string(strconv.FormatInt(parsedTime.Unix(), 10))
5643

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)
44+
// Pushing loglines in database 0
45+
if _, err := r.Do("SELECT", 0); err != nil {
46+
r.Close()
47+
return err
48+
}
49+
_, err := redis.Bool(r.Do("HSET", fmt.Sprintf("%v:%v", md["date"], md["host"]), "username", md["username"], "src", md["src"]))
6150
if err != nil {
62-
log.Fatal("Could connect to the Redis database")
51+
r.Close()
52+
return err
53+
}
54+
55+
// Pushing statistics in database 1
56+
if _, err := r.Do("SELECT", 1); err != nil {
57+
r.Close()
58+
return err
6359
}
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)
60+
_, err = redis.String(r.Do("ZINCRBY", fmt.Sprintf("%v%v%v:statssrc", parsedTime.Year(), int(parsedTime.Month()), parsedTime.Day()), 1, md["src"]))
6861
if err != nil {
69-
log.Fatal("Could connect to the Redis database")
62+
r.Close()
63+
return err
7064
}
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)
65+
_, err = redis.String(r.Do("ZINCRBY", fmt.Sprintf("%v%v%v:statsusername", parsedTime.Year(), int(parsedTime.Month()), parsedTime.Day()), 1, md["username"]))
7466
if err != nil {
75-
log.Fatal("Could connect to the Redis database")
67+
r.Close()
68+
return err
7669
}
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)
70+
_, err = redis.String(r.Do("ZINCRBY", fmt.Sprintf("%v%v%v:statshost", parsedTime.Year(), int(parsedTime.Month()), parsedTime.Day()), 1, md["host"]))
7971
if err != nil {
80-
log.Fatal("Could connect to the Redis database")
72+
r.Close()
73+
return err
8174
}
8275

8376
return nil
8477
}
85-
86-
// Push pushed the parsed line into redis
87-
func (s *SshdParser) Push() error {
88-
//TODO
89-
return nil
90-
}
91-
92-
// Pop returns the list of attributes
93-
func (s *SshdParser) Pop() map[string]string {
94-
//TODO
95-
return nil
96-
}

main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func main() {
111111
}
112112
rd4.redisQueue = string(config.ReadConfigFile(*confdir, "redis_queue"))
113113
// Connect to D4 Redis
114+
// TODO use DialOptions to Dial with a timeout
114115
redisD4, err = redis.Dial("tcp", rd4.redisHost+":"+rd4.redisPort, redis.DialDatabase(rd4.redisDB))
115116
if err != nil {
116117
log.Fatal(err)
@@ -149,8 +150,9 @@ func main() {
149150
if err != nil {
150151
log.Fatal("Could connect to the Redis database")
151152
}
152-
sshd := logparser.New(&sshdrcon)
153-
torun = append(torun, sshd)
153+
sshd := logparser.SshdParser{}
154+
sshd.Set(&sshdrcon)
155+
torun = append(torun, &sshd)
154156
}
155157
}
156158
} else if *specific != "" {
@@ -178,7 +180,10 @@ func main() {
178180

179181
// Run the parsers
180182
for _, v := range torun {
181-
v.Parse(logline)
183+
err := v.Parse(logline)
184+
if err != nil {
185+
log.Fatal(err)
186+
}
182187
}
183188

184189
}

0 commit comments

Comments
 (0)