
import time

from windows_process_mem_cpu import WindowsProcessMemCpuUsageData

class WindowsProcessInfoManager(object):
    def get_mem_cpu_usage_data(self, pid, last_cpu, last_time):
        return WindowsProcessMemCpuUsageData(pid, last_cpu, last_time)

    def get_all_processes(self):
        return []

    def runforever(self):
        while True:
            time.sleep(5)


import psutil
import time

from windows_process_mem_cpu import WindowsProcessMemCpuUsageData


def _service_status_to_state(status):
    if status == 'running':
        return 'PROCESS_STATE_RUNNING'
    else:
        return 'PROCESS_STATE_STOPPED'

def _get_service_by_name(name):
    service = None
    try:
        service = psutil.win_service_get(name)
    except:
        pass
    return service

def _get_process_by_pid(pid):
    process = None
    try:
        process = psutil.Process(pid)
    except:
        pass
    return process

class WindowsProcessInfoManager(object):
    def get_mem_cpu_usage_data(self, pid, last_cpu, last_time):
        return WindowsProcessMemCpuUsageData(pid, last_cpu, last_time)

    def get_all_processes(self):
        agent_service = _get_service_by_name('ContrailAgent')
        if agent_service != None:
            info = {}
            info['name'] = 'contrail-vrouter-agent'
            info['group'] = info['name']
            info['statename'] = _service_status_to_state(agent_service.status())
            if info['statename'] == 'PROCESS_STATE_RUNNING':
                info['pid'] = agent_service.pid()
                agent_process = _get_process_by_pid(info['pid'])
                if agent_process != None:
                    info['start'] = str(int(agent_process.create_time() * 1000000))
            return [info]
        else:
            return []

    def runforever(self):
        while True:
            time.sleep(5)

Implement checking if agent is up on Windows
Implement checking if agent is up on Windows

Very simple implementation using psutil

Change-Id: I2b7c65d6d677f0f57e79277ac2298f0b73729b94
Partial-Bug: #1783539

  
+ import psutil
  import time
  
  from windows_process_mem_cpu import WindowsProcessMemCpuUsageData
+ 
+ 
+ def _service_status_to_state(status):
+     if status == 'running':
+         return 'PROCESS_STATE_RUNNING'
+     else:
+         return 'PROCESS_STATE_STOPPED'
+ 
+ def _get_service_by_name(name):
+     service = None
+     try:
+         service = psutil.win_service_get(name)
+     except:
+         pass
+     return service
+ 
+ def _get_process_by_pid(pid):
+     process = None
+     try:
+         process = psutil.Process(pid)
+     except:
+         pass
+     return process
  
  class WindowsProcessInfoManager(object):
      def get_mem_cpu_usage_data(self, pid, last_cpu, last_time):
          return WindowsProcessMemCpuUsageData(pid, last_cpu, last_time)
  
      def get_all_processes(self):
+         agent_service = _get_service_by_name('ContrailAgent')
+         if agent_service != None:
+             info = {}
+             info['name'] = 'contrail-vrouter-agent'
+             info['group'] = info['name']
+             info['statename'] = _service_status_to_state(agent_service.status())
+             if info['statename'] == 'PROCESS_STATE_RUNNING':
+                 info['pid'] = agent_service.pid()
+                 agent_process = _get_process_by_pid(info['pid'])
+                 if agent_process != None:
+                     info['start'] = str(int(agent_process.create_time() * 1000000))
+             return [info]
+         else:
-         return []
+             return []
  
      def runforever(self):
          while True:
              time.sleep(5)
  
Implement checking if agent is up on Windows
## Code Before:

import time

from windows_process_mem_cpu import WindowsProcessMemCpuUsageData

class WindowsProcessInfoManager(object):
    def get_mem_cpu_usage_data(self, pid, last_cpu, last_time):
        return WindowsProcessMemCpuUsageData(pid, last_cpu, last_time)

    def get_all_processes(self):
        return []

    def runforever(self):
        while True:
            time.sleep(5)

## Instruction:
Implement checking if agent is up on Windows
## Code After:

import psutil
import time

from windows_process_mem_cpu import WindowsProcessMemCpuUsageData


def _service_status_to_state(status):
    if status == 'running':
        return 'PROCESS_STATE_RUNNING'
    else:
        return 'PROCESS_STATE_STOPPED'

def _get_service_by_name(name):
    service = None
    try:
        service = psutil.win_service_get(name)
    except:
        pass
    return service

def _get_process_by_pid(pid):
    process = None
    try:
        process = psutil.Process(pid)
    except:
        pass
    return process

class WindowsProcessInfoManager(object):
    def get_mem_cpu_usage_data(self, pid, last_cpu, last_time):
        return WindowsProcessMemCpuUsageData(pid, last_cpu, last_time)

    def get_all_processes(self):
        agent_service = _get_service_by_name('ContrailAgent')
        if agent_service != None:
            info = {}
            info['name'] = 'contrail-vrouter-agent'
            info['group'] = info['name']
            info['statename'] = _service_status_to_state(agent_service.status())
            if info['statename'] == 'PROCESS_STATE_RUNNING':
                info['pid'] = agent_service.pid()
                agent_process = _get_process_by_pid(info['pid'])
                if agent_process != None:
                    info['start'] = str(int(agent_process.create_time() * 1000000))
            return [info]
        else:
            return []

    def runforever(self):
        while True:
            time.sleep(5)
