forked from agnosticdev/Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_parser.py
More file actions
50 lines (35 loc) · 1.38 KB
/
Copy pathlog_parser.py
File metadata and controls
50 lines (35 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
#
# Python example of a function to use along side a unit test.
#
import os, sys
def keyword_occurrences_any(keywords_list, logfile_str):
occurrences = 0
logfile_lines = logfile_str.split('\n')
for line in logfile_lines:
if any(key in line for key in keywords_list):
occurrences += 1
return occurrences
def keyword_occurrences_for(keywords_list, logfile_str):
occurrences = 0
logfile_lines = logfile_str.split('\n')
for line in logfile_lines:
line_split = line.split(" ")
for word in line_split:
if word in keywords_list:
occurrences += 1
return occurrences
log_file = """
systemd[1]: Starting python Network Manager Script Dispatcher Service...
systemd[1]: Started Network Manager Script Dispatcher Service.
nm-dispatcher: req:1 'dhcp4-change' [enp4s0]: new request (1 scripts)
nm-dispatcher: req:1 'dhcp4-change' [enp4s0]: start running ordered scripts...
systemd[1]: Starting Clean python session files...
systemd[1]: Started Clean python session files.
CRON[8283]: (agnosticdev) CMD (cd / && run-parts --report /etc/cron.hourly)
"""
keywords = ['python', 'error', 'Network', 'failure']
matches = keyword_occurrences_any(keywords, log_file)
print("{0} matches found in the log file".format(matches))
matches = keyword_occurrences_for(keywords, log_file)
print("{0} matches found in the log file".format(matches))