SNMP4J代理和Net-SNMP“客户端”

时间:2014-05-29 16:24:14

标签: snmp net-snmp snmp4j

我还在学习SNMP,所以请保持温和。

我使用snmp4j做了一个代理,它似乎正在工作,我有一个标量应该记录自代理启动以来已经过了多少时间。

我只需要做代理,然后我想看看net-snmp的标量值。

问题是,当我启动代理时,我将我的标量SystemUpTime设置为0,然后每次有人尝试使用net-snmp检查它时,我会尝试更新SystemUpTime,该值不会发生变化。

每次尝试访问SystemUpTime时,如何让我的代理更新SystemUpTime? 我有一个方法MOScalar getSystemUpTime,因为它在返回之前更新了SystemUpTime,我认为它可以完成这项工作,但它无法正常工作。

你们有什么感觉?

编辑(我的代理人代码,我已经取消了一些强制性的方法来缩短这个东西)

public class Agent extends BaseAgent {
    // not needed but very useful of course
    static {
        LogFactory.setLogFactory(new Log4jLogFactory());
    }
        static long starttime=0;
    private String address;
       static  OID oid= new OID(".1.3.6.1.4.1.1.1.0");
       public static MOScalar sysUpTime;
       private static MOFactory moFactory = 
    DefaultMOFactory.getInstance();
    public Agent(String address) throws IOException {

        // These files does not exist and are not used but has to be specified
        // Read snmp4j docs for more info
        super(new File("conf.agent"), new File("bootCounter.agent"),
                new CommandProcessor(
                        new OctetString(MPv3.createLocalEngineID())));
        this.address = address;
    }


    /**
     * Clients can register the MO they need
     */
    public void registerManagedObject(ManagedObject mo) {
        try {
            server.register(mo, null);
                        System.out.println("MIB FILLED!");
        } catch (DuplicateRegistrationException ex) {
            throw new RuntimeException(ex);
        }
    }


    /**
     * Start method invokes some initialization methods needed to
     * start the agent
     * @throws IOException
     */
    public void start() throws IOException {

        init();
        // This method reads some old config from a file and causes
        // unexpected behavior.
        // loadConfig(ImportModes.REPLACE_CREATE); 
        addShutdownHook();
        getServer().addContext(new OctetString("public"));
        finishInit();
        run();
        sendColdStartNotification();
    }



    protected void unregisterManagedObjects() {
        // here we should unregister those objects previously registered...
    }

    /**
     * The table of community strings configured in the SNMP
     * engine's Local Configuration Datastore (LCD).
     * 
     * We only configure one, "public".
     */
    protected void addCommunities(SnmpCommunityMIB communityMIB) {
        Variable[] com2sec = new Variable[] { 
                new OctetString("public"), // community name
                new OctetString("cpublic"), // security name
                getAgent().getContextEngineID(), // local engine ID
                new OctetString("public"), // default context name
                new OctetString(), // transport tag
                new Integer32(StorageType.nonVolatile), // storage type
                new Integer32(RowStatus.active) // row status
        };
    }

        public static void main(String[] args) throws IOException, InterruptedException {
Agent agent = new Agent("127.0.0.1/6666");
                sysUpTime=moFactory.createScalar(oid,
                             moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY), 
                             new TimeTicks(0));
                starttime=System.currentTimeMillis();
                agent.registerManagedObject(sysUpTime);
        agent.start();
        while(true) {
            System.out.println("Agent running...");
            Thread.sleep(5000);
            }
    }

     public MOScalar getSystemID() {
     Variable var = new Integer32((int) (System.currentTimeMillis() - starttime));
     sysUpTime.setValue(var);
    return sysUpTime;
      }

}

0 个答案:

没有答案
相关问题