7
7
8
8
from file_read_backwards import FileReadBackwards
9
9
from json import loads , dumps
10
+ import os
10
11
11
12
class PSConfigRunMetrics :
12
13
guid = None
@@ -99,103 +100,106 @@ def tasks_log_file(self):
99
100
def find_guid (self ):
100
101
guid = None
101
102
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
112
114
return guid
113
-
114
115
115
116
def run_metrics (self , guid_match = '.+?' ):
116
117
stats = PSConfigRunMetrics (self .agent_name )
117
118
118
119
#Get guid, pid start and end info from agent log
119
120
run_end_regex = re .compile (f'^(.+) INFO pid=(.+?) prog=.+? line=.+? guid=({ guid_match } ) msg=Agent completed running$' )
120
121
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 :
134
128
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\.\.\.$' )
140
142
141
143
#Check if we found a guid, return if not
142
144
if stats .guid is None :
143
145
return stats
144
146
145
147
#If we have guid, calculate stats
146
148
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
178
181
179
182
return stats
180
183
181
184
def get_tasks (self , guid , print_func = None ):
182
185
tasks = []
183
186
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