1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package goldengate.snmp.test;
21
22 import goldengate.common.logging.GgSlf4JLoggerFactory;
23 import goldengate.snmp.GgMOFactory;
24 import goldengate.snmp.GgSnmpAgent;
25 import goldengate.snmp.utils.GgMOScalar;
26
27 import java.io.File;
28 import java.io.IOException;
29
30 import org.jboss.netty.logging.InternalLoggerFactory;
31 import org.snmp4j.event.ResponseEvent;
32 import org.snmp4j.event.ResponseListener;
33 import org.snmp4j.mp.SnmpConstants;
34
35 import ch.qos.logback.classic.Level;
36
37
38
39
40
41
42
43 public class GgTestSnmpClientAgent {
44 static GgSnmpAgent agent;
45
46 static GgSimpleSnmpClient client;
47
48 static GgImplPrivateMib test;
49
50 public static void main(String[] args) throws Exception {
51 InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
52 Level.ALL));
53 setUp(args[0]);
54 System.out.println("Test SysDescr");
55 verifySysDescr();
56 System.out.println("Test Table");
57 verifyTableContents();
58 Thread.sleep(10000);
59 sendNotification();
60 Thread.sleep(30000);
61 System.out.println("Stopping");
62 tearDown();
63 }
64
65 public static void setUp(String file) throws Exception {
66
67 client = new GgSimpleSnmpClient("udp:127.0.0.1/2001", 1162);
68
69 GgPrivateMonitor monitor = new GgPrivateMonitor();
70
71 test = new GgImplPrivateMib("GoldenGate Test SNMP", 6666, 66666, 66,
72 "F. Bregier", "GoldenGate Test SNMP V1.0", "Paris, France", 72);
73
74 GgMOFactory.factory = new GgTestVariableFactory();
75
76 agent = new GgSnmpAgent(new File(file), monitor, test);
77 agent.start();
78 }
79
80 public static void tearDown() throws Exception {
81 agent.stop();
82 client.stop();
83 }
84
85 private static boolean assertEquals(String a, String b) {
86 if (!a.equals(b)) {
87 System.err.println("Not Equals! " + a + " and " + b);
88 return false;
89 }
90 System.out.println("Equal: " + a);
91 return true;
92 }
93
94
95
96
97
98 public static void verifySysDescr() throws IOException {
99 assertEquals(test.textualSysDecr,
100 client.getAsString(SnmpConstants.sysDescr));
101 }
102
103
104
105
106 public static void verifyTableContents() {
107 for (GgMOScalar scalar: test.rowInfo.row) {
108 try {
109 System.out.println("Read " + scalar.getID() + ":" +
110 client.getAsString(scalar.getID()));
111 } catch (IOException e) {
112 System.err.println(scalar.getID() + ":" + e.getMessage());
113 continue;
114 }
115 }
116 }
117
118 public static void sendNotification() {
119 test.notifyInfo("Une alerte", "un autre texte d'alerte", 1971);
120 test.notifyError("Une seconde alerte", "un second texte d'alerte", 42);
121 }
122
123 static class StringResponseListener implements ResponseListener {
124
125 private String value = null;
126
127 @Override
128 public void onResponse(ResponseEvent event) {
129 System.out.println(event.getResponse());
130 if (event.getResponse() != null) {
131 value = GgSimpleSnmpClient.extractSingleString(event);
132 }
133 }
134
135 public String getValue() {
136 System.out.println(value);
137 return value;
138 }
139
140 }
141
142 }