私はまだSNMPを学んでいるので、優しくしてください。
snmp4j でエージェントを作成しましたが、動作しているようです。エージェントが開始してから経過した時間を登録する必要があるスカラーがあります。
エージェントを実行するだけで、net-snmp でスカラーの値を確認したいと思います。
問題は、エージェントを開始するときにスカラー SystemUpTime を 0 に設定し、誰かが net-snmp でチェックしようとするたびに SystemUpTime を更新しようとすることです。その値は変更されません。
何かがアクセスしようとするたびにエージェントに SystemUpTime を更新させるにはどうすればよいですか? SystemUpTime を返す前に更新するため、メソッド MOScalar getSystemUpTime があり、それが仕事をすると思っていましたが、機能していません。
あなたたちは何を提案しますか?
編集(私のエージェントコード、このことを少し短くするためにいくつかの必須メソッドを削除しました)
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;
}
}