1
|
|
|
require 'faraday' |
2
|
|
|
require 'faraday_middleware' |
3
|
|
|
require 'faraday-cookie_jar' |
4
|
|
|
require 'multi_json' |
5
|
|
|
|
6
|
|
|
module SumoLogic |
7
|
|
|
VERSION = '0.1.0' |
8
|
|
|
URL = 'https://api.sumologic.com/api/v1' |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
attr_accessor :http |
12
|
|
|
|
13
|
|
|
def initialize(access_id=nil, access_key=nil, endpoint=SumoLogic::URL) |
14
|
|
|
@endpoint = endpoint |
15
|
|
|
headers = {'Content-Type' => 'application/json', 'Accept' => 'application/json'} |
16
|
|
|
@http = Faraday.new(url: @endpoint, headers: headers) do |conn| |
17
|
|
|
conn.basic_auth(access_id, access_key) |
18
|
|
|
conn.use FaradayMiddleware::FollowRedirects, limit: 5 |
19
|
|
|
conn.use :cookie_jar |
20
|
|
|
conn.request :json |
21
|
|
|
conn.response :json, content_type: 'application/json' |
22
|
|
|
conn.adapter Faraday.default_adapter |
23
|
|
|
end |
24
|
|
|
end |
25
|
|
|
|
26
|
|
|
def search(query, from_time=nil, to_time=nil, time_zone='UTC') |
27
|
|
|
@http.get do |req| |
28
|
|
|
req.url 'logs/search' |
29
|
|
|
req.params = {q: query, from: from_time, to: to_time, tz: time_zone} |
30
|
|
|
end |
31
|
|
|
end |
32
|
|
|
|
33
|
|
|
def search_job(query, from_time=nil, to_time=nil, time_zone='UTC') |
34
|
|
|
params = {query: query, from: from_time, to: to_time, timeZone: time_zone} |
35
|
|
|
@http.post do |req| |
36
|
|
|
req.url 'search/jobs' |
37
|
|
|
req.body = MultiJson.encode(params) |
38
|
|
|
end |
39
|
|
|
end |
40
|
|
|
|
41
|
|
|
def search_job_status(search_job={}) |
42
|
|
|
@http.get "search/jobs/#{search_job['id']}" |
43
|
|
|
end |
44
|
|
|
|
45
|
|
View Code Duplication |
def search_job_messages(search_job, limit=nil, offset=0) |
|
|
|
|
46
|
|
|
@http.get "search/jobs/#{search_job['id']}/messages", params_limit_offset(limit, offset) |
47
|
|
|
end |
48
|
|
|
|
49
|
|
View Code Duplication |
def search_job_records(search_job, limit=nil, offset=0) |
|
|
|
|
50
|
|
|
@http.get "search/jobs/#{search_job['id']}/records", params_limit_offset(limit, offset) |
51
|
|
|
end |
52
|
|
|
|
53
|
|
|
def collectors(limit=nil, offset=nil) |
54
|
|
|
@http.get 'collectors', params_limit_offset(limit, offset) |
55
|
|
|
end |
56
|
|
|
|
57
|
|
|
def collector(collector_id) |
58
|
|
|
@http.get "collectors/#{collector_id}" |
59
|
|
|
end |
60
|
|
|
|
61
|
|
|
def update_collector(collector, etag) |
62
|
|
|
@http.put do |req| |
63
|
|
|
req.url "collectors/#{collector['collector']['id']}" |
64
|
|
|
req.headers['If-Match'] = etag |
65
|
|
|
req.body = collector |
66
|
|
|
end |
67
|
|
|
end |
68
|
|
|
|
69
|
|
|
def delete_collector(collector) |
70
|
|
|
@http.delete "collectors/#{collector['id']}" |
71
|
|
|
end |
72
|
|
|
|
73
|
|
|
def sources(collector_id, limit=nil, offset=nil) |
74
|
|
|
@http.get "collectors/#{collector_id}/sources", params_limit_offset(limit, offset) |
75
|
|
|
end |
76
|
|
|
|
77
|
|
|
def source(collector_id, source_id) |
78
|
|
|
@http.get "collectors/#{collector_id}/sources/#{source_id}" |
79
|
|
|
end |
80
|
|
|
|
81
|
|
|
def update_source(collector_id, source, etag) |
82
|
|
|
@http.put do |req| |
83
|
|
|
req.url "collectors/#{collector_id}/sources/#{source['source']['id']}" |
84
|
|
|
req.headers['If-Match'] = etag |
85
|
|
|
req.body = source |
86
|
|
|
end |
87
|
|
|
end |
88
|
|
|
|
89
|
|
|
def delete_source(collector_id, source) |
90
|
|
|
@http.delete "collectors/#{collector_id}/sources/#{source['source']['id']}" |
91
|
|
|
end |
92
|
|
|
|
93
|
|
|
def create_content(path, data) |
94
|
|
|
@http.post "content/#{path}", data |
95
|
|
|
end |
96
|
|
|
|
97
|
|
|
def get_content(path) |
98
|
|
|
@http.get "content/#{path}" |
99
|
|
|
end |
100
|
|
|
|
101
|
|
|
def delete_content(path) |
102
|
|
|
@http.delete "content/#{path}" |
103
|
|
|
end |
104
|
|
|
|
105
|
|
|
def dashboards(monitors=false) |
106
|
|
|
r = @http.get 'dashboards', {dashboards: monitors} |
107
|
|
|
return r.body.key?('dashboards') ? r.body['dashboards'] : nil |
108
|
|
|
end |
109
|
|
|
|
110
|
|
|
def dashboard(dashboard_id) |
111
|
|
|
r = @http.get "dashboards/#{dashboard_id}" |
112
|
|
|
return r.body.key?('dashboard') ? r.body['dashboard'] : nil |
113
|
|
|
end |
114
|
|
|
|
115
|
|
|
def dashboard_data(dashboard_id) |
116
|
|
|
r = @http.get "dashboards/#{dashboard_id}/data" |
117
|
|
|
return r.body.key?('dashboardMonitorDatas') ? r.body['dashboardMonitorDatas'] : nil |
118
|
|
|
end |
119
|
|
|
|
120
|
|
|
def params_limit_offset(limit, offset) |
121
|
|
|
params = {} |
122
|
|
|
params[:limit] = limit unless limit.nil? |
123
|
|
|
params[:offset] = offset unless offset.nil? |
124
|
|
|
params |
125
|
|
|
end |
126
|
|
|
end |
127
|
|
|
end |
128
|
|
|
|