Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vizor.tools;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- public class FdChecker
- {
- public static void main(String[] args)
- {
- try
- {
- String pid = args[0];
- Set<String> prevUnix = null;
- Map<String,String> prevFds = null;
- System.out.println("Starting");
- long lastLog = System.currentTimeMillis();
- while (true)
- {
- Process p = Runtime.getRuntime().exec(new String[] { "sudo", "lsof", "-p", pid });
- BufferedReader ri = new BufferedReader(new InputStreamReader(p.getInputStream()));
- BufferedReader re = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- int totalFd = 0;
- int totalUnix = 0;
- int totalMem = 0;
- Set<String> newUnix = new HashSet<String>();
- Map<String,String> newFds = new HashMap<String,String>();
- String line;
- while ((line = ri.readLine()) != null)
- {
- line = line.replaceAll(" +", " ").trim();
- if (line.length() > 0)
- {
- String[] f = line.split(" ");
- if (f.length > 5)
- {
- totalFd++;
- String fd = f[3];
- String type = f[4];
- if (fd.endsWith("u") || fd.endsWith("w") || fd.endsWith("r"))
- fd = fd.substring(0, fd.length() - 1);
- if (!"mem".equals(fd))
- {
- newFds.put(fd, line);
- if ("unix".equals(type))
- {
- newUnix.add(fd);
- totalUnix++;
- if (prevUnix != null && prevFds != null && !prevUnix.contains(fd))
- {
- System.out.println("new unix socket: '" + line + "', prev: '" + prevFds.get(fd) +"'");
- }
- }
- }
- else
- {
- totalMem++;
- }
- }
- }
- }
- // Просто пропускаем errorlog
- while (re.readLine() != null);
- p.waitFor();
- prevFds = newFds;
- prevUnix = newUnix;
- if (System.currentTimeMillis() >= (lastLog + 5000))
- {
- lastLog = System.currentTimeMillis();
- System.out.println("All: " + totalFd + ", unix: " + totalUnix + ", mem: " + totalMem);
- }
- Thread.sleep(200);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement