SNMP实现主机检查

package com.boonya.mecache.store;
import java.util.ArrayList;

public class HostCheck {
	
	static String targetOid = ".1.3.6.1.4.1.9600.1.1.5.1.5";
	static String ip = "127.0.0.1";
	static String community = "public";
	static SnmpWalk tester = new SnmpWalk();

	/**
	 * CPU使用率
	 */
	@SuppressWarnings("rawtypes")
	public static void cpuUsingRate() {
		ArrayList arr = (ArrayList) tester.snmpWalk(ip, community, targetOid);
		for (int i = 0; i < arr.size(); i++) {
			if (i == arr.size() - 1) {
				System.out.println("CPU_ALL使用率:" + arr.get(i) + "%");
			} else {
				System.out.println("CPU_" + i + "使用率:" + arr.get(i) + "%");
			}
		}
	}
	
	/**
	 * 内存使用
	 */
	@SuppressWarnings("rawtypes")
	public static void memory(){
		targetOid = ".1.3.6.1.4.1.9600.1.1.2.3";
		ArrayList arr = (ArrayList) tester.snmpWalk(ip, community, targetOid);
		for (int i = 0; i < arr.size(); i++) {
			if (i == arr.size() - 1) {
				System.out.println("内存空闲:" + arr.get(i) + "M");
			}
		}
	}
	
	/**
	 * 进程占用
	 */
	@SuppressWarnings("rawtypes")
	public static void process(){
		targetOid = ".1.3.6.1.4.1.9600.1.1.4.1";
		ArrayList arr = (ArrayList) tester.snmpWalk(ip, community, targetOid);
		for (int i = 0; i < arr.size(); i++) {
			if (i == arr.size() - 1) {
				System.out.println("当前进程数:" + arr.get(i) + "个");
			}
		}
	}
	
	/**
	 * 硬盘使用
	 */
	@SuppressWarnings("rawtypes")
	public static void hardDisk(){
		targetOid = ".1.3.6.1.4.1.9600.1.1.1.1.1";
		ArrayList arr = (ArrayList) tester.snmpWalk(ip, community, targetOid);
		for (int i = 0; i < arr.size(); i++) {
			if (i < arr.size() - 1) {
				targetOid = ".1.3.6.1.4.1.9600.1.1.1.1.20";
				ArrayList subarr = (ArrayList) tester.snmpWalk(ip, community,
						targetOid);
				System.out.println("磁盘剩余:" + arr.get(i) + subarr.get(i) + "M");
			}
		}
	}
	
    /**
     *函数入口 
     * @param args
     */
	public static void main(String[] args) {
		HostCheck.cpuUsingRate();
		HostCheck.memory();
		HostCheck.process();
		HostCheck.hardDisk();
	}
}

你可能感兴趣的:(网络协议,snmp)