sumologic-query 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +105 -200
- data/bin/sumo-query +5 -101
- data/lib/sumologic/cli.rb +208 -0
- data/lib/sumologic/client.rb +44 -200
- data/lib/sumologic/configuration.rb +55 -0
- data/lib/sumologic/http/authenticator.rb +20 -0
- data/lib/sumologic/http/client.rb +80 -0
- data/lib/sumologic/metadata/collector.rb +33 -0
- data/lib/sumologic/metadata/source.rb +72 -0
- data/lib/sumologic/search/job.rb +68 -0
- data/lib/sumologic/search/paginator.rb +67 -0
- data/lib/sumologic/search/poller.rb +80 -0
- data/lib/sumologic/version.rb +1 -1
- data/lib/sumologic.rb +33 -2
- metadata +25 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ec844dca9513804180d8e25badb43d663e46467d9683366750131d1c6acc42f7
|
|
4
|
+
data.tar.gz: 0e5dc7f91e61572866e817ea9c6db84112d1cca3d5156826bfc71d829359f3fb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1dbd2cf57b915d5a4e4bd0a43b2f911089087e906a4b01cab318dd3901c3d1645271718c1ff112a7d14769074e7a6dc5f4751001538f72ac703c23b4cb58c505
|
|
7
|
+
data.tar.gz: 20481e3c0b0f2c86b5b06f4f1fc5d32f4753e96defc6cd4f9a35e671666b28e24c1c388390cddb56443c6020c33123e40cdb93424ae25179bb0bafcd825ac60c
|
data/README.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
# Sumo Logic Query Tool
|
|
2
2
|
|
|
3
|
-
> A lightweight Ruby CLI for querying Sumo Logic logs
|
|
3
|
+
> A lightweight Ruby CLI for querying Sumo Logic logs and metadata. Simple, fast, read-only access to your logs.
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/rb/sumologic-query)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
8
|
## Why This Tool?
|
|
9
9
|
|
|
10
|
-
- **
|
|
10
|
+
- **Minimal dependencies**: Uses only Ruby stdlib + Thor for CLI
|
|
11
11
|
- **Fast queries**: Efficient polling and automatic pagination
|
|
12
12
|
- **Simple interface**: Just query, get results, done
|
|
13
13
|
- **Read-only**: No write operations, perfect for safe log access
|
|
14
|
-
- **
|
|
14
|
+
- **Modular architecture**: Clean separation of concerns (HTTP, Search, Metadata)
|
|
15
|
+
- **Metadata support**: List collectors and sources alongside log queries
|
|
15
16
|
|
|
16
|
-
All existing Ruby Sumo Logic gems are unmaintained (2-9 years dormant). This tool provides a fresh, minimal approach focused
|
|
17
|
+
All existing Ruby Sumo Logic gems are unmaintained (2-9 years dormant). This tool provides a fresh, minimal approach focused on querying logs and metadata.
|
|
17
18
|
|
|
18
19
|
## Installation
|
|
19
20
|
|
|
@@ -48,7 +49,7 @@ Export your Sumo Logic API credentials:
|
|
|
48
49
|
```bash
|
|
49
50
|
export SUMO_ACCESS_ID="your_access_id"
|
|
50
51
|
export SUMO_ACCESS_KEY="your_access_key"
|
|
51
|
-
export SUMO_DEPLOYMENT="us2" # Optional: us1, us2 (default), eu, au
|
|
52
|
+
export SUMO_DEPLOYMENT="us2" # Optional: us1, us2 (default), eu, au, etc.
|
|
52
53
|
```
|
|
53
54
|
|
|
54
55
|
**Getting credentials:**
|
|
@@ -60,18 +61,27 @@ export SUMO_DEPLOYMENT="us2" # Optional: us1, us2 (default), eu, au
|
|
|
60
61
|
### 2. Run Your First Query
|
|
61
62
|
|
|
62
63
|
```bash
|
|
63
|
-
|
|
64
|
+
# Search logs
|
|
65
|
+
sumo-query search --query 'error' \
|
|
64
66
|
--from '2025-11-13T14:00:00' \
|
|
65
67
|
--to '2025-11-13T15:00:00' \
|
|
66
68
|
--limit 10
|
|
69
|
+
|
|
70
|
+
# List collectors
|
|
71
|
+
sumo-query collectors
|
|
72
|
+
|
|
73
|
+
# List sources
|
|
74
|
+
sumo-query sources
|
|
67
75
|
```
|
|
68
76
|
|
|
69
77
|
## Usage
|
|
70
78
|
|
|
71
|
-
|
|
79
|
+
The CLI provides three main commands:
|
|
80
|
+
|
|
81
|
+
### Search Logs
|
|
72
82
|
|
|
73
83
|
```bash
|
|
74
|
-
sumo-query --query "YOUR_QUERY" \
|
|
84
|
+
sumo-query search --query "YOUR_QUERY" \
|
|
75
85
|
--from "START_TIME" \
|
|
76
86
|
--to "END_TIME" \
|
|
77
87
|
[--output FILE] \
|
|
@@ -79,87 +89,85 @@ sumo-query --query "YOUR_QUERY" \
|
|
|
79
89
|
[--time-zone TZ]
|
|
80
90
|
```
|
|
81
91
|
|
|
82
|
-
|
|
83
|
-
|
|
92
|
+
**Required options:**
|
|
84
93
|
- `-q, --query QUERY` - Sumo Logic query string
|
|
85
|
-
- `-f, --from TIME` - Start time
|
|
86
|
-
- `-t, --to TIME` - End time
|
|
87
|
-
|
|
88
|
-
### Optional Options
|
|
94
|
+
- `-f, --from TIME` - Start time (ISO 8601 format)
|
|
95
|
+
- `-t, --to TIME` - End time (ISO 8601 format)
|
|
89
96
|
|
|
97
|
+
**Optional options:**
|
|
90
98
|
- `-z, --time-zone TZ` - Time zone (default: UTC)
|
|
91
99
|
- `-l, --limit N` - Limit number of messages
|
|
92
|
-
- `-o, --output FILE` - Save
|
|
100
|
+
- `-o, --output FILE` - Save to file (default: stdout)
|
|
93
101
|
- `-d, --debug` - Enable debug output
|
|
94
|
-
- `-h, --help` - Show help message
|
|
95
|
-
- `-v, --version` - Show version
|
|
96
102
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
### Error Analysis
|
|
103
|
+
### List Collectors
|
|
100
104
|
|
|
101
105
|
```bash
|
|
102
|
-
|
|
103
|
-
sumo-query --query 'error' \
|
|
104
|
-
--from '2025-11-13T14:00:00' \
|
|
105
|
-
--to '2025-11-13T15:00:00' \
|
|
106
|
-
--output errors.json
|
|
107
|
-
|
|
108
|
-
# Error timeline with 5-minute buckets
|
|
109
|
-
sumo-query --query 'error | timeslice 5m | count by _timeslice' \
|
|
110
|
-
--from '2025-11-13T14:00:00' \
|
|
111
|
-
--to '2025-11-13T15:00:00'
|
|
106
|
+
sumo-query collectors [--output FILE]
|
|
112
107
|
```
|
|
113
108
|
|
|
114
|
-
|
|
109
|
+
Lists all collectors in your account with status and metadata.
|
|
115
110
|
|
|
116
|
-
|
|
117
|
-
# Search for specific text
|
|
118
|
-
sumo-query --query '"connection timeout"' \
|
|
119
|
-
--from '2025-11-13T14:00:00' \
|
|
120
|
-
--to '2025-11-13T15:00:00'
|
|
111
|
+
### List Sources
|
|
121
112
|
|
|
122
|
-
|
|
123
|
-
sumo-query --
|
|
124
|
-
--from '2025-11-13T14:00:00' \
|
|
125
|
-
--to '2025-11-13T15:00:00'
|
|
113
|
+
```bash
|
|
114
|
+
sumo-query sources [--output FILE]
|
|
126
115
|
```
|
|
127
116
|
|
|
128
|
-
|
|
117
|
+
Lists all sources from active collectors.
|
|
129
118
|
|
|
130
|
-
|
|
131
|
-
# Filter by source category
|
|
132
|
-
sumo-query --query '_sourceCategory=prod/api error' \
|
|
133
|
-
--from '2025-11-13T14:00:00' \
|
|
134
|
-
--to '2025-11-13T15:00:00'
|
|
119
|
+
**See [examples/queries.md](examples/queries.md) for more query patterns and examples.**
|
|
135
120
|
|
|
136
|
-
|
|
137
|
-
sumo-query --query '(_sourceCategory=prod/api OR _sourceCategory=prod/web) AND error' \
|
|
138
|
-
--from '2025-11-13T14:00:00' \
|
|
139
|
-
--to '2025-11-13T15:00:00'
|
|
140
|
-
```
|
|
121
|
+
## Ruby Library Usage
|
|
141
122
|
|
|
142
|
-
|
|
123
|
+
Use the library directly in your Ruby code:
|
|
143
124
|
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
sumo-query --query '* | count by status_code' \
|
|
147
|
-
--from '2025-11-13T14:00:00' \
|
|
148
|
-
--to '2025-11-13T15:00:00'
|
|
125
|
+
```ruby
|
|
126
|
+
require 'sumologic'
|
|
149
127
|
|
|
150
|
-
#
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
128
|
+
# Initialize client
|
|
129
|
+
client = Sumologic::Client.new(
|
|
130
|
+
access_id: ENV['SUMO_ACCESS_ID'],
|
|
131
|
+
access_key: ENV['SUMO_ACCESS_KEY'],
|
|
132
|
+
deployment: 'us2'
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Search logs
|
|
136
|
+
results = client.search(
|
|
137
|
+
query: 'error',
|
|
138
|
+
from_time: '2025-11-13T14:00:00',
|
|
139
|
+
to_time: '2025-11-13T15:00:00',
|
|
140
|
+
time_zone: 'UTC',
|
|
141
|
+
limit: 1000
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
results.each do |message|
|
|
145
|
+
puts message['map']['message']
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# List collectors
|
|
149
|
+
collectors = client.list_collectors
|
|
150
|
+
|
|
151
|
+
# List all sources
|
|
152
|
+
sources = client.list_all_sources
|
|
154
153
|
```
|
|
155
154
|
|
|
156
|
-
|
|
155
|
+
**See [docs/api-reference.md](docs/api-reference.md) for complete API documentation.**
|
|
156
|
+
|
|
157
|
+
## Time Formats
|
|
158
|
+
|
|
159
|
+
Use ISO 8601 format for timestamps:
|
|
157
160
|
|
|
158
161
|
```bash
|
|
159
|
-
#
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
162
|
+
# UTC timestamps (default)
|
|
163
|
+
--from "2025-11-13T14:30:00" --to "2025-11-13T15:00:00"
|
|
164
|
+
|
|
165
|
+
# With timezone
|
|
166
|
+
--from "2025-11-13T14:30:00" --time-zone "America/New_York"
|
|
167
|
+
|
|
168
|
+
# Using shell helpers
|
|
169
|
+
--from "$(date -u -v-1H '+%Y-%m-%dT%H:%M:%S')" # 1 hour ago
|
|
170
|
+
--to "$(date -u '+%Y-%m-%dT%H:%M:%S')" # now
|
|
163
171
|
```
|
|
164
172
|
|
|
165
173
|
## Output Format
|
|
@@ -186,157 +194,54 @@ Results are returned as JSON:
|
|
|
186
194
|
}
|
|
187
195
|
```
|
|
188
196
|
|
|
189
|
-
## Time Formats
|
|
190
|
-
|
|
191
|
-
Use ISO 8601 format for timestamps:
|
|
192
|
-
|
|
193
|
-
```bash
|
|
194
|
-
# UTC timestamps (default)
|
|
195
|
-
--from "2025-11-13T14:30:00"
|
|
196
|
-
--to "2025-11-13T15:00:00"
|
|
197
|
-
|
|
198
|
-
# With timezone
|
|
199
|
-
--from "2025-11-13T14:30:00" --time-zone "America/New_York"
|
|
200
|
-
|
|
201
|
-
# Alternative: Relative times (in your shell)
|
|
202
|
-
--from "$(date -u -v-1H '+%Y-%m-%dT%H:%M:%S')" # 1 hour ago
|
|
203
|
-
--to "$(date -u '+%Y-%m-%dT%H:%M:%S')" # now
|
|
204
|
-
```
|
|
205
|
-
|
|
206
197
|
## Performance
|
|
207
198
|
|
|
208
199
|
Query execution time depends on data volume:
|
|
209
200
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
201
|
+
| Messages | Typical Time |
|
|
202
|
+
|----------|--------------|
|
|
203
|
+
| < 10K | 30-60 seconds |
|
|
204
|
+
| 10K-100K | 1-2 minutes |
|
|
205
|
+
| 100K+ | 2-5 minutes |
|
|
213
206
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
To improve performance:
|
|
207
|
+
**Tips for faster queries:**
|
|
217
208
|
- Narrow your time range
|
|
218
|
-
- Add
|
|
209
|
+
- Add `_sourceCategory` filters
|
|
219
210
|
- Use `--limit` to cap results
|
|
220
|
-
- Use aggregation queries instead of
|
|
221
|
-
|
|
222
|
-
## Troubleshooting
|
|
211
|
+
- Use aggregation queries instead of raw messages
|
|
223
212
|
|
|
224
|
-
|
|
213
|
+
## Documentation
|
|
225
214
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
**
|
|
231
|
-
```bash
|
|
232
|
-
export SUMO_ACCESS_ID="your_access_id"
|
|
233
|
-
export SUMO_ACCESS_KEY="your_access_key"
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
### Timeout Error
|
|
237
|
-
|
|
238
|
-
```
|
|
239
|
-
Timeout Error: Search job timed out after 300 seconds
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
**Solutions**:
|
|
243
|
-
- Reduce time range
|
|
244
|
-
- Add more specific filters (`_sourceCategory`, `_sourceName`)
|
|
245
|
-
- Use `--limit` to cap results
|
|
246
|
-
- Consider using aggregation instead of raw messages
|
|
247
|
-
|
|
248
|
-
### Empty Results
|
|
249
|
-
|
|
250
|
-
```json
|
|
251
|
-
{
|
|
252
|
-
"message_count": 0,
|
|
253
|
-
"messages": []
|
|
254
|
-
}
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
**Check**:
|
|
258
|
-
- Time range matches your expected data
|
|
259
|
-
- Query syntax is valid (test in Sumo Logic UI first)
|
|
260
|
-
- Source categories are correct
|
|
261
|
-
- Time zone is correct (default is UTC)
|
|
262
|
-
|
|
263
|
-
### Rate Limit Error
|
|
264
|
-
|
|
265
|
-
```
|
|
266
|
-
HTTP 429: Rate limit exceeded
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
**Solution**: Wait 1-2 minutes between queries. Sumo Logic enforces rate limits per account.
|
|
215
|
+
- **[Quick Reference (tldr)](docs/tldr.md)** - Concise command examples in tldr format
|
|
216
|
+
- **[Query Examples](examples/queries.md)** - Common query patterns and use cases
|
|
217
|
+
- **[API Reference](docs/api-reference.md)** - Complete Ruby library documentation
|
|
218
|
+
- **[Architecture](docs/architecture.md)** - How the tool works internally
|
|
219
|
+
- **[Troubleshooting](docs/troubleshooting.md)** - Common issues and solutions
|
|
270
220
|
|
|
271
221
|
## Development
|
|
272
222
|
|
|
273
|
-
|
|
223
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, testing, and contribution guidelines.
|
|
224
|
+
|
|
225
|
+
Quick start:
|
|
274
226
|
|
|
275
227
|
```bash
|
|
228
|
+
# Clone and install
|
|
229
|
+
git clone https://github.com/patrick204nqh/sumologic-query.git
|
|
230
|
+
cd sumologic-query
|
|
276
231
|
bundle install
|
|
277
|
-
bundle exec rake spec
|
|
278
|
-
```
|
|
279
232
|
|
|
280
|
-
|
|
233
|
+
# Run tests
|
|
234
|
+
bundle exec rspec
|
|
281
235
|
|
|
282
|
-
|
|
236
|
+
# Run linter
|
|
283
237
|
bundle exec rubocop
|
|
284
|
-
bundle exec rubocop -A # Auto-fix issues
|
|
285
|
-
```
|
|
286
238
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
```bash
|
|
290
|
-
# Without installing
|
|
291
|
-
bundle exec bin/sumo-query --query "error" \
|
|
292
|
-
--from "2025-11-13T14:00:00" \
|
|
293
|
-
--to "2025-11-13T15:00:00"
|
|
294
|
-
|
|
295
|
-
# With debug output
|
|
296
|
-
SUMO_DEBUG=1 bundle exec bin/sumo-query --query "error" \
|
|
239
|
+
# Test locally
|
|
240
|
+
bundle exec bin/sumo-query search --query "error" \
|
|
297
241
|
--from "2025-11-13T14:00:00" \
|
|
298
242
|
--to "2025-11-13T15:00:00"
|
|
299
243
|
```
|
|
300
244
|
|
|
301
|
-
## How It Works
|
|
302
|
-
|
|
303
|
-
This tool uses the Sumo Logic Search Job API:
|
|
304
|
-
|
|
305
|
-
1. **Create Job** - POST to `/api/v1/search/jobs` with your query
|
|
306
|
-
2. **Poll Status** - GET `/api/v1/search/jobs/:id` every 20 seconds until complete
|
|
307
|
-
3. **Fetch Messages** - GET `/api/v1/search/jobs/:id/messages` (automatically paginated)
|
|
308
|
-
4. **Clean Up** - DELETE `/api/v1/search/jobs/:id`
|
|
309
|
-
|
|
310
|
-
All steps are handled automatically. You just provide the query and get results.
|
|
311
|
-
|
|
312
|
-
## API Reference
|
|
313
|
-
|
|
314
|
-
### Ruby Library Usage
|
|
315
|
-
|
|
316
|
-
You can also use the library directly in your Ruby code:
|
|
317
|
-
|
|
318
|
-
```ruby
|
|
319
|
-
require 'sumologic'
|
|
320
|
-
|
|
321
|
-
client = Sumologic::Client.new(
|
|
322
|
-
access_id: 'your_access_id',
|
|
323
|
-
access_key: 'your_access_key',
|
|
324
|
-
deployment: 'us2'
|
|
325
|
-
)
|
|
326
|
-
|
|
327
|
-
results = client.search(
|
|
328
|
-
query: 'error',
|
|
329
|
-
from_time: '2025-11-13T14:00:00',
|
|
330
|
-
to_time: '2025-11-13T15:00:00',
|
|
331
|
-
time_zone: 'UTC',
|
|
332
|
-
limit: 1000
|
|
333
|
-
)
|
|
334
|
-
|
|
335
|
-
results.each do |message|
|
|
336
|
-
puts message['map']['message']
|
|
337
|
-
end
|
|
338
|
-
```
|
|
339
|
-
|
|
340
245
|
## Contributing
|
|
341
246
|
|
|
342
247
|
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
|
|
@@ -351,17 +256,17 @@ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for det
|
|
|
351
256
|
|
|
352
257
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
353
258
|
|
|
259
|
+
## Support
|
|
260
|
+
|
|
261
|
+
- **Issues**: [GitHub Issues](https://github.com/patrick204nqh/sumologic-query/issues)
|
|
262
|
+
- **Discussions**: [GitHub Discussions](https://github.com/patrick204nqh/sumologic-query/discussions)
|
|
263
|
+
- **Documentation**: [docs/](docs/)
|
|
264
|
+
|
|
354
265
|
## Resources
|
|
355
266
|
|
|
356
267
|
- **Sumo Logic API Docs**: https://help.sumologic.com/docs/api/search-job/
|
|
357
268
|
- **Query Language**: https://help.sumologic.com/docs/search/
|
|
358
269
|
- **Bug Reports**: https://github.com/patrick204nqh/sumologic-query/issues
|
|
359
|
-
- **Feature Requests**: https://github.com/patrick204nqh/sumologic-query/issues
|
|
360
|
-
|
|
361
|
-
## Support
|
|
362
|
-
|
|
363
|
-
- **Issues**: [GitHub Issues](https://github.com/patrick204nqh/sumologic-query/issues)
|
|
364
|
-
- **Discussions**: [GitHub Discussions](https://github.com/patrick204nqh/sumologic-query/discussions)
|
|
365
270
|
|
|
366
271
|
---
|
|
367
272
|
|
data/bin/sumo-query
CHANGED
|
@@ -1,110 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
-
# Simple CLI wrapper for querying Sumo Logic logs
|
|
5
|
-
# Usage: sumo-query --query "error" --from "2025-11-13T14:00:00" --to "2025-11-13T15:00:00"
|
|
6
|
-
|
|
7
4
|
require_relative '../lib/sumologic'
|
|
8
|
-
require 'optparse'
|
|
9
|
-
require 'json'
|
|
10
|
-
|
|
11
|
-
options = {
|
|
12
|
-
time_zone: 'UTC'
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
OptionParser.new do |opts|
|
|
16
|
-
opts.banner = <<~BANNER
|
|
17
|
-
Sumo Logic Query Tool - Fast, read-only log access
|
|
18
|
-
|
|
19
|
-
Usage: sumo-query [options]
|
|
20
|
-
|
|
21
|
-
Examples:
|
|
22
|
-
# Error timeline with 5-minute buckets
|
|
23
|
-
sumo-query --query 'error | timeslice 5m | count' \\
|
|
24
|
-
--from '2025-11-13T14:00:00' --to '2025-11-13T15:00:00'
|
|
25
|
-
|
|
26
|
-
# Search for specific text
|
|
27
|
-
sumo-query --query '"connection timeout"' \\
|
|
28
|
-
--from '2025-11-13T14:00:00' --to '2025-11-13T15:00:00' \\
|
|
29
|
-
--limit 100
|
|
30
|
-
|
|
31
|
-
# Filter by source category
|
|
32
|
-
sumo-query --query '_sourceCategory=prod/api error' \\
|
|
33
|
-
--from '2025-11-13T14:00:00' --to '2025-11-13T15:00:00' \\
|
|
34
|
-
--output results.json
|
|
35
|
-
|
|
36
|
-
Options:
|
|
37
|
-
BANNER
|
|
38
|
-
|
|
39
|
-
opts.on('-q', '--query QUERY', 'Search query (required)') { |v| options[:query] = v }
|
|
40
|
-
opts.on('-f', '--from TIME', 'Start time in ISO 8601 format (required)') { |v| options[:from] = v }
|
|
41
|
-
opts.on('-t', '--to TIME', 'End time in ISO 8601 format (required)') { |v| options[:to] = v }
|
|
42
|
-
opts.on('-z', '--time-zone TZ', 'Time zone (default: UTC)') { |v| options[:time_zone] = v }
|
|
43
|
-
opts.on('-l', '--limit N', Integer, 'Limit number of messages') { |v| options[:limit] = v }
|
|
44
|
-
opts.on('-o', '--output FILE', 'Output file (default: stdout)') { |v| options[:output] = v }
|
|
45
|
-
opts.on('-d', '--debug', 'Enable debug output') { $DEBUG = true }
|
|
46
|
-
opts.on('-h', '--help', 'Show this help') do
|
|
47
|
-
puts opts
|
|
48
|
-
exit
|
|
49
|
-
end
|
|
50
|
-
opts.on('-v', '--version', 'Show version') do
|
|
51
|
-
puts "sumologic-query v#{Sumologic::VERSION}"
|
|
52
|
-
exit
|
|
53
|
-
end
|
|
54
|
-
end.parse!
|
|
55
|
-
|
|
56
|
-
# Validate required options
|
|
57
|
-
unless options[:query] && options[:from] && options[:to]
|
|
58
|
-
warn 'Error: --query, --from, and --to are required'
|
|
59
|
-
warn 'Run with --help for usage information'
|
|
60
|
-
exit 1
|
|
61
|
-
end
|
|
62
5
|
|
|
63
6
|
begin
|
|
64
|
-
|
|
65
|
-
client = Sumologic::Client.new
|
|
66
|
-
|
|
67
|
-
warn "Querying Sumo Logic: #{options[:from]} to #{options[:to]}"
|
|
68
|
-
warn "Query: #{options[:query]}"
|
|
69
|
-
warn 'This may take 1-3 minutes depending on data volume...'
|
|
70
|
-
$stderr.puts
|
|
71
|
-
|
|
72
|
-
# Execute search
|
|
73
|
-
results = client.search(
|
|
74
|
-
query: options[:query],
|
|
75
|
-
from_time: options[:from],
|
|
76
|
-
to_time: options[:to],
|
|
77
|
-
time_zone: options[:time_zone],
|
|
78
|
-
limit: options[:limit]
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
# Format output
|
|
82
|
-
output = {
|
|
83
|
-
query: options[:query],
|
|
84
|
-
from: options[:from],
|
|
85
|
-
to: options[:to],
|
|
86
|
-
time_zone: options[:time_zone],
|
|
87
|
-
message_count: results.size,
|
|
88
|
-
messages: results
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
json_output = JSON.pretty_generate(output)
|
|
92
|
-
|
|
93
|
-
# Write to file or stdout
|
|
94
|
-
if options[:output]
|
|
95
|
-
File.write(options[:output], json_output)
|
|
96
|
-
warn "\nResults saved to: #{options[:output]}"
|
|
97
|
-
warn "Message count: #{results.size}"
|
|
98
|
-
else
|
|
99
|
-
puts json_output
|
|
100
|
-
end
|
|
101
|
-
rescue Sumologic::AuthenticationError => e
|
|
102
|
-
warn "\nAuthentication Error: #{e.message}"
|
|
103
|
-
warn "\nPlease set environment variables:"
|
|
104
|
-
warn " export SUMO_ACCESS_ID='your_access_id'"
|
|
105
|
-
warn " export SUMO_ACCESS_KEY='your_access_key'"
|
|
106
|
-
warn " export SUMO_DEPLOYMENT='us2' # Optional, defaults to us2"
|
|
107
|
-
exit 1
|
|
7
|
+
Sumologic::CLI.start(ARGV)
|
|
108
8
|
rescue Sumologic::TimeoutError => e
|
|
109
9
|
warn "\nTimeout Error: #{e.message}"
|
|
110
10
|
warn "\nTry:"
|
|
@@ -115,6 +15,10 @@ rescue Sumologic::TimeoutError => e
|
|
|
115
15
|
rescue Sumologic::Error => e
|
|
116
16
|
warn "\nError: #{e.message}"
|
|
117
17
|
exit 1
|
|
18
|
+
rescue StandardError => e
|
|
19
|
+
warn "\nUnexpected Error: #{e.message}"
|
|
20
|
+
warn e.backtrace.first(5).join("\n") if $DEBUG
|
|
21
|
+
exit 1
|
|
118
22
|
rescue Interrupt
|
|
119
23
|
warn "\nInterrupted by user"
|
|
120
24
|
exit 130
|