from cbagent.collectors import Collector
from cbagent.collectors.libstats.psstats import PSStats


class PS(Collector):

    COLLECTOR = "atop"  # Legacy

    KNOWN_PROCESSES = ("beam.smp", "memcached", "indexer", "projector",
                       "cbq-engine")

    def __init__(self, settings):
        super(PS, self).__init__(settings)
        self.nodes = settings.hostnames or list(self.get_nodes())

        if hasattr(settings, "monitor_clients") and settings.monitor_clients\
                and settings.master_node in settings.monitor_clients:
            self.nodes = settings.monitor_clients
            self.KNOWN_PROCESSES = ("backup", "cbbackupwrapper", )

        if hasattr(settings, "fts_server"):
            self.KNOWN_PROCESSES = ("beam.smp", "memcached", "cbft",)

        self.ps = PSStats(hosts=self.nodes,
                          user=self.ssh_username, password=self.ssh_password)

    def update_metadata(self):
        self.mc.add_cluster()
        for node in self.nodes:
            self.mc.add_server(node)

    def sample(self):
        for process in self.KNOWN_PROCESSES:
            for node, stats in self.ps.get_samples(process).items():
                if stats:
                    self.update_metric_metadata(stats.keys(), server=node)
                    self.store.append(stats,
                                      cluster=self.cluster, server=node,
                                      collector=self.COLLECTOR)

from cbagent.collectors import Collector
from cbagent.collectors.libstats.psstats import PSStats


class PS(Collector):

    COLLECTOR = "atop"  # Legacy

    KNOWN_PROCESSES = ("beam.smp", "memcached", "indexer", "projector",
                       "cbq-engine")

    def __init__(self, settings):
        super(PS, self).__init__(settings)
        self.nodes = settings.hostnames or list(self.get_nodes())

        if hasattr(settings, "monitor_clients") and settings.monitor_clients\
                and settings.master_node in settings.monitor_clients:
            self.nodes = settings.monitor_clients
            self.KNOWN_PROCESSES = ("backup", "cbbackupwrapper", )

        if hasattr(settings, "fts_server") and settings.fts_server:
            self.KNOWN_PROCESSES = ("beam.smp", "memcached", "cbft",)

        self.ps = PSStats(hosts=self.nodes,
                          user=self.ssh_username, password=self.ssh_password)

    def update_metadata(self):
        self.mc.add_cluster()
        for node in self.nodes:
            self.mc.add_server(node)

    def sample(self):
        for process in self.KNOWN_PROCESSES:
            for node, stats in self.ps.get_samples(process).items():
                if stats:
                    self.update_metric_metadata(stats.keys(), server=node)
                    self.store.append(stats,
                                      cluster=self.cluster, server=node,
                                      collector=self.COLLECTOR)

Fix condition for FTS stats collection
CBPS-186: Fix condition for FTS stats collection

Currently, stats collection for processes such as indexer and
cbq-engine is disabled due to a wrong "if" statement.

The "settings object" always has "fts_server" attribute. We should
check its boolean value instead.

Change-Id: I017f4758f3603e674f094af27b6293716796418a
Reviewed-on: http://review.couchbase.org/67781
Tested-by: buildbot <80754af91bfb6d1073585b046fe0a474ce868509@couchbase.com>
Reviewed-by: Pavel Paulau <dd88eded64e90046a680e3a6c0828ceb8fe8a0e7@gmail.com>

  from cbagent.collectors import Collector
  from cbagent.collectors.libstats.psstats import PSStats
  
  
  class PS(Collector):
  
      COLLECTOR = "atop"  # Legacy
  
      KNOWN_PROCESSES = ("beam.smp", "memcached", "indexer", "projector",
                         "cbq-engine")
  
      def __init__(self, settings):
          super(PS, self).__init__(settings)
          self.nodes = settings.hostnames or list(self.get_nodes())
  
          if hasattr(settings, "monitor_clients") and settings.monitor_clients\
                  and settings.master_node in settings.monitor_clients:
              self.nodes = settings.monitor_clients
              self.KNOWN_PROCESSES = ("backup", "cbbackupwrapper", )
  
-         if hasattr(settings, "fts_server"):
+         if hasattr(settings, "fts_server") and settings.fts_server:
              self.KNOWN_PROCESSES = ("beam.smp", "memcached", "cbft",)
  
          self.ps = PSStats(hosts=self.nodes,
                            user=self.ssh_username, password=self.ssh_password)
  
      def update_metadata(self):
          self.mc.add_cluster()
          for node in self.nodes:
              self.mc.add_server(node)
  
      def sample(self):
          for process in self.KNOWN_PROCESSES:
              for node, stats in self.ps.get_samples(process).items():
                  if stats:
                      self.update_metric_metadata(stats.keys(), server=node)
                      self.store.append(stats,
                                        cluster=self.cluster, server=node,
                                        collector=self.COLLECTOR)
  
Fix condition for FTS stats collection
## Code Before:
from cbagent.collectors import Collector
from cbagent.collectors.libstats.psstats import PSStats


class PS(Collector):

    COLLECTOR = "atop"  # Legacy

    KNOWN_PROCESSES = ("beam.smp", "memcached", "indexer", "projector",
                       "cbq-engine")

    def __init__(self, settings):
        super(PS, self).__init__(settings)
        self.nodes = settings.hostnames or list(self.get_nodes())

        if hasattr(settings, "monitor_clients") and settings.monitor_clients\
                and settings.master_node in settings.monitor_clients:
            self.nodes = settings.monitor_clients
            self.KNOWN_PROCESSES = ("backup", "cbbackupwrapper", )

        if hasattr(settings, "fts_server"):
            self.KNOWN_PROCESSES = ("beam.smp", "memcached", "cbft",)

        self.ps = PSStats(hosts=self.nodes,
                          user=self.ssh_username, password=self.ssh_password)

    def update_metadata(self):
        self.mc.add_cluster()
        for node in self.nodes:
            self.mc.add_server(node)

    def sample(self):
        for process in self.KNOWN_PROCESSES:
            for node, stats in self.ps.get_samples(process).items():
                if stats:
                    self.update_metric_metadata(stats.keys(), server=node)
                    self.store.append(stats,
                                      cluster=self.cluster, server=node,
                                      collector=self.COLLECTOR)

## Instruction:
Fix condition for FTS stats collection
## Code After:
from cbagent.collectors import Collector
from cbagent.collectors.libstats.psstats import PSStats


class PS(Collector):

    COLLECTOR = "atop"  # Legacy

    KNOWN_PROCESSES = ("beam.smp", "memcached", "indexer", "projector",
                       "cbq-engine")

    def __init__(self, settings):
        super(PS, self).__init__(settings)
        self.nodes = settings.hostnames or list(self.get_nodes())

        if hasattr(settings, "monitor_clients") and settings.monitor_clients\
                and settings.master_node in settings.monitor_clients:
            self.nodes = settings.monitor_clients
            self.KNOWN_PROCESSES = ("backup", "cbbackupwrapper", )

        if hasattr(settings, "fts_server") and settings.fts_server:
            self.KNOWN_PROCESSES = ("beam.smp", "memcached", "cbft",)

        self.ps = PSStats(hosts=self.nodes,
                          user=self.ssh_username, password=self.ssh_password)

    def update_metadata(self):
        self.mc.add_cluster()
        for node in self.nodes:
            self.mc.add_server(node)

    def sample(self):
        for process in self.KNOWN_PROCESSES:
            for node, stats in self.ps.get_samples(process).items():
                if stats:
                    self.update_metric_metadata(stats.keys(), server=node)
                    self.store.append(stats,
                                      cluster=self.cluster, server=node,
                                      collector=self.COLLECTOR)
