BmnRoot
Loading...
Searching...
No Matches
raw_main_inspector.py
Go to the documentation of this file.
1#!/usr/bin/python3
2
3import datetime
4import psycopg2
5import hashlib
6import json
7import re
8import os
9
10
11with open("inspector.json","r") as fp: #full PATH to file
12 set = json.load(fp)
13 storage = set["storage"]
14 if(set["check_type"] == 1):
15 check_type=1
16 table = "simulation_file"
17 else:
18 check_type=2
19 table = "run_"
20
21found_errors=0
22mail_adress="*"
23
25 conn_bmn = psycopg2.connect(dbname="bmn_db", user="*", password="*", host="*", port="5432")
26 cursor = conn_bmn.cursor()
27 cursor.execute("SELECT file_path, file_md5 FROM "+ table.format())
28 records = cursor.fetchall()
29 cursor.close()
30 conn_bmn.close()
31 return records, cursor.rowcount
32
33def integrity_check(conn_integrity, check_date):
34 file_count = connection_bmn_database()[1]
35 cursor = conn_integrity.cursor()
36 cursor.execute("INSERT INTO integrity_check(type_id, storage_id, check_date, file_count) VALUES (%s, %s, %s, %s) RETURNING check_id", [check_type, storage, check_date, file_count])
37 check_id = cursor.fetchone()[0]
38 conn_integrity.commit()
39 cursor.close()
40 return check_id
41
42
43def failed_record(conn_integrity, check_id, error_id, file_path, error_details):
44 cursor = conn_integrity.cursor()
45 cursor.execute("INSERT INTO failed_record(check_id, error_id, file_path, error_details) VALUES (%s, %s, %s, %s)", [check_id, error_id, file_path, error_details])
46 conn_integrity.commit()
47 cursor.close()
48
49def inspection(count, conn_integrity, check_id, records):
50 global found_errors
51 for i in range(count):
52 path_to_file=str(records[i][0]).strip()
53 if (path_to_file.__contains__('/') != True):
54 print(path_to_file)
55 continue
56 if (path_to_file.strip()[-1] == '/'):
57 print(path_to_file)
58 continue
59 if(str(records[i][1]).strip() == "None"):
60 found_errors=found_errors+1
61 failed_record(conn_integrity, check_id, 3, path_to_file,'')
62 else:
63 hash_md5 = hashlib.md5()
64 try:
65 with open(path_to_file, 'rb') as fp:
66 for chunk in iter(lambda: fp.read(8192), b""):
67 hash_md5.update(chunk)
68 except FileNotFoundError as e:
69 found_errors=found_errors+1
70 failed_record(conn_integrity, check_id,1,path_to_file,'')
71 except BaseException as e:
72 found_errors=found_errors+1
73 e = re.sub(": '"+path_to_file+"'", "", str(e))
74 failed_record(conn_integrity, check_id,2,path_to_file,str(e))
75 else:
76 if (str(records[i][1]).strip() != hash_md5.hexdigest()):
77 found_errors=found_errors+1
78 failed_record(conn_integrity, check_id,4,path_to_file,'')
79# --- START ---
81 conn_integrity = psycopg2.connect(dbname="*", user="*", password="*", host="*", port="5432")
82 check_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
83 check_id = integrity_check(conn_integrity, check_date)
84 records, count = connection_bmn_database()
85 inspection(count, conn_integrity, check_id, records)
86 finish_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
87 cursor = conn_integrity.cursor()
88 cursor.execute("UPDATE integrity_check SET complete_date=%s WHERE check_id=%s", [finish_date, check_id])
89 conn_integrity.commit()
90 cursor.close()
91 conn_integrity.close()
92
93 sendmail_location = "/usr/sbin/sendmail"
94 p = os.popen("%s -t" % sendmail_location, "w")
95 p.write("From: %s\n" % "NCX Inspector < * >")
96 p.write("To: %s\n" % mail_adress)
97 p.write("Subject: NCX Inspector Robot\n")
98 p.write("\n")
99 p.write("Inspection of the * files has been completed.")
100 p.write("\n")
101 p.write("Operating time: " + check_date + " - " + finish_date)
102 p.write("\n")
103 p.write("Number of errors: " + str(found_errors))
104 p.close()
105
integrity_check(conn_integrity, check_date)
inspection(count, conn_integrity, check_id, records)
failed_record(conn_integrity, check_id, error_id, file_path, error_details)