-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
190 lines (155 loc) · 4.94 KB
/
database.go
File metadata and controls
190 lines (155 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"strconv"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type dbParams struct {
Host string `json:"host"`
User string `json:"user"`
Password string `json:"password"`
Name string `json:"name"`
Port int `json:"port"`
LogToDB bool `json:"log-to-db"`
LogToStdOut bool `json:"log-to-stdout"`
}
type keywordsTable struct {
ID uint
Datetime time.Time
Host string
Keyword string
State bool
Comment string
}
type pingsTable struct {
ID uint
Datetime time.Time
Host string
State bool
Comment string
}
type portsTable struct {
ID uint
Datetime time.Time
Host string
Port int
Comment string
}
type webRequestsTable struct {
ID uint
Datetime time.Time
Host string
DesiredResp int
ActualResp int
Comment string
}
// Checks if it's possible to establish connection to the database with
// parameters defined in conf.json file.
func checkDbConn(params *dbParams) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
err = db.DB().Ping()
if err != nil {
return fmt.Errorf("can't open db conn: %v", err)
}
return nil
}
// Checks if required tables exist in the database.
func checkTables(params *dbParams) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
if !db.HasTable(&keywordsTable{}) {
return fmt.Errorf("required db tables do not exist")
} else if !db.HasTable(&pingsTable{}) {
return fmt.Errorf("required db tables do not exist")
} else if !db.HasTable(&portsTable{}) {
return fmt.Errorf("required db tables do not exist")
} else if !db.HasTable(&webRequestsTable{}) {
return fmt.Errorf("required db tables do not exist")
}
return nil
}
// Creates required tables in the database.
func createTables(params *dbParams) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
db.CreateTable(&keywordsTable{})
db.CreateTable(&pingsTable{})
db.CreateTable(&portsTable{})
db.CreateTable(&webRequestsTable{})
return nil
}
// Logs the data about the ping check to the database.
func logPing(params *dbParams, data *pingResp) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
nr := pingsTable{Datetime: data.datetime, Host: data.host,
State: data.state, Comment: data.comment}
db.Create(&nr)
return nil
}
// Logs the data about the keyword check to the database.
func logKeyword(params *dbParams, data *keywordResp) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
nr := keywordsTable{Datetime: data.datetime, Host: data.host,
Keyword: data.keyword, State: data.state, Comment: data.comment}
db.Create(&nr)
return nil
}
// Logs the data about the port check to the database.
func logPort(params *dbParams, data *portResp) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
nr := portsTable{Datetime: data.datetime, Host: data.host,
Port: data.port, Comment: data.comment}
db.Create(&nr)
return nil
}
// Logs the data about the request check to the database.
func logRequest(params *dbParams, data *fetchResp) error {
dsn := params.User + ":" + params.Password + "@tcp(" + params.Host + ":" +
strconv.Itoa(params.Port) + ")/" + params.Name + "?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("can't get db handle: %v", err)
}
defer db.Close()
nr := webRequestsTable{Datetime: data.datetime, Host: data.host,
DesiredResp: data.desiredResp, ActualResp: data.actualResp,
Comment: data.comment}
db.Create(&nr)
return nil
}