目录
PostMan瞬时数据查询:Windows主机空闲内存量
请求
curl -X GET \ 'http://10.0.50.225:9090/api/v1/query?query= windows_os_physical_memory_free_bytes' \ -H 'Postman-Token: a01350a7-d0a2-46cb-a356-ae57926d81e9' \ -H 'cache-control: no-cache'
响应数据
{ "status": "success", "data": { "resultType": "vector", "result": [ { "metric": { "__name__": "windows_os_physical_memory_free_bytes", "instance": "10.0.50.225:9182", "job": "Win-225" }, "value": [ 1599698348.272, "7697670144" ] }, { "metric": { "__name__": "windows_os_physical_memory_free_bytes", "instance": "10.0.50.236:9182", "job": "Win-236" }, "value": [ 1599698348.272, "14977138688" ] } ] } }
Java:调用PromQL工具类(瞬时数据查询)
Prometheus返回结果反序列化
import lombok.Data; /** * @Title: prometheus返回信息 * @Description: * * @Copyright 2020-2021 - Powered By 研发中心 * @author: 王延飞 * @date: 2020/9/8 0008 17:00 * @version V1.0 */ @Data public class PromResponceInfo { /** * 状态 * 成功-- success */ private String status; /** * prometheus指标属性和值 */ private PromDataInfo data; }
import lombok.Data; import java.util.List; /** * @Title: prometheus指标属性和值 * @Description: * * @Copyright 2020-2021 - Powered By 研发中心 * @author: 王延飞 * @date: 2020/9/8 0008 17:00 * @version V1.0 */ @Data public class PromDataInfo { /** * prometheus结果类型 * vector--瞬时向量 * matrix--区间向量 * scalar--标量 * string--字符串 */ private String resultType; /** * prometheus指标属性和值 */ private List<PromResultInfo> result; }
import lombok.Data; /** * @Title: prometheus指标值 * @Description: * * @Copyright 2020-2021 捷安高科 - Powered By 研发中心 * @author: 王延飞 * @date: 2020/9/8 0008 16:53 * @version V1.0 */ @Data public class PromResultInfo { /** * prometheus指标属性 */ private PromMetricInfo metric; /** * prometheus指标值 */ private String[] value; }
import lombok.Data; /** * @Title: prometheus指标属性(这里只反序列化了需要的属性,用户可根据需要做增减) * @Description: * * @Copyright 2020-2021 - Powered By 研发中心 * @author: 王延飞 * @date: 2020/9/8 0008 16:53 * @version V1.0 */ @Data public class PromMetricInfo { /** * prometheus指标名称 */ private String __name__; /** * prometheus实例名称 */ private String instance; /** * prometheus任务名称 */ private String job; }
Prometheus常用PromQL常量
/** * @Title: prometheus常量信息 * @Description: * * @Copyright 2020-2021 - Powered By 研发中心 * @author: 王延飞 * @date: 2020/9/9 0009 9:50 * @version V1.0 */ public class PromConstants { /** * prometheus-查询SUCCESS */ public static final String SUCCESS = "success"; /** * prometheus-查询参数 */ public static final String QUERY = "query"; /** * Windows主机操作系统信息 */ public static final String WINDOWS_OS_INFO = "windows_os_info"; /** * Windows主机空闲内存量(单位:字节,1GB=1024*1024*1024Bytes) */ public static final String WINDOWS_OS_PHYSICAL_MEMORY_FREE_BYTES = "windows_os_physical_memory_free_bytes"; /** * Windows主机空闲CPU量(单位:GHZ) */ public static final String WINDOWS_CPU_TIME_TOTAL_IDLE = "sum by (instance) (irate(windows_cpu_time_total{mode=\"idle\"}[1m]))"; /** * Windows主机CPU量--按模式(单位:GHZ) */ // public static final String WINDOWS_CPU_TIME_TOTAL_MODE = "sum by (mode) (irate(windows_cpu_time_total[1m]))"; /** * Windows主机空闲GPU量(单位:GB) */ public static final String WINDOWS_GPU_MEMORY_FREE = "memory_free"; }
Java调用Prometheus API
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; import java.util.Objects; /** * @Title: prometheus工具类 * @Description: * * @Copyright 2020-2021 捷安高科 - Powered By 研发中心 * @author: 王延飞 * @date: 2020/9/9 0009 11:20 * @version V1.0 */ public class PromInfoUtils { private static final Logger log = LoggerFactory.getLogger(PromInfoUtils.class); /** * @Title: Windows主机操信息 * @MethodName: getWindowsOsInfo * @param promURL * @param promQL * @Exception * @Description: * * @author: 王延飞 * @date: 2020/9/9 0009 11:21 */ public static List<PromResultInfo> getWindowsInfo(String promURL, String promQL) { log.info("【Windows主机信息】,请求地址:{},请求QL:{}", promURL, promQL); JSONObject param = new JSONObject(); param.put(PromConstants.QUERY, promQL); String http = null; try { http = RestTemplateUtils.getHttp(promURL, param, 10000, 10000, 1); } catch (Exception e) { log.error("【Windows主机信息】异常,请求地址:{},请求QL:{},异常信息:{}", promURL, promQL, e); } PromResponceInfo responceInfo = JSON.parseObject(http, PromResponceInfo.class); log.info("【Windows主机信息】,请求地址:{},请求QL:{},返回信息:{}", promURL, promQL, responceInfo); if (Objects.isNull(responceInfo)) { return null; } String status = responceInfo.getStatus(); if (StringUtils.isBlank(status) || !PromConstants.SUCCESS.equals(status) ) { return null; } List<PromResultInfo> result = responceInfo.getData().getResult(); return result; } public static void main(String[] args) { // List<PromResultInfo> windowsOsInfo = getWindowsInfo("http://10.0.50.225:9090/api/v1/query", PromConstants.WINDOWS_OS_INFO); List<PromResultInfo> windowsOsInfo = getWindowsInfo("http://10.0.50.225:9090/api/v1/query", PromConstants.WINDOWS_GPU_MEMORY_FREE); System.out.println(windowsOsInfo); } }
其中RestTemplateUtils,参考