Skip to content

Commit 3316486

Browse files
committed
Fix modeling of fired alerts.
1 parent ba37215 commit 3316486

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

examples/fired_alerts.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ def main():
2626
opts = parse(sys.argv[1:], {}, ".splunkrc")
2727
service = connect(**opts.kwargs)
2828

29-
for item in service.fired_alerts:
30-
print "[%s]" % item.name
31-
content = item.content
32-
for key in sorted(content.keys()):
33-
value = content[key]
34-
print "%s: %s" % (key, value)
35-
print
29+
for group in service.fired_alerts:
30+
header = "%s (count: %d)" % (group.name, group.count)
31+
print "%s" % header
32+
print '='*len(header)
33+
alerts = group.alerts
34+
for alert in alerts.list():
35+
content = alert.content
36+
for key in sorted(content.keys()):
37+
value = content[key]
38+
print "%s: %s" % (key, value)
39+
print
3640

3741
if __name__ == "__main__":
3842
main()

splunklib/client.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def event_types(self):
151151

152152
@property
153153
def fired_alerts(self):
154-
return Collection(self, PATH_FIRED_ALERTS)
154+
return Collection(self, PATH_FIRED_ALERTS, item=AlertGroup)
155155

156156
@property
157157
def indexes(self):
@@ -371,7 +371,7 @@ def itemmeta(self):
371371
}
372372

373373
# kwargs: count, offset, search, sort_dir, sort_key, sort_mode
374-
def list(self, count=0, **kwargs):
374+
def list(self, count=-1, **kwargs):
375375
response = self.get(count=count, **kwargs)
376376
return self._load_list(response)
377377

@@ -396,6 +396,22 @@ def submit(self, stanza):
396396
self.service.request(self.path, message)
397397
return self
398398

399+
class AlertGroup(Entity):
400+
"""An entity that represents a group of fired alerts that can be accessed
401+
through the `alerts` property."""
402+
def __init__(self, service, path, **kwargs):
403+
Entity.__init__(self, service, path, **kwargs)
404+
405+
@property
406+
def alerts(self):
407+
"""Returns a collection of triggered alert instances."""
408+
return Collection(self.service, self.path)
409+
410+
@property
411+
def count(self):
412+
"""Returns the count of triggered alerts."""
413+
return int(self.content.triggered_alert_count)
414+
399415
class Index(Entity):
400416
"""Index class access to specific operations."""
401417
def __init__(self, service, path, **kwargs):
@@ -542,7 +558,7 @@ def list(self, *args):
542558
for kind in kinds:
543559
response = None
544560
try:
545-
response = self.service.get(self.kindpath(kind), count=0)
561+
response = self.service.get(self.kindpath(kind), count=-1)
546562
except HTTPError as e:
547563
if e.status == 404:
548564
continue # No inputs of this kind
@@ -656,6 +672,9 @@ def create(self, query, **kwargs):
656672
sid = _load_sid(response)
657673
return Job(self.service, PATH_JOBS + sid)
658674

675+
def list(self, count=0, **kwargs):
676+
return Collection.list(self, count, **kwargs)
677+
659678
class Message(Entity):
660679
def __init__(self, service, name, **kwargs):
661680
Entity.__init__(self, service, _path(PATH_MESSAGES, name), **kwargs)

tests/splunklib.client.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
['Collection', 'Conf', 'Confs', 'Context', 'Endpoint', 'Entity', 'HTTPError', 'INPUT_KINDMAP', 'Index', 'Input', 'Inputs', 'Job', 'Jobs', 'MATCH_ENTRY_CONTENT', 'Message', 'NotSupportedError', 'OperationError', 'PATH_APPS', 'PATH_CAPABILITIES', 'PATH_CONF', 'PATH_CONFS', 'PATH_EVENT_TYPES', 'PATH_FIRED_ALERTS', 'PATH_INDEXES', 'PATH_INPUTS', 'PATH_JOBS', 'PATH_LOGGER', 'PATH_MESSAGES', 'PATH_ROLES', 'PATH_SAVED_SEARCHES', 'PATH_STANZA', 'PATH_USERS', 'SavedSearch', 'SavedSearches', 'Service', 'Settings', 'Stanza', 'Users', 'XNAMEF_ATOM', 'XNAME_CONTENT', 'XNAME_ENTRY', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_filter_content', '_load_atom', '_load_atom_entries', '_load_sid', '_parse_atom_entry', '_path', '_path_conf', 'connect', 'data', 'quote', 'record', 'sleep', 'urlencode']
1+
['AlertGroup', 'Collection', 'Conf', 'Confs', 'Context', 'Endpoint', 'Entity', 'HTTPError', 'INPUT_KINDMAP', 'Index', 'Input', 'Inputs', 'Job', 'Jobs', 'MATCH_ENTRY_CONTENT', 'Message', 'NotSupportedError', 'OperationError', 'PATH_APPS', 'PATH_CAPABILITIES', 'PATH_CONF', 'PATH_CONFS', 'PATH_EVENT_TYPES', 'PATH_FIRED_ALERTS', 'PATH_INDEXES', 'PATH_INPUTS', 'PATH_JOBS', 'PATH_LOGGER', 'PATH_MESSAGES', 'PATH_ROLES', 'PATH_SAVED_SEARCHES', 'PATH_STANZA', 'PATH_USERS', 'SavedSearch', 'SavedSearches', 'Service', 'Settings', 'Stanza', 'Users', 'XNAMEF_ATOM', 'XNAME_CONTENT', 'XNAME_ENTRY', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_filter_content', '_load_atom', '_load_atom_entries', '_load_sid', '_parse_atom_entry', '_path', '_path_conf', 'connect', 'data', 'quote', 'record', 'sleep', 'urlencode']
22

tests/test_fired_alert.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,11 @@ def test_crud(self):
117117
wait(search, lambda search: alert_count(search) == count)
118118
self.assertEqual(alert_count(search), count)
119119

120-
# And now .. after all that trouble, verify that we see the
121-
# expected alerts!
120+
# And now .. after all that trouble, verify that we see the alerts!
122121
self.assertTrue(search_name in fired_alerts)
123-
alerts = fired_alerts[search_name]
124-
self.assertEqual(alerts.name, search_name)
125-
actual = int(alerts.content.triggered_alert_count)
126-
self.assertEqual(actual, count)
122+
alert_group = fired_alerts[search_name]
123+
self.assertEqual(alert_group.name, search_name)
124+
self.assertEqual(alert_group.count, count)
127125

128126
# Cleanup
129127
searches.delete(search_name)
@@ -132,10 +130,11 @@ def test_crud(self):
132130

133131
def test_read(self):
134132
service = client.connect(**opts.kwargs)
135-
fired_alerts = service.fired_alerts
136133

137-
for fired_alert in fired_alerts:
138-
fired_alert.content
134+
for alert_group in service.fired_alerts:
135+
alert_group.count
136+
for alert in alert_group.alerts:
137+
alert.content
139138

140139
if __name__ == "__main__":
141140
opts = parse(sys.argv[1:], {}, ".splunkrc")

0 commit comments

Comments
 (0)