syslog: split process name into seperate column, bugfix export while here as well (when limit equals 0, dump all data)

This commit is contained in:
Ad Schellevis 2020-07-20 11:37:05 +02:00
parent 78c81babfd
commit b0acd180ac
5 changed files with 32 additions and 13 deletions

View File

@ -81,7 +81,7 @@ class LogController extends ApiControllerBase
$this->response->setRawHeader("Content-Type: text/csv");
$this->response->setRawHeader("Content-Disposition: attachment; filename=" . $scope . ".log");
foreach (json_decode($response, true)['rows'] as $row) {
printf("%s\t%s\n", $row['timestamp'], $row['line']);
printf("%s\t%s\t%s\n", $row['timestamp'], $row['process_name'], $row['line']);
}
return;
}

View File

@ -78,6 +78,7 @@
<tr>
<th data-column-id="pos" data-type="numeric" data-identifier="true" data-visible="false">#</th>
<th data-column-id="timestamp" data-width="11em" data-type="string">{{ lang._('Date') }}</th>
<th data-column-id="process_name" data-width="2em" data-type="string">{{ lang._('Process') }}</th>
<th data-column-id="line" data-type="string">{{ lang._('Line') }}</th>
</tr>
</thead>

View File

@ -63,6 +63,12 @@ class BaseLogFormat:
"""
return line
@staticmethod
def process_name(line):
""" Return process name
"""
return ""
class FormatContainer:
def __init__(self, filename):

View File

@ -48,10 +48,16 @@ class SysLogFormat(BaseLogFormat):
@staticmethod
def line(line):
# strip timestamp from log line
# parse [date] [hostname] [process_name] [line] format
response = line[16:]
# strip hostname from log line
return response[response.find(' ')+1:].strip()
tmp = response.find(':')
return response[tmp+1:].strip() if tmp > -1 else response[response.find(' ')+1:].strip()
@staticmethod
def process_name(line):
response = line[16:]
tmp = response.find(':')
return response[:tmp].strip().split()[-1] if tmp > -1 else ""
class SysLogFormatEpoch(BaseLogFormat):

View File

@ -88,22 +88,28 @@ if __name__ == '__main__':
filename = fetch_clog(log_filename)
except Exception as e:
filename = log_filename
for record in reverse_log_reader(filename):
if record['line'] != "" and filter_regexp.match(('%s' % record['line']).lower()):
for rec in reverse_log_reader(filename):
if rec['line'] != "" and filter_regexp.match(('%s' % rec['line']).lower()):
result['total_rows'] += 1
if (len(result['rows']) < limit or limit == 0) and result['total_rows'] >= offset:
record['timestamp'] = None
record['parser'] = None
frmt = format_container.get_format(record['line'])
record = {
'timestamp': None,
'parser': None,
'process_name': ''
}
frmt = format_container.get_format(rec['line'])
if frmt:
record['timestamp'] = frmt.timestamp(record['line'])
record['line'] = frmt.line(record['line'])
record['timestamp'] = frmt.timestamp(rec['line'])
record['process_name'] = frmt.process_name(rec['line'])
record['line'] = frmt.line(rec['line'])
record['parser'] = frmt.name
else:
record['line'] = rec['line']
result['rows'].append(record)
elif result['total_rows'] > offset + limit:
elif limit > 0 and result['total_rows'] > offset + limit:
# do not fetch data until end of file...
break
if result['total_rows'] > offset + limit:
if limit > 0 and result['total_rows'] > offset + limit:
break
# output results