Last active
October 4, 2020 19:18
-
-
Save xbulat/29e4e960a03c76f6dda81d194802627d to your computer and use it in GitHub Desktop.
rundeck_logs_to_elasticsearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'json' | |
require 'open-uri' | |
require 'elasticsearch' | |
RUNDECK_TOKEN = '<TOKEN>' | |
RUNDECK_URI = 'https://rundeck.example.com/api' | |
PROJECT_ID = 123 | |
ELASTICSEARCH_HOST = 'elasticsearch.example.com' | |
ELASTICSEARCH_INDEX = 'rundeck-logs' | |
class RundeckApi | |
class HttpConnect < RundeckApi | |
def initialize(api) | |
@connection = open( | |
"#{RUNDECK_URI}/#{api}", | |
'User-Agent' => "Ruby/#{RUBY_VERSION}", | |
'Content-Type' => 'application/json', | |
'Accept' => "application/json", | |
'X-Rundeck-Auth-Token' => RUNDECK_TOKEN | |
).read | |
end | |
def http_data | |
JSON.parse(@connection) | |
end | |
end | |
class Main < RundeckApi | |
attr_reader :last_job_id | |
def initialize(last_job_id) | |
@jobs_history = [] | |
@last_job_id = last_job_id.to_i | |
end | |
def rundeck_api_request(api) | |
RundeckApi::HttpConnect.new(api).http_data | |
end | |
def api_projects | |
rundeck_api_request("/#{PROJECT_ID}/projects") | |
end | |
def api_project_history(project) | |
rundeck_api_request("/#{PROJECT_ID}/project/#{project}/history?max=60").fetch('events') | |
end | |
def history | |
@jobs_history.select(&:any?).flatten | |
end | |
def job_history(project) | |
api_project_history(project).select {|pr| pr['execution']['id'].to_i > last_job_id}.map do |job| | |
{ | |
project: job['project'], | |
title: job['title'], | |
status: job['statusString'].upcase, | |
user: job['user'], | |
job_id: job['execution']['id'].to_i, | |
link: job['execution']['permalink'], | |
timestamp: job['date-started'] | |
} | |
end | |
end | |
def make_report | |
api_projects.each {|pr| @jobs_history << job_history(pr['name'])} | |
end | |
end | |
end | |
class ElasticApi | |
def initialize | |
@es_index = "#{ELASTICSEARCH_INDEX}-#{Time.now.strftime("%Y.%m")}" | |
@client ||= Elasticsearch::Client.new(log: true, host: ELASTICSEARCH_HOST) | |
end | |
def index_exists? | |
@client.indices.exists?(index: @es_index) | |
end | |
def query_last_job_id | |
@client.search( | |
index: @es_index, | |
body: { | |
"aggs" => { | |
"max_job_id" => { | |
"max" => { | |
"field" => "job_id" | |
} | |
} | |
} | |
}, | |
size: 1, | |
) | |
end | |
def last_job_id | |
index_exists? ? query_last_job_id["aggregations"]["max_job_id"]["value"].to_i : 0 | |
end | |
def query_add_job(job) | |
@client.index(index: @es_index, body: job) | |
end | |
end | |
e = ElasticApi.new | |
r = RundeckApi::Main.new(e.last_job_id) | |
r.make_report | |
r.history.each {|job| e.query_add_job(job)} if r.history.any? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment