Skip to content

Commit 85a9f39

Browse files
committed
post event clean up and simple version
1 parent e54ded0 commit 85a9f39

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ You can use this method you use to send an event to Sysdig Cloud. The events you
294294
**Success Return Value**
295295
A dictionary describing the new event.
296296
**Example**
297+
[examples/post_event_simple.py](examples/post_event_simple.py)
297298
[examples/post_event.py](examples/post_event.py).
298299

299300
#### `update_notification_resolution(self, notification, resolved)`

examples/post_event.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,37 @@
66
import os
77
import sys
88
import json
9+
import argparse
910
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1011
from sdcclient import SdcClient
1112

1213
#
1314
# Parse arguments
1415
#
15-
if len(sys.argv) < 3:
16-
print 'usage: %s <sysdig-token> <name> [description] [severity] [scope] [tags]' % sys.argv[0]
17-
print ' sysdig-token: You can find your token at https://app.sysdigcloud.com/#/settings/user'
18-
print ' severity: syslog style from 0 (high) to 7 (low)'
19-
print ' scope: metadata, in Sysdig Cloud format, of nodes to associate with the event, eg: \'host.hostName = "ip-10-1-1-1" and container.name = "foo"\''
20-
print ' tags: dictionary of arbitrary key-value pairs, eg: \'{"app":"my_app", "file":"text.py"}\''
21-
sys.exit(1)
22-
23-
sdc_token = sys.argv[1]
24-
name = sys.argv[2]
25-
kwargs = {}
26-
27-
if len(sys.argv) > 3:
28-
kwargs['description'] = sys.argv[3]
29-
30-
if len(sys.argv) > 4:
31-
kwargs['severity'] = int(sys.argv[4])
32-
33-
if len(sys.argv) > 5:
34-
kwargs['event_filter'] = sys.argv[5]
16+
# Usage: post_event.py [-h] [-d DESCRIPTION] [-s SEVERITY] [-c SCOPE] [-t TAGS] sysdig_token name
17+
#
18+
parser = argparse.ArgumentParser()
19+
parser.add_argument('-d','--description')
20+
parser.add_argument('-s','--severity', help='syslog style from 0 (high) to 7 (low)')
21+
parser.add_argument('-c','--scope', help='metadata, in Sysdig Cloud format, of nodes to associate with the event, eg: \'host.hostName = "ip-10-1-1-1" and container.name = "foo"\'')
22+
parser.add_argument('-t','--tags', help='dictionary of arbitrary key-value pairs, eg: \'{"app":"my_app", "file":"text.py"}\'')
23+
parser.add_argument('sysdig_token', help='You can find your token at https://app.sysdigcloud.com/#/settings/user')
24+
parser.add_argument('name')
25+
args = parser.parse_args()
3526

36-
if len(sys.argv) > 6:
37-
kwargs['tags'] = json.loads(sys.argv[6])
27+
tags=None
28+
if args.tags:
29+
tags=json.loads(args.tags)
3830

3931
#
4032
# Instantiate the SDC client
4133
#
42-
sdclient = SdcClient(sdc_token)
34+
sdclient = SdcClient(args.sysdig_token)
4335

4436
#
4537
# Post the event using post_event(self, name, description=None, severity=None, event_filter=None, tags=None)
4638
#
47-
res = sdclient.post_event(name, **kwargs)
39+
res = sdclient.post_event(args.name, args.description, args.severity, args.scope, tags)
4840

4941
#
5042
# Return the result

examples/post_event_simple.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# Post a user event to 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+
name = sys.argv[2]
21+
description = sys.argv[3]
22+
23+
severity = 6
24+
if len(sys.argv) < 4:
25+
severity = int(sys.argv[4])
26+
27+
#
28+
# Instantiate the SDC client
29+
#
30+
sdclient = SdcClient(sdc_token)
31+
32+
#
33+
# Post the event
34+
#
35+
res = sdclient.post_event(name, description, severity, tags={"tag1" : "value1"})
36+
37+
#
38+
# Return the result
39+
#
40+
if res[0]:
41+
print 'Event Posted Successfully'
42+
else:
43+
print res[1]
44+
sys.exit(1)

0 commit comments

Comments
 (0)