package com.jpragma.snmp.samples;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import com.jpragma.snmp.SnmpAgent;
import com.jpragma.snmp.SnmpClient;
import com.jpragma.snmp.agent.Mib2System;
import com.jpragma.snmp.agent.MibEntry;
import com.jpragma.snmp.asn.AsnInteger;
import com.jpragma.snmp.asn.AsnOctetString;
import com.jpragma.snmp.asn.SmiGauge32;

public class JvmStatSnmpAgentStandalone {
 
public static void main(String[] args) {
   
// Create an instance of our POJO object
   
JvmStatStandalone jvmStatStandalone = new JvmStatStandalone();
   
// Now create the agent
   
SnmpAgent snmpAgent = new SnmpAgent();
   
// Set communities (SNMP passwords)
   
snmpAgent.setReadOnlyComunity("public");
    snmpAgent.setReadWriteComunity
("private");
   
// We handle mib2-system and enterprises.28824 subtrees
   
snmpAgent.setHandledOidPrefixes(new String[]{"1.3.6.1.2.1", "1.3.6.1.4.1.28824"});
   
// Create MIB2-SYSTEM object
   
Mib2System mib2System = new Mib2System();
    mib2System.setSysObjectId
("1.3.6.1.4.1.28824.99");
    mib2System.setSysContact
("Isaac Levin");
    mib2System.setSysDescr
("JVM Statistics service");
    mib2System.setSysLocation
("Main Building, room 2001");
    snmpAgent.setMib2System
(mib2System);
   
// Create MIB entries for JvmStatStandalone
   
Set mibEntries = new HashSet();
    MibEntry entry =
null;
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.1.0", jvmStatStandalone, "appName", true, AsnOctetString.class);
    mibEntries.add
(entry);
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.2.0", jvmStatStandalone, "poolSize", true, AsnInteger.class);
    mibEntries.add
(entry);
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.3.0", jvmStatStandalone, "availableProcessors", AsnInteger.class);
    mibEntries.add
(entry);
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.4.0", jvmStatStandalone, "freeMemory", SmiGauge32.class);
    mibEntries.add
(entry);
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.5.0", jvmStatStandalone, "maxMemory", SmiGauge32.class);
    mibEntries.add
(entry);
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.6.0", jvmStatStandalone, "totalMemory", SmiGauge32.class);
    mibEntries.add
(entry);
    entry =
new MibEntry("1.3.6.1.4.1.28824.99.7.0", jvmStatStandalone, "numberOfThreads", AsnInteger.class);
    mibEntries.add
(entry);
    snmpAgent.setMibEntries
(mibEntries);

   
// Create proxy to another SNMP agent (next in the chain)
   
SnmpClient snmpClient = new SnmpClient("localhost", 1161, "public", "public");
    snmpClient.setTimeout
(1000);
    snmpAgent.setProxySnmpClient
(snmpClient);

   
// Start the agent
   
snmpAgent.start();
    System.out.println
("SNMP Agent has been started on port " + snmpAgent.getListeningPort());
    System.out.println
("Hit any key to interrupt");
   
try {
     
System.in.read();
   
} catch (IOException e) {
    }
   
snmpAgent.stop();
 
}
}