<commit_before>package ar.com.threelegs.newrelic.cassandra;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.newrelic.metrics.publish.Agent;
import com.newrelic.metrics.publish.processors.EpochCounter;
import com.typesafe.config.Config;

/**
 * @author juanformoso
 */
public class Varnish extends Agent {

	private String name;
	private Config config;
	private Map<Pattern, String> units = new HashMap<Pattern, String>();
	private Map<Pattern, String> groups = new HashMap<Pattern, String>();

	public Varnish(Config config) {
		super("ar.com.3legs.newrelic.varnish", "0.0.2");
		this.name = config.getString("name");
		this.config = config;

		units.put(Pattern.compile("client_.*"), "connections");
		units.put(Pattern.compile("backend_.*"), "connections");
		units.put(Pattern.compile("cache_.*"), "requests");
		units.put(Pattern.compile("fetch_.*"), "fetchs");
		units.put(Pattern.compile("n_wrk.*"), "threads");

		groups.put(Pattern.compile("n_wrk.*"), "threads");
	}

	@Override
	public String getComponentHumanLabel() {
		return name;
	}

	@Override
	public void pollCycle() {
		List<Metric> allMetrics = new ArrayList<Metric>();
		try {
			String command = "";

			if (config.hasPath("user")) {
				command += "ssh -t " + config.getString("user") + "@" + config.getString("host");
			}

			command += " varnishstat -1 -x";

			if (config.hasPath("instance")) {
				command += " -n " + config.getString("instance");
			}

			Process p = Runtime.getRuntime().exec(command);

			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
			StringBuilder builder = new StringBuilder();
			String aux = "";

			while ((aux = in.readLine()) != null) {
				builder.append(aux);
			}

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(new InputSource(new ByteArrayInputStream(builder.toString().replace("\t", "").getBytes("utf-8"))));

			NodeList nlist = dom.getElementsByTagName("stat");

			for (int i = 0; i < nlist.getLength(); i++) {
				allMetrics.add(getMetric((Element) nlist.item(i)));
			}

			p.waitFor();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			System.out.println("pushing " + allMetrics.size() + " metrics...");
			for (Metric m : allMetrics) {
				System.out.println(m.name + "[" + m.valueType + "] --> " + m.value);
				reportMetric(m.name, m.valueType, m.value);
			}
			System.out.println("done!");
		}
	}

	private Map<String, EpochCounter> epochCounters = new HashMap<String, EpochCounter>();

	private Metric getMetric(Element n) throws ParseException {
		String type = n.getElementsByTagName("type").item(0) != null ? n.getElementsByTagName("type").item(0).getTextContent() : null;
		String ident = n.getElementsByTagName("ident").item(0) != null ? n.getElementsByTagName("ident").item(0).getTextContent() : null;
		String name = n.getElementsByTagName("name").item(0) != null ? n.getElementsByTagName("name").item(0).getTextContent() : null;
		String description = n.getElementsByTagName("description").item(0) != null ? n.getElementsByTagName("description").item(0).getTextContent()
				: null;
		String value = n.getElementsByTagName("value").item(0) != null ? n.getElementsByTagName("value").item(0).getTextContent() : null;
		String flag = n.getElementsByTagName("flag").item(0) != null ? n.getElementsByTagName("flag").item(0).getTextContent() : null;

		StringBuilder key = new StringBuilder();
		key.append("Varnish/");<commit_msg>removed loggin to console of varnish metrics<commit_after>package ar.com.threelegs.newrelic.cassandra;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.newrelic.metrics.publish.Agent;
import com.newrelic.metrics.publish.processors.EpochCounter;
import com.typesafe.config.Config;

/**
 * @author juanformoso
 */
public class Varnish extends Agent {

	private String name;
	private Config config;
	private Map<Pattern, String> units = new HashMap<Pattern, String>();
	private Map<Pattern, String> groups = new HashMap<Pattern, String>();

	public Varnish(Config config) {
		super("ar.com.3legs.newrelic.varnish", "0.0.2");
		this.name = config.getString("name");
		this.config = config;

		units.put(Pattern.compile("client_.*"), "connections");
		units.put(Pattern.compile("backend_.*"), "connections");
		units.put(Pattern.compile("cache_.*"), "requests");
		units.put(Pattern.compile("fetch_.*"), "fetchs");
		units.put(Pattern.compile("n_wrk.*"), "threads");

		groups.put(Pattern.compile("n_wrk.*"), "threads");
	}

	@Override
	public String getComponentHumanLabel() {
		return name;
	}

	@Override
	public void pollCycle() {
		List<Metric> allMetrics = new ArrayList<Metric>();
		try {
			String command = "";

			if (config.hasPath("user")) {
				command += "ssh -t " + config.getString("user") + "@" + config.getString("host");
			}

			command += " varnishstat -1 -x";

			if (config.hasPath("instance")) {
				command += " -n " + config.getString("instance");
			}

			Process p = Runtime.getRuntime().exec(command);

			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
			StringBuilder builder = new StringBuilder();
			String aux = "";

			while ((aux = in.readLine()) != null) {
				builder.append(aux);
			}

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(new InputSource(new ByteArrayInputStream(builder.toString().replace("\t", "").getBytes("utf-8"))));

			NodeList nlist = dom.getElementsByTagName("stat");

			for (int i = 0; i < nlist.getLength(); i++) {
				allMetrics.add(getMetric((Element) nlist.item(i)));
			}

			p.waitFor();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			System.out.println("pushing " + allMetrics.size() + " metrics...");
			for (Metric m : allMetrics) {
				// System.out.println(m.name + "[" + m.valueType + "] --> " +
				// m.value);
				reportMetric(m.name, m.valueType, m.value);
			}
			System.out.println("done!");
		}
	}

	private Map<String, EpochCounter> epochCounters = new HashMap<String, EpochCounter>();

	private Metric getMetric(Element n) throws ParseException {
		String type = n.getElementsByTagName("type").item(0) != null ? n.getElementsByTagName("type").item(0).getTextContent() : null;
		String ident = n.getElementsByTagName("ident").item(0) != null ? n.getElementsByTagName("ident").item(0).getTextContent() : null;
		String name = n.getElementsByTagName("name").item(0) != null ? n.getElementsByTagName("name").item(0).getTextContent() : null;
		String description = n.getElementsByTagName("description").item(0) != null ? n.getElementsByTagName("description").item(0).getTextContent()
				: null;
		String value = n.getElementsByTagName("value").item(0) != null ? n.getElementsByTagName("value").item(0).getTextContent() : null;
		String flag = n.getElementsByTagName("flag").item(0) != null ? n.getElementsByTagName("flag").item(0).getTextContent() : null;

		StringBuilder key = new StringBuilder();
		key.append("Varnish/");
{'fraction_non_alphanumeric': 0.16466806774632217, 'fraction_numerical': 0.0054394857213499815, 'mean_word_length': 4.749822316986496, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2270076', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2546, 'n_words': 489}