from cbagent.collectors.libstats.remotestats import RemoteStats, parallel_task


class PSStats(RemoteStats):

    METRICS = (
        ("rss", 1024),    # kB -> B
        ("vsize", 1024),
    )

    PS_CMD = "ps -eo pid,rss,vsize,comm | " \
        "grep {} | grep -v grep | sort -n -k 2 | tail -n 1"

    TOP_CMD = "top -b n2 -d1 -p {0} | grep {0}"

    @parallel_task(server_side=True)
    def get_server_samples(self, process):
        return self.get_samples(process)

    @parallel_task(server_side=False)
    def get_client_samples(self, process):
        return self.get_samples(process)

    def get_samples(self, process):
        samples = {}

        stdout = self.run(self.PS_CMD.format(process))
        if stdout:
            for i, value in enumerate(stdout.split()[1:1 + len(self.METRICS)]):
                metric, multiplier = self.METRICS[i]
                title = "{}_{}".format(process, metric)
                samples[title] = float(value) * multiplier
            pid = stdout.split()[0]
        else:
            return samples

        stdout = self.run(self.TOP_CMD.format(pid))
        if stdout:
            title = "{}_cpu".format(process)
            samples[title] = float(stdout.split()[8])
        return samples

from cbagent.collectors.libstats.remotestats import RemoteStats, parallel_task


class PSStats(RemoteStats):

    METRICS = (
        ("rss", 1024),    # kB -> B
        ("vsize", 1024),
    )

    PS_CMD = "ps -eo pid,rss,vsize,comm | " \
        "grep {} | grep -v grep | sort -n -k 2 | tail -n 1"

    TOP_CMD = "top -b n2 -d1 -p {0} | grep ^{0}"

    @parallel_task(server_side=True)
    def get_server_samples(self, process):
        return self.get_samples(process)

    @parallel_task(server_side=False)
    def get_client_samples(self, process):
        return self.get_samples(process)

    def get_samples(self, process):
        samples = {}

        stdout = self.run(self.PS_CMD.format(process))
        if stdout:
            for i, value in enumerate(stdout.split()[1:1 + len(self.METRICS)]):
                metric, multiplier = self.METRICS[i]
                title = "{}_{}".format(process, metric)
                samples[title] = float(value) * multiplier
            pid = stdout.split()[0]
        else:
            return samples

        stdout = self.run(self.TOP_CMD.format(pid))
        if stdout:
            title = "{}_cpu".format(process)
            samples[title] = float(stdout.split()[8])
        return samples

Use more precise grep expression
Use more precise grep expression

Otherwise we match wrong lines when memory stats contain PID.

Change-Id: I924c1b151ddaad8209445a514bf02a7af5d2e0e0
Reviewed-on: http://review.couchbase.org/79848
Reviewed-by: Pavel Paulau <dd88eded64e90046a680e3a6c0828ceb8fe8a0e7@gmail.com>
Tested-by: Pavel Paulau <dd88eded64e90046a680e3a6c0828ceb8fe8a0e7@gmail.com>
