status_client.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import csv
  2. import os
  3. import subprocess
  4. import zipfile
  5. from datetime import datetime
  6. from pathlib import Path
  7. import config
  8. from status_client.db_info import DatabaseInfo
  9. from status_client.ftp_client import FtpClient
  10. from status_client.path_info import PathInfo
  11. def run_command(cmd, logfile):
  12. with open(logfile, "wb") as stream:
  13. p = subprocess.Popen(cmd, stdout=stream, stderr=stream)
  14. p.wait()
  15. def task_scheduler(logfile):
  16. run_command('schtasks /query /v /fo CSV | find ".bat"', logfile)
  17. def task_manager(logfile):
  18. run_command("tasklist /fo CSV", logfile)
  19. def shared_files(logfile):
  20. run_command("openfiles /Query /fo CSV", logfile)
  21. def model_datasources(cfg, model_file: Path, logfile):
  22. cmd = (
  23. f'"{cfg.cognos7.program_dir}\\runmac32.exe" "{cfg.tools_dir}\\VBS\\list-datasources-c11.mac" '
  24. + f'"{model_file.parent}","{model_file.name}","{logfile}"'
  25. )
  26. p = subprocess.Popen(cmd)
  27. p.wait()
  28. def datasources_all_models(cfg: config.Config):
  29. os.makedirs(cfg.tasks_dir + "\\config\\models", exist_ok=True)
  30. for model_file in Path(cfg.system_dir + "\\Models").glob("*.pyj"):
  31. model_datasources(cfg, model_file, cfg.tasks_dir + "\\config\\models\\" + model_file.name + ".log")
  32. def database_info(cfg: config.Config, csv_file):
  33. dbinfo = DatabaseInfo()
  34. result = []
  35. for db in dbinfo.databases():
  36. result.extend(dbinfo.table_size(db[0]))
  37. with open(csv_file, "w", encoding="latin-1", newline="") as fwh:
  38. wr = csv.writer(fwh, delimiter=";")
  39. wr.writerow(
  40. [
  41. "DatabaseName",
  42. "SchemaName",
  43. "TableName",
  44. "RowCounts",
  45. "TotalSpaceKB",
  46. "UsedSpaceKB",
  47. "UnusedSpaceKB",
  48. "LastChanged",
  49. ]
  50. )
  51. for row in result:
  52. wr.writerow(row)
  53. return result
  54. def zip_to_file(base_dir, zip_file):
  55. filter = ["config/*", "config/info/*", "config/models/*", "logs/*", "*.ini", "*.bat"]
  56. with zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zip:
  57. for f in filter:
  58. for e in Path(base_dir).glob(f):
  59. if ".zip" not in e.name:
  60. zip.write(e, e.relative_to(base_dir))
  61. def system_check():
  62. cfg = config.Config()
  63. # Aufgabenplanung aktueller Stand
  64. task_scheduler(cfg.log_dir + "\\schtasks.csv.log")
  65. # Laufende Prozesse
  66. task_manager(cfg.log_dir + "\\tasklist.csv.log")
  67. # aktuelle Freigabe-Sessions
  68. shared_files(cfg.log_dir + "\\openfiles.csv.log")
  69. # Tabellengrößen
  70. database_info(cfg, cfg.log_dir + "\\db_info.csv.log")
  71. # datasources_all_models(cfg)
  72. # Liste aller Dateien im GAPS-Verzeichnis
  73. # filename;size;cdate;mdate
  74. ti = PathInfo()
  75. ti.check_dir(cfg.portal_dir)
  76. ti.write_logfile(cfg.log_dir + "\\path_info.csv.log")
  77. # Logdateien aus Tasks/logs und System/prot
  78. timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")
  79. zip_file = f"{cfg.tasks_dir}\\logs\\zip\\{cfg.kunde_safe}_{timestamp}.zip"
  80. zip_to_file(cfg.tasks_dir, zip_file)
  81. # Upload auf FTP
  82. FtpClient().upload(zip_file)
  83. if __name__ == "__main__":
  84. system_check()