service_status.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import psutil
  2. import requests
  3. from datetime import datetime
  4. from pathlib import Path
  5. import config
  6. def get_service(name):
  7. service = None
  8. try:
  9. service = psutil.win_service_get(name)
  10. service = service.as_dict()
  11. if service['pid']:
  12. service['process'] = psutil.Process(service['pid']).as_dict()
  13. else:
  14. service['process'] = ''
  15. except Exception as ex:
  16. print(str(ex))
  17. return service
  18. def process_info(program_path):
  19. offset = len(program_path) + 1
  20. info = []
  21. for p in psutil.process_iter():
  22. try:
  23. if p.exe().startswith(program_path):
  24. info.append({'path': p.exe()[offset:], 'memory': str(p.memory_info().rss//1048576), 'cpu': str(p.cpu_percent())})
  25. except psutil.AccessDenied:
  26. pass
  27. return sorted(info, key=lambda x: x['path'])
  28. def service_status(name, stats=False):
  29. service = get_service(name)
  30. if not service:
  31. print(f"Dienst '{name}' nicht vorhanden !!")
  32. return
  33. print(f"Dienst {service['display_name']} ({name})... ", end='')
  34. if service['status'] != 'running':
  35. print('inaktiv!')
  36. print(f"!! Fehler: Status {service['status']} !!")
  37. return
  38. print("läuft.")
  39. delta = (datetime.now() - datetime.fromtimestamp(service['process']['create_time'])).total_seconds()
  40. uptime = "{0:n} Tage, {1:n} Stunden, {2:n} Minuten".format(
  41. delta//86400, (delta%86400)//3600, ((delta%86400)%3600)//60)
  42. print(f" Pfad: {service['process']['exe']} ")
  43. print(f" Laufzeit: {uptime} ")
  44. # print(f" Speichernutzung: {service['process']['memory_info'].rss//1048576}")
  45. if not stats:
  46. return
  47. program_path = str(Path(service['process']['exe']).parent.parent)
  48. print('')
  49. print(' Prozess RAM CPU')
  50. for info in process_info(program_path):
  51. print(' ' + info['path'].ljust(32) + (info['memory'] + ' MB').rjust(8) + (info['cpu'] + ' %').rjust(9))
  52. def webservice_status(site):
  53. print(f"Cognos-Webservice {site}... ", end='')
  54. try:
  55. res = requests.get(site)
  56. if res.status_code not in [200, 441]:
  57. print(res.status_code)
  58. if b'DPR-ERR-2109' in res.content:
  59. print('!! Fehler: Dienst wird noch gestartet, Dispatcher ist nicht erreichbar !!')
  60. else:
  61. print('online.')
  62. if site.endswith('health'):
  63. print(res.json())
  64. except requests.ConnectionError as e:
  65. print('OFFLINE !!')
  66. print('!! Fehler: Dienst ist nicht erreichbar !!')
  67. return
  68. def cognos11_services():
  69. service_status('apacheds-cognos')
  70. service_status('MsgServ')
  71. service_status('ol_cognoscm')
  72. service_status('IBM Cognos', stats=True)
  73. cfg = config.config()
  74. webservice_status(cfg.cognos_url)
  75. webservice_status(cfg.cognos_url + 'health')
  76. if __name__ == '__main__':
  77. cognos11_services()