add http.cm and probe
This commit is contained in:
124
probe.cm
Normal file
124
probe.cm
Normal file
@@ -0,0 +1,124 @@
|
||||
var http = use('http')
|
||||
var json = use('json')
|
||||
|
||||
var registry = {}
|
||||
var server_fd = null
|
||||
var port = 9000
|
||||
|
||||
function handle_request(req) {
|
||||
var result = null
|
||||
var _try = null
|
||||
|
||||
if (req.method == "GET" && req.path == "/discover") {
|
||||
result = discover()
|
||||
http.respond(req._conn, 200, {"content-type": "application/json"},
|
||||
json.encode(result))
|
||||
return
|
||||
}
|
||||
|
||||
if (req.method == "POST" && req.path == "/probe") {
|
||||
_try = function() {
|
||||
result = handle_probe(req)
|
||||
} disruption {
|
||||
result = {ok: false, error: "probe failed"}
|
||||
}
|
||||
_try()
|
||||
http.respond(req._conn, 200, {"content-type": "application/json"},
|
||||
json.encode(result))
|
||||
return
|
||||
}
|
||||
|
||||
if (req.method == "POST" && req.path == "/snapshot") {
|
||||
_try = function() {
|
||||
result = handle_snapshot(req)
|
||||
} disruption {
|
||||
result = {ok: false, error: "snapshot failed"}
|
||||
}
|
||||
_try()
|
||||
http.respond(req._conn, 200, {"content-type": "application/json"},
|
||||
json.encode(result))
|
||||
return
|
||||
}
|
||||
|
||||
http.respond(req._conn, 404, {"content-type": "application/json"},
|
||||
json.encode({ok: false, error: "not found"}))
|
||||
}
|
||||
|
||||
function discover() {
|
||||
var targets = {}
|
||||
var target_keys = array(registry)
|
||||
var i = 0
|
||||
while (i < length(target_keys)) {
|
||||
targets[target_keys[i]] = array(registry[target_keys[i]])
|
||||
i = i + 1
|
||||
}
|
||||
return {ok: true, targets: targets}
|
||||
}
|
||||
|
||||
function handle_probe(req) {
|
||||
var body = json.decode(req.body)
|
||||
var target = body.target
|
||||
var name = body.name
|
||||
var args = body.args
|
||||
|
||||
if (target == null || name == null) {
|
||||
return {ok: false, error: "missing target or name"}
|
||||
}
|
||||
|
||||
var target_probes = registry[target]
|
||||
if (target_probes == null) {
|
||||
return {ok: false, error: "unknown target: " + target}
|
||||
}
|
||||
|
||||
var probe_fn = target_probes[name]
|
||||
if (probe_fn == null) {
|
||||
return {ok: false, error: "unknown probe: " + target + "/" + name}
|
||||
}
|
||||
|
||||
if (args == null) args = {}
|
||||
var result = probe_fn(args)
|
||||
return {ok: true, result: result}
|
||||
}
|
||||
|
||||
function handle_snapshot(req) {
|
||||
var body = json.decode(req.body)
|
||||
var probes = body.probes
|
||||
if (probes == null) {
|
||||
return {ok: false, error: "missing probes array"}
|
||||
}
|
||||
|
||||
var results = {}
|
||||
var i = 0
|
||||
var p = null
|
||||
var target_probes = null
|
||||
var probe_fn = null
|
||||
var key = null
|
||||
while (i < length(probes)) {
|
||||
p = probes[i]
|
||||
key = p.target + "/" + p.name
|
||||
target_probes = registry[p.target]
|
||||
if (target_probes != null) {
|
||||
probe_fn = target_probes[p.name]
|
||||
if (probe_fn != null) {
|
||||
results[key] = probe_fn(p.args != null ? p.args : {})
|
||||
}
|
||||
}
|
||||
i = i + 1
|
||||
}
|
||||
return {ok: true, results: results}
|
||||
}
|
||||
|
||||
function start_server() {
|
||||
server_fd = http.serve(port)
|
||||
http.on_request(server_fd, handle_request)
|
||||
}
|
||||
|
||||
function register(target, probes) {
|
||||
registry[target] = probes
|
||||
if (server_fd == null) start_server()
|
||||
}
|
||||
|
||||
return {
|
||||
register: register,
|
||||
port: port
|
||||
}
|
||||
Reference in New Issue
Block a user