package com.nitorcreations.willow.deployer.statistics;

import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.beanutils.PropertyUtils;
import org.hyperic.sigar.ProcCpu;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;

import com.nitorcreations.willow.deployer.Main;
import com.nitorcreations.willow.messages.ProcessCPU;

@Named("process")
public class ProcessStatSender extends AbstractStatisticsSender {
  @Inject
  protected SigarProxy sigar;
  private String childName;
  private StatisticsConfig conf;
  @Inject
  protected Main main;
  private long nextProcCpus;


  @Override
  public void execute() {
    if (conf == null) {
      throw new IllegalStateException("Tried to start unconfigured statistics sender");
    }
    ProcCpu pCStat;
    long now = System.currentTimeMillis();
    if (now > nextProcCpus) {
      try {
        long pid = main.getChildPid(childName);
        if (pid > 0) {
          pCStat = sigar.getProcCpu(pid);
          ProcessCPU msg = new ProcessCPU();
          PropertyUtils.copyProperties(msg, pCStat);
          msg.addTags("category_processcpu_" + childName);
          transmitter.queue(msg);
        }
      } catch (SigarException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        logger.log(Level.WARNING, "Failed to Process CPU statistics", e);
      }
      nextProcCpus = nextProcCpus + conf.getIntervalProcCpus();
    }
    try {
      TimeUnit.MILLISECONDS.sleep(conf.shortest());
    } catch (InterruptedException e) {
      logger.info("Process statistics interrupted");
      return;
    }
  }

  @Override
  public void setProperties(Properties properties) {
    conf = new StatisticsConfig(properties);
    nextProcCpus = System.currentTimeMillis() + conf.getIntervalProcCpus();
    childName = properties.getProperty("childName");
    if (childName == null) {
      String[] children = main.getChildNames();
      if (children.length > 0) {
        childName = children[0];
      }
    }
  }
}

@@ -1070,25 +1070,28 @@
 hildPid(
-c
+getC
 hildName
 );%0A     
@@ -1074,32 +1074,34 @@
 Pid(getChildName
+()
 );%0A        if (p
@@ -2031,24 +2031,65 @@
 hildName%22);%0A
+  %7D%0A%0A  protected String getChildName() %7B%0A
     if (chil
@@ -2229,18 +2229,40 @@
     %7D%0A    %7D%0A
+    return childName;%0A
   %7D%0A%7D%0A

Fix processcpu statsender