-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_system.py
More file actions
executable file
·187 lines (150 loc) · 6.21 KB
/
monitor_system.py
File metadata and controls
executable file
·187 lines (150 loc) · 6.21 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
import time
import argparse
import subprocess
import logging
from datetime import datetime, timedelta
import os
import re
import sys
import yaml
def load_config(config_path):
if not os.path.exists(config_path):
print(f"Config file not found: {config_path}", file=sys.stderr)
sys.exit(1)
with open(config_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
def setup_logger(log_file):
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[
logging.FileHandler(log_file),
]
)
return logging.getLogger(__name__)
def run(cmd):
try:
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=10)
return result.stdout.strip()
except Exception:
return ""
def get_memory_info():
free_out = run("free -b")
lines = free_out.splitlines()
if len(lines) < 3:
return "MEM: N/A | SWAP: N/A | CPU_LOAD: N/A"
mem = lines[1].split()
swap = lines[2].split()
load = run("cat /proc/loadavg").split()[0] if run("cat /proc/loadavg") else "?"
total_mem = int(mem[1])
used_mem = int(mem[2])
total_swap = int(swap[1])
used_swap = int(swap[2]) if len(swap) > 2 else 0
mem_pct = (used_mem / total_mem * 100) if total_mem > 0 else 0
swap_pct = (used_swap / total_swap * 100) if total_swap > 0 else 0
return (
f"MEM: {used_mem//1024//1024}M/{total_mem//1024//1024}M ({mem_pct:.1f}%) | "
f"SWAP: {used_swap//1024//1024}M/{total_swap//1024//1024}M ({swap_pct:.1f}%) | "
f"CPU_LOAD: {load}"
)
def get_kernel_events_since(prio, since_seconds):
cmd = f"journalctl -k -p {prio} -S -{since_seconds}s --no-pager --output=short-iso"
raw = run(cmd)
if not raw or "-- No entries --" in raw:
return ""
return raw
def get_system_journal_since(prio, since_seconds):
cmd = f"journalctl -p {prio} -S -{since_seconds}s --no-pager --output=short-iso"
raw = run(cmd)
if not raw or "-- No entries --" in raw:
return ""
lines = [line for line in raw.splitlines() if "kernel:" not in line]
return "\n".join(lines)
def check_critical_conditions(config, mem_info_str, kernel_events, journal_events):
critical = False
reasons = []
if config['resources']['enable_resource_check']:
mem_match = re.search(r"MEM:.*\((\d+\.\d+)%\)", mem_info_str)
swap_match = re.search(r"SWAP:.*\((\d+\.\d+)%\)", mem_info_str)
if mem_match:
mem_pct = float(mem_match.group(1))
if mem_pct >= config['resources']['mem_usage_percent_threshold']:
critical = True
reasons.append(f"Память: {mem_pct:.1f}%")
if swap_match:
swap_pct = float(swap_match.group(1))
if swap_pct >= config['resources']['swap_usage_percent_threshold']:
critical = True
reasons.append(f"Swap: {swap_pct:.1f}%")
if config['events']['enable_kernel_check'] and kernel_events.strip():
critical = True
reasons.append("Kernel event")
if config['events']['enable_journal_check'] and journal_events.strip():
critical = True
reasons.append("System event")
return critical, reasons
def generate_report(config, logger, mem_info, kernel_events, journal_events):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_path = config['report']['output_file'].replace(".txt", f"_{timestamp}.txt")
report = []
report.append(f"time: {datetime.now().isoformat()}")
report.append("")
report.append("Resource:")
report.append(mem_info)
report.append("")
prio = config['journalctl']['max_priority']
report.append(f"Kernel event (journalctl -k -p {prio}, last {config['monitoring']['interval_seconds']} sec.):")
if kernel_events:
report.extend([f" • {line}" for line in kernel_events.splitlines()])
else:
report.append(" — No event.")
report.append("")
report.append(f"System event (journalctl -p {prio}, last {config['monitoring']['interval_seconds']} sec.):")
if journal_events:
report.extend([f" • {line}" for line in journal_events.splitlines()])
else:
report.append(" — No event.")
report.append("")
full_text = "\n".join(report)
with open(report_path, "w") as f:
f.write(full_text)
logger.info(f"Report saved at: {report_path}")
def main():
parser = argparse.ArgumentParser(description="Monitoring resourses and logs")
parser.add_argument("-c", "--config", default="monitor_config.yaml", help="Path to yaml-config")
args = parser.parse_args()
config = load_config(args.config)
logger = setup_logger(config['report']['log_file'])
interval_sec = config['monitoring']['interval_seconds']
duration_min = config['monitoring']['duration_minutes']
end_time = (datetime.now() + timedelta(minutes=duration_min)) if duration_min > 0 else None
prio = config['journalctl']['max_priority']
try:
while True:
if end_time and datetime.now() >= end_time:
print("Monitoring was finished.")
break
mem_info = get_memory_info()
kernel_events = get_kernel_events_since(prio, interval_sec)
journal_events = get_system_journal_since(prio, interval_sec)
ts = datetime.now().strftime("%H:%M:%S")
print(f"[{ts}] {mem_info}")
if kernel_events:
for line in kernel_events.splitlines():
print(f"[{ts}] KERNEL: {line}")
if journal_events:
for line in journal_events.splitlines():
print(f"[{ts}] JOURNAL: {line}")
is_critical, reasons = check_critical_conditions(
config, mem_info, kernel_events, journal_events
)
if is_critical:
logger.info(f"Critical conditions was found: {', '.join(reasons)}")
generate_report(config, logger, mem_info, kernel_events, journal_events)
time.sleep(interval_sec)
except KeyboardInterrupt:
print("\nMonitoring was stopped.")
if __name__ == "__main__":
main()