Skip to content

Commit e42c93e

Browse files
committed
Fix issues with .children property and walk_chidren.py example
1 parent ae33597 commit e42c93e

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

examples/response/walk_children.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
# This function is called for every child of the given process
1010
def visitor(proc, depth):
1111
try:
12-
print("%s%s: %s" % (' '*(depth-1), proc.start, proc.cmdline))
12+
start_time = proc.get("start") or "<unknown>"
13+
end_time = proc.get("end") or "<unknown>"
14+
15+
print("%s%s -- %s: %s %s" % (' '*(depth + 1), start_time, end_time, proc.cmdline,
16+
"(suppressed)" if proc.suppressed_process else ""))
1317
except Exception as e:
1418
print("** Encountered error while walking children: {0:s}".format(str(e)))
1519

@@ -42,8 +46,15 @@ def main():
4246
return 2
4347

4448
for root_proc in procs:
45-
print("Process {0:s} on {1:s} executed by {2:s} children:".format(root_proc.path, root_proc.hostname,
49+
if not root_proc.terminated:
50+
duration = "still running"
51+
else:
52+
duration = str(root_proc.end - root_proc.start)
53+
54+
print("Process {0:s} on {1:s} executed by {2:s}:".format(root_proc.cmdline, root_proc.hostname,
4655
root_proc.username))
56+
print("started at {0} ({1})".format(str(root_proc.start), duration))
57+
print("Cb Response console link: {0}".format(root_proc.webui_link))
4758
root_proc.walk_children(visitor)
4859
print("")
4960

src/cbapi/response/models.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,20 @@ def start(self):
22132213
"""
22142214
return convert_from_solr(self._attribute('start', -1))
22152215

2216+
@property
2217+
def end(self):
2218+
"""
2219+
Returns the end time of the process (based on the last event received). If the process has not yet exited,
2220+
"end" will return None.
2221+
2222+
:return: datetime object of the last event received for the process, if it has terminated. Otherwise, None.
2223+
"""
2224+
if self.get("end") is not None:
2225+
return convert_from_solr(self._attribute('end', -1))
2226+
2227+
if self.get("terminated", False) == True and self.get("last_update") is not None:
2228+
return convert_from_solr(self._attribute('last_update', -1))
2229+
22162230
def require_events(self):
22172231
event_key_list = ['filemod_complete', 'regmod_complete', 'modload_complete', 'netconn_complete',
22182232
'crossproc_complete', 'childproc_complete']
@@ -2346,7 +2360,8 @@ def children(self):
23462360

23472361
if self._children_info is not None:
23482362
for i, child in enumerate(self._children_info):
2349-
yield CbChildProcEvent(self, convert_event_time(child.get("start") or "1970-01-01T00:00:00Z"), i,
2363+
timestamp = convert_event_time(child.get("start") or "1970-01-01T00:00:00Z")
2364+
yield CbChildProcEvent(self, timestamp, i,
23502365
{
23512366
"procguid": child["unique_id"],
23522367
"md5": child["process_md5"],
@@ -2784,12 +2799,15 @@ def process(self):
27842799
if path:
27852800
proc_data["path"] = path
27862801

2802+
proc_data["parent_unique_id"] = self.parent._model_unique_id
2803+
proc_data["parent_id"] = self.parent.id
2804+
27872805
try:
27882806
(sensor_id, proc_pid, proc_createtime) = parse_process_guid(self.parent.id)
2789-
proc_data["parent_unique_id"] = self.parent._model_unique_id
2790-
proc_data["parent_id"] = self.parent.id
2791-
proc_data["sensor_id"] = sensor_id
2792-
proc_data["start"] = proc_createtime
2807+
if "sensor_id" not in proc_data:
2808+
proc_data["sensor_id"] = sensor_id
2809+
if "start" not in proc_data:
2810+
proc_data["start"] = convert_to_solr(proc_createtime)
27932811
except Exception:
27942812
# silently fail if the GUID is not able to be parsed
27952813
pass

0 commit comments

Comments
 (0)