Chinaunix首页 | 论坛 | 博客
  • 博客访问: 605007
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-02-09 08:51:48


点击(此处)折叠或打开


  1. package org.fh.controller.tools;

  2. import java.net.InetAddress;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Properties;

  6. import org.fh.controller.base.BaseController;
  7. import org.fh.entity.PageData;
  8. import org.hyperic.sigar.CpuInfo;
  9. import org.hyperic.sigar.CpuPerc;
  10. import org.hyperic.sigar.FileSystem;
  11. import org.hyperic.sigar.FileSystemUsage;
  12. import org.hyperic.sigar.Mem;
  13. import org.hyperic.sigar.NetInterfaceConfig;
  14. import org.hyperic.sigar.NetInterfaceStat;
  15. import org.hyperic.sigar.Sigar;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.ResponseBody;

  19. /**
  20.  * 说明:服务器信息监控
  21.  * 作者:FH Admin
  22.  * from:fhadmin.cn
  23.  */
  24. @Controller
  25. @RequestMapping("/serverRunstate")
  26. public class ServerRunstateController extends BaseController {
  27.     
  28.     /**获取常量数据
  29.      * @return
  30.      */
  31.     @RequestMapping(value="/getData")
  32.     @ResponseBody
  33.     public Object getData() throws Exception{
  34.         Map<String,Object> map = new HashMap<String,Object>();
  35.         Sigar sigar = new Sigar();
  36.         String errInfo = "success";
  37.         PageData pd = new PageData();
  38.         property(pd);                    //基本信息
  39.         gethardDiskData(pd,sigar);        //硬盘使用情况
  40.         map.put("pd", pd);
  41.         map.put("result", errInfo);
  42.         return map;
  43.     }
  44.     
  45.     /**获取事实数据
  46.      * @return
  47.      */
  48.     @RequestMapping(value="/realTimeData")
  49.     @ResponseBody
  50.     public Object realTimeData() throws Exception{
  51.         Map<String,Object> map = new HashMap<String,Object>();
  52.         Sigar sigar = new Sigar();
  53.         String errInfo = "success";
  54.         PageData pd = new PageData();
  55.         getJvmMemoryData(pd);            //JVM内存
  56.         getServerMemoryData(pd,sigar);    //服务器内存
  57.         getCpuData(pd,sigar);            //CPU
  58.         map.put("pd", pd);
  59.         map.put("result", errInfo);
  60.         return map;
  61.     }
  62.     
  63.     /**获取网速
  64.      * @return
  65.      */
  66.     @RequestMapping(value="/networkspeed")
  67.     @ResponseBody
  68.     public Object networkspeed() throws Exception{
  69.         Map<String,Object> map = new HashMap<String,Object>();
  70.         Sigar sigar = new Sigar();
  71.         String errInfo = "success";
  72.         PageData pd = new PageData();
  73.         long[] fhbytes1 = getNet(sigar);
  74.         Thread.sleep(1000);
  75.         long[] fhbytes2 = getNet(sigar);
  76.         long rxspeed = fhbytes2[0] - fhbytes1[0];
  77.         long txspeed = fhbytes2[1] - fhbytes1[1];
  78.         pd.put("rxspeed", (double) Math.round((rxspeed/2014.0) * 100) / 100);        //下载网速
  79.         pd.put("txspeed", (double) Math.round((txspeed/1024.0) * 100) / 100);        //上传网速
  80.         map.put("pd", pd);
  81.         map.put("result", errInfo);
  82.         return map;
  83.     }
  84.     
  85.     /**获取JVM内存数据
  86.      * @param pd
  87.      * @return
  88.      * @throws Exception
  89.      */
  90.     private PageData getJvmMemoryData(PageData pd){
  91.         double byteToMb = 1024.0 * 1024.0;
  92.         double jvmTotal = 0;
  93.         double jvmFree = 0;
  94.         double jvmUse = 0;
  95.         Runtime rt = Runtime.getRuntime();
  96.         jvmTotal = rt.totalMemory() / byteToMb;
  97.         jvmFree = rt.freeMemory() / byteToMb;
  98.         jvmUse = jvmTotal - jvmFree;
  99.         pd.put("totalJvmMemory", (double) Math.round(jvmTotal * 100) / 100);        //JVM总内存空间
  100.         pd.put("useJvmMemory", (double) Math.round(jvmUse * 100) / 100);            //JVM已使用的内存
  101.         return pd;
  102.     }
  103.     
  104.     /**获取服务器内存数据
  105.      * @param pd
  106.      * @param sigar
  107.      * @throws Exception
  108.      */
  109.     private void getServerMemoryData(PageData pd, Sigar sigar) throws Exception {
  110.         double byteToGb = 1024.0 * 1024.0 * 1024.0;
  111.         double serveotal = 0;
  112.         double serverUse = 0;
  113.         Mem mem = sigar.getMem();
  114.         serveotal = mem.getTotal() / byteToGb;
  115.         serverUse = mem.getUsed() / byteToGb;
  116.         pd.put("totalServerMemory", (double) Math.round(serveotal * 100) / 100);        //服务器总内存空间
  117.         pd.put("useServerMemory", (double) Math.round(serverUse * 100) / 100);            //服务器已使用的内存
  118.     }
  119.     
  120.     /**获取服务器CUP使用率
  121.      * @param pd
  122.      * @param sigar
  123.      * @throws Exception
  124.      */
  125.     private void getCpuData(PageData pd, Sigar sigar) throws Exception {
  126.         CpuInfo infos[] = sigar.getCpuInfoList();
  127.         CpuPerc cpuList[] = null;
  128.         cpuList = sigar.getCpuPercList();
  129.         double cpuuse = 0;
  130.         for (int i = 0; i < infos.length; i++) {                //多块或者多核心CPU
  131.             cpuuse += cpuList[i].getCombined() * 100.00;
  132.         }
  133.         pd.put("cpuuse", (double) Math.round(((cpuuse/(infos.length * 100))*100) * 100) / 100 );//使用率
  134.     }
  135.     
  136.     /**获取服务器硬盘情况
  137.      * @param pd
  138.      * @param sigar
  139.      * @throws Exception
  140.      */
  141.     private void gethardDiskData(PageData pd, Sigar sigar) throws Exception {
  142.         double byteToGb = 1024.0 * 1024.0;
  143.         long totalSize = 0;
  144.         long useSize = 0;
  145.         FileSystem fslist[] = sigar.getFileSystemList();
  146.         for (int i = 0; i < fslist.length; i++) {
  147.             FileSystem fs = fslist[i];
  148.             FileSystemUsage usage = null;
  149.             usage = sigar.getFileSystemUsage(fs.getDirName());
  150.             switch (fs.getType()) {
  151.             case 0: // TYPE_UNKNOWN :未知
  152.                 break;
  153.             case 1: // TYPE_NONE
  154.                 break;
  155.             case 2: // TYPE_LOCAL_DISK : 本地硬盘
  156.                 totalSize += usage.getTotal();    //总大小
  157.                 useSize += usage.getUsed();        //已经使用量
  158.                 break;
  159.             case 3:// TYPE_NETWORK :网络
  160.                 break;
  161.             case 4:// TYPE_RAM_DISK :闪存
  162.                 break;
  163.             case 5:// TYPE_CDROM :光驱
  164.                 break;
  165.             case 6:// TYPE_SWAP :页面交换
  166.                 break;
  167.             }
  168.         }
  169.         pd.put("totalDiskSize",(double) Math.round((totalSize / byteToGb) * 10) / 10);    //硬盘总大小
  170.         pd.put("useDiskSize",(double) Math.round((useSize / byteToGb) * 10) / 10);        //硬盘已使用大小
  171.     }
  172.     
  173.     /**获取基础信息
  174.      * @param pd
  175.      * @return
  176.      * @throws Exception
  177.      */
  178.     private PageData property(PageData pd) throws Exception {
  179.         Runtime r = Runtime.getRuntime();
  180.         Properties props = System.getProperties();
  181.         InetAddress addr;
  182.         addr = InetAddress.getLocalHost();
  183.         String ip = addr.getHostAddress();
  184.         Map<String, String> map = System.getenv();
  185.         String userName = map.get("USERNAME");// 获取用户名
  186.         pd.put("userName", userName);            //计算机用户
  187.         pd.put("IP", ip);                        //本地ip地址
  188.         pd.put("HostName", addr.getHostName());    //本地主机名
  189.         pd.put("availableProcessors", r.availableProcessors());        //JVM可以使用的处理器个数
  190.         pd.put("javaversion", props.getProperty("java.version"));    //Java的运行环境版本
  191.         pd.put("javahome", props.getProperty("java.home"));            //Java的安装路径
  192.         pd.put("javavmversion", props.getProperty("java.vm.specification.version"));    //Java的虚拟机规范版本
  193.         pd.put("javavmname", props.getProperty("java.vm.name"));                //Java的虚拟机实现名称
  194.         pd.put("javaclassversion", props.getProperty("java.class.version"));    //Java的类格式版本号
  195.         pd.put("osarch", props.getProperty("os.arch"));        //操作系统的构架
  196.         pd.put("userdir", props.getProperty("user.dir"));    //用户的当前工作目录
  197.         return pd;
  198.     }

  199.     /**获取接收字节数
  200.      * @return
  201.      * @throws Exception
  202.      */
  203.     private static long[] getNet(Sigar sigar) throws Exception {
  204.         long[] fhbytes = new long[2];
  205.         String ifNames[] = sigar.getNetInterfaceList();
  206.         for (int i = 0; i < ifNames.length; i++) {
  207.             String name = ifNames[i];
  208.             NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
  209.             if ((ifconfig.getFlags() & 1L) <= 0L) {
  210.                 continue;
  211.             }
  212.             NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
  213.             long fhRxBytes = ifstat.getRxBytes();
  214.             long fhTxBytes = ifstat.getTxBytes();
  215.             if(fhRxBytes > 0) {
  216.                 fhbytes[0] = fhRxBytes;
  217.                 fhbytes[1] = fhTxBytes;
  218.                 return fhbytes;
  219.             }
  220.         }
  221.         fhbytes[0] = 0;
  222.         fhbytes[1] = 0;
  223.         return fhbytes;
  224.     }
  225.     
  226. }


阅读(632) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~