This repository was archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLoggly.php
More file actions
180 lines (138 loc) · 5.35 KB
/
Loggly.php
File metadata and controls
180 lines (138 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
Copyright 2012 Loggly Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
class LogglyException extends Exception {}
class Loggly {
public $domain = 'loggly.com';
public $proxy = 'logs.loggly.com';
public $subdomain = '';
public $username = '';
public $password = '';
public $inputs = array();
public function __construct() {}
public function makeRequest($path, $params = null, $method = 'GET') {
# maintain compatibility with Python Hoover library
if ($path[0] !== '/') {
$path = '/' . $path;
}
$method = strtoupper($method);
$url = sprintf('https://%s.%s%s', $this->subdomain, $this->domain, $path);
$curl = curl_init();
if ($method === 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
}
if ($method === 'PUT' || $method === 'DELETE') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
}
# set HTTP headers
curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
# handle URL params
if ($params) {
$segments = array();
foreach ($params as $k => $v) {
$segments[] .= $k . '=' . urlencode($v);
}
$qs = join($segments, '&');
if ($method === 'POST' || $method === 'PUT') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $qs);
} else {
$url .= '?' . $qs;
}
}
curl_setopt($curl, CURLOPT_URL, $url);
# satisfy content length header requirement for PUT
if ($method === 'PUT') {
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($qs)));
}
$result = curl_exec($curl);
if (!$result) {
throw new LogglyException(curl_error($curl));
}
$json = json_decode($result, true);
if (!$json) {
# API is inconsistent
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status >= 200 && $status <= 299) {
return null;
}
curl_close($curl);
throw new LogglyException($result);
}
curl_close($curl);
return $json;
}
public function getInputs() {
return $this->makeRequest('/api/inputs');
}
public function getDevices() {
return $this->makeRequest('/api/devices');
}
# input-related methods
public function addDevice($inputId) {
return $this->makeRequest('/api/inputs/' . $inputId . '/adddevice', null, 'POST');
}
public function removeDevice($inputId) {
return $this->makeRequest('/api/inputs/' . $inputId . '/removedevice', null, 'POST');
}
public function enableDiscovery($inputId) {
return $this->makeRequest('/api/inputs/' . $inputId . '/discover', null, 'POST');
}
public function disableDiscovery($inputId) {
return $this->makeRequest('/api/inputs/' . $inputId . '/discover', null, 'DELETE');
}
# search-related methods
public function search($q, $params = null) {
$params['q'] = $q;
return $this->makeRequest('/api/search', $params);
}
public function facet($q, $facet = 'date', $params = null) {
$params['q'] = $q;
return $this->makeRequest('/api/facets/' . $facet, $params);
}
public function getSavedSearches() {
return $this->makeRequest('/api/savedsearches/');
}
# $params is an array with keys 'foo' and 'context'
# context is a JSON blob, e.g.
# $params = array('name' => 'foo',
# 'context' => '{"search_type":"search", "terms":"error AND 500", "from":"NOW-1HOUR", "until":"NOW", "inputs":["app","staging"]}');
public function createSavedSearch($params) {
return $this->makeRequest('/api/savedsearches', $params, 'POST');
}
# $params must contain a key 'id'
public function updateSavedSearch($params) {
return $this->makeRequest('/api/savedsearches', $params, 'PUT');
}
public function deleteSavedSearch($id) {
return $this->makeRequest('/api/savedsearches/' . $id, null, 'DELETE');
}
public function runSavedSearch($id, $facet = false, $facetBy = 'date') {
if (!$facet) {
return $this->makeRequest('/api/savedsearches/' . $id . '/results');
} else {
return $this->makeRequest('/api/savedsearches/' . $id . '/facets/' . $facetBy);
}
}
# account-related methods
# always returns current Loggly account
public function getCustomer() {
return $this->makeRequest('/api/customers/');
}
# always returns current Loggly account
public function getCustomerStats() {
return $this->makeRequest('/api/customers/stats');
}
}
?>