操作系统安全课程设计报告-0906130204-廖浩伟
import java.io.LineNumberReader; import java.io.OutputStream; import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
public class MonitorServiceImpl implements IMonitorService { public static ProcessStruct[] proce = new ProcessStruct[200]; public static ServiceStruct[] servi = new ServiceStruct[200]; public static int processCount = 0; public static int threadCount = 0; public static int serviceCount = 0; public static int handleCount = 0;
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系统
String osName = System.getProperty(\);
String osVersion = System.getProperty(\); // 总的物理内存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / (kb * kb);
// 剩余的物理内存
long freePhysicalMemorySize =
osmxb.getFreePhysicalMemorySize() / (kb * kb); // 已使用的物理内存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / (kb * kb);
if (osName.toLowerCase().startsWith(\)) { this.getProcessForWindows(); }
// 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize); infoBean.setOsName(osName +\+osVersion); infoBean.setTotalMemorySize(totalMemorySize);
- 9 -
操作系统安全课程设计报告-0906130204-廖浩伟
infoBean.setUsedMemory(usedMemory); return infoBean; }
public void getProcessForWindows() { try {
String procCmd = System.getenv(\)
+ \Caption,CSName,Priority,ProcessId,ThreadCount\; // 取进程信息
readProcess(Runtime.getRuntime().exec(procCmd));
} catch (Exception ex) { ex.printStackTrace(); } }
public void getServiceForWindows() { try {
String servCmd = System.getenv(\)
+ \Name,ProcessId,DisplayName,State,StartName\; // 取服务信息
readService(Runtime.getRuntime().exec(servCmd));
} catch (Exception ex) { ex.printStackTrace(); } }
public void getHandleCount(){ try {
String handCmd = System.getenv(\)
+ \Name,HandleCount\; // 取句柄信息
readHandle(Runtime.getRuntime().exec(handCmd)); } catch (Exception ex) { ex.printStackTrace(); } }
public void readHandle(final Process hand){ // 总句柄数
- 10 -
操作系统安全课程设计报告-0906130204-廖浩伟
int i = 0; try {
hand.getOutputStream().close();
InputStreamReader ir = new
InputStreamReader(hand.getInputStream());
LineNumberReader input = new LineNumberReader(ir); String line = input.readLine();
int cn = line.indexOf(\);
int ch = line.indexOf(\);
while ((line = input.readLine()) != null) { if (line.length() < cn) { continue; }
int handle = Integer.parseInt(line.substring(ch, cn-1).trim());
i += handle; }
} catch (Exception ex) { ex.printStackTrace(); } finally { try {
hand.getInputStream().close(); } catch (Exception e) { e.printStackTrace(); } }
this.handleCount = i; }
public void readProcess(final Process proc) { // 总进程数 int i = 0; int t = 0; try {
proc.getOutputStream().close();
InputStreamReader ir = new
InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir); String line = input.readLine();
- 11 -
操作系统安全课程设计报告-0906130204-廖浩伟
int capidx = line.indexOf(\); int cmdidx = line.indexOf(\); int prioidx = line.indexOf(\); int pid = line.indexOf(\); int tc = line.indexOf(\);
while ((line = input.readLine()) != null) { if (line.length() < tc) { continue; }
// 字段出现顺序:
Caption,CommandLine,Priority,ProcessId,ThreadCount
String caption = line.substring(capidx, cmdidx - 1).trim();
String caname = line.substring(cmdidx, prioidx - 1).trim();
String priority = line.substring(prioidx, pid - 1).trim();
String processId = line.substring(pid, tc - 1).trim();
String ThreadCount = line.substring(tc, line.length() - 1)
.trim();
proce[i] = new ProcessStruct(caption, caname, priority,
processId, ThreadCount);
int th = Integer.parseInt(ThreadCount); i = i + 1; t += th;
}
} catch (Exception ex) { ex.printStackTrace(); } finally { try {
proc.getInputStream().close(); } catch (Exception e) { e.printStackTrace(); } }
this.threadCount = t;
- 12 -
操作系统安全课程设计报告-0906130204-廖浩伟
this.processCount = i;
}
public void readService(final Process serv) { // 总服务数 int i = 0; try {
serv.getOutputStream().close();
InputStreamReader ir = new
InputStreamReader(serv.getInputStream());
LineNumberReader input = new LineNumberReader(ir); String line = input.readLine();
int capidx = line.indexOf(\); int cmdidx = line.indexOf(\);
int prioidx = line.indexOf(\); int pid = line.indexOf(\); int tc = line.indexOf(\);
while ((line = input.readLine()) != null) { if (line.length() < tc) { continue; }
String displayname = line.substring(capidx, cmdidx - 1).trim();
String name = line.substring(cmdidx, prioidx - 1).trim();
String processid = line.substring(prioidx, pid - 1).trim();
String startname = line.substring(pid, tc - 1).trim();
String state = line.substring(tc, line.length() - 1) .trim();
servi[i] = new ServiceStruct(name, processid, displayname,
state, startname); i = i + 1;
}
} catch (Exception ex) {
- 13 -