zno2

kube 小工具 java log

 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class log {

    public static void main(String[] args) throws IOException {

        String namespace = null;
        String appname = null;
        
        if(args.length > 0) {
            namespace = args[0];
        }
        if(args.length > 1) {
            appname = args[1];
        }
        
        
        String out = kubectlgetpods(namespace);

        List<String> lines = toLines(out);
        StringBuffer sb = new StringBuffer();
        for (String line : lines) {
            sb.append(line).append("\r\n");
        }

        if(namespace == null) {
            return;
        }
        
        List<Map<String, String>> table = table(sb.toString(),
                new String[] { "NAME", "READY", "STATUS", "RESTARTS", "AGE"},
                new String[] { "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"});

        String name = null;
        for (Map<String, String> map : table) {
            if(appname != null && map.get("NAME").contains(appname)) {
                name = map.get("NAME");
                 kubectllogs(namespace,name);
            }
        }

       


    }

    public static void kubectllogs(String namespace, String name) throws IOException {
//        Runtime runtime = Runtime.getRuntime();
        String command = "kubectl -n "+namespace+" logs -f " + name + " --since=10m";
        System.out.println(command);
//        Process process = runtime.exec(command);
//        String result = toStr(process.getInputStream());
//        process.destroy();
//        return result;
    }
    
    public static String kubectlgetpods(String namespace) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        String cmd = "kubectl get namespaces";
        if(namespace != null) {
            cmd = "kubectl -n "+namespace+" get pods";
        }
        Process process = runtime.exec(cmd);
        String result = toStr(process.getInputStream());
        process.destroy();
        System.out.println(result);
        return result;
    }

    /**
     * 将流转为文本
     * @param in
     * @return
     * @throws IOException
     */
    public static String toStr(InputStream in) throws IOException {
        BufferedInputStream b = new BufferedInputStream(in);
        byte[] buffer = new byte[1024];
        int l = 0;
        StringBuffer sb = new StringBuffer();
        while ((l = b.read(buffer)) != -1) {
            sb.append(new String(buffer, 0, l));
        }
        return sb.toString();
    }
    
    /**
     * 将shell命令输出的文本转换为表格 
     * @param text    shell output
     * @param columns
     * @param aligns   LEFT or RIGHT
     * @return
     * @throws IOException
     */
    public static List<Map<String, String>> table(String text, String[] columns, String[] aligns) throws IOException {

        // 不提供默认左对齐 
        if (aligns == null) {
            aligns = new String[columns.length];
            for (int i = 0; i < aligns.length; i++) {
                aligns[i] = "LEFT";
            }
        }

        List<String> lines = toLines(text);
        List<Integer> verticalLineList = new ArrayList<Integer>();

        String titleLine = lines.get(0);

        for (int i = 0; i < columns.length - 1; i++) {
            String columnA = columns[i];
            String columnB = columns[i + 1];
            int boardLeft = 0;
            int boardRigth = 0;
            if (titleLine.indexOf(" " + columnA + " ") > -1) {
                boardLeft = titleLine.indexOf(" " + columnA + " ") + 1;
            } else if (titleLine.indexOf(columnA + " ") > -1) {
                boardLeft = titleLine.indexOf(columnA + " ");
            } else {
                boardLeft = titleLine.indexOf(columnA);
            }
            if (titleLine.indexOf(" " + columnB + " ") > -1) {
                boardRigth = titleLine.indexOf(" " + columnB + " ") + columnB.length() + 1;
            } else if (titleLine.indexOf(" " + columnB) > -1) {
                boardRigth = titleLine.indexOf(" " + columnB) + columnB.length() + 1;
            } else {
                boardRigth = titleLine.indexOf(columnB) + columnB.length();
            }

            Integer verticalLine = null;
            if ("LEFT".equals(aligns[i]) && "RIGHT".equals(aligns[i + 1])) {
                Pattern p = Pattern.compile("[^  \r\r\n]+");
                verticalLine = boardLeft + columnA.length();

                for (String line : lines) {
                    String frag = line.substring(boardLeft, boardRigth);
                    Matcher matcher = p.matcher(frag);
                    matcher.find();
                    String group = matcher.group();
                    if (boardLeft + group.length() > verticalLine) {
                        verticalLine = boardLeft + group.length();
                    }
                }
            } else if ("LEFT".equals(aligns[i]) && "LEFT".equals(aligns[i + 1])) {
                verticalLine = boardRigth - columnB.length();
            } else if ("RIGHT".equals(aligns[i]) && "RIGHT".equals(aligns[i + 1])) {
                verticalLine = boardLeft + columnA.length();
            } else if ("RIGHT".equals(aligns[i]) && "LEFT".equals(aligns[i + 1])) {
                verticalLine = boardLeft + columnA.length();
            }
            verticalLineList.add(verticalLine);
        }

        List<Map<String, String>> table = new ArrayList<>();
        for (String line : lines) {
            Map<String, String> item = new LinkedHashMap<>();
            int size = verticalLineList.size();
            for (int i = 0; i < size; i++) {
                Integer right = verticalLineList.get(i);
                Integer left = 0;
                if (i > 0) {
                    left = verticalLineList.get(i - 1);
                }
                item.put(columns[i], line.substring(left, right).trim());
            }
            item.put(columns[size], line.substring(verticalLineList.get(size - 1)));
            table.add(item);
        }

        return table;
    }
    
    /**
     * 将文本转为行列表 
     * @param text
     * @return
     * @throws IOException
     */
    public static List<String> toLines(String text) throws IOException{
        List<String> lines = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new StringReader(text));
        String l;
        while((l=br.readLine()) != null) {
            lines.add(l);
        }
        return lines;
    }
}

 

示例:

java log

[qishi@xs-srv-dev-01 ~]$ java log
NAME                              STATUS   AGE
apollo                            Active   90d
apollo-block                      Active   9d
default                           Active   108d
dev-xxxxx                         Active   108d
dev-qishi-12328                   Active   90d
fat-xxxxx                         Active   108d
fat-xxxxx-12328                   Active   90d
kube-node-lease                   Active   108d
kube-public                       Active   108d
kube-system                       Active   108d
kubekey-system                    Active   108d
kubesphere-controls-system        Active   108d
kubesphere-monitoring-federated   Active   108d
kubesphere-monitoring-system      Active   108d
kubesphere-system                 Active   108d
uat-xxxxx                         Active   108d

 

java log apollo

NAME                                                      READY   STATUS    RESTARTS   AGE
apollo-portal-5f75d7c7f5-qckqr                            1/1     Running   0          31d
apollo-service-fat-apollo-adminservice-6f4f98d688-vzbhx   1/1     Running   0          31d
apollo-service-fat-apollo-configservice-6fc8d478d-ngg9r   1/1     Running   0          31d

 

java log apollo portal

NAME                                                      READY   STATUS    RESTARTS   AGE
apollo-portal-5f75d7c7f5-qckqr                            1/1     Running   0          31d
apollo-service-fat-apollo-adminservice-6f4f98d688-vzbhx   1/1     Running   0          31d
apollo-service-fat-apollo-configservice-6fc8d478d-ngg9r   1/1     Running   0          31d

kubectl -n apollo logs -f apollo-portal-5f75d7c7f5-qckqr --since=10m

 

posted on 2023-08-03 19:45  zno2  阅读(8)  评论(0编辑  收藏  举报

导航