From ccbbeac23d39eae455681dbdc13ee4447286fe6f Mon Sep 17 00:00:00 2001 From: Jeff Groom <33snipehunt@gmail.com> Date: Mon, 15 Feb 2016 13:45:26 +1300 Subject: Create http.rb --- lib/oxidized/output/http.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/oxidized/output/http.rb diff --git a/lib/oxidized/output/http.rb b/lib/oxidized/output/http.rb new file mode 100644 index 0000000..2b18320 --- /dev/null +++ b/lib/oxidized/output/http.rb @@ -0,0 +1,56 @@ +module Oxidized + class Http < Output + attr_reader :commitref + def initialize + @cfg = Oxidized.config.output.http + end + + def setup + if @cfg.empty? + CFGS.user.output.http.user = 'Oxidized' + CFGS.user.output.http.pasword = 'secret' + CFGS.user.output.http.url = 'http://localhost/web-api/oxidized' + CFGS.save :user + raise NoConfig, 'no output http config, edit ~/.config/oxidized/config' + end + end + + def store node, outputs, opt={} + @commitref = nil + json = JSON.pretty_generate( + { + 'msg' => opt[:msg], + 'user' => opt[:user], + 'email' => opt[:email], + 'group' => opt[:group], + 'node' => node, + 'config' => outputs.to_cfg, + # actually we need to also iterate outputs, for other types like in gitlab. But most people don't use 'type' functionality. + } + ) + uri = URI.parse @cfg.url + http = Net::HTTP.new uri.host, uri.port + #http.use_ssl = true if uri.scheme = 'https' + req = Net::HTTP::Post.new(uri.request_uri, initheader = { 'Content-Type' => 'application/json'}) + req.basic_auth @cfg.user, @cfg.password + req.body = json + response = http.request req + + case response.code.to_i + when 200 || 201 + Oxidized.logger.info "Configuration http backup complete for #{node}" + p [:success] + when (400..499) + Oxidized.logger.info "Configuration http backup for #{node} failed status: #{response.body}" + p [:bad_request] + when (500..599) + p [:server_problems] + Oxidized.logger.info "Configuration http backup for #{node} failed status: #{response.body}" + end + + end + + end +end + + -- cgit v1.2.1 From 219880c35151fe071a855205bc33de599a0f0991 Mon Sep 17 00:00:00 2001 From: Jeff Groom <33snipehunt@gmail.com> Date: Mon, 15 Feb 2016 13:56:30 +1300 Subject: Update README.md --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index a130961..8e17aa0 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Oxidized is a network device configuration backup tool. It's a RANCID replacemen * [Source: SQLite](#source-sqlite) * [Source: HTTP](#source-http) * [Output: GIT](#output-git) + * [Output: HTTP](#output-http) * [Output: File](#output-file) * [Output types](#output-types) * [Advanced Configuration](#advanced-configuration) @@ -363,6 +364,19 @@ output: repo: "/var/lib/oxidized/devices.git" ``` +### Output: Http + +POST a config to the specified URL + +``` +output: + default: http + http: + user: admin + password: changeit + url: "http://192.168.162.50:8080/db/coll" +``` + ### Output types If you prefer to have different outputs in different files and/or directories, you can easily do this by modifying the corresponding model. To change the behaviour for IOS, you would edit `lib/oxidized/model/ios.rb`. -- cgit v1.2.1 From 1209fb0343348f3d71457161625b2b827c6d8717 Mon Sep 17 00:00:00 2001 From: Jeff Groom <33snipehunt@gmail.com> Date: Mon, 22 Feb 2016 16:55:44 +1300 Subject: add requires --- lib/oxidized/output/http.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/oxidized/output/http.rb b/lib/oxidized/output/http.rb index 2b18320..13ba300 100644 --- a/lib/oxidized/output/http.rb +++ b/lib/oxidized/output/http.rb @@ -14,7 +14,9 @@ module Oxidized raise NoConfig, 'no output http config, edit ~/.config/oxidized/config' end end - + require "net/http" + require "uri" + require "json" def store node, outputs, opt={} @commitref = nil json = JSON.pretty_generate( -- cgit v1.2.1