Skip to content

Commit cea07b9

Browse files
committed
Merge branch '5.2.0' of https://github.com/perfsonar/psconfig into 5.2.0
2 parents d71e77e + 2eed1c6 commit cea07b9

File tree

2 files changed

+86
-78
lines changed

2 files changed

+86
-78
lines changed

psconfig/perfsonar-psconfig/bin/commands/stats

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import sys
5+
import os
56

67
from psconfig.utilities.cli import PSCONFIG_CLI_AGENTS, CLIUtil
78
from psconfig.utilities.metrics import PSConfigMetricCalculator
@@ -27,6 +28,9 @@ cli = CLIUtil()
2728
valid_agents = []
2829
agent = None
2930
for cli_agent in PSCONFIG_CLI_AGENTS:
31+
# check if agent is present
32+
if not os.path.isfile(cli_agent['command']):
33+
continue
3034
valid_agents.append(cli_agent['name'].lower())
3135
if cli_agent['name'].lower() == args.agent.lower():
3236
agent = cli_agent

psconfig/perfsonar-psconfig/psconfig/utilities/metrics.py

+82-78
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from file_read_backwards import FileReadBackwards
99
from json import loads, dumps
10+
import os
1011

1112
class PSConfigRunMetrics:
1213
guid = None
@@ -99,103 +100,106 @@ def tasks_log_file(self):
99100
def find_guid(self):
100101
guid = None
101102
guid_regex = re.compile('^.*guid=(.+?) msg=Agent completed running$')
102-
with FileReadBackwards(self.agent_log_file(), encoding="utf-8") as frb:
103-
while True:
104-
l = frb.readline()
105-
if not l:
106-
break
107-
108-
guid_match = guid_regex.match(l)
109-
if guid_match:
110-
guid = guid_match.group(1)
111-
break
103+
if os.path.isfile(self.agent_log_file()):
104+
with FileReadBackwards(self.agent_log_file(), encoding="utf-8") as frb:
105+
while True:
106+
l = frb.readline()
107+
if not l:
108+
break
109+
110+
guid_match = guid_regex.match(l)
111+
if guid_match:
112+
guid = guid_match.group(1)
113+
break
112114
return guid
113-
114115

115116
def run_metrics(self, guid_match='.+?'):
116117
stats = PSConfigRunMetrics(self.agent_name)
117118

118119
#Get guid, pid start and end info from agent log
119120
run_end_regex = re.compile(f'^(.+) INFO pid=(.+?) prog=.+? line=.+? guid=({guid_match}) msg=Agent completed running$')
120121
run_start_regex = None
121-
with FileReadBackwards(self.agent_log_file(), encoding="utf-8") as frb:
122-
while True:
123-
#Check end of file
124-
l = frb.readline()
125-
if not l:
126-
break
127-
128-
#Check end of run (we'll see this first since going backwards through)
129-
run_end_match = run_end_regex.match(l)
130-
if run_start_regex:
131-
run_start_match = run_start_regex.match(l)
132-
if run_start_match:
133-
stats.start = run_start_match.group(1)
122+
if os.path.isfile(self.agent_log_file()):
123+
with FileReadBackwards(self.agent_log_file(), encoding="utf-8") as frb:
124+
while True:
125+
#Check end of file
126+
l = frb.readline()
127+
if not l:
134128
break
135-
elif run_end_match:
136-
stats.end = run_end_match.group(1)
137-
stats.pid = run_end_match.group(2)
138-
stats.guid = run_end_match.group(3)
139-
run_start_regex = re.compile(f'^(.+) INFO pid={stats.pid} prog=.+? line=.+? guid={stats.guid} msg=Running agent\.\.\.$')
129+
130+
#Check end of run (we'll see this first since going backwards through)
131+
run_end_match = run_end_regex.match(l)
132+
if run_start_regex:
133+
run_start_match = run_start_regex.match(l)
134+
if run_start_match:
135+
stats.start = run_start_match.group(1)
136+
break
137+
elif run_end_match:
138+
stats.end = run_end_match.group(1)
139+
stats.pid = run_end_match.group(2)
140+
stats.guid = run_end_match.group(3)
141+
run_start_regex = re.compile(f'^(.+) INFO pid={stats.pid} prog=.+? line=.+? guid={stats.guid} msg=Running agent\.\.\.$')
140142

141143
#Check if we found a guid, return if not
142144
if stats.guid is None:
143145
return stats
144146

145147
#If we have guid, calculate stats
146148
run_ctx_regex = re.compile(f"^.+ INFO guid={stats.guid} (.+) task=.+$")
147-
with FileReadBackwards(self.tasks_log_file(), encoding="utf-8") as frb:
148-
while True:
149-
#end if no more lines
150-
l = frb.readline()
151-
if not l:
152-
break
153-
154-
#match run context fields
155-
run_ctx_match = run_ctx_regex.match(l)
156-
if not run_ctx_match:
157-
#skip if no match
158-
continue
159-
160-
# we have a match, let's do some math
161-
stats.total += 1
162-
163-
#figure out context fields (these vary, hence not in regex)
164-
ctx_map = {}
165-
for ctx_kv in re.findall(r'(\w+?)=(.+?)( |$)', run_ctx_match.group(1)):
166-
ctx_map[ctx_kv[0]] = ctx_kv[1]
167-
168-
#calculate categorical stats
169-
if ctx_map.get("config_src", None):
170-
curr_stat = stats.by_src.setdefault(ctx_map["config_src"], {'total': 0, 'by_url': {} })
171-
curr_stat['total'] += 1
172-
if ctx_map.get("config_url", None):
173-
curr_stat['by_url'].setdefault(ctx_map["config_url"], 0)
174-
curr_stat['by_url'][ctx_map["config_url"]] += 1
175-
elif ctx_map.get("config_file", None):
176-
curr_stat['by_url'].setdefault(ctx_map["config_file"], 0)
177-
curr_stat['by_url'][ctx_map["config_file"]] += 1
149+
if os.path.isfile(self.tasks_log_file()):
150+
with FileReadBackwards(self.tasks_log_file(), encoding="utf-8") as frb:
151+
while True:
152+
#end if no more lines
153+
l = frb.readline()
154+
if not l:
155+
break
156+
157+
#match run context fields
158+
run_ctx_match = run_ctx_regex.match(l)
159+
if not run_ctx_match:
160+
#skip if no match
161+
continue
162+
163+
# we have a match, let's do some math
164+
stats.total += 1
165+
166+
#figure out context fields (these vary, hence not in regex)
167+
ctx_map = {}
168+
for ctx_kv in re.findall(r'(\w+?)=(.+?)( |$)', run_ctx_match.group(1)):
169+
ctx_map[ctx_kv[0]] = ctx_kv[1]
170+
171+
#calculate categorical stats
172+
if ctx_map.get("config_src", None):
173+
curr_stat = stats.by_src.setdefault(ctx_map["config_src"], {'total': 0, 'by_url': {} })
174+
curr_stat['total'] += 1
175+
if ctx_map.get("config_url", None):
176+
curr_stat['by_url'].setdefault(ctx_map["config_url"], 0)
177+
curr_stat['by_url'][ctx_map["config_url"]] += 1
178+
elif ctx_map.get("config_file", None):
179+
curr_stat['by_url'].setdefault(ctx_map["config_file"], 0)
180+
curr_stat['by_url'][ctx_map["config_file"]] += 1
178181

179182
return stats
180183

181184
def get_tasks(self, guid, print_func=None):
182185
tasks = []
183186
task_regex = re.compile(f"^.+ INFO guid={guid} .+ task=(.+)$")
184-
with FileReadBackwards(self.tasks_log_file(), encoding="utf-8") as frb:
185-
while True:
186-
l = frb.readline()
187-
if not l:
188-
break
189-
190-
task_match = task_regex.match(l)
191-
if task_match:
192-
task_json = task_match.group(1)
193-
try:
194-
tasks.append(loads(task_json))
195-
except Exception as e:
196-
if print_func:
197-
print_func("Error parsing task: {}".format(str(e)))
198-
elif tasks:
199-
#no use in looking, we reached end of guid
200-
break
201-
return tasks
187+
if os.path.isfile(self.tasks_log_file()):
188+
with FileReadBackwards(self.tasks_log_file(), encoding="utf-8") as frb:
189+
while True:
190+
l = frb.readline()
191+
if not l:
192+
break
193+
194+
task_match = task_regex.match(l)
195+
if task_match:
196+
task_json = task_match.group(1)
197+
try:
198+
tasks.append(loads(task_json))
199+
except Exception as e:
200+
if print_func:
201+
print_func("Error parsing task: {}".format(str(e)))
202+
elif tasks:
203+
#no use in looking, we reached end of guid
204+
break
205+
return tasks

0 commit comments

Comments
 (0)