/*
 * Copyright (c) 2013 mgm technology partners GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mgmtp.perfload.perfmon.cmd;

import static com.mgmtp.perfload.perfmon.util.PerfMonUtils.appendLineBreak;
import static com.mgmtp.perfload.perfmon.util.PerfMonUtils.appendPercent;

import org.apache.commons.lang.text.StrBuilder;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;

/**
 * @author rnaegele
 */
public class PerfMonCpu extends BasePerfMonCommand {

	private static final String TYPE_CPU_AVG = "cpu_X";
	private static final String TYPE_CPU_X = "cpu_";

	public PerfMonCpu(final String separator) {
		super(separator);
	}

	@Override
	public String executeCommand(final SigarProxy sigar) throws SigarException {
		StrBuilder sb = new StrBuilder(200);

		CpuPerc[] cpus = sigar.getCpuPercList();
		CpuPerc cpuAll = sigar.getCpuPerc();

		sb.append(TYPE_CPU_AVG);
		sb.append(separator);
		writeCpu(sb, cpuAll);

		if (cpus.length > 1) { // more than one CPU
			appendLineBreak(sb);
			for (int i = 0; i < cpus.length; i++) {
				appendLineBreak(sb, i);
				sb.append(TYPE_CPU_X + i);
				sb.append(separator);
				writeCpu(sb, cpus[i]);
			}
		}

		return sb.toString();
	}

	private void writeCpu(final StrBuilder sb, final CpuPerc cpuPerc) {
		appendPercent(sb, cpuPerc.getCombined());
		sb.append(separator);
		appendPercent(sb, cpuPerc.getUser());
		sb.append(separator);
		appendPercent(sb, cpuPerc.getNice());
		sb.append(separator);
		appendPercent(sb, cpuPerc.getSys());
		sb.append(separator);
		appendPercent(sb, cpuPerc.getWait());
		sb.append(separator);
		appendPercent(sb, cpuPerc.getIdle());
	}
}

@@ -1082,19 +1082,17 @@
 YPE_CPU_
-AVG
+X
  = %22cpu_
@@ -1124,34 +1124,32 @@
  String TYPE_CPU
-_X
  = %22cpu_%22;%0A%0A%09pub
@@ -1450,11 +1450,9 @@
 CPU_
-AVG
+X
 );%0A%09
@@ -1662,18 +1662,16 @@
 TYPE_CPU
-_X
  + i);%0A%09

Rename constants