summaryrefslogtreecommitdiff
path: root/lib/oxidized/api/rest.rb
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2013-04-20 00:41:23 +0300
committerSaku Ytti <saku@ytti.fi>2013-04-20 00:41:23 +0300
commitf1287a7925901bf3518eb69079304bfb97f7434d (patch)
tree3dd2779e4d087b73b99d7136f37db88c76091d8d /lib/oxidized/api/rest.rb
parent5a393d6102655f575549311e6b250534b4dcbb59 (diff)
Example of Syslog triggered fetch
'syslog.rb' listed to UDP port (or reads file). When IOS or JunOS style config change/commit message is seen, it triggers immediate update ot config It transports commit message (junos) remote host from which change was mde (ios) and who made the change (junos+ios). This is carried over to the 'output' methods, that is, 'git blame' will show IOS/JunOS user-name who made the change.
Diffstat (limited to 'lib/oxidized/api/rest.rb')
-rw-r--r--lib/oxidized/api/rest.rb45
1 files changed, 38 insertions, 7 deletions
diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb
index 4a5fbca..9e00837 100644
--- a/lib/oxidized/api/rest.rb
+++ b/lib/oxidized/api/rest.rb
@@ -3,6 +3,14 @@ module Oxidized
require 'json'
module API
class Rest
+ module Helpers
+ def send res, msg='OK', status=200
+ res['Content-Type'] = 'application/json'
+ res.status = status
+ res.body = JSON.dump msg
+ end
+ end
+ include Oxidized::API::Rest::Helpers
def initialize nodes, listen
@nodes = nodes
addr, port = listen.to_s.split ':'
@@ -18,26 +26,49 @@ module Oxidized
end
end
def maps
+ @web.mount '/nodes/next', Next, @nodes
@web.mount_proc '/nodes' do |req, res|
#script_name, #path_info
case req.path_info[1..-1]
# /nodes/reload - reloads list of nodes
when 'reload'
@nodes.load
- res.body = JSON.dump 'OK'
- # /nodes/next/node - moves node to head of queue
- when /next\/(.*)/
- @nodes.next $1
- res.body = JSON.dump 'OK'
+ send res
# /nodes/list - returns list of nodes
when 'list'
- res.body = JSON.dump @nodes.list
+ send res, @nodes.list
# /nodes/show/node - returns data about node
when /show\/(.*)/
- res.body = JSON.dump @nodes.show $1
+ send res, @nodes.show($1)
+ end
+ end
+ end
+
+ # /nodes/next/node - moves node to head of queue
+ class Next < WEBrick::HTTPServlet::AbstractServlet
+ include Oxidized::API::Rest::Helpers
+ def initialize server, nodes
+ super server
+ @nodes = nodes
+ end
+ def do_GET req, res
+ @nodes.next req.path_info[1..-1]
+ send res
+ end
+ def do_PUT req, res
+ node = req.path_info[1..-1]
+ begin
+ opt = JSON.load req.body
+ Log.debug "before: #{@nodes.list}"
+ @nodes.next node, opt
+ Log.debug "after: #{@nodes.list}"
+ send res
+ rescue JSON::ParserError
+ send res, 'broken JSON', 400
end
end
end
+
end
end
end