forked from eaciit/live
/
ping.go
150 lines (127 loc) · 3.08 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
package live
import (
"fmt"
"github.com/eaciit/toolkit"
//"io/ioutil"
//"net/http"
"net"
"regexp"
"strings"
)
type PingTypeEnum int
const (
PingType_Network PingTypeEnum = iota
PingType_HttpStatus
PingType_HttpBody
PingType_Command
PingType_Custom
)
type HttpBodyEnum int
const (
HttpBody_Contains HttpBodyEnum = iota
HttpBody_Equals
)
type ResponseEnum int
const (
Response_Contains ResponseEnum = iota
Response_Equals
Response_RegEx
)
type Ping struct {
Type PingTypeEnum
User string
Password string
Host string
LastStatus string
//--- these attributes are used for command check
Command string
CommandParms []string
ResponseType ResponseEnum
ResponseValue string
HttpBodyType HttpBodyEnum
HttpBodySearch string
FnPing func(*Ping) error
}
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
}
func (p *Ping) checkNetwork() error {
_, e := net.Dial("tcp", p.Host)
if e != nil {
return fmt.Errorf("Unable to access %s, %s", p.Host, e.Error())
}
return nil
}
func (p *Ping) checkHttpStatus() error {
r, e := toolkit.HttpCall(p.Host, "GET", nil, false, "", "")
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)
}
return nil
}
func (p *Ping) checkHttpBody() error {
r, e := toolkit.HttpCall(p.Host, "GET", nil, false, "", "")
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")
}
return nil
}
func (p *Ping) checkCommand() error {
ps := []string{}
if p.CommandParms != nil {
ps = p.CommandParms
}
res, e := toolkit.RunCommand(p.Command, ps...)
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)
}
}
return nil
}
func (p *Ping) checkCustom() error {
if p.FnPing == nil {
return nil
}
return p.FnPing(p)
}