diff options
author | rtz12 <koenig@fagott.pw> | 2016-10-11 19:04:33 (UTC) |
---|---|---|
committer | rtz12 <koenig@fagott.pw> | 2016-10-11 19:04:33 (UTC) |
commit | 245ab6f11d7026e8e8218752839659bc04fdc102 (patch) | |
tree | 1dcabad66a76893f8f4c564269ff6e784f168389 |
NSA implementiert
-rw-r--r-- | nsa.go | 129 |
1 files changed, 129 insertions, 0 deletions
@@ -0,0 +1,129 @@ | |||
1 | package nsa | ||
2 | |||
3 | import ( | ||
4 | "database/sql" | ||
5 | "encoding/json" | ||
6 | "fmt" | ||
7 | "os" | ||
8 | "strings" | ||
9 | ) | ||
10 | |||
11 | var database *sql.DB | ||
12 | var eventmap = make(map[string]int) | ||
13 | |||
14 | type Logger struct { | ||
15 | ip string | ||
16 | } | ||
17 | |||
18 | func NewLogger(ip string) *Logger { | ||
19 | return &Logger{ | ||
20 | ip: ip, | ||
21 | } | ||
22 | } | ||
23 | |||
24 | func (l *Logger) getEventID(event string) int { | ||
25 | evID, ok := eventmap[event] | ||
26 | if !ok { | ||
27 | logErr("Event %s does not exist.", event) | ||
28 | return 0 | ||
29 | } | ||
30 | return evID | ||
31 | } | ||
32 | |||
33 | func (l *Logger) getJSON(data interface{}) string { | ||
34 | jsonStr, err := json.Marshal(data) | ||
35 | if err != nil { | ||
36 | logErr("%v", err) | ||
37 | return "" | ||
38 | } | ||
39 | return string(jsonStr) | ||
40 | } | ||
41 | |||
42 | func (l *Logger) insertEvent(id int, data string) { | ||
43 | _, err := database.Exec( | ||
44 | "INSERT INTO nsa.events (type, data, client_ip) VALUES ($1, $2, $3);", | ||
45 | id, data, l.ip) | ||
46 | if err != nil { | ||
47 | logErr("%v", err) | ||
48 | } | ||
49 | } | ||
50 | |||
51 | func (l *Logger) Log(event string, data interface{}) { | ||
52 | go func() { | ||
53 | id := l.getEventID(event) | ||
54 | if id == 0 { | ||
55 | return | ||
56 | } | ||
57 | d := l.getJSON(data) | ||
58 | if d == "" { | ||
59 | return | ||
60 | } | ||
61 | l.insertEvent(id, d) | ||
62 | }() | ||
63 | } | ||
64 | |||
65 | func logErr(msg string, params ...interface{}) { | ||
66 | fmt.Fprintf( | ||
67 | os.Stderr, | ||
68 | "NSA: Error: %v\n", | ||
69 | fmt.Sprintf(msg, params...)) | ||
70 | } | ||
71 | |||
72 | func Init(db *sql.DB) error { | ||
73 | err := db.Ping() | ||
74 | if err != nil { | ||
75 | return err | ||
76 | } | ||
77 | database = db | ||
78 | return nil | ||
79 | } | ||
80 | |||
81 | func SetEvents(events []string) error { | ||
82 | rows, err := database.Query("SELECT id, name FROM nsa.event_types;") | ||
83 | if err != nil { | ||
84 | return err | ||
85 | } | ||
86 | defer rows.Close() | ||
87 | for rows.Next() { | ||
88 | var id int | ||
89 | var name string | ||
90 | rows.Scan(&id, &name) | ||
91 | eventmap[name] = id | ||
92 | } | ||
93 | newEvents := []string{} | ||
94 | params := "" | ||
95 | paramNum := 1 | ||
96 | for _, event := range events { | ||
97 | e := strings.ToUpper(event) | ||
98 | if _, ok := eventmap[event]; ok { | ||
99 | continue | ||
100 | } | ||
101 | newEvents = append(newEvents, e) | ||
102 | params += fmt.Sprintf("($%d),", paramNum) | ||
103 | paramNum++ | ||
104 | } | ||
105 | if len(newEvents) > 0 { | ||
106 | params = params[0 : len(params)-1] | ||
107 | values := []interface{}{} | ||
108 | for _, event := range newEvents { | ||
109 | values = append(values, event) | ||
110 | } | ||
111 | rows, err = database.Query( | ||
112 | fmt.Sprintf( | ||
113 | "INSERT INTO nsa.event_types (name) VALUES %s RETURNING id;", | ||
114 | params), | ||
115 | values...) | ||
116 | if err != nil { | ||
117 | return err | ||
118 | } | ||
119 | defer rows.Close() | ||
120 | i := 0 | ||
121 | for rows.Next() { | ||
122 | var id int | ||
123 | rows.Scan(&id) | ||
124 | eventmap[newEvents[i]] = id | ||
125 | i++ | ||
126 | } | ||
127 | } | ||
128 | return nil | ||
129 | } | ||