From f1287a7925901bf3518eb69079304bfb97f7434d Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Sat, 20 Apr 2013 00:41:23 +0300 Subject: 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. --- lib/oxidized/api/rest.rb | 45 +++++++++++++++++++++++++++++++++------- lib/oxidized/config/bootstrap.rb | 4 ++-- lib/oxidized/config/defaults.rb | 1 + lib/oxidized/node.rb | 6 +++++- lib/oxidized/nodes.rb | 10 +++++++-- lib/oxidized/worker.rb | 6 +++++- lib/tst | 19 ++++++++++++----- 7 files changed, 73 insertions(+), 18 deletions(-) (limited to 'lib') 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 diff --git a/lib/oxidized/config/bootstrap.rb b/lib/oxidized/config/bootstrap.rb index 2991fa3..c3ea09d 100644 --- a/lib/oxidized/config/bootstrap.rb +++ b/lib/oxidized/config/bootstrap.rb @@ -4,10 +4,10 @@ module Oxidized CFG.username = 'username' CFG.password = 'password' CFG.model = 'junos' - CFG.interval = 30 + CFG.interval = 60 CFG.log = File.join Config::Root, 'log' CFG.debug = false - CFG.threads = 10 + CFG.threads = 30 CFG.timeout = 5 CFG.prompt = /^([\w\.\-@]{3,30}[#>]\s?)$/ CFG.rest = 8888 diff --git a/lib/oxidized/config/defaults.rb b/lib/oxidized/config/defaults.rb index 943d90b..d189ca3 100644 --- a/lib/oxidized/config/defaults.rb +++ b/lib/oxidized/config/defaults.rb @@ -1,6 +1,7 @@ module Oxidized class Config Root = File.join ENV['HOME'], '.config', 'oxidized' + Crash = File.join Root, 'crash' InputDir = File.join Directory, %w(lib oxidized input) OutputDir = File.join Directory, %w(lib oxidized output) ModelDir = File.join Directory, %w(lib oxidized model) diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index de04c98..35a5948 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -4,7 +4,7 @@ module Oxidized class ModelNotFound < StandardError; end class Node attr_reader :name, :ip, :model, :input, :output, :group, :auth, :prompt - attr_accessor :last, :running + attr_accessor :last, :running, :user, :msg, :from alias :running? :running def initialize opt @name = opt[:name] @@ -47,6 +47,10 @@ module Oxidized h end + def reset + @user = @msg = @from = nil + end + private def resolve_prompt opt diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb index 3e58348..e5e87e5 100644 --- a/lib/oxidized/nodes.rb +++ b/lib/oxidized/nodes.rb @@ -31,9 +31,15 @@ module Oxidized delete_at i if i end # @param node [String] name of the node moved into the head of array - def next node + def next node, opt={} + require 'pp' n = del node - put n if n + if n + n.user = opt['user'] + n.msg = opt['msg'] + n.from = opt['from'] + put n + end end alias :top :next # @return [String] node from the head of the array diff --git a/lib/oxidized/worker.rb b/lib/oxidized/worker.rb index adbaa0e..6966322 100644 --- a/lib/oxidized/worker.rb +++ b/lib/oxidized/worker.rb @@ -23,8 +23,12 @@ module Oxidized node.last = job @jobs.duration job.time if job.status == :success + msg = "update #{node.name}" + msg += " from #{node.from}" if node.from + msg += " with message '#{node.msg}'" if node.msg node.output.new.update node.name, job.config, - :msg => "update #{node.name}", :group => node.group + :msg => msg, :user => node.user, :group => node.group + node.reset else Log.warn "#{node.name} status #{job.status}" end diff --git a/lib/tst b/lib/tst index b529653..6b314cc 100755 --- a/lib/tst +++ b/lib/tst @@ -1,10 +1,19 @@ #!/usr/bin/env ruby20 $: << '.' -require 'pry' -#require 'pp' -#require 'rubygems' +require 'pry' if ENV['DEV'] require 'oxidized' -k = Oxidized.new - +begin + Oxidized.new +rescue Exception => e + open Oxidized::Config::Crash, 'w' do |file| + file.puts '-' * 50 + file.puts Time.now.utc + file.puts '-' * 50 + file.puts e.backtrace + file.puts '-' * 50 + file.puts caller + end + raise +end -- cgit v1.2.1