Authored by jack

更新写入的数据格式

... ... @@ -9,6 +9,6 @@ influxdb.connect.timeout=15;15
influxdb.read.timeout=40;40
influxdb.write.timeout=20;20
aws.influxdb.url=http://172.16.6.104:8086
aws.influxdb.url=http://192.168.102.76:8086
aws.influxdb.user=root
aws.influxdb.pwd=root
\ No newline at end of file
... ...
package com.monitor.zabbix.comp;
import com.monitor.zabbix.constants.Constants;
import com.squareup.okhttp.OkHttpClient;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
... ... @@ -34,7 +36,6 @@ public class InfluxdbComp {
@PostConstruct
public void init() {
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(15, TimeUnit.SECONDS);
... ... @@ -57,4 +58,15 @@ public class InfluxdbComp {
}
}
public void doWritePoint(Point point) {
try {
if (null != point) {
influxDBClient.write(Constants.INFLUX_DB,"default",point);
}
} catch (Exception e) {
DEBUG.error("Failed to write point {} to influx,error {} ", point, e);
}
}
}
... ...
... ... @@ -11,6 +11,8 @@ import org.influxdb.dto.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
... ... @@ -59,7 +61,7 @@ public class PointBuilder {
return point;
}
private static String findValueByKey(String key, List<ItemResponse.Item> items, List<HistoryResponse.HistoryItem> historyItems) {
private static Double findValueByKey(String key, List<ItemResponse.Item> items, List<HistoryResponse.HistoryItem> historyItems) {
String itemId = StringUtils.EMPTY;
... ... @@ -107,6 +109,28 @@ public class PointBuilder {
DEBUG.info("Find zabbix item key {} id {} value {}", key, itemId, itemValue);
return itemValue;
Double value = Double.parseDouble(itemValue);
return format(key, value);
}
public static Double format(String key, Double value) {
DecimalFormat decimalFormat = new DecimalFormat("0.00");
if (StringUtils.startsWith(key, "system.cpu")) {
//%
return new Double(decimalFormat.format(value));
} else if (StringUtils.startsWith(key, "net.if")) {
//kBps
return new Double(decimalFormat.format((value / 8192)));
} else if (StringUtils.startsWith(key, "vm.memory")) {
//MB
return new Double(decimalFormat.format((value / (1024 * 1024))));
} else {
return new Double(0.00);
}
}
}
... ...