0

我还在学习 SNMP,所以请温柔一点。

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

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

问题是,当我启动代理时,我将标量 SystemUpTime 设置为 0,然后每当有人尝试使用 net-snmp 检查它时,我都会尝试更新 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;
      }

}
4

0 回答 0