-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_check.py
72 lines (55 loc) · 2.27 KB
/
health_check.py
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
import psutil
import logging
import shutil
import os
from datetime import datetime , timedelta
#Logging kısmı için ayarlar + olarak 30 günde bir log doyasını yedekleyecek kodlar eklenmiştir
LOG_DIR = 'logs'
LOG_FILE = os.path.join(LOG_DIR, 'health_check.log')
BACKUP_DIR = os.path.join(LOG_DIR, 'backup')
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
logging.basicConfig(filename='logs/health_check.log' , level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')
def backup_logs():
current_time = datetime.now()
last_backup_time_file = os.path.join(LOG_DIR, 'last_backup_time.txt')
if os.path.exists(last_backup_time_file):
with open(last_backup_time_file, 'r') as file:
last_backup_time = datetime.strptime(file.read(), '%Y-%m-%d %H:%M:%S')
else:
last_backup_time = current_time - timedelta(days=31)
if (current_time - last_backup_time).days >= 30:
backup_filename = os.path.join(BACKUP_DIR, f'health_check_{current_time.strftime("%Y%m%d%H%M%S")}.log')
shutil.copy(LOG_FILE, backup_filename )
logging.info(f'Log dosyası yedeklendi: {backup_filename}')
with open(last_backup_time_file, 'w') as file:
file.write(current_time.strftime('%Y-%m-%d %H:%M:%S'))
def check_cpu_usage():
usage = psutil.cpu_percent(interval=1)
logging.info(f'CPU Usage: {usage}%')
if usage > 80:
warning_message = 'WARNING: CPU usage is over 80%'
logging.warning(warning_message)
def check_memory_usage():
memory = psutil.virtual_memory()
logging.info(f'Memory Usage: {memory.percent}%')
if memory.percent > 80:
warning_message = 'WARNING: Memory usage is dengerously high!'
logging.warning(warning_message)
def check_disk_usage():
disk = psutil.disk_usage('/')
logging.info(f'Disk Usage: {disk.percent}%')
if disk.percent > 80:
warning_message = 'WARNING: Disk usage is dangerously high!'
logging.warning(warning_message)
def main():
logging.info('Health Check Started...')
backup_logs()
check_cpu_usage()
check_memory_usage()
check_disk_usage()
logging.info('Health Check Completed...')
if __name__ == '__main__':
main()