| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | import psutilimport requestsfrom datetime import datetimefrom pathlib import Pathimport configdef get_service(name):    service = None    try:        service = psutil.win_service_get(name)        service = service.as_dict()        if service['pid']:            service['process'] = psutil.Process(service['pid']).as_dict()        else:            service['process'] = ''    except Exception as ex:        print(str(ex))    return servicedef process_info(program_path):    offset = len(program_path) + 1    info = []    for p in psutil.process_iter():        try:            if p.exe().startswith(program_path):                info.append({                    'path': p.exe()[offset:],                    'memory': str(p.memory_info().rss // 1048576),                    'cpu': str(p.cpu_percent())                })        except psutil.AccessDenied:            pass    return sorted(info, key=lambda x: x['path'])def service_status(name, stats=False):    service = get_service(name)    if not service:        print(f"Dienst '{name}' nicht vorhanden !!")        return    print(f"Dienst {service['display_name']} ({name})... ", end='')    if service['status'] != 'running':        print('inaktiv!')        print(f"!! Fehler: Status {service['status']} !!")        return    print("läuft.")    delta = (datetime.now() - datetime.fromtimestamp(service['process']['create_time'])).total_seconds()    uptime = "{0:n} Tage, {1:n} Stunden, {2:n} Minuten".format(             delta // 86400, (delta % 86400) // 3600, ((delta % 86400) % 3600) // 60)    print(f"   Pfad:      {service['process']['exe']} ")    print(f"   Laufzeit:  {uptime} ")    # print(f"   Speichernutzung: {service['process']['memory_info'].rss//1048576}")    if not stats:        return    program_path = str(Path(service['process']['exe']).parent.parent)    print('')    print('   Prozess                           RAM       CPU')    for info in process_info(program_path):        print('    ' + info['path'].ljust(32) + (info['memory'] + ' MB').rjust(8) + (info['cpu'] + ' %').rjust(9))def webservice_status(site):    print(f"Cognos-Webservice {site}... ", end='')    try:        res = requests.get(site)        if res.status_code not in [200, 441]:            print(res.status_code)            if b'DPR-ERR-2109' in res.content:                print('!! Fehler: Dienst wird noch gestartet, Dispatcher ist nicht erreichbar !!')        else:            print('online.')            if site.endswith('health'):                print(res.json())    except requests.ConnectionError:        print('OFFLINE !!')        print('!! Fehler: Dienst ist nicht erreichbar !!')        returndef cognos11_services():    service_status('apacheds-cognos')    service_status('MsgServ')    service_status('ol_cognoscm')    service_status('IBM Cognos', stats=True)    cfg = config.Config()    webservice_status(cfg.cognos_url)    webservice_status(cfg.cognos_url + 'health')if __name__ == '__main__':    cognos11_services()
 |