wukai 7 сар өмнө
parent
commit
1da5810109

+ 16 - 0
jjt-biz/src/main/java/com/jjt/biz/service/IPinpointService.java

@@ -68,6 +68,14 @@ public interface IPinpointService {
     void cleanAgent();
 
     /**
+     * 删除agent
+     *
+     * @param applicationName 应用名称
+     * @param agentId         应用ID
+     */
+    void removeAgent(String applicationName, String agentId);
+
+    /**
      * 通过应用名称获取agent列表
      *
      * @param applicationName 应用名称
@@ -76,4 +84,12 @@ public interface IPinpointService {
      * @return 结果
      */
     List<AgentVO> agentList(String applicationName, LocalDateTime st, LocalDateTime ed);
+
+    /**
+     * 通过应用名称获取agent列表,不带时间
+     *
+     * @param applicationName 应用名称
+     * @return 结果
+     */
+    List<AgentVO> agentList(String applicationName);
 }

+ 66 - 1
jjt-biz/src/main/java/com/jjt/biz/service/impl/PinpointServiceImpl.java

@@ -238,7 +238,33 @@ public class PinpointServiceImpl implements IPinpointService {
     }
 
     /**
-     * 通过应用名称获取agent列表
+     * 删除agent
+     *
+     * @param applicationName 应用名称
+     * @param agentId         应用ID
+     */
+    @Override
+    public void removeAgent(String applicationName, String agentId) {
+        String uri = baseUri() + "/admin/removeAgentId.pinpoint";
+        Map<String, Object> map = new HashMap<>(16);
+        map.put("applicationName", applicationName);
+        map.put("agentId", agentId);
+        map.put("password", "admin");
+        HttpRequest request = HttpUtil.createGet(uri);
+        request.setMethod(Method.GET);
+        request.form(map);
+        request.setConnectionTimeout(2000);
+        try (HttpResponse execute = request.execute()) {
+            if (!execute.isOk()) {
+                throw new RuntimeException("status:" + execute.getStatus() + "\tres:" + execute.body());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 通过应用名称获取agent列表,带时间
      *
      * @param applicationName 应用名称
      * @param st              开始时间
@@ -282,4 +308,43 @@ public class PinpointServiceImpl implements IPinpointService {
         return result;
     }
 
+    /**
+     * 通过应用名称获取agent列表,不带时间
+     *
+     * @param applicationName 应用名称
+     * @return 结果
+     */
+    @Override
+    public List<AgentVO> agentList(String applicationName) {
+        List<AgentVO> result = new ArrayList<>();
+        String uri = baseUri() + "/getAgentList.pinpoint";
+        Map<String, Object> map = new HashMap<>(16);
+        map.put("application", applicationName);
+        HttpRequest request = HttpUtil.createGet(uri);
+        request.setMethod(Method.GET);
+        request.form(map);
+        request.setConnectionTimeout(2000);
+        try (HttpResponse execute = request.execute()) {
+            if (execute.isOk()) {
+                String res = new String(execute.body().getBytes(), StandardCharsets.UTF_8);
+                cn.hutool.json.JSONObject object = JSONUtil.parseObj(res, true);
+                for (String key : object.keySet()) {
+                    cn.hutool.json.JSONArray arr = object.getJSONArray(key);
+                    for (int i = 0; i < arr.size(); i++) {
+                        AgentVO vo = new AgentVO();
+                        cn.hutool.json.JSONObject obj = arr.getJSONObject(i);
+                        Integer code = obj.getByPath("status.state.code", Integer.class);
+                        String agentId = obj.getByPath("status.agentId", String.class);
+                        vo.setCode(code);
+                        vo.setAgentId(agentId);
+                        result.add(vo);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return result;
+    }
+
 }

+ 37 - 3
jjt-biz/src/main/java/com/jjt/task/CleanTask.java

@@ -1,13 +1,21 @@
 package com.jjt.task;
 
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import com.jjt.biz.domain.BizObj;
 import com.jjt.biz.service.IBizObjMetricsDataService;
+import com.jjt.biz.service.IBizObjService;
 import com.jjt.biz.service.IPinpointService;
+import com.jjt.biz.service.IPrometheusService;
+import com.jjt.biz.vo.AgentVO;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.time.LocalDate;
 import java.time.ZoneId;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
 
 /**
  * 定时任务调度测试
@@ -19,7 +27,11 @@ public class CleanTask {
     @Resource
     private IBizObjMetricsDataService dataService;
     @Resource
-    private IPinpointService pinpointService;
+    private IPinpointService ppService;
+    @Resource
+    private IPrometheusService pmService;
+    @Resource
+    private IBizObjService objService;
 
     /**
      * 清理历史数据
@@ -34,8 +46,30 @@ public class CleanTask {
     /**
      * 清理pinpoint失效agent
      */
-    public void ppAgent() {
-//        dataService.clean(date);
+    public void agent() {
+        String exp = "kube_pod_info {namespace=\"default\",exported_pod=~\"${instance}\"}";
+        String findKey = "exported_pod";
+        BizObj queryObj = new BizObj();
+        queryObj.setObjType("1");
+        objService.selectBizObjList(new BizObj()).forEach(obj -> {
+            String instance = obj.getObjAddr();
+            String str = exp.replace("${instance}", instance + ".*");
+            JSONObject jsonObject = pmService.find(str);
+            List<String> existsAgentIds = new ArrayList<>();
+            if (jsonObject != null) {
+                JSONArray metric = jsonObject.getByPath("data.result", cn.hutool.json.JSONArray.class);
+                for (int i = 0; i < metric.size(); i++) {
+                    String agentId = metric.getJSONObject(i).getByPath("metric." + findKey, String.class);
+                    existsAgentIds.add(agentId);
+                }
+            }
+            List<AgentVO> agents = ppService.agentList(instance);
+            for (AgentVO vo : agents) {
+                if (!existsAgentIds.contains(vo.getAgentId())) {
+                    ppService.removeAgent(instance, vo.getAgentId());
+                }
+            }
+        });
     }