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.ArrayList;
033: import java.util.HashMap;
034:
035: import javax.management.ObjectName;
036: import com.caucho.jmx.Jmx;
037: import com.caucho.management.server.AbstractManagedObject;
038:
039: public class SnmpAdmin extends AbstractManagedObject {
040: private String _host = "";
041: private int _port;
042:
043: private String _sysContact = "";
044: private String _sysLocation = "";
045:
046: private static int CAUCHO_PRIVATE_ENTERPRISE_NUMBER = 0;
047:
048: private HashMap<String, Oid> _mibMap = new HashMap<String, Oid>();
049:
050: public SnmpAdmin(String host, int port) {
051: _host = host;
052: _port = port;
053:
054: init();
055:
056: registerSelf();
057: }
058:
059: private void init() {
060: String mbean = getObjectName().toString();
061:
062: /*
063: addAttribute(new Oid(mbean, "sysDescr", "1.3.6.1.2.1.1.1"));
064: addAttribute(new Oid(mbean, "sysObjectId", "1.3.6.1.2.1.1.2", "OBJECT_IDENTIFIER"));
065: addAttribute(new Oid(mbean, "sysUpTime", "1.3.6.1.2.1.1.3", "TIME_TICKS"));
066: addAttribute(new Oid(mbean, "sysContact", "1.3.6.1.2.1.1.4"));
067: addAttribute(new Oid(mbean, "sysName", "1.3.6.1.2.1.1.5"));
068: addAttribute(new Oid(mbean, "sysLocation", "1.3.6.1.2.1.1.6"));
069: addAttribute(new Oid(mbean, "sysServices", "1.3.6.1.2.1.1.7"));
070: */
071: }
072:
073: public void addAttribute(Oid attr) {
074: _mibMap.put(attr.getName(), attr);
075: }
076:
077: public Oid getAttribute(String s) {
078: return _mibMap.get(s);
079: }
080:
081: @Override
082: public String getName() {
083: return _host + ":" + _port;
084: }
085:
086: public String getSysDescr() {
087: try {
088: ObjectName name = new ObjectName("resin:type=Resin");
089:
090: return Jmx.getMBeanServer().getAttribute(name, "Version")
091: .toString();
092: } catch (Exception e) {
093: return "";
094: }
095: }
096:
097: public String sysObjectID() {
098: return "1.3.6.1.4.1." + CAUCHO_PRIVATE_ENTERPRISE_NUMBER;
099: }
100:
101: public long getSysUpTime() {
102: try {
103: ObjectName name = new ObjectName("java.lang:type=Runtime");
104:
105: Object obj = Jmx.getMBeanServer().getAttribute(name,
106: "Uptime");
107:
108: if (obj instanceof Number)
109: return ((Number) obj).longValue();
110: else
111: return 0;
112: } catch (Exception e) {
113: return 0;
114: }
115: }
116:
117: public String getSysContact() {
118: return _sysContact;
119: }
120:
121: public void setSysContact(String sysContact) {
122: _sysContact = sysContact;
123: }
124:
125: public String getSysName() {
126: try {
127: ObjectName name = new ObjectName(
128: "resin:type=Host,name=default");
129:
130: Object obj = Jmx.getMBeanServer().getAttribute(name, "URL");
131:
132: return obj.toString();
133: } catch (Exception e) {
134: return "";
135: }
136: }
137:
138: public String getSysLocation() {
139: return _sysLocation;
140: }
141:
142: public void setSysLocation(String sysLocation) {
143: _sysLocation = sysLocation;
144: }
145:
146: public int getSysServices() {
147: // this means that Resin is on the application layer of the OSI model
148: return 0x02 << (7 - 1);
149: }
150:
151: static class Mbean {
152: private String _name;
153: private String _prefix = "";
154:
155: private ArrayList<Oid> _attrList = new ArrayList<Oid>();
156:
157: public void setName(String name) {
158: _name = name;
159: }
160:
161: public String getName() {
162: return _name;
163: }
164:
165: public void setPrefix(String prefix) {
166: _prefix = prefix;
167: }
168:
169: public String getPrefix() {
170: return _prefix;
171: }
172:
173: public void addAttribute(Oid attr) {
174: _attrList.add(attr);
175: }
176:
177: public boolean equals(Object obj) {
178: return obj instanceof Mbean
179: && ((Mbean) obj).getPrefix().equals(_prefix);
180: }
181: }
182: }
|