-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7327 from freedomofpress/stg-cleanup-ossec
Clean out old OSSEC diff and state files
- Loading branch information
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
securedrop/debian/config/lib/systemd/system/securedrop-cleanup-ossec.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[Unit] | ||
Description=Cleanup OSSEC diff queue | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/usr/bin/securedrop-cleanup-ossec.py | ||
User=root |
10 changes: 10 additions & 0 deletions
10
securedrop/debian/config/lib/systemd/system/securedrop-cleanup-ossec.timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[Unit] | ||
Description=Cleanup OSSEC diff queue | ||
|
||
[Timer] | ||
OnCalendar=daily | ||
Persistent=true | ||
RandomizedDelaySec=5m | ||
|
||
[Install] | ||
WantedBy=timers.target |
33 changes: 33 additions & 0 deletions
33
securedrop/debian/config/usr/bin/securedrop-cleanup-ossec.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Delete OSSEC diff/state files older than a year | ||
Runs as root on both app and mon servers | ||
""" | ||
|
||
import os | ||
import re | ||
from datetime import datetime, timedelta | ||
|
||
OSSEC_DIFFS = "/var/ossec/queue/diff/local/" | ||
KEEP_DAYS = 365 | ||
# Match e.g. state.1667271785 | ||
RE_REMOVE = re.compile(r"^(state|diff)\.\d+$") | ||
|
||
|
||
def main() -> None: | ||
cutoff_date = datetime.now() - timedelta(days=KEEP_DAYS) | ||
|
||
for root, dirs, files in os.walk(OSSEC_DIFFS): | ||
for file in files: | ||
if RE_REMOVE.match(file): | ||
file_path = os.path.join(root, file) | ||
modified_time = os.path.getmtime(file_path) | ||
file_modified_date = datetime.fromtimestamp(modified_time) | ||
if file_modified_date < cutoff_date: | ||
os.remove(file_path) | ||
print(f"Deleted file: {file_path} (Last modified: {file_modified_date})") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters