Skip to content

Commit 32a8561

Browse files
get events
1 parent 7278ded commit 32a8561

File tree

8 files changed

+108
-21
lines changed

8 files changed

+108
-21
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,20 @@ Return the list of data retention intervals, with beginning and end UTC time for
151151
**Success Return Value**
152152
A dictionary containing the list of available sampling intervals.
153153
**Example**
154-
[examples/print_data_retention_info.py](examples/print_data_retention_info.py).
154+
[examples/print_data_retention_info.py](examples/print_data_retention_info.py).
155+
156+
#### `get_events(self, name=None, from_ts=None, to_ts=None, tags=None)`
157+
**Description**
158+
Returns the list of Sysdig Cloud events.
159+
**Arguments**
160+
- **name**: filter events by name.
161+
- **from_ts**: filter events created after `from_ts`.
162+
- **to_ts**: filter events created before `to_ts`.
163+
- **tags**: filter events by tags. Can be, for example `tag1 = 'value1'`
164+
**Success Return Value**
165+
A dictionary containing the list of events.
166+
**Example**
167+
[examples/list_events.py](examples/list_events.py).
155168

156169
#### `get_explore_grouping_hierarchy(self)`
157170
**Description**
@@ -185,7 +198,7 @@ A dictionary containing information about the user, for example its email and th
185198
**Example**
186199
[examples/print_user_info.py](examples/print_user_info.py).
187200

188-
#### `post_event(self, name, description='', severity=6, host='', tags={})`
201+
#### `post_event(self, name, description=None, severity=6, host=None, tags=None)`
189202
**Description**
190203
You can use this method you use to send an event to Sysdig Cloud. The events you post are available in the Events tab in the Sysdig Cloud UI and can be overlied to charts.
191204
**Arguments**

examples/add_notification_email.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
print 'Recipient added successfully'
3737
else:
3838
print res[1]
39+
sys.exit(1)

examples/create_alert.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
#
4444
# Validate a print the results.
4545
#
46-
if res[0]:
47-
data = res[1]
48-
else:
49-
print res[1]
46+
print res[1]
47+
if not res[0]:
5048
sys.exit(1)
51-
52-
print res

examples/create_dashboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@
5858
print 'Dashboard copied successfully'
5959
else:
6060
print res[1]
61-
61+
sys.exit(1)

examples/get_data_advanced.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@
6464
#
6565
# Show the result!
6666
#
67-
if res[0]:
68-
data = res[1]
69-
else:
70-
print res[1]
67+
print res[1]
68+
if not res[0]:
7169
sys.exit(1)
72-
73-
print data

examples/list_events.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get user events from Sysdig Cloud
4+
#
5+
6+
import os
7+
import sys
8+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
9+
from sdcclient import SdcClient
10+
11+
#
12+
# Parse arguments
13+
#
14+
# if len(sys.argv) < 4:
15+
# print 'usage: %s <sysdig-token> name description [severity]' % sys.argv[0]
16+
# print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
17+
# sys.exit(1)
18+
19+
sdc_token = sys.argv[1]
20+
21+
#
22+
# Instantiate the SDC client
23+
#
24+
sdclient = SdcClient(sdc_token)
25+
26+
#
27+
# Get the entire list of events
28+
#
29+
res = sdclient.get_events()
30+
31+
print res[1]
32+
if not res[0]:
33+
sys.exit(1)
34+
35+
#
36+
# Get the events that match a period in time
37+
#
38+
res = sdclient.get_events(from_ts=1460365211, to_ts=1460465211)
39+
40+
print res[1]
41+
if not res[0]:
42+
sys.exit(1)
43+
44+
#
45+
# Get the events that match a name
46+
#
47+
res = sdclient.get_events(name='test event')
48+
49+
print res[1]
50+
if not res[0]:
51+
sys.exit(1)
52+
53+
#
54+
# Get the events that match a tag/value pair
55+
#
56+
res = sdclient.get_events(tags="tag1 = 'value1'")
57+
58+
print res[1]
59+
if not res[0]:
60+
sys.exit(1)

examples/post_event.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
#
3333
# Post the event
3434
#
35-
res = sdclient.post_event(name, description, severity)
35+
res = sdclient.post_event(name, description, severity, tags={"tag1" : "value1"})
3636

3737
#
3838
# Return the result
3939
#
4040
if res[0]:
4141
print 'Event Posted Successfully'
4242
else:
43-
print res[1]
43+
print res[1]
44+
sys.exit(1)

sdcclient/_client.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,28 +496,48 @@ def delete_dashboard(self, dashname):
496496
"key2": "value2",
497497
"key3": "value3"}
498498
'''
499-
def post_event(self, name, description='', severity=6, host='', tags={}):
499+
def post_event(self, name, description=None, severity=6, host=None, tags=None):
500500
edata = {
501501
'event': {
502502
'name': name,
503503
'severity': severity
504504
}
505505
}
506506

507-
if description != '':
507+
if description is not None:
508508
edata['event']['description'] = description
509509

510-
if host != '':
510+
if host is not None:
511511
edata['event']['host'] = host
512512

513-
if tags != {}:
513+
if tags is not None:
514514
edata['event']['tags'] = tags
515515

516516
r = requests.post(self.url + '/api/events/', headers=self.hdrs, data=json.dumps(edata))
517517
if not self.__checkResponse(r):
518518
return [False, self.lasterr]
519519
return [True, r.json()]
520520

521+
def get_events(self, name=None, from_ts=None, to_ts=None, tags=None):
522+
params = {}
523+
524+
if name is not None:
525+
params['name'] = name
526+
527+
if from_ts is not None:
528+
params['from'] = from_ts
529+
530+
if to_ts is not None:
531+
params['to'] = to_ts
532+
533+
if tags is not None:
534+
params['tags'] = tags
535+
536+
r = requests.get(self.url + '/api/events/', headers=self.hdrs, params=params)
537+
if not self.__checkResponse(r):
538+
return [False, self.lasterr]
539+
return [True, r.json()]
540+
521541
def get_data(self, metrics, start_ts, end_ts=0, sampling_s=0,
522542
filter='', datasource_type='host'):
523543
reqbody = {

0 commit comments

Comments
 (0)