Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
Log MatomNom
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
3
Issues
3
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libre
Log MatomNom
Commits
579cae8f
Commit
579cae8f
authored
Nov 14, 2020
by
Michał Woźniak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP: initial implementation of the main loop (with some stubs here and there)
parent
00590590
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
26 deletions
+69
-26
logwatch.py
logwatch.py
+69
-26
No files found.
logwatch.py
View file @
579cae8f
...
...
@@ -8,11 +8,8 @@ import sys
import
os.path
from
glob
import
glob
from
enum
import
Enum
class
states
(
Enum
):
NEW
=
1
OPEN
=
2
READY
=
3
import
time
import
fnmatch
class
Configuration
(
import_logs
.
Configuration
):
...
...
@@ -23,7 +20,7 @@ class Configuration(import_logs.Configuration):
'--ingestion-start-delay'
,
dest
=
'ingestion_start_delay'
,
type
=
'int'
,
default
=
2
00
,
default
=
10
00
,
help
=
"Delay (in ms) between noticing a logfile to be processed and starting ingesting it."
"This is part of the built-in heuristic for determining that a file is not being modified "
"or moved anymore and can be safely ingested."
...
...
@@ -39,7 +36,7 @@ class Configuration(import_logs.Configuration):
if
not
os
.
path
.
isdir
(
f
):
raise
IOError
(
"Path is not a directory: "
%
f
)
sanitized_filenames
.
append
(
f
)
self
.
filenames
=
sanitized_filenames
self
.
filenames
=
list
(
set
(
sanitized_filenames
))
if
len
(
self
.
filenames
)
==
0
:
raise
Exception
(
"No existing directories to watch!"
)
...
...
@@ -50,28 +47,74 @@ watch_flags = flags.CREATE | flags.MODIFY | flags.DELETE_SELF | flags.CLOSE_WRIT
# files to process
logfile_glob
=
"*.log"
logfiles
=
[]
# array of dicts, each with "path", "state"
logfiles_busy
=
[]
# this is where newly discovered logfiles are added
logfiles_free
=
[]
# logfiles which we can assume are not busy
# set up watches
print
(
"+-- watching:"
)
# active watches
watches
=
{}
for
path
in
config
.
filenames
:
print
(
" - %s"
%
path
)
# set up watches
wd
=
inotify
.
add_watch
(
path
,
watch_flags
)
watches
[
wd
]
=
path
# glob any files already existing in the directory
for
p
in
glob
(
os
.
path
.
join
(
path
,
logfile_glob
)):
logfiles
.
append
({
"path"
:
p
,
"state"
:
states
.
NEW
})
print
(
logfiles
)
# set up watches
def
setup_watches
():
global
watches
global
logfiles_busy
print
(
" +-- watching:"
)
for
path
in
config
.
filenames
:
print
(
" - %s"
%
path
)
# set up watches
wd
=
inotify
.
add_watch
(
path
,
watch_flags
)
watches
[
wd
]
=
path
# glob any files already existing in the directory
for
p
in
glob
(
os
.
path
.
join
(
path
,
logfile_glob
)):
if
p
not
in
logfiles_busy
:
logfiles_busy
.
append
(
p
)
print
(
"+-- setting up watches..."
)
setup_watches
()
if
len
(
logfiles_busy
)
>
0
:
print
(
"+-- initial logfiles:"
)
for
f
in
logfiles_busy
:
print
(
" - %s"
%
f
)
# And see the corresponding events:
timestamp
=
time
.
time
()
time
.
sleep
(
config
.
options
.
ingestion_start_delay
/
1000
)
while
True
:
print
(
"+-- loop..."
)
for
event
in
inotify
.
read
(
config
.
options
.
ingestion_start_delay
):
print
(
' +-- event! %s: %s'
%
(
os
.
path
.
join
(
watches
[
event
.
wd
],
event
.
name
),
', '
.
join
([
str
(
f
)
for
f
in
flags
.
from_mask
(
event
.
mask
)])))
new_timestamp
=
time
.
time
()
print
(
"+-- loop... %0.5fs"
%
(
new_timestamp
-
timestamp
))
timestamp
=
new_timestamp
logfiles_free
=
list
(
set
(
logfiles_free
+
logfiles_busy
))
logfiles_busy
=
[]
for
event
in
inotify
.
read
(
config
.
options
.
ingestion_start_delay
,
config
.
options
.
ingestion_start_delay
):
if
event
.
wd
not
in
watches
:
continue
f
=
os
.
path
.
join
(
watches
[
event
.
wd
],
event
.
name
)
print
(
' +-- event! %s: %s'
%
(
f
,
', '
.
join
([
str
(
flag
)
for
flag
in
flags
.
from_mask
(
event
.
mask
)])))
if
f
in
logfiles_free
:
logfiles_free
.
remove
(
f
)
logfiles_busy
.
append
(
f
)
print
(
" - busy..."
)
elif
f
not
in
logfiles_busy
:
if
fnmatch
.
fnmatch
(
f
,
logfile_glob
):
print
(
" - new logfile spotted!"
)
logfiles_busy
.
append
(
f
)
if
len
(
logfiles_free
)
>
0
:
print
(
" +-- free logfiles:"
)
for
f
in
logfiles_free
:
print
(
" - %s"
%
f
)
print
(
" +-- removing watches"
)
for
wd
in
watches
.
items
():
print
(
" - %s"
%
wd
[
1
])
inotify
.
rm_watch
(
wd
[
0
])
watches
=
{}
print
(
" +-- simulating log processing of free logfiles..."
)
for
f
in
logfiles_free
:
print
(
" - %s"
%
f
)
time
.
sleep
(
config
.
options
.
ingestion_start_delay
/
200
)
logfiles_free
=
[]
setup_watches
()
time
.
sleep
(
config
.
options
.
ingestion_start_delay
/
1000
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment