Advertisement
Guest User

Untitled

a guest
Aug 21st, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package com.vizor.tools;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Map;
  8. import java.util.Set;
  9.  
  10. public class FdChecker
  11. {
  12. public static void main(String[] args)
  13. {
  14. try
  15. {
  16. String pid = args[0];
  17.  
  18. Set<String> prevUnix = null;
  19. Map<String,String> prevFds = null;
  20.  
  21. System.out.println("Starting");
  22. long lastLog = System.currentTimeMillis();
  23.  
  24. while (true)
  25. {
  26. Process p = Runtime.getRuntime().exec(new String[] { "sudo", "lsof", "-p", pid });
  27. BufferedReader ri = new BufferedReader(new InputStreamReader(p.getInputStream()));
  28. BufferedReader re = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  29.  
  30. int totalFd = 0;
  31. int totalUnix = 0;
  32. int totalMem = 0;
  33.  
  34. Set<String> newUnix = new HashSet<String>();
  35. Map<String,String> newFds = new HashMap<String,String>();
  36.  
  37. String line;
  38. while ((line = ri.readLine()) != null)
  39. {
  40. line = line.replaceAll(" +", " ").trim();
  41. if (line.length() > 0)
  42. {
  43. String[] f = line.split(" ");
  44. if (f.length > 5)
  45. {
  46. totalFd++;
  47.  
  48. String fd = f[3];
  49. String type = f[4];
  50.  
  51. if (fd.endsWith("u") || fd.endsWith("w") || fd.endsWith("r"))
  52. fd = fd.substring(0, fd.length() - 1);
  53.  
  54. if (!"mem".equals(fd))
  55. {
  56. newFds.put(fd, line);
  57. if ("unix".equals(type))
  58. {
  59. newUnix.add(fd);
  60. totalUnix++;
  61.  
  62. if (prevUnix != null && prevFds != null && !prevUnix.contains(fd))
  63. {
  64. System.out.println("new unix socket: '" + line + "', prev: '" + prevFds.get(fd) +"'");
  65. }
  66. }
  67. }
  68. else
  69. {
  70. totalMem++;
  71. }
  72. }
  73. }
  74. }
  75.  
  76. // Просто пропускаем errorlog
  77. while (re.readLine() != null);
  78.  
  79. p.waitFor();
  80.  
  81. prevFds = newFds;
  82. prevUnix = newUnix;
  83.  
  84. if (System.currentTimeMillis() >= (lastLog + 5000))
  85. {
  86. lastLog = System.currentTimeMillis();
  87. System.out.println("All: " + totalFd + ", unix: " + totalUnix + ", mem: " + totalMem);
  88. }
  89.  
  90. Thread.sleep(200);
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement