linux监控cpu和内存使用情况,发送邮件

linux监控cpu和内存使用情况,发送邮件

2018年08月01日 16:06:45 阅读数:66更多

个人分类: Linux

目录

1.整体架构图

2.代码

3.邮件发送过慢的问题


这几天老大安排做linux服务器的工具,又接了几个巨坑,不过还是get新技能点。

1.整体架构图

    主要功能 :监控linux的cpu和内存使用率,当频率过高时,发送邮件提醒功能。

    这里的SendMail工具类在上一章节写过,需要的朋友可以去看下,付链接

        https://mp.csdn.net/postedit/81332504

     还有一个发送邮件问题,在linux上发送邮件出现延迟问题,需要去配置一下,第三点会讲下

2.代码

    cpuinfo()  读取linux下的/proc/stat文件,获取cpu信息。

    cpuUsage()  获取两次cpuinfo的内容,比较两次的差别,得到cpu使用率。

   memoryUsage()  读取linux下的/proc/meminfo文件,获取内存信息。

   main() 主入口,当使用率过高,定时发送邮件提醒。

 
  1. package LinuxMonitorUtils;

  2.  
  3. import java.io.BufferedReader;

  4. import java.io.FileInputStream;

  5. import java.io.InputStreamReader;

  6. import java.util.ArrayList;

  7. import java.util.HashMap;

  8. import java.util.List;

  9. import java.util.Map;

  10. import java.util.StringTokenizer;

  11. import java.util.Timer;

  12. import java.util.TimerTask;

  13.  
  14. import sendMailUtils.SendMail;

  15.  
  16. public class OSUtils {

  17.  
  18. /**

  19. * 功能:获取Linux系统cpu使用率

  20. */

  21. public static float cpuUsage() {

  22. try {

  23. Map<?, ?> map1 = OSUtils.cpuinfo();

  24. Thread.sleep(5 * 1000);

  25. Map<?, ?> map2 = OSUtils.cpuinfo();

  26.  
  27. long user1 = Long.parseLong(map1.get("user").toString());

  28. long nice1 = Long.parseLong(map1.get("nice").toString());

  29. long system1 = Long.parseLong(map1.get("system").toString());

  30. long idle1 = Long.parseLong(map1.get("idle").toString());

  31.  
  32. long user2 = Long.parseLong(map2.get("user").toString());

  33. long nice2 = Long.parseLong(map2.get("nice").toString());

  34. long system2 = Long.parseLong(map2.get("system").toString());

  35. long idle2 = Long.parseLong(map2.get("idle").toString());

  36.  
  37. long total1 = user1 + system1 + nice1;

  38. long total2 = user2 + system2 + nice2;

  39. float total = total2 - total1;

  40.  
  41. long totalIdle1 = user1 + nice1 + system1 + idle1;

  42. long totalIdle2 = user2 + nice2 + system2 + idle2;

  43. float totalidle = totalIdle2 - totalIdle1;

  44.  
  45. float cpusage = (total / totalidle) * 100;

  46. System.out.println("cpu使用率:"+ cpusage+"%");

  47. return cpusage;

  48. } catch (InterruptedException e) {

  49. e.printStackTrace();

  50. }

  51. return 0;

  52. }

  53.  
  54. /**

  55. * 功能:CPU使用信息

  56. */

  57. public static Map<?, ?> cpuinfo() {

  58. InputStreamReader inputs = null;

  59. BufferedReader buffer = null;

  60. Map<String, Object> map = new HashMap<String, Object>();

  61. try {

  62. inputs = new InputStreamReader(new FileInputStream("/proc/stat"));

  63. buffer = new BufferedReader(inputs);

  64. String line = "";

  65. while (true) {

  66. line = buffer.readLine();

  67. if (line == null) {

  68. break;

  69. }

  70. if (line.startsWith("cpu")) {

  71. StringTokenizer tokenizer = new StringTokenizer(line);

  72. List<String> temp = new ArrayList<String>();

  73. while (tokenizer.hasMoreElements()) {

  74. String value = tokenizer.nextToken();

  75. temp.add(value);

  76. }

  77. map.put("user", temp.get(1));

  78. map.put("nice", temp.get(2));

  79. map.put("system", temp.get(3));

  80. map.put("idle", temp.get(4));

  81. map.put("iowait", temp.get(5));

  82. map.put("irq", temp.get(6));

  83. map.put("softirq", temp.get(7));

  84. map.put("stealstolen", temp.get(8));

  85. break;

  86. }

  87. }

  88. } catch (Exception e) {

  89. e.printStackTrace();

  90. } finally {

  91. try {

  92. buffer.close();

  93. inputs.close();

  94. } catch (Exception e2) {

  95. e2.printStackTrace();

  96. }

  97. }

  98. return map;

  99. }

  100.  
  101. /**

  102. * 功能:内存使用率

  103. */

  104. public static long memoryUsage() {

  105. Map<String, Object> map = new HashMap<String, Object>();

  106. InputStreamReader inputs = null;

  107. BufferedReader buffer = null;

  108. try {

  109. inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));

  110. buffer = new BufferedReader(inputs);

  111. String line = "";

  112. while (true) {

  113. line = buffer.readLine();

  114. if (line == null)

  115. break;

  116. int beginIndex = 0;

  117. int endIndex = line.indexOf(":");

  118. if (endIndex != -1) {

  119. String key = line.substring(beginIndex, endIndex);

  120. beginIndex = endIndex + 1;

  121. endIndex = line.length();

  122. String memory = line.substring(beginIndex, endIndex);

  123. String value = memory.replace("kB", "").trim();

  124. map.put(key, value);

  125. }

  126. }

  127.  
  128. long memTotal = Long.parseLong(map.get("MemTotal").toString());

  129. System.out.println("内存总量"+memTotal+"KB");

  130. long memFree = Long.parseLong(map.get("MemFree").toString());

  131. System.out.println("剩余内存"+memFree+"KB");

  132. long memused = memTotal - memFree;

  133. System.out.println("已用内存"+memused+"KB");

  134. long buffers = Long.parseLong(map.get("Buffers").toString());

  135. long cached = Long.parseLong(map.get("Cached").toString());

  136.  
  137. double usage = (double) (memused - buffers - cached) / memTotal * 100;

  138. System.out.println("内存使用率"+usage+"%");

  139.  
  140. return memFree;

  141. } catch (Exception e) {

  142. e.printStackTrace();

  143. } finally {

  144. try {

  145. buffer.close();

  146. inputs.close();

  147. } catch (Exception e2) {

  148. e2.printStackTrace();

  149. }

  150. }

  151. return 0;

  152. }

  153.  
  154.  
  155. /**

  156. * 主入口

  157. * @param args

  158. */

  159. public static void main(String[] args) {

  160. // //1. 创建计时器类

  161. // Timer timer = new Timer();

  162. // //2. 创建任务类

  163. // TimerTask task = new TimerTask() {

  164. // @Override

  165. // public void run() {

  166. //cup使用率

  167. float cpuUsage = cpuUsage();

  168. if(cpuUsage > 10.0 ){

  169. SendMail.sendMail("xxxxx@qq.com", "服务器cpu使用率过高,请注意查看", "服务器提醒");

  170. }

  171. //内存使用情况

  172. long memoryUsage = memoryUsage();

  173. if((memoryUsage/1024) < 100){

  174. SendMail.sendMail("xxxxx@qq.com","服务器内存剩余空间不足,请注意查看", "服务器提醒");

  175. }

  176. System.out.println("-----------");

  177. // }

  178. // };

  179. // timer.schedule(task, 1000, 1000*10);

  180.  
  181. }

  182. }

3.邮件发送过慢的问题

   在测试代码的时候,我们发现了邮件发送过慢的问题,这在本机上并不在,所以我们还需要修改下linux的配置文件

    在linux控制台输入,  查看本地端口的配置,因为sendmail监听的是127.0.0.1,但却无法确认主机名peixin,所以在我们的127.0.0.1 最后面配置主机名,例如我的是peixin,在最后配置peixin,之后重启下邮件发送。

    # cat /etc/hosts

127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4 peixin

::1             localhost localhost.localdomain localhost6 localhost6.localdomain6

    # service sendmail restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值