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
|
|
|
params = {limit: limit, offset: offset} |
47
|
|
|
@http.get "search/jobs/#{search_job['id']}/messages", params |
48
|
|
|
end |
49
|
|
|
|
50
|
|
View Code Duplication |
def search_job_records(search_job, limit=nil, offset=0) |
|
|
|
|
51
|
|
|
params = {limit: limit, offset: offset} |
52
|
|
|
@http.get "search/jobs/#{search_job['id']}/records", params |
53
|
|
|
end |
54
|
|
|
|
55
|
|
|
def collectors(limit=nil, offset=nil) |
56
|
|
|
@http.get 'collectors', params_limit_offset(limit, offset) |
57
|
|
|
end |
58
|
|
|
|
59
|
|
|
def collector(collector_id) |
60
|
|
|
@http.get "collectors/#{collector_id}" |
61
|
|
|
end |
62
|
|
|
|
63
|
|
|
def update_collector(collector, etag) |
64
|
|
|
@http.put do |req| |
65
|
|
|
req.url "collectors/#{collector['collector']['id']}" |
66
|
|
|
req.headers['If-Match'] = etag |
67
|
|
|
req.body = collector |
68
|
|
|
end |
69
|
|
|
end |
70
|
|
|
|
71
|
|
|
def delete_collector(collector) |
72
|
|
|
@http.delete "collectors/#{collector['id']}" |
73
|
|
|
end |
74
|
|
|
|
75
|
|
|
def sources(collector_id, limit=nil, offset=nil) |
76
|
|
|
@http.get "collectors/#{collector_id}/sources", params_limit_offset(limit, offset) |
77
|
|
|
end |
78
|
|
|
|
79
|
|
|
def source(collector_id, source_id) |
80
|
|
|
@http.get "collectors/#{collector_id}/sources/#{source_id}" |
81
|
|
|
end |
82
|
|
|
|
83
|
|
|
def update_source(collector_id, source, etag) |
84
|
|
|
@http.put do |req| |
85
|
|
|
req.url "collectors/#{collector_id}/sources/#{source['source']['id']}" |
86
|
|
|
req.headers['If-Match'] = etag |
87
|
|
|
req.body = source |
88
|
|
|
end |
89
|
|
|
end |
90
|
|
|
|
91
|
|
|
def delete_source(collector_id, source) |
92
|
|
|
@http.delete "collectors/#{collector_id}/sources/#{source['source']['id']}" |
93
|
|
|
end |
94
|
|
|
|
95
|
|
|
def create_content(path, data) |
96
|
|
|
@http.post "content/#{path}", data |
97
|
|
|
end |
98
|
|
|
|
99
|
|
|
def get_content(path) |
100
|
|
|
@http.get "content/#{path}" |
101
|
|
|
end |
102
|
|
|
|
103
|
|
|
def delete_content(path) |
104
|
|
|
@http.delete "content/#{path}" |
105
|
|
|
end |
106
|
|
|
|
107
|
|
|
def dashboards(monitors=false) |
108
|
|
|
r = @http.get 'dashboards', {dashboards: monitors} |
109
|
|
|
return r.body.key?('dashboards') ? r.body['dashboards'] : nil |
110
|
|
|
end |
111
|
|
|
|
112
|
|
|
def dashboard(dashboard_id) |
113
|
|
|
r = @http.get "dashboards/#{dashboard_id}" |
114
|
|
|
return r.body.key?('dashboard') ? r.body['dashboard'] : nil |
115
|
|
|
end |
116
|
|
|
|
117
|
|
|
def dashboard_data(dashboard_id) |
118
|
|
|
r = @http.get "dashboards/#{dashboard_id}/data" |
119
|
|
|
return r.body.key?('dashboardMonitorDatas') ? r.body['dashboardMonitorDatas'] : nil |
120
|
|
|
end |
121
|
|
|
|
122
|
|
|
def params_limit_offset(limit, offset) |
123
|
|
|
params = {} |
124
|
|
|
params[:limit] = limit unless limit.nil? |
125
|
|
|
params[:offset] = offset unless offset.nil? |
126
|
|
|
params |
127
|
|
|
end |
128
|
|
|
end |
129
|
|
|
end |
130
|
|
|
|