Пример #1
0
def get_info(component_path, format):
    component_details = open(os.path.join(component_path,
                                          ZATO_INFO_FILE)).read()

    out = {
        'component_details': component_details,
        'component_full_path': component_path,
        'component_host': current_host(),
        'component_running': False,
        'current_time': datetime.now().isoformat(),
        'current_time_utc': datetime.utcnow().isoformat(),
        'master_proc_connections': None,
        'master_proc_pid': None,
        'master_proc_name': None,
        'master_proc_create_time': None,
        'master_proc_create_time_utc': None,
        'master_proc_username': None,
        'master_proc_workers_no': None,
        'master_proc_workers_pids': None,
    }

    master_proc_pid = None
    try:
        master_proc_pid = int(
            open(os.path.join(component_path, MISC.PIDFILE)).read())
    except (IOError, ValueError):
        # Ok, no such file or it's empty
        pass

    if master_proc_pid:
        out['component_running'] = True
        master_proc = Process(master_proc_pid)
        workers_pids = sorted(elem.pid for elem in master_proc.children())

        out['master_proc_connections'] = format_connections(
            master_proc.connections(), format)
        out['master_proc_pid'] = master_proc.pid
        out['master_proc_create_time'] = datetime.fromtimestamp(
            master_proc.create_time()).isoformat()
        out['master_proc_create_time_utc'] = datetime.fromtimestamp(
            master_proc.create_time(), UTC).isoformat()
        out['master_proc_username'] = master_proc.username()
        out['master_proc_name'] = master_proc.name()
        out['master_proc_workers_no'] = len(workers_pids)
        out['master_proc_workers_pids'] = workers_pids

        for pid in workers_pids:
            worker = Process(pid)
            out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(
                worker.create_time()).isoformat()
            out['worker_{}_create_time_utc'.format(
                pid)] = datetime.fromtimestamp(worker.create_time(),
                                               UTC).isoformat()
            out['worker_{}_connections'.format(pid)] = format_connections(
                worker.connections(), format)

    return out
Пример #2
0
def get_info(component_path, format):
    component_details = open(os.path.join(component_path, ZATO_INFO_FILE)).read()

    out = {
        'component_details': component_details,
        'component_full_path': component_path,
        'component_host': current_host(),
        'component_running': False,
        'current_time': datetime.now().isoformat(),
        'current_time_utc': datetime.utcnow().isoformat(),
        'master_proc_connections': None,
        'master_proc_pid': None,
        'master_proc_name': None,
        'master_proc_create_time': None,
        'master_proc_create_time_utc': None,
        'master_proc_username': None,
        'master_proc_workers_no': None,
        'master_proc_workers_pids': None,
    }

    master_proc_pid = None
    try:
        master_proc_pid = int(open(os.path.join(component_path, MISC.PIDFILE)).read())
    except(IOError, ValueError):
        # Ok, no such file or it's empty
        pass

    if master_proc_pid:
        out['component_running'] = True
        master_proc = Process(master_proc_pid)
        workers_pids = sorted(elem.pid for elem in master_proc.children())

        out['master_proc_connections'] = format_connections(master_proc.connections(), format)
        out['master_proc_pid'] = master_proc.pid
        out['master_proc_create_time'] = datetime.fromtimestamp(master_proc.create_time()).isoformat()
        out['master_proc_create_time_utc'] = datetime.fromtimestamp(master_proc.create_time(), UTC).isoformat()
        out['master_proc_username'] = master_proc.username()
        out['master_proc_name'] = master_proc.name()
        out['master_proc_workers_no'] = len(workers_pids)
        out['master_proc_workers_pids'] = workers_pids

        for pid in workers_pids:
            worker = Process(pid)
            out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(worker.create_time()).isoformat()
            out['worker_{}_create_time_utc'.format(pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat()
            out['worker_{}_connections'.format(pid)] = format_connections(worker.connections(), format)

    return out
Пример #3
0
def _check_connections(proc: psutil.Process, laddr: Tuple) -> bool:
    try:
        return laddr in [i.laddr for i in proc.connections()]
    except psutil.AccessDenied:
        return False
    except psutil.ZombieProcess:
        return False
    except psutil.NoSuchProcess:
        return False
Пример #4
0
def collect_status(pid, appname, site):
    ip_out = get_host_info()[0]
    ip_inner = get_host_info()[1]
    server_id = get_host_info()[2]
    physical_mem = psutil.virtual_memory().total / 1024 / 1024  #Unit of M
    cpu_count = psutil.cpu_count()
    its = int(time.time())
    p_ins = Process(pid)
    pstatus = p_ins.status()
    create_time = time.strftime("%Y%m%d %H:%M:%S",
                                time.localtime(p_ins.create_time()))
    memory_percent = p_ins.memory_percent()
    memory_used = memory_percent * physical_mem
    cpu_calc_list = []
    for i in range(6):
        cpu_calc = p_ins.cpu_percent(interval=0.1)
        cpu_calc_list.append(cpu_calc)
    cpu_percent = float(sum(cpu_calc_list) / len(cpu_calc_list))
    num_fds = p_ins.num_fds()
    connections = p_ins.connections()
    connections_num = len(connections)

    #appname=p_ins.cwd()
    if p_ins.name() == 'jsvc':
        app_path = p_ins.exe().split('/')[:-2]
    else:
        app_path = p_ins.cwd()

    #appname = app_path.split('/')[-1]
    appname = appname
    if p_ins.children(recursive=True):
        children_list = str(p_ins.children(recursive=True))
    else:
        children_list = None
    message = {
        'site': site,
        'ip': ip_out,
        'ip_inner': ip_inner,
        'server_id': server_id,
        'pstatus': pstatus,
        'metric_name': 'app_monitor',
        'its': its,
        'pid': pid,
        'physical_mem': physical_mem,
        'memory_used': memory_used,
        'memory_percent': memory_percent,
        'cpu_count': cpu_count,
        'cpu_percent': cpu_percent,
        'num_fds': num_fds,
        'connections_num': connections_num,
        'create_time': create_time,
        'appname': appname,
        'app_path': app_path,
        'children': children_list
    }
    return message
Пример #5
0
def common_option(pid):
    ''' common options to be used in both restore and dump '''

    options = list()
    proc = Process(pid)
    if proc.terminal():
        options.append("--shell-job")
    if proc.connections(kind="inet"):
        options.append("--tcp-established")

    return options
Пример #6
0
def common_option(pid):
    ''' common options to be used in both restore and dump '''

    options = list()
    proc = Process(pid)
    if proc.terminal():
        options.append("--shell-job")
    if proc.connections(kind="inet"):
        options.append("--tcp-established")

    return options
Пример #7
0
        def _restart_program(args):
            """Restarts the current program, with file objects and descriptors cleanup"""
            try:
                p = Process(getpid())
                for handler in p.open_files() + p.connections():
                    close(handler.fd)
            except Exception as e:
                print("error found > ", e)

            python3 = executable
            print(args)
            execv(python3, ["python3"] + [__file__] + args)
Пример #8
0
    def restart(self, *args, **kwargs):
        shutdown_msg = 'restart failed, shutting down...'
        try:
            p = Process(getpid())
            for handler in p.get_open_files() + p.connections():
                close(handler.fd)
        except Exception as e:
            self.logger.error(shutdown_msg, 'BlueShift')
            self.graceful_exit(*args, **kwargs)

        python = sys_executable
        execl(python, python, *sys_argv)
        sys_exit(1)
Пример #9
0
def restart_self():
    # https://stackoverflow.com/questions/11329917/restart-python-script-from-within-itself
    log.info('Shutting down')
    try:
        p = Process(getpid())
        for handler in p.open_files() + p.connections():
            close(handler.fd)
    except Exception as e:
        log.warning(e)
    python = executable
    args = argv
    # weird hack that i don't understand
    args = ['-m', 'ch2'] + args[1:]
    log.info(f'Restarting {python} {args}')
    execl(python, python, *args)
Пример #10
0
  def status(self):
    """Check by socket if the openoffice work."""
    pid = self.pid()
    if pid is None or not pid_exists(pid):
      return False

    process = Process(pid)
    try:
      for connection in process.connections():
        if connection.status == 'LISTEN' and \
            connection.local_address[1] == self.port:
          return True
    except AccessDenied:
      return False

    return False
Пример #11
0
    def status(self):
        """Check by socket if the openoffice work."""
        pid = self.pid()
        if pid is None or not pid_exists(pid):
            return False

        process = Process(pid)
        try:
            for connection in process.connections():
                if connection.status == 'LISTEN' and \
                    connection.laddr[1] == self.port:
                    return True
        except AccessDenied:
            return False

        return False
Пример #12
0
    def add_new_information_from_process_object(self, process: psutil.Process,
                                                data_retrieval_timestamp: datetime.datetime) -> None:
        """
        Adds the new information about the process to the application profile.
        This should be mainly used for applications with only one process.
        :raises TypeError if process is not of type psutil.Process or data_retrieval_timestamp is not of type
            datetime.datetime.
        :raises ValueError if data_retrieval_timestamp is newer than current time.
        :param process: Information about the specific process.
        :type process: psutil.Process
        :param data_retrieval_timestamp: The time the data was retrieved.
        :type data_retrieval_timestamp: datetime.datetime
        """
        if not (isinstance(process, psutil.Process)):
            raise TypeError(expected_type_but_received_message.format("process", "psutil.Process", process))
        if not (isinstance(data_retrieval_timestamp, datetime.datetime)):
            raise TypeError(expected_type_but_received_message.format("data_retrieval_timestamp", "datetime.datetime",
                                                                      data_retrieval_timestamp))
        if data_retrieval_timestamp.replace(tzinfo=None) > datetime.datetime.now():
            raise ValueError("Argument data_retrieval_timestamp cannot be newer than current time. Value receive: {}"
                             .format(data_retrieval_timestamp))

        # Get info from the process object. One of the following calls may raise an Error (OS, AccessDenied, etc).
        open_files = process.open_files()
        memory_info = process.memory_info()
        child_process_count = len(process.children())
        username = process.username()
        threads_number = process.num_threads()
        process.cpu_percent()
        try:
            connections_num = len(process.connections())
        except psutil.AccessDenied:
            connections_num = 0

        time.sleep(0.1)  # wait for cpu_percent to return a meaningful value.
        cpu_percentage = process.cpu_percent()

        self.add_open_files(open_files=open_files, data_retrieval_timestamp=data_retrieval_timestamp)
        self.__memory_usages.append(memory_info.rss)
        self.__data_retrieval_timestamp.append(data_retrieval_timestamp)
        self.__child_processes_count.append(child_process_count)
        self.__users.extend(username)
        self.__cpu_percent_usages.append(cpu_percentage)
        self.__threads_numbers.append(threads_number)
        self.__connections_numbers.append(connections_num)
Пример #13
0
    async def reboot(self, ctx):
        """Restart the bot on the system level."""
        em = Embed(title="Rebooting the bot!",
                   description="Please wait while the bot reboots...",
                   color=message_color)
        em.set_footer(text=generate_footer(ctx))
        await ctx.send(embed=em)

        # Get bot process
        p = Process(getpid())
        for handler in p.open_files() + p.connections():
            # Close all active connections and processes
            close(handler.fd)

        # Get python exec
        python = executable
        # Start python process
        execl(python, python, *argv)
Пример #14
0
def main():
    '''Main Function.'''
    try:
        if len(argv) < 2:
            show_process_pid_name()        
            print("\nYou need to provided a process PID.")
            finish(1)
        pid = int(argv[1])
        if not pid_exists(pid):
            print("\nPID not found.")
            finish(1)
        p = Process(pid)
        print("Proccess: {} ({})".format(p.name(), p.pid))
        print("----------------------------------------------")
        print("Files opens:")
        files = p.open_files()
        for f in files:
            print(f.path)
        print("----------------------------------------------")
        print("Network Connections:")
        connections = p.connections()
        printed_connections = []
        for c in connections:
            if hasattr(c, "laddr"):
                laddr = c.laddr
                if hasattr(laddr, "ip") and hasattr(laddr, "port"):
                    if laddr not in printed_connections:
                        printed_connections.append(laddr)
                        print("Local - {}:{}".format(laddr.ip, laddr.port))
            if hasattr(c, "raddr"):
                raddr = c.raddr
                if hasattr(raddr, "ip") and hasattr(raddr, "port"):
                    if raddr not in printed_connections:
                        printed_connections.append(raddr)
                        print("Remote - {}:{}".format(raddr.ip, raddr.port))
                print("")
        printed_connections.clear()
    except Exception:
        print("\n[ERROR] {}".format(format_exc()))
        finish(1)
    finish(0)
Пример #15
0
    def _on_server(self, args):
        
        os.chdir(self.original_dir)
        abs_args_path = os.path.abspath(args.path)
        component_details = open(os.path.join(abs_args_path, ZATO_INFO_FILE)).read()
        
        out = {
            'component_details': component_details,
            'component_full_path': abs_args_path,
            'component_host': current_host(),
            'component_running': False,
            'current_time': datetime.now().isoformat(),
            'current_time_utc': datetime.utcnow().isoformat(),
            'master_proc_connections': None,
            'master_proc_pid': None,
            'master_proc_name': None,
            'master_proc_create_time': None,
            'master_proc_create_time_utc': None,
            'master_proc_username': None,
            'master_proc_workers_no': None,
            'master_proc_workers_pids': None,
        }

        master_proc_pid = None
        try:
            master_proc_pid = int(open(os.path.join(abs_args_path, MISC.PIDFILE)).read())
        except(IOError, ValueError):
            # Ok, no such file or it's empty
            pass

        if master_proc_pid:
            out['component_running'] = True
            master_proc = Process(master_proc_pid)
            workers_pids = sorted(elem.pid for elem in master_proc.children())
            
            out['master_proc_connections'] = master_proc.connections()
            out['master_proc_pid'] = master_proc.pid
            out['master_proc_create_time'] = datetime.fromtimestamp(master_proc.create_time()).isoformat()
            out['master_proc_create_time_utc'] = datetime.fromtimestamp(master_proc.create_time(), UTC).isoformat()
            out['master_proc_username'] = master_proc.username()
            out['master_proc_name'] = master_proc.name()
            out['master_proc_workers_no'] = len(workers_pids)
            out['master_proc_workers_pids'] = workers_pids
            
            for pid in workers_pids:
                worker = Process(pid)
                out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(worker.create_time()).isoformat()
                out['worker_{}_create_time_utc'.format(pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat()
                out['worker_{}_connections'.format(pid)] = worker.connections()
            
        if getattr(args, 'json', False):
            out['component_details'] = loads(out['component_details'])
            self.logger.info(dumps(out))
        else:
            cols_width = args.cols_width if args.cols_width else DEFAULT_COLS_WIDTH
            cols_width = (elem.strip() for elem in cols_width.split(','))
            cols_width = [int(elem) for elem in cols_width]
            
            table = Texttable()
            table.set_cols_width(cols_width)
            
            # Use text ('t') instead of auto so that boolean values don't get converted into ints
            table.set_cols_dtype(['t', 't'])
            
            rows = [['Key', 'Value']]
            rows.extend(sorted(out.items()))
            
            table.add_rows(rows)
            
            self.logger.info(table.draw())
Пример #16
0
def gerar_processo(PID):
    process = Process(PID)
    conn = process.connections()
    return json.dumps(conn)
Пример #17
0
    assert not find_objects(zeronimo.Collector)


@pytest.mark.trylast
def test_no_call_leak():
    assert not find_objects(zeronimo.messaging.Call)


@pytest.mark.trylast
def test_no_reply_leak():
    assert not find_objects(zeronimo.messaging.Reply)


proc = Process(os.getpid())
collect_conns = lambda: set(
    conn for conn in proc.connections()
    if conn.status not in ('CLOSE_WAIT', 'NONE')
)
# Mark initial connections.  They are not leacked connections.
initial_conns = collect_conns()


@pytest.mark.trylast
def test_no_socket_leak():
    for x in range(3):
        conns = collect_conns() - initial_conns
        if not conns:
            break
        gevent.sleep(0.1)
    else:
        pytest.fail('{0} connections leacked:\n{1}'.format(
Пример #18
0
def gerar_processo(PID): # Função que retorna informações de um processo pelo PID (em JSON)
    process = Process(PID)
    conn = process.connections()
    return json.dumps(conn)
Пример #19
0
class _ProcessMonitor:
    WARNING_THRESHOLD = 100 * 1024 * 1024

    busy = False

    def __init__(self):
        self.process = Process()
        self.peak_mem_res = 0
        self.low_mem_warning = False

    def monitor_task(self):
        if sys.stdout.isatty():

            while self.busy:
                try:
                    # only print the data out every 10 seconds
                    if datetime.now().second / 10 == 0:
                        info = self._get_info()

                        output.debug(info)
                    else:
                        # call get_mem so that we record peak more accurately
                        self._get_mem()

                    time.sleep(1)
                except Exception:
                    output.debug_exception()

                    self.busy = False

                    pass
        else:
            # if this isn't a TTY, no point in doing any of this
            self.busy = False

    def _get_info(self) -> str:
        from yawast.external.memory_size import Size

        # prime the call to cpu_percent, as the first call doesn't return useful data
        self.process.cpu_percent(interval=1)

        # use oneshot() to cache the data, so we minimize hits
        with self.process.oneshot():
            pct = self.process.cpu_percent()

            times = self.process.cpu_times()
            mem = self._get_mem()
            mem_res = "{0:cM}".format(Size(mem.rss))
            mem_virt = "{0:cM}".format(Size(mem.vms))

            thr = self.process.num_threads()

            vm = psutil.virtual_memory()
            mem_total = "{0:cM}".format(Size(vm.total))
            mem_avail_bytes = vm.available
            mem_avail = "{0:cM}".format(Size(vm.available))

            if mem_avail_bytes < self.WARNING_THRESHOLD and not self.low_mem_warning:
                self.low_mem_warning = True

                output.error(f"Low RAM Available: {mem_avail}")

            cons = -1
            try:
                cons = len(self.process.connections(kind="inet"))
            except Exception:
                # we don't care if this fails
                output.debug_exception()

            cpu_freq = psutil.cpu_freq()

        info = (f"Process Stats: CPU: {pct}% - Sys: {times.system} - "
                f"User: {times.user} - Res: {mem_res} - Virt: {mem_virt} - "
                f"Available: {mem_avail}/{mem_total} - Threads: {thr} - "
                f"Connections: {cons} - CPU Freq: "
                f"{int(cpu_freq.current)}MHz/{int(cpu_freq.max)}MHz")

        return info

    def _get_mem(self):
        mem = self.process.memory_info()

        if mem.rss > self.peak_mem_res:
            self.peak_mem_res = mem.rss
            output.debug(f"New high-memory threshold: {self.peak_mem_res}")

        return mem

    def __enter__(self):
        self.busy = True
        threading.Thread(target=self.monitor_task).start()

        return self

    def __exit__(self, exception, value, tb):
        self.busy = False

        if exception is not None:
            return False