001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Nam Nguyen
028: */
029:
030: package com.caucho.server.snmp;
031:
032: import java.util.HashMap;
033:
034: import com.caucho.server.connection.Connection;
035: import com.caucho.server.port.Port;
036: import com.caucho.server.port.Protocol;
037: import com.caucho.server.port.ServerRequest;
038:
039: import com.caucho.server.snmp.types.OctetStringValue;
040: import com.caucho.server.snmp.types.SnmpValue;
041:
042: /*
043: * SNMP v1 protocol.
044: */
045: public class SnmpProtocol extends Protocol {
046: private String _protocolName = "snmp";
047:
048: //holds the mappings from SNMP oids to MBeans
049: private HashMap<String, Oid> _mibMap = new HashMap<String, Oid>();
050:
051: //specially reserved for Caucho by iana.org
052: private final int PRIVATE_ENTERPRISE_NUMBER = 30350;
053:
054: private String _community;
055:
056: public SnmpProtocol() {
057: addOid("1.3.6.1.2.1.1.1", "resin:type=Resin", "Version",
058: SnmpValue.OCTET_STRING);
059:
060: addOid("1.3.6.1.2.1.1.3", "java.lang:type=Runtime", "UpTime",
061: SnmpValue.TIME_TICKS);
062:
063: addOid("1.3.6.1.2.1.1.5", "resin:type=Host,name=default",
064: "URL", SnmpValue.OCTET_STRING);
065:
066: String penPrefix = "1.3.6.1.4.1." + PRIVATE_ENTERPRISE_NUMBER
067: + ".";
068:
069: addOid(penPrefix + "1.1", "resin:type=Server",
070: "KeepaliveCountTotal", SnmpValue.GAUGE);
071:
072: addOid(penPrefix + "1.2", "resin:type=Server",
073: "RequestCountTotal", SnmpValue.GAUGE);
074:
075: addOid(penPrefix + "1.3", "resin:type=Server", "RuntimeMemory",
076: SnmpValue.GAUGE);
077:
078: addOid(penPrefix + "1.4", "resin:type=Server",
079: "RuntimeMemoryFree", SnmpValue.GAUGE);
080:
081: addOid(penPrefix + "1.5", "resin:type=Server",
082: "ThreadActiveCount", SnmpValue.GAUGE);
083:
084: addOid(penPrefix + "1.6", "resin:type=Server",
085: "ThreadKeepaliveCount", SnmpValue.GAUGE);
086:
087: addOid(penPrefix + "2.1", "resin:type=ThreadPool",
088: "ThreadActiveCount", SnmpValue.GAUGE);
089:
090: addOid(penPrefix + "2.2", "resin:type=ThreadPool",
091: "ThreadCount", SnmpValue.GAUGE);
092:
093: addOid(penPrefix + "2.3", "resin:type=ThreadPool",
094: "ThreadIdleCount", SnmpValue.GAUGE);
095:
096: addOid(penPrefix + "2.4", "resin:type=ThreadPool",
097: "ThreadIdleMax", SnmpValue.GAUGE);
098:
099: addOid(penPrefix + "2.5", "resin:type=ThreadPool",
100: "ThreadIdleMin", SnmpValue.GAUGE);
101:
102: addOid(penPrefix + "2.6", "resin:type=ThreadPool", "ThreadMax",
103: SnmpValue.GAUGE);
104:
105: addOid(penPrefix + "3.1", "resin:type=ProxyCache",
106: "HitCountTotal", SnmpValue.GAUGE);
107:
108: addOid(penPrefix + "3.2", "resin:type=ProxyCache",
109: "MissCountTotal", SnmpValue.GAUGE);
110:
111: /* These return complex mbeans
112: addOid(penPrefix + "4.1",
113: "java.lang:type=Memory",
114: "HeapMemoryUsage",
115: SnmpValue.GAUGE);
116:
117: addOid(penPrefix + "4.2",
118: "java.lang:type=Memory",
119: "NonHeapMemoryUsage",
120: SnmpValue.GAUGE);
121: */
122: }
123:
124: /**
125: * Sets the containing port.
126: */
127: @Override
128: public void setParent(Port port) {
129: super .setParent(port);
130: }
131:
132: /**
133: * Returns the protocol name.
134: */
135: public String getProtocolName() {
136: return _protocolName;
137: }
138:
139: /**
140: * Sets the protocol name.
141: */
142: public void setProtocolName(String name) {
143: _protocolName = name;
144: }
145:
146: /*
147: * Adds an SNMP-MBean mapping.
148: */
149: public void addOid(Oid oid) throws Exception {
150: _mibMap.put(oid.getName(), oid);
151: }
152:
153: private void addOid(String name, String mbean, String attribute,
154: int type) {
155: Oid oid = new Oid(name, mbean, attribute, type);
156:
157: _mibMap.put(name, oid);
158: }
159:
160: public HashMap<String, Oid> getMib() {
161: return _mibMap;
162: }
163:
164: public String getCommunity() {
165: if (_community == null)
166: return "public";
167: else
168: return _community;
169: }
170:
171: public void setCommunity(String s) {
172: _community = s;
173: }
174:
175: public ServerRequest createRequest(Connection connection) {
176: OctetStringValue community;
177:
178: if (_community == null)
179: community = OctetStringValue.PUBLIC;
180: else
181: community = new OctetStringValue(_community);
182:
183: return new SnmpRequest(connection, _mibMap, community);
184: }
185: }
|