📄 ncdperfdatabean.java
字号:
private boolean first = true; private boolean foundEthernet = false;public ethernet (String h, int p, String c, int i, SnmpContext con){ super(h, p, c, i, con); first = true; foundEthernet = false; speed = -1; prevSpeed = -1; prevSys = -1; prevOper = -1; prevInO = -1; prevOutO = -1;}public long getSpeed(){ return speed;}public void doPdu() throws PduException, java.io.IOException{ if (first) { // first go looking which interface is the ethernet one typePdu = new GetNextPdu_vec(context, 2); typePdu.addObserver(this); typePdu.addOid(ifIndex); typePdu.addOid(ifType); typePdu.send(); first = false; } else if (foundEthernet) { ethernetPdu = new GetPdu_vec(context, 5); ethernetPdu.addObserver(this); ethernetPdu.addOid(sysUpTime); ethernetPdu.addOid(ifType + "." + index); ethernetPdu.addOid(ifOperStatus + "." + index); ethernetPdu.addOid(ifInOctets + "." + index); ethernetPdu.addOid(ifOutOctets + "." + index); ethernetPdu.send(); }}public void update(Observable obs, Object ov){ varbind [] vars; if (obs == typePdu) { if (typePdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { vars = (varbind[]) ov; if (vars[0].getOid().toString().startsWith(ifIndex)) { int i = ((AsnInteger) vars[0].getValue()).getValue(); int t = ((AsnInteger) vars[1].getValue()).getValue(); if (t == ethernetType) { // found it! index = i; foundEthernet = true; isPduInFlight = false; } else { // not found it, ask for the next one typePdu = new GetNextPdu_vec(context, 2); typePdu.addObserver(this); typePdu.addOid(vars[0]); typePdu.addOid(vars[1]); try { typePdu.send(); } catch (PduException exc) { System.out.println("PduException " + exc.getMessage()); } catch (IOException exc) { System.out.println("IOException " + exc.getMessage()); } } } else { setMessage("Ethernet interface not available!"); } } else { setMessage("Ethernet interface not available!"); } } else { speed = -1; int oper = -1; if (ethernetPdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { vars = (varbind[]) ov; long sys = ((AsnUnsInteger) vars[0].getValue()).getValue(); int t = ((AsnInteger) vars[1].getValue()).getValue(); oper = ((AsnInteger) vars[2].getValue()).getValue(); long inO = ((AsnUnsInteger) vars[3].getValue()).getValue(); long outO = ((AsnUnsInteger) vars[4].getValue()).getValue(); if (t == ethernetType) { if (oper == statusUp && prevOper == statusUp) { long tdif = (sys - prevSys); if (tdif != 0) { speed = ((inO - prevInO) + (outO - prevOutO)) / tdif * 100; firePropertyChange (NcdPerfDataBean.speedPropertyName, new Long(prevSpeed), new Long(speed)); } } prevSys = sys; prevInO = inO; prevOutO = outO; } else { // the index has changed, start searching again first = true; foundEthernet = false; } } prevSpeed = speed; prevOper = oper; isPduInFlight = false; }}}/** * <p> * This class collects information about the user that is logged in. * </p> * * <p> * The first time it will search the NCD environment table to find the * entry describing the environment variable USER. After it has been found, * it will ask for the information to calculate the speed. * </p> * * <p> * When it cannot be found, it will try to see the difference between * the situation where nobody is logged in and the situation where such * information will never be avaible since it does not concern a NCD. * </p> * * <p> * It is not so easy as it may seem. If the machine is shut off, it seem * if the machine is not a NCD. * </p> * * @see ethernet * @see memory */class user extends ncdPart implements Observer{ private final static String USER = "USER"; private final static String ncdPrefEnvVarTableIndex = "1.3.6.1.4.1.82.2.3.15.54.1.1"; private final static String ncdPrefEnvVarTableName = "1.3.6.1.4.1.82.2.3.15.54.1.2"; private final static String ncdPrefEnvVarTableValue = "1.3.6.1.4.1.82.2.3.15.54.1.3"; private int index = -1; private String name = ""; private String prevName = ""; GetNextPdu_vec namePdu; GetPdu_vec userPdu; private boolean first = true; private boolean isNcd = false; private boolean foundUser = false;public user (String h, int p, String c, int i, SnmpContext con){ super(h, p, c, i, con); first = true; isNcd = false; foundUser = false; userPdu = null; name = ""; prevName = "";}public String getUserName(){ return name;}public void doPdu() throws PduException, java.io.IOException{ if (first || (isNcd && !foundUser)) { // first go looking which interface is the ethernet one namePdu = new GetNextPdu_vec(context, 3); namePdu.addObserver(this); namePdu.addOid(ncdPrefEnvVarTableIndex); namePdu.addOid(ncdPrefEnvVarTableName); namePdu.addOid(ncdPrefEnvVarTableValue); namePdu.send(); first = false; } else if (foundUser) { userPdu = new GetPdu_vec(context, 3); userPdu.addObserver(this); userPdu.addOid(ncdPrefEnvVarTableIndex + "." + index); userPdu.addOid(ncdPrefEnvVarTableName + "." + index); userPdu.addOid(ncdPrefEnvVarTableValue + "." + index); userPdu.send(); }}public void update(Observable obs, Object ov){ varbind [] vars; if (obs == namePdu) { if (namePdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { vars = (varbind[]) ov; if (vars[0].getOid().toString().startsWith(ncdPrefEnvVarTableIndex)) { // as soon as one response concerns the NCD environment // table, I assume it concerns a NCD machine. isNcd = true; int i = ((AsnInteger) vars[0].getValue()).getValue(); String n = ((AsnOctets) vars[1].getValue()).getValue(); String v = ""; if (vars[2].getValue() instanceof AsnOctets) { v = ((AsnOctets) vars[2].getValue()).getValue(); } if (n.equals(USER)) { // found it! index = i; foundUser = true; name = v; isPduInFlight = false; } else { // not found it, ask for the next one namePdu = new GetNextPdu_vec(context, 3); namePdu.addObserver(this); namePdu.addOid(vars[0]); namePdu.addOid(vars[1]); namePdu.addOid(vars[2]); try { namePdu.send(); } catch (PduException exc) { System.out.println("PduException " + exc.getMessage()); } catch (IOException exc) { System.out.println("IOException " + exc.getMessage()); } } } else { if (isNcd) { name = NcdPerfDataBean.noLogin; } else { name = NcdPerfDataBean.noName; } if (!name.equals(prevName)) { firePropertyChange (NcdPerfDataBean.userPropertyName, prevName, name); } prevName = name; isPduInFlight = false; } } else { prevName = name; isPduInFlight = false; } } else { name = ""; if (userPdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { vars = (varbind[]) ov; int i = ((AsnInteger) vars[0].getValue()).getValue(); String n = ((AsnOctets) vars[1].getValue()).getValue(); String v = ""; if (vars[2].getValue() instanceof AsnOctets) { v = ((AsnOctets) vars[2].getValue()).getValue(); } name = v; if (!name.equals(prevName)) { firePropertyChange (NcdPerfDataBean.userPropertyName, prevName, name); } } else { first = true; isNcd = true; foundUser = false; } prevName = name; isPduInFlight = false; }}}/** * <p> * This class collects information about the available memory. * </p> * * <p> * If this information is not available, probably because it is not a * NCD machine, this class will not try again. * </p> * * @see ethernet * @see user */class memory extends ncdPart implements Observer{ private final static String ncdSysMemAvail = "1.3.6.1.4.1.82.2.1.1.2.0"; GetPdu memoryPdu; private long memory = -1; private long prevMemory = -1; private boolean first = true; private boolean isAvailable = false;public memory (String h, int p, String c, int i, SnmpContext con){ super(h, p, c, i, con); first = true; isAvailable = false;}public long getMemory(){ return memory;}public void doPdu() throws PduException, java.io.IOException{ if (first || isAvailable) { memoryPdu = new GetPdu(context); memoryPdu.addOid(ncdSysMemAvail); memoryPdu.addObserver(this); first = false; }}public void update(Observable obs, Object ov){ varbind var; memory = -1; if (memoryPdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { var = (varbind) ov; memory = ((AsnUnsInteger) var.getValue()).getValue(); firePropertyChange (NcdPerfDataBean.memoryPropertyName, new Long(prevMemory), new Long(memory)); isAvailable = true; } else { setMessage("Memory data not available!"); } prevMemory = memory; isPduInFlight = false;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -