-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpowerPing.groovy
172 lines (154 loc) · 5.8 KB
/
powerPing.groovy
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
package powerPing
@GrabResolver(name='artifactory', root='http://artifact.praqma.net:/artifactory/repo/', m2Compatible=true)
@Grab(group = 'org.yaml', module = 'snakeyaml', version = '1.5')
import org.yaml.snakeyaml.Yaml
@Grab('com.github.kevinsawicki:http-request:3.1')
import com.github.kevinsawicki.http.*
class powerPing extends Script {
final Yaml YAML = new Yaml()
@Override
Object run() {
// Check for correct script parameters
if (!args) {
println "ERROR: Missing script parameters."
println "powerPing [config file] [environments...]"
System.exit(1)
}
// Load configuration and default ports
def configFile = new File(args[0])
if (!configFile.exists()) {
println "ERROR: Could not find configuration file ${args[0]}"
System.exit(1)
}
def configuration = YAML.load(configFile.text)
def defaultsMap = loadDefaults(configuration)
// Loop through passed in environments and curl their addresses:ports
def failure = false
def environmentNames = args[1..-1]
for(def environmentName : environmentNames){
if(environmentName.equalsIgnoreCase("configuration")){
println "ERROR: $environmentName is a reserved keyword. It cannot be tested as an environment!"
failure = true
continue
}
def environment = configuration[environmentName]
if (!environment) {
println "ERROR: Environment $environmentName not found in configuration file!"
failure = true
continue
}
println "***** TESTING: $environmentName"
for(def address : environment) {
def addressName = address.key
def ports = address.value.ports
def paths = address.value.paths
println ""
println "$addressName $ports"
println "-----"
if (!doDnsLookup(addressName)) {
println "ERROR: $configFile in DNS lookup: could not lookup $addressName"
failure = true
println "----------"
continue
}
def expandedPorts = []
def expSuccess = fillWithExpandedPorts(expandedPorts, ports, defaultsMap)
if (!expSuccess){
failure = true
} else {
for(def port : expandedPorts) {
if (!doPortCheck(addressName, port)) {
println "ERROR: $configFile in port check: could not connect to $addressName on port $port"
failure = true
}
}
}
for(def path : paths) {
if (!doCurl(path)) {
println "ERROR: $configFile in curl check: could not access URL $path"
failure = true
}
}
println "----------"
}
}
if (failure)
System.exit(1)
}
/**
* Fills a list with all the given ports, replacing strings with ports from the defaults map
* @param expandedPorts the List to fill
* @param portValues the port values to expand
* @param defaultsMap the default ports map
* @return true if successful, false if expansion failed
*/
boolean fillWithExpandedPorts(def expandedPorts, def portValues, def defaultsMap) {
for(def portValue : portValues){
if (portValue.toString().isNumber()) {
expandedPorts.add(portValue)
} else {
def defaultPorts = defaultsMap[portValue]
if (defaultPorts)
expandedPorts.addAll(defaultPorts)
else {
println("ERROR: Failed to expand port value $portValue.")
return false
}
}
}
return true
}
/**
* Fills a map with all the ports defined in the config's 'defaults' section.
* @param yamlMap the loaded YAML map
* @return a Map<String, Int[]> with all the default ports
*/
private Map loadDefaults(yamlMap) {
def defaultMap = [:]
if (yamlMap['configuration']) {
yamlMap['configuration'].each {
defaultMap.put(it.key, it.value)
}
}
return defaultMap
}
boolean doCurl(String url) {
print "curl $url: "
try {
def req = HttpRequest.get(url)
req.trustAllCerts()
req.trustAllHosts()
def response = req.code()
println "ok (response: $response)"
return true
} catch (Exception ex) { // FIXME - could we catch some specific exception, to catch timeout? to catch unkonw host? unknow url and print out that error together with stack trace? then for the rest, just catch them by your generic ERROR
print "ERROR "
println "(" + ex + ")"
return false
}
}
private boolean doDnsLookup(String address) {
print "dns: "
try {
def inetAddress = InetAddress.getByName(address)
println "ok (found: $inetAddress.hostAddress)"
return true
} catch (Exception ex) {
print "ERROR "
println "(" + ex + ")"
return false
}
}
private boolean doPortCheck(String address, int port) {
print "port $port: "
try {
new Socket().connect(new InetSocketAddress(address, port), 2000);
println "ok"
return true
} catch (Exception ex) {
print "ERROR "
println "(" + ex + ")"
return false
}
}
}