-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynology_activebackuplogs-example-1.py
More file actions
72 lines (58 loc) · 1.9 KB
/
synology_activebackuplogs-example-1.py
File metadata and controls
72 lines (58 loc) · 1.9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# This example will return the ERRORs from the last hour
import datetime
# TRMM snippet for production
# {{synology_activebackuplogs_snippet.py}}
# Dev
import synology_activebackuplogs_snippet
def main():
# timedelta docs: https://docs.python.org/3/library/datetime.html#timedelta-objects
# Note: "years" is not valid. Use "days=365" to represent one year.
# Values include:
# weeks
# days
# hours
# minutes
# seconds
after = datetime.timedelta(hours=1)
# Production using TRMM snippet
# logs = SynologyActiveBackupLogs(
# Development
logs = synology_activebackuplogs_snippet.SynologyActiveBackupLogs(
# Search logs within the period specified.
# timedelta() will be off by 1 minute because 1 minute is added to detect if the log entry is last year vs.
# this year. This should be negligible.
after=after,
# Use different log location
# log_path="custom/log/path",
# Use different filename globbing
# filename_glob="log.txt*",
)
# Load the log entries
logs.load()
# Search for entries that match the criteria.
find = {
'priority': 'ERROR',
}
found = logs.search(find=find)
if not found:
# The timestamp above is not
ts = (datetime.datetime.now() - after).strftime("%Y-%m-%d %X")
print(f"No log entries found since {ts}")
return
# Check for errors
errors_found = False
# Print the log events
for event in found:
ts = event["datetime"].strftime("%Y-%m-%d %X")
print(f"{event['priority']}: {ts}: {event['method_name']} {event['message']}")
errors_found = True
if errors_found:
# Errors found. Exit with failure
exit(1)
else:
# No errors found. Exit successful
exit(0)
# Main entrance here...
if __name__ == '__main__':
main()