SendAlarmTool.java 1.14 KB
/**
 * 
 */
package com.yoho.jobs.server.alarm;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yanzhang.fu
 *
 */
public class SendAlarmTool {

	private Logger logger = LoggerFactory.getLogger(SendAlarmTool.class);

	private InfluxDbSource dbSource;

	private static final String DATABASE = "yoho-monitor";

	private static final String MEASUREMENT = "yoho_job_alarm";

	public void sendAlarm(Map<String, String> tags, Map<String, Object> fields) {
		logger.info("send  job alarm");
		try {
			Builder builder = Point.measurement(MEASUREMENT).time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
			builder.tag(tags);
			builder.fields(fields);
			dbSource.getInfluxDb().write(DATABASE, "default", builder.build());
			logger.info("send alarm {} success", this.MEASUREMENT);
		} catch (Exception e) {
			logger.error("send job alarm failed.", e);
		}
	}

	public InfluxDbSource getDbSource() {
		return dbSource;
	}

	public void setDbSource(InfluxDbSource dbSource) {
		this.dbSource = dbSource;
	}

}