|
|
@@ -1,5 +1,6 @@
|
|
|
import csv
|
|
|
import os
|
|
|
+import re
|
|
|
import subprocess
|
|
|
import zipfile
|
|
|
from datetime import datetime
|
|
|
@@ -17,12 +18,27 @@ def run_command(cmd, logfile):
|
|
|
p.wait()
|
|
|
|
|
|
|
|
|
-def task_scheduler(logfile):
|
|
|
- run_command('schtasks /query /v /fo CSV | find ".bat"', logfile)
|
|
|
+def task_scheduler(logfile, portal_dir):
|
|
|
+ run_command("schtasks /query /v /fo CSV", logfile)
|
|
|
+ logfile_filter = lambda line: portal_dir.lower() in line.lower() # noqa: E731
|
|
|
+ filter_logfile(logfile, logfile_filter)
|
|
|
+
|
|
|
+
|
|
|
+def filter_logfile(logfile, logfile_filter):
|
|
|
+ logfile_temp = logfile + ".temp"
|
|
|
+ Path(logfile_temp).unlink(missing_ok=True)
|
|
|
+ Path(logfile).rename(logfile_temp)
|
|
|
+ with open(logfile_temp, "r", encoding="cp850") as frh:
|
|
|
+ with open(logfile, "w", encoding="latin-1", newline="") as fwh:
|
|
|
+ fwh.write(frh.readline()) # header
|
|
|
+ fwh.writelines(line for line in frh.readlines() if logfile_filter(line))
|
|
|
+ # os.remove(logfile_temp)
|
|
|
|
|
|
|
|
|
def task_manager(logfile):
|
|
|
run_command("tasklist /fo CSV", logfile)
|
|
|
+ logfile_filter = lambda line: not re.search(r"chrome|msedge|svchost|conhost", line, re.IGNORECASE) # noqa: E731
|
|
|
+ filter_logfile(logfile, logfile_filter)
|
|
|
|
|
|
|
|
|
def shared_files(logfile):
|
|
|
@@ -54,14 +70,12 @@ def database_info(cfg: config.Config, csv_file):
|
|
|
wr = csv.writer(fwh, delimiter=";")
|
|
|
wr.writerow(
|
|
|
[
|
|
|
- "DatabaseName",
|
|
|
- "SchemaName",
|
|
|
- "TableName",
|
|
|
- "RowCounts",
|
|
|
- "TotalSpaceKB",
|
|
|
- "UsedSpaceKB",
|
|
|
- "UnusedSpaceKB",
|
|
|
- "LastChanged",
|
|
|
+ "database_name",
|
|
|
+ "schema_name",
|
|
|
+ "table_name",
|
|
|
+ "row_count",
|
|
|
+ "total_space_kb",
|
|
|
+ "last_changed",
|
|
|
]
|
|
|
)
|
|
|
for row in result:
|
|
|
@@ -70,12 +84,19 @@ def database_info(cfg: config.Config, csv_file):
|
|
|
|
|
|
|
|
|
def zip_to_file(base_dir, zip_file):
|
|
|
- filter = ["config/*", "config/info/*", "config/models/*", "logs/*", "*.ini", "*.bat"]
|
|
|
+ filter = [
|
|
|
+ "Tasks/config/*",
|
|
|
+ "Tasks/config/info/*",
|
|
|
+ "Tasks/config/models/*",
|
|
|
+ "Tasks/logs/*",
|
|
|
+ "Tasks/*.ini",
|
|
|
+ "Tasks/*.bat",
|
|
|
+ ]
|
|
|
|
|
|
with zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zip:
|
|
|
for f in filter:
|
|
|
for e in Path(base_dir).glob(f):
|
|
|
- if ".zip" not in e.name:
|
|
|
+ if e.is_file() and ".zip" not in e.name:
|
|
|
zip.write(e, e.relative_to(base_dir))
|
|
|
|
|
|
|
|
|
@@ -83,11 +104,11 @@ def system_check():
|
|
|
cfg = config.Config()
|
|
|
|
|
|
# Aufgabenplanung aktueller Stand
|
|
|
- task_scheduler(cfg.log_dir + "\\schtasks.csv.log")
|
|
|
+ task_scheduler(cfg.log_dir + "\\schtasks.csv.log", cfg.portal_dir)
|
|
|
# Laufende Prozesse
|
|
|
task_manager(cfg.log_dir + "\\tasklist.csv.log")
|
|
|
# aktuelle Freigabe-Sessions
|
|
|
- shared_files(cfg.log_dir + "\\openfiles.csv.log")
|
|
|
+ # shared_files(cfg.log_dir + "\\openfiles.csv.log")
|
|
|
# Tabellengrößen
|
|
|
database_info(cfg, cfg.log_dir + "\\db_info.csv.log")
|
|
|
|
|
|
@@ -101,7 +122,8 @@ def system_check():
|
|
|
# Logdateien aus Tasks/logs und System/prot
|
|
|
timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")
|
|
|
zip_file = f"{cfg.tasks_dir}\\logs\\zip\\{cfg.kunde_safe}_{timestamp}.zip"
|
|
|
- zip_to_file(cfg.tasks_dir, zip_file)
|
|
|
+ os.makedirs(Path(zip_file).parent, exist_ok=True)
|
|
|
+ zip_to_file(cfg.portal_dir, zip_file)
|
|
|
|
|
|
# Upload auf FTP
|
|
|
FtpClient().upload(zip_file)
|