先创建一个自定义钉钉机器人。

在头像里面点击设置

然后找到机器人管理,前往设置

 

打开后自定义一个机器人并设置好安全设置,我这里采用指定ip

设置好之后拷贝Webhook地址到下面代码中替换就行了

public static void main(String[] args) throws Exception {
        File[] disks = File.listRoots();
        long totalSpace = 0;
        long freeSpace = 0;
        for (File file : disks) {
            totalSpace += file.getTotalSpace();
            freeSpace += file.getFreeSpace();
        }
        double a = totalSpace / (1024 * 1024) / 1024;
        double b = freeSpace / (1024 * 1024) / 1024;
        double c = (b / a);
        if (c < 1) {
            // 钉钉的webhook
            String dingDingToken = "替换成你自己的地址";
            Map<String, Object> json = new HashMap();
            Map<String, Object> text = new HashMap();
            json.put("msgtype", "text");
            text.put("content", "磁盘的空间大小为:" + a + "G\n" + "磁盘的剩余空间大小为:" + b + "G\n剩余空间不足" + String.format("%.2f", c * 100) + "%");
            json.put("text", text);
            System.out.println(JSON.toJSONString(json));
            HttpUtil.sendJSON(dingDingToken, JSON.toJSONString(json));
        }
    }

 这是发送的代码

maven

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.5</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.76</version>
    </dependency>

 Java代码

    public static void main(String[] args) throws Exception {
        File[] disks = File.listRoots();
        long totalSpace = 0;
        long freeSpace = 0;
        for (File file : disks) {
            totalSpace += file.getTotalSpace();
            freeSpace += file.getFreeSpace();
        }
        double a = totalSpace / (1024 * 1024) / 1024;
        double b = freeSpace / (1024 * 1024) / 1024;
        double c = (b / a);
        if (c < 1) {
            // 钉钉的webhook
            String dingDingToken = "替换成你自己的地址";
            Map<String, Object> json = new HashMap();
            Map<String, Object> text = new HashMap();
            json.put("msgtype", "text");
            text.put("content", "磁盘的空间大小为:" + a + "G\n" + "磁盘的剩余空间大小为:" + b + "G\n剩余空间不足" + String.format("%.2f", c * 100) + "%");
            json.put("text", text);
            System.out.println(JSON.toJSONString(json));
            sendJSON(dingDingToken, JSON.toJSONString(json));
        }
    }
    public static String sendJSON(String url, String json) throws Exception {
        String res = "";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost request = new HttpPost(url);
        StringEntity params = new StringEntity(json, HTTP.UTF_8);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        CloseableHttpResponse response = httpClient.execute(request);
        res = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        httpClient.close();
        return res;
    }

最后就大功告成了

 

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐