/
ping.go
179 lines (157 loc) · 4.02 KB
/
ping.go
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
package live
import (
"fmt"
"github.com/eaciit/toolkit"
"net"
"regexp"
"strings"
)
type PingTypeEnum int
const (
// these variable used for determine ping to execute
PingType_Network PingTypeEnum = iota
PingType_HttpStatus
PingType_HttpBody
PingType_Command
PingType_Custom
)
type HttpBodyEnum int
const (
// these variable used for validation after ping http body
HttpBody_Contains HttpBodyEnum = iota
HttpBody_Equals
)
type ResponseEnum int
const (
// these variable used for validation response after ping http rest
Response_Contains ResponseEnum = iota
Response_Equals
Response_RegEx
)
type Ping struct {
// these attributes are used for initialization ping such us server location, and ping type
Type PingTypeEnum
User string
Password string
Host string
LastStatus string
// these attributes are used for command check
Command string
CommandParms []string
// these attributes are used for validate response after ping
ResponseType ResponseEnum
ResponseValue string
// these attributes are used for validate httpbody after ping
HttpBodyType HttpBodyEnum
HttpBodySearch string
FnPing func(*Ping) error
}
/*
Select ping type before execute
*/
func (p *Ping) Check() error {
var e error
pingType := p.Type
if pingType == PingType_Network {
e = p.checkNetwork()
} else if pingType == PingType_HttpStatus {
e = p.checkHttpStatus()
} else if pingType == PingType_HttpBody {
e = p.checkHttpBody()
} else if pingType == PingType_Command {
e = p.checkCommand()
} else if pingType == PingType_Custom {
e = p.checkCustom()
}
return e
}
/*
Ping service over network tcp status
*/
func (p *Ping) checkNetwork() error {
_, e := net.Dial("tcp", p.Host)
p.LastStatus = "Fail"
if e != nil {
return fmt.Errorf("Unable to access %s, %s", p.Host, e.Error())
}
p.LastStatus = "OK"
return nil
}
/*
Ping service over http status
*/
func (p *Ping) checkHttpStatus() error {
r, e := toolkit.HttpCall(p.Host, "GET", nil, nil)
p.LastStatus = "Fail"
if e != nil {
return fmt.Errorf("Unable to access %s, %s", p.Host, e.Error())
}
if r.StatusCode != 200 {
return fmt.Errorf("Unable to access %s, code: %d status: %s", p.Host, r.StatusCode, r.Status)
}
p.LastStatus = "OK"
return nil
}
/*
Ping service over http body and validate response
*/
func (p *Ping) checkHttpBody() error {
p.LastStatus = "Fail"
r, e := toolkit.HttpCall(p.Host, "GET", nil, nil)
if e != nil {
return e
}
body := toolkit.HttpContentString(r)
if p.HttpBodyType == HttpBody_Contains {
if !strings.Contains(body, p.HttpBodySearch) {
return fmt.Errorf("Phrase %s could not be found on response body", p.HttpBodySearch)
}
} else if p.HttpBodyType == HttpBody_Equals {
if body != p.HttpBodySearch {
return fmt.Errorf("Response is not valid. Expecting for %s", p.HttpBodySearch)
}
} else {
return fmt.Errorf("Invalid parameter")
}
p.LastStatus = "OK"
return nil
}
/*
Ping service over local command execute
*/
func (p *Ping) checkCommand() error {
ps := []string{}
if p.CommandParms != nil {
ps = p.CommandParms
}
res, e := toolkit.RunCommand(p.Command, ps...)
p.LastStatus = "Fail"
if e != nil {
return e
}
if p.ResponseType == Response_Equals {
if res != p.ResponseValue {
return fmt.Errorf("Response is not valid. Expecting for %s", p.ResponseValue)
}
} else if p.ResponseType == Response_Contains {
if !strings.Contains(res, p.ResponseValue) {
return fmt.Errorf("Phrase %s could not be found on response", p.ResponseValue)
}
} else if p.ResponseType == Response_RegEx {
match, _ := regexp.MatchString(p.ResponseValue, res)
if !match {
return fmt.Errorf("Response is not valid. Not match with pattern %s", p.ResponseValue)
}
}
p.LastStatus = "OK"
return nil
}
/*
NEXT Feature, ping with custom method
*/
func (p *Ping) checkCustom() error {
if p.FnPing == nil {
return nil
}
return p.FnPing(p)
}