package com.youchain.modules.license;

import com.youchain.utils.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * <p>用于获取客户Linux服务器的基本信息</p>
 * @author 92525
 */
public class LinuxServerInfos extends AGxServerInfos {



    @Override
    protected String getCpuSerial() throws Exception {
        //序列号
        String serialNumber = "";

        try{
            String[] shell = {"/bin/bash","-c","dmidecode -t processor | grep 'ID' | awk -F ':' '{print $2}' | head -n 1"};
            Process process = Runtime.getRuntime().exec(shell);
            process.getOutputStream().close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = reader.readLine().trim();
            if(StringUtils.isNotBlank(line)){
                serialNumber = line;
            }
            reader.close();
        }catch (Exception e){
            serialNumber="";
        }
        //使用dmidecode命令获取CPU序列号

        return serialNumber;
    }

    @Override
    protected String getMainBoardSerial() throws Exception {
        //序列号
        String serialNumber = "";
        try{
            //使用dmidecode命令获取主板序列号
            String[] shell = {"/bin/bash","-c","dmidecode | grep 'Serial Number' | awk -F ':' '{print $2}' | head -n 1"};
            Process process = Runtime.getRuntime().exec(shell);
            process.getOutputStream().close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = reader.readLine().trim();
            if(StringUtils.isNotBlank(line)){
                serialNumber = line;
            }
            reader.close();
        }catch (Exception e){
            serialNumber = "";
        }
        return serialNumber;
    }
}
