`
baron.unsafe
  • 浏览: 80256 次
社区版块
存档分类
最新评论

Systemenv

    博客分类:
  • java
阅读更多
package com.xxx.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import sun.management.ManagementFactory;

import com.sun.management.OperatingSystemMXBean;

public class test 
{
	static HashMap <String, String> hm = new HashMap<String, String>();
	
	static
	{
		hm.put("java.version", "Java 运行时环境版本");
		hm.put("java.vendor","Java 运行时环境供应商");
		hm.put("java.vendor.url","Java 供应商的 URL");
		hm.put("java.home","Java 安装目录");
		hm.put("java.vm.specification.version","Java 虚拟机规范版本");
		hm.put("java.vm.specification.vendor","Java 虚拟机规范供应商");
		hm.put("java.vm.specification.name","Java 虚拟机规范名称");
		hm.put("java.vm.version","Java 虚拟机实现版本");
		hm.put("java.vm.vendor","Java 虚拟机实现供应商");
		hm.put("java.vm.name","Java 虚拟机实现名称");
		hm.put("java.specification.version","Java 运行时环境规范版本");
		hm.put("java.specification.vendor","Java 运行时环境规范供应商");
		hm.put("java.specification.name","Java 运行时环境规范名称");
		hm.put("java.class.version","Java 类格式版本号");
		hm.put("java.class.path","Java 类路径");
		hm.put("java.library.path","加载库时搜索的路径列表");
		hm.put("java.io.tmpdir","默认的临时文件路径");
		hm.put("java.compiler","要使用的 JIT 编译器的名称");
		hm.put("java.ext.dirs","一个或多个扩展目录的路径");
		hm.put("os.name","操作系统的名称");
		hm.put("os.arch","操作系统的架构");
		hm.put("os.version","操作系统的版本");
		hm.put("file.separator","文件分隔符(在 UNIX 系统中是“/”)");
		hm.put("path.separator","路径分隔符(在 UNIX 系统中是“:”)");
		hm.put("line.separator","行分隔符(在 UNIX 系统中是“/n”)");
		hm.put("user.name","用户的账户名称");
		hm.put("user.home","用户的主目录");
		hm.put("user.dir","用户的当前工作目录");
	}

	public static String getSystemEnv(String str)
	{
		return System.getProperty(str);
	}
	
	public static void main(String[] args)
	{
        System.out.println(SystemInfo());
        //SystemInfo4Html();
        
        OSBean();
	}
	
	public static void OSBean()
	{
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        
		// 总的物理内存
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize();
        // 剩余的物理内存
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
        // 已使用的物理内存
        long usedMemory = totalMemorySize - freePhysicalMemorySize;
        
        ThreadGroup parentThread;
        for (parentThread = Thread.currentThread().getThreadGroup(); 
        		parentThread.getParent() != null; 
        			parentThread = parentThread.getParent());
        
        int totalThread = parentThread.activeCount();
        
        double cpuRatio = 0;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().startsWith("windows")) 
        {
            cpuRatio = getCpuRatioForWindows();
        }
        else 
        {
        	cpuRatio = getCpuRateForLinux();
        }
        System.out.println("totalMemorySize=" + toMb(totalMemorySize) + "MB");
        System.out.println("freePhysicalMemorySize=" + toMb(freePhysicalMemorySize) + "MB");
        System.out.println("usedMemory=" + toMb(usedMemory) + "MB");
        System.out.println("totalThread=" + totalThread);
        System.out.println("cpuRatio=" + cpuRatio);
	}
	public static String SystemInfo4Html()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("[\"").append("<span style='color:red;'>运行环境:</span>\",\"\"],\n");
		sb.append("[\"").append("虚拟机可用处理器的数目\",\"").append(Runtime.getRuntime().availableProcessors()).append("\"],\n");
        sb.append("[\"").append("虚拟机空闲内存量\",\"").append(toMb(Runtime.getRuntime().freeMemory()) + "Mb").append("\"],\n");
        sb.append("[\"").append("虚拟机试图使用的最大内存\",\"").append(toMb(Runtime.getRuntime().maxMemory()) + "Mb").append("\"],\n");
        sb.append("[\"").append("虚拟机中的内存总量\",\"").append(toMb(Runtime.getRuntime().totalMemory()) + "Mb").append("\"],\n");

		sb.append("[\"").append("<span style='color:red;'>系统属性:</span>\",\"\"],\n");
		sb.append(systemProperties(true));
		
		sb.append("[\"").append("<span style='color:red;'>环境变量:</span>\",\"\"],\n");
		for (Map.Entry entry: System.getenv().entrySet()) 
	    {
			sb.append("[\"").append(entry.getKey()).append("\",\"").append(entry.getValue().toString().replaceAll("\\\\", "\\\\\\\\")).append("\"],\n");
		}
		String str = sb.substring(0, sb.lastIndexOf(","));
		//System.out.println(str);
		return str;
	}
	public static String SystemInfo()
	{
		StringBuffer sb = new StringBuffer();
        sb.append("系统属性:\n");
        sb.append(systemProperties());        
        sb.append("===============================================\n");
        
        sb.append("虚拟机可用处理器的数目 = " + Runtime.getRuntime().availableProcessors()).append("\n");
        sb.append("虚拟机空闲内存量 = " + toMb(Runtime.getRuntime().freeMemory()) + "Mb").append("\n");
        sb.append("虚拟机试图使用的最大内存量 = " + toMb(Runtime.getRuntime().maxMemory()) + "Mb").append("\n");
        sb.append("虚拟机中的内存总量 = " + toMb(Runtime.getRuntime().totalMemory()) + "Mb").append("\n");
		
        sb.append("================================================\n");
        sb.append("环境变量:\n");
        for (Map.Entry entry: System.getenv().entrySet()) 
        {
        	sb.append(entry.getKey() + " = " + entry.getValue()).append("\n");
        }	
        
        sb.append("================================================\n");
        sb.append("可用字符集:\n");
        sb.append(getCharsets());
		return sb.toString();
	}
	public static String getSystemInfo()
	{
		OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
		// 总的物理内存
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize();
        // 剩余的物理内存
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
        // 已使用的物理内存
        long usedMemory = totalMemorySize - freePhysicalMemorySize;
        ThreadGroup parentThread;
        for (parentThread = Thread.currentThread().getThreadGroup(); 
        		parentThread.getParent() != null; 
        			parentThread = parentThread.getParent());
        
        int totalThread = parentThread.activeCount();
        
		return "virtual_cpu_num:" + Runtime.getRuntime().availableProcessors() + "," +
			   "virtual_mem_free:\"" + toMb(Runtime.getRuntime().freeMemory()) + "Mb\"," + 
			   "virtual_mem_max:\"" + toMb(Runtime.getRuntime().maxMemory()) + "Mb\"," +
			   "virtual_mem_total:\"" + toMb(Runtime.getRuntime().totalMemory()) + "Mb\"," +
			   
			   "totalMemorySize:\"" + toMb(totalMemorySize) + "MB\"," +
			   "freePhysicalMemorySize:\"" + toMb(freePhysicalMemorySize) + "MB\"," +
			   "usedMemory:\"" + toMb(usedMemory) + "MB\"," +
			   "totalThread:" + totalThread;
		
	}
	private static double toMb(long mem)
	{
		return (double)(mem/1024/1024);
	}
	public static <K, V> String getCharsets()
	{
		StringBuffer sb = new StringBuffer();
		SortedMap <String, Charset> sm = java.nio.charset.Charset.availableCharsets();
		Iterator <String> itr = sm.keySet().iterator();
		while (itr.hasNext())
        {
            String key = itr.next();
            Set <String> value = sm.get(key).aliases();
            
            sb.append(key).append(" = ").append(value.toString()).append("\n");
        }
        return sb.toString();
	}
	
	public static <K, V> String systemProperties() 
	{
		return systemProperties(false);
	}
	@SuppressWarnings("unchecked")
    public static <K, V> String systemProperties(boolean bArray) 
	{
		StringBuffer sb = new StringBuffer();
        Properties p = System.getProperties();
        TreeMap<K, V> map = new TreeMap<K, V>();
        map.putAll((Map<? extends K, ? extends V>) p);
        Iterator itr = map.keySet().iterator();
        while (itr.hasNext())
        {
            String key = (String) itr.next();
            String value = (String) map.get(key);
            if (bArray)
            {
            	if (key.equals("line.separator")) continue;
            	sb.append("[\"").append(key).append("\",\"").append(value.replaceAll("\\\\", "\\\\\\\\")).append("\"],\n");
            }
            else
            {
            	sb.append(key).append(" = ").append(value).append("\n");
            }
        }
        return sb.toString();
    }
	
	public static String systemProperties_()
	{
		StringBuffer sb = new StringBuffer();
		Properties ps = System.getProperties();
        Enumeration <?> pn = ps.propertyNames();

        while(pn.hasMoreElements())
        {
        	String pName = (String)pn.nextElement();
            String pValue = ps.getProperty(pName);
            sb.append(pName + " = " + pValue).append("\n");
        }
        return sb.toString();
	}
	
	
	private static double getCpuRateForLinux(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader brStat = null;
        StringTokenizer tokenStat = null;
        try{
            System.out.println("Get usage rate of CUP , linux version: " + linuxVersion);

            Process process = Runtime.getRuntime().exec("top -b -n 1");
            is = process.getInputStream();                    
            isr = new InputStreamReader(is);
            brStat = new BufferedReader(isr);
            
            if(linuxVersion.equals("2.4")){
                brStat.readLine();
                brStat.readLine();
                brStat.readLine();
                brStat.readLine();
                
                tokenStat = new StringTokenizer(brStat.readLine());
                tokenStat.nextToken();
                tokenStat.nextToken();
                String user = tokenStat.nextToken();
                tokenStat.nextToken();
                String system = tokenStat.nextToken();
                tokenStat.nextToken();
                String nice = tokenStat.nextToken();
                
                System.out.println(user+" , "+system+" , "+nice);
                
                user = user.substring(0,user.indexOf("%"));
                system = system.substring(0,system.indexOf("%"));
                nice = nice.substring(0,nice.indexOf("%"));
                
                float userUsage = new Float(user).floatValue();
                float systemUsage = new Float(system).floatValue();
                float niceUsage = new Float(nice).floatValue();
                
                return (userUsage+systemUsage+niceUsage)/100;
            }else{
                brStat.readLine();
                brStat.readLine();
                    
                tokenStat = new StringTokenizer(brStat.readLine());
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                String cpuUsage = tokenStat.nextToken();
                    
                
                System.out.println("CPU idle : "+cpuUsage);
                Float usage = new Float(cpuUsage.substring(0,cpuUsage.indexOf("%")));
                
                return (1-usage.floatValue()/100);
            }

             
        } catch(IOException ioe){
            System.out.println(ioe.getMessage());
            freeResource(is, isr, brStat);
            return 1;
        } finally{
            freeResource(is, isr, brStat);
        }

    }
    private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader br){
        try{
            if(is!=null)
                is.close();
            if(isr!=null)
                isr.close();
            if(br!=null)
                br.close();
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }
    }
    
    private static double getCpuRatioForWindows() {
        try {
            String procCmd = System.getenv("windir")
                    + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
                    + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
            // 取进程信息
            long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
            Thread.sleep(CPUTIME);
            long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
            if (c0 != null && c1 != null) {
                long idletime = c1[0] - c0[0];
                long busytime = c1[1] - c0[1];
                return Double.valueOf(
                        PERCENT * (busytime) / (busytime + idletime))
                        .doubleValue();
            } else {
                return 0.0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0.0;
        }
    }
    
    private static long[] readCpu(final Process proc) {
        long[] retn = new long[2];
        try {
            proc.getOutputStream().close();
            InputStreamReader ir = new InputStreamReader(proc.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line = input.readLine();
            if (line == null || line.length() < FAULTLENGTH) {
                return null;
            }
            int capidx = line.indexOf("Caption");
            int cmdidx = line.indexOf("CommandLine");
            int rocidx = line.indexOf("ReadOperationCount");
            int umtidx = line.indexOf("UserModeTime");
            int kmtidx = line.indexOf("KernelModeTime");
            int wocidx = line.indexOf("WriteOperationCount");
            long idletime = 0;
            long kneltime = 0;
            long usertime = 0;
            while ((line = input.readLine()) != null) {
                if (line.length() < wocidx) {
                    continue;
                }
                // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
                // ThreadCount,UserModeTime,WriteOperation
                String caption = Bytes.substring(line, capidx, cmdidx - 1)
                        .trim();
                String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
                if (cmd.indexOf("wmic.exe") >= 0) {
                    continue;
                }
                // log.info("line="+line);
                if (caption.equals("System Idle Process")
                        || caption.equals("System")) {
                    idletime += Long.valueOf(
                            Bytes.substring(line, kmtidx, rocidx - 1).trim())
                            .longValue();
                    idletime += Long.valueOf(
                            Bytes.substring(line, umtidx, wocidx - 1).trim())
                            .longValue();
                    continue;
                }

                kneltime += Long.valueOf(
                        Bytes.substring(line, kmtidx, rocidx - 1).trim())
                        .longValue();
                usertime += Long.valueOf(
                        Bytes.substring(line, umtidx, wocidx - 1).trim())
                        .longValue();
            }
            retn[0] = idletime;
            retn[1] = kneltime + usertime;
            return retn;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                proc.getInputStream().close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    private static final int CPUTIME = 30;
    private static final int PERCENT = 100;
    private static final int FAULTLENGTH = 10;    
    private static final File versionFile = new File("/proc/version");
    private static String linuxVersion = null;
    
    public static class Bytes {
        public static String substring(String src, int start_idx, int end_idx)
        {
            byte[] b = src.getBytes();
            String tgt = "";
            for(int i=start_idx; i<=end_idx; i++){
                tgt +=(char)b[i];
            }
            return tgt;
        }
    }
}



分享到:
评论

相关推荐

    泛微OA ecology 二次开发实例 开发完整说明.pdf

    2. 由于支持多语言,页面上的中文文本会通过标签系统显示,例如`&lt;%=SystemEnv.getHtmlLabelName(413,user.getLanguage())%&gt;`来表示“姓名”。对应的数据库查询是:`select labelname from htmllabelinfo where ...

    泛微OAecology二次开发实例开发完整说明.pdf

    在Ecology系统中,JSP页面支持多语言,通常使用标签的形式来显示中文,例如:&lt;%=SystemEnv.getHtmlLabelName(413,user.getLanguage())%&gt;,其中的数字413表示姓名,同时可以通过“select labelname from ...

    泛微OAecology二次开发实例开发完整说明书 (2).docx

    在ECOLOGY中,由于支持多语言,中文显示通常通过标签实现,如`&lt;%=SystemEnv.getHtmlLabelName(413,user.getLanguage())%&gt;`,413为标签索引,对应不同语言环境的显示名称。 2. **阅读JSP页面**:理解页面中的标签和...

    第三十课:解决msfvenom命令自动补全1

    echo "Could not find msfvenom path in system env, please run msfvenom with path." fi if [[ ! -d ${VENOM_CACHE_FILE:h} ]]; then mkdir -p ${VENOM_CACHE_FILE:h} fi if [[ ! -f $VENOM_CACHE_FILE ]]...

    第三十课:解决msfvenom命令自动补全.docx

    echo "Could not find msfvenom path in system env, please run msfvenom with path." fi if [[ ! -d ${VENOM_CACHE_FILE:h} ]]; then mkdir -p ${VENOM_CACHE_FILE:h} fi if [[ ! -f $VENOM_CACHE_FILE ]]...

    泛微OAecology二次开发实例开发完整说明.doc

    在ECOLOGY系统中,由于支持多语言,页面上的中文通常不直接显示,而是通过标签如`&lt;%=SystemEnv.getHtmlLabelName(413,user.getLanguage())%&gt;`来实现,其中的数字413对应特定的中文标签,通过查询数据库获取对应的...

    aix系统基础培训资料

    - 1985年推出Systemenv.2。 - 1986年发布Systemv.3。 - 1989年推出Systemv.4。 - 1980年发布Version 4。 - 1982年发布Version 4.2。 - 1986年发布Version 4.3。 - IBM在1986年开始研发AIX,并于同年推出了第...

    AIX基础

    - **1985年**:Systemenv.2发布。 - **1986年**:System v.3发布。 - **1989年**:System v.4发布。 - **1980年**:Version 4发布。 - **1982年**:Version 4.2发布。 - **1986年**:Version 4.3发布。 - **1986年**...

    rtl8189es_r16m验证通过20170818_1659没有外层目录.7z

    # frameworks/native/data/etc/android.software.managed_users.xml:system/etc/permissions/android.software.managed_users.xml PRODUCT_COPY_FILES += \ device/softwinner/astar-evb30/configs/camera.cfg...

    !!!!rtl8189es_r16m验证通过20170818_1659没有外层目录.zip

    # frameworks/native/data/etc/android.software.managed_users.xml:system/etc/permissions/android.software.managed_users.xml PRODUCT_COPY_FILES += \ device/softwinner/astar-evb30/configs/camera.cfg...

    r40_tinav2.1_最终验证通过_使用CB-S来验证OV5640有横条纹fpscamera+SPI2.0成功_20171114_1443没有外层目录.7z

    r40_tinav2.1_最终验证通过_使用CB-S来验证OV5640有横条纹fpscamera+SPI2.0成功_20171114_1443没有外层目录.7z 开发板:CB-S 1、(可选修改/调试技巧:) 除了ov5640.c之外,其它的驱动都不编译,节省编译时间!...

    rethinkdb-adapter:用于Casbin https的RethinkDB适配器

    RethinkDB适配器 RethinkDB适配器是适配器 。 使用此库,Casbin可以从RethinkDB加载策略或将策略保存到其中。 安装 ...简单的例子 ... Getenv ( "RETHINKDB_URL" ) //Get the Rethinkdb url from system env if

    config.tar.gz

    所用的k8s容器相关配置 - name: dts-rsrc-sync image: dts-rsrc-sync:20191226110645 ... systemEnv: pod_ip: PODIP pod_name: PODNAME ports: - name: "ffn-18507" containerPort: 18507

    env文件,不知道好用么

    在Android系统中,env文件通常位于/system/etc目录下,包含了一系列启动时设置的环境变量。这些变量可以影响系统的启动过程、设备的硬件驱动配置、网络设置等。例如,你可以通过修改env文件来改变设备的默认语言,...

    安卓env读取

    3. **持久化环境变量**:要使环境变量在设备重启后依然有效,通常需要修改系统的启动脚本,如 `/system/etc/init/*.rc` 文件,或者在应用的 `AndroidManifest.xml` 中使用 `android:environmentVariables` 属性...

    cadence 16.3 安装后出现can not find allegro env file.doc

    ### Cadence 16.3安装后出现“can not find allegro env file”问题解析及解决方法 在进行Cadence 16.3的安装与配置过程中,可能会遇到一个常见的错误提示:“can not find allegro env file”。这个问题通常出现在...

    [Platform Builder] 设置SVM OS build Env

    本文将深入探讨如何配置SVM(System Virtual Machine)环境,以便在Visual Studio 2005中进行OS构建。SVM是一种虚拟化技术,它允许一个处理器执行多个操作系统实例,每个实例都在自己的虚拟机中运行,提高了硬件资源...

    java-dotenv,dotenv是一个从.env文件加载环境变量的模块.zip

    Java-dotenv是一个专门为Java和Kotlin开发者设计的模块,它实现了与Ruby的Dotenv项目相似的功能,即从`.env`文件中读取并加载环境变量。这个库的主要目的是帮助开发者在不暴露敏感信息(如数据库凭证、API密钥等)到...

    slim-env-nodeclient:slim-env节点客户端,提供Express和Koa中间件,用于CLI的bin文件以及用于配置的若干功能

    app.use(slimEnv.mw({in:['YOUR_SYSTEM_ENV_NAME_1', 'YOUR_SYSTEM_ENV_NAME_2']})) - 或者 -对于Koa app.use(slimEnv.mw.koa({in:['YOUR_SYSTEM_ENV_NAME_1', 'YOUR_SYSTEM_ENV_NAME_2']})) -快递 app.use(slimEnv...

Global site tag (gtag.js) - Google Analytics