'use strict';

const os = require('os');
const numberOfCpus = os.cpus().length;
const Monitor = require('./objects/monitor');

class StatsdAgentMonitor extends Monitor {
    constructor() {
        super('statsd_agent');
    }

    collect() {
        const lastProcessCpuUsage = process.cpuUsage(this.lastProcessCpuUsage);

        if (this.lastProcessCpuUsage != null) {
            const lastCollectIntervalMicroseconds = (Date.now() - this.lastCollectTime) * numberOfCpus * 1000;

            this.setStatistics([
                ['user', ((lastProcessCpuUsage.user / lastCollectIntervalMicroseconds) * 100).toFixed(2)],
                ['system', ((lastProcessCpuUsage.system / lastCollectIntervalMicroseconds) * 100).toFixed(2)],
            ]);
        }

        this.lastProcessCpuUsage = lastProcessCpuUsage;
        this.lastCollectTime = Date.now();
    }
}

module.exports = StatsdAgentMonitor;
@@ -18,112 +18,560 @@
 nst 
-os = require('os');%0Aconst numberOfCpus = os.cpus().length;%0Aconst Monitor = require('./objects/monitor');
+Promise = require('bluebird');%0Aconst os = require('os');%0Aconst fs = Promise.promisifyAll(require('fs'));%0A%0Aconst Monitor = require('./objects/monitor');%0A%0Aconst numberOfTicksPerSecond = 100;%0Aconst ticksInMicrosecond = 100000 / numberOfTicksPerSecond;%0Aconst numberOfCpus = os.cpus().length;%0Aconst platform = process.platform;%0Aconst hasProcessCpuUsage = process.cpuUsage != null && typeof process.cpuUsage === 'function';%0A%0Aif (platform !== 'linux' && !hasProcessCpuUsage) %7B%0A    throw new Error(%60Monitor fails to find NodeJS v6.1.0+ or a linux platform%60);%0A%7D
 %0A%0Acl
@@ -693,25 +693,23 @@
         
-const las
+this.ge
 tProcess
@@ -720,33 +720,53 @@
 sage
- = p
+(this.lastP
 rocess
-.c
+C
 puUsage
-(this.
+)%0A            .then(
 last
@@ -780,20 +780,30 @@
 CpuUsage
-);%0A%0A
+ =%3E %7B%0A        
         
@@ -850,24 +850,32 @@
             
+     
+   
 const lastCo
@@ -970,24 +970,32 @@
             
+        
 this.setStat
@@ -1019,16 +1019,24 @@
         
+ 
+       
 %5B'user',
@@ -1134,16 +1134,24 @@
         
+    
+    
 %5B'system
@@ -1249,16 +1249,24 @@
         
+        
 %5D);%0A    
@@ -1273,19 +1273,31 @@
     
+  
-%7D%0A%0A
+      %7D%0A%0A    
         
 this
@@ -1284,32 +1284,36 @@
  %7D%0A%0A            
+    
 this.lastProcess
@@ -1344,16 +1344,24 @@
 uUsage;%0A
+        
         
@@ -1395,16 +1395,930 @@
 .now();%0A
+            %7D)%0A            .catchConsoleError();%0A    %7D%0A%0A    getProcessCpuUsage(lastProcessCpuUsage) %7B%0A        if (hasProcessCpuUsage) %7B%0A            return Promise.resolve(process.cpuUsage(lastProcessCpuUsage));%0A        %7D%0A%0A        return fs%0A            .readFileAsync(%60/proc/$%7Bprocess.pid%7D/stat%60)%0A            .then(data =%3E %7B%0A                const procParts = data.toString().split(' ');%0A%0A                const userTime = parseInt(procParts%5B13%5D) * ticksInMicrosecond;%0A                const systemTime = parseInt(procParts%5B14%5D) * ticksInMicrosecond;%0A%0A                if (lastProcessCpuUsage == null) %7B%0A                    return %7Buser: userTime, system: systemTime%7D;%0A                %7D else %7B%0A                    return %7B%0A                        user: userTime - lastProcessCpuUsage.user,%0A                        system: systemTime - lastProcessCpuUsage.system%0A                    %7D;%0A                %7D%0A            %7D);%0A
     %7D%0A%7D%0A

add linux support for self monitor below node 6.1