require "cjson" -- Generic decoder for Canary JSON data. This will extract all JSON -- keys and add them to the `Fields` variable of the created -- Heka message. -- -- Example use: -- -- [CanaryDecoder] -- type = "SandboxDecoder" -- script_type = "lua" -- filename = "/usr/share/heka/lua_decoders/canary.lua" -- module_directory = "/usr/share/lua" -- -- [HttpInput] -- url = "https://api.canary.io/checks/https-github.com/measurements?range=10" -- ticker_interval = 1 -- decoder = "CanaryDecoder" -- -- [fileoutput] -- type = "FileOutput" -- message_matcher = "Fields[location] == 'do-ny2'" -- path = "/var/log/heka/canary-output.log" -- prefix_ts = true -- perm = "666" function process_message() local raw_message = read_message("Payload") local json = cjson.decode(raw_message) if not json then -- When plain text is found, ship it in it's raw form. inject_message(raw_message) return 0 end for element = 1, #json do local cj2 = cjson.new() local fields = json[element] if fields.check then fields.check_id = fields.check.id fields.check_url = fields.check.url fields.check = nil end local message = { Type = "Canary", Payload = cj2.encode(json[element]), Fields = fields } inject_message(message) end return 0 end