-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_cifs_read_write.py
77 lines (65 loc) · 2.66 KB
/
check_cifs_read_write.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
73
74
75
76
#!/bin/python
#filename: check_cifs_read_write.py
#description: checks the cifs and sends an email if there is an issue.
#checked more to work around cifs timeouts
import sys
import os
import re
import subprocess
import datetime
import time
# Generate a timestamp in the format YYYYMMDD_HHMMSS
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# Update the file name with the timestamp
file_name = "cifstests_{}.txt".format(timestamp)
script_name = os.path.basename(__file__) #name of this file.
# Global variables
error_count = 0
max_error_count = 2 # Maximum number of allowed errors
email_recipient = '[email protected]'
email_subject = "CIFS Write Failure"
data = [""] #data for body of the email
SERVERNAME=os.uname().nodename if hasattr(os, 'uname') else os.getenv('HOSTNAME', os.getenv('COMPUTERNAME')) #get hostname of server
imagery_file_path = "/media/imagery/{}".format(file_name)
media_file_path = "/media/share/GIS/{}".format(file_name)
def write_lines_to_file(file_path, lines):
os.system('sync')
time.sleep(2)
"""Write lines to a file."""
with open(file_path, mode="w") as file:
for line in lines:
file.write(line.rstrip() + "\n")
def write_file_with_error_handling(file_path, lines):
"""Write lines to a file with error handling."""
try:
print("Writing to file: {}".format(file_path))
write_lines_to_file(file_path, lines)
except IOError as e:
print("Error writing to file: {}".format(file_path))
global error_count
error_count += 1
def remove_file_with_error_handling(file_path):
os.system('sync')
time.sleep(2)
"""Remove a file with error handling."""
try:
os.remove(file_path)
print("File removed: {}".format(file_path))
except OSError as e:
print("Error removing file: {}".format(file_path))
global error_count
error_count += 1
print("Debug info on {}.".format(script_name))
print(imagery_file_path)
print(media_file_path)
write_file_with_error_handling(imagery_file_path, data)
write_file_with_error_handling(media_file_path, data)
if error_count >= max_error_count:
df_output = subprocess.check_output(["df", "-h"]).decode('utf-8')
message = "Errors occurred during CIFS file writes on {}. This may indicate an unmounted filesystem.\nError count: {}/{}\n{}".format(SERVERNAME, error_count, max_error_count, df_output)
print(message)
os.system("echo '{}' | mutt -s '{}' {}".format(message, email_subject, email_recipient))
else:
print("Success. No errors occurred during the writes on {}.".format(SERVERNAME))
remove_file_with_error_handling(imagery_file_path)
remove_file_with_error_handling(media_file_path)