123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import csv
- import os
- import subprocess
- import zipfile
- from datetime import datetime
- from pathlib import Path
- import config
- from status_client.db_info import DatabaseInfo
- from status_client.ftp_client import FtpClient
- from status_client.path_info import PathInfo
- def run_command(cmd, logfile):
- with open(logfile, "wb") as stream:
- p = subprocess.Popen(cmd, stdout=stream, stderr=stream)
- p.wait()
- def task_scheduler(logfile):
- run_command('schtasks /query /v /fo CSV | find ".bat"', logfile)
- def task_manager(logfile):
- run_command("tasklist /fo CSV", logfile)
- def shared_files(logfile):
- run_command("openfiles /Query /fo CSV", logfile)
- def model_datasources(cfg, model_file: Path, logfile):
- cmd = (
- f'"{cfg.cognos7.program_dir}\\runmac32.exe" "{cfg.tools_dir}\\VBS\\list-datasources-c11.mac" '
- + f'"{model_file.parent}","{model_file.name}","{logfile}"'
- )
- p = subprocess.Popen(cmd)
- p.wait()
- def datasources_all_models(cfg: config.Config):
- os.makedirs(cfg.tasks_dir + "\\config\\models", exist_ok=True)
- for model_file in Path(cfg.system_dir + "\\Models").glob("*.pyj"):
- model_datasources(cfg, model_file, cfg.tasks_dir + "\\config\\models\\" + model_file.name + ".log")
- def database_info(cfg: config.Config, csv_file):
- dbinfo = DatabaseInfo()
- result = []
- for db in dbinfo.databases():
- result.extend(dbinfo.table_size(db[0]))
- with open(csv_file, "w", encoding="latin-1", newline="") as fwh:
- wr = csv.writer(fwh, delimiter=";")
- wr.writerow(
- [
- "DatabaseName",
- "SchemaName",
- "TableName",
- "RowCounts",
- "TotalSpaceKB",
- "UsedSpaceKB",
- "UnusedSpaceKB",
- "LastChanged",
- ]
- )
- for row in result:
- wr.writerow(row)
- return result
- def zip_to_file(base_dir, zip_file):
- filter = ["config/*", "config/info/*", "config/models/*", "logs/*", "*.ini", "*.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:
- zip.write(e, e.relative_to(base_dir))
- def system_check():
- cfg = config.Config()
- # Aufgabenplanung aktueller Stand
- task_scheduler(cfg.log_dir + "\\schtasks.csv.log")
- # Laufende Prozesse
- task_manager(cfg.log_dir + "\\tasklist.csv.log")
- # aktuelle Freigabe-Sessions
- shared_files(cfg.log_dir + "\\openfiles.csv.log")
- # Tabellengrößen
- database_info(cfg, cfg.log_dir + "\\db_info.csv.log")
- # datasources_all_models(cfg)
- # Liste aller Dateien im GAPS-Verzeichnis
- # filename;size;cdate;mdate
- ti = PathInfo()
- ti.check_dir(cfg.portal_dir)
- ti.write_logfile(cfg.log_dir + "\\path_info.csv.log")
- # 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)
- # Upload auf FTP
- FtpClient().upload(zip_file)
- if __name__ == "__main__":
- system_check()
|