mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Maxim Thomas
25.00.2024 1cb77604cc0b6d17ece9b40fe800d0a43eb6893f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2009 Sun Microsystems, Inc.
 * Portions Copyright 2014 ForgeRock AS.
 * Portions Copyright 2024 3A Systems, LLC.
 */
package org.opends.server.snmp;
 
import com.sun.management.snmp.agent.SnmpMib;
 
import java.util.Iterator;
import java.util.Set;
 
import javax.management.MBeanServer;
import javax.management.ObjectName;
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.guitools.controlpanel.util.Utilities;
 
/**
 * The class is used for implementing the "DsTableEntry" group implementation.
 * The group is defined with the following oid: 1.3.6.1.2.1.66.1.1.
 */
public class DsTableEntryImpl extends DsTableEntry implements DsEntry {
 
    /**
     * The serial version identifier required to satisfy the compiler because
     * this class implements the <CODE>java.io.Serializable</CODE> interface.
     * This value was generated using the <CODE>serialver</CODE> command-line
     * utility included with the Java SDK.
     */
    private static final long serialVersionUID = -3346380035687141480L;
 
    private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
    /**
     * Directory Server MBeanServer.
     */
    private MBeanServer server;
    /**
     * Mapping Class.
     */
    private SNMPMonitor monitor;
    /**
     * ObjectName of the entry.
     */
    private ObjectName entryName;
    /**
     * Index of the Directory Server Instance (applIndex).
     */
    private Integer applIndex;
 
    /**
     * Creates a DsTableEntry.
     * @param mib the SNMP Mib where the entry will be created
     * @param server where the mapping objects will be found
     * @param index of the entry in the DsTable
     */
    public DsTableEntryImpl(SnmpMib mib,
            MBeanServer server,
            int index) {
        super(mib);
        this.server = server;
        this.monitor = SNMPMonitor.getMonitor(server);
        this.applIndex = new Integer(index);
    }
 
    /**
     * Getter for the "DsServerType" variable.
     * @return a Byte[] representing the Ds Server Type
     */
    public Byte[] getDsServerType() {
        try {
            String value1 = (String) this.monitor.getAttribute(
                    SNMPConnectionHandlerDefinitions.MONITOR_VERSION_OBJECTNAME,
                    "fullVersion");
            String value2 = (String) this.monitor.getAttribute(
                    SNMPConnectionHandlerDefinitions.MONITOR_VERSION_OBJECTNAME,
                    "buildID");
            return SNMPMonitor.string2ByteArray(value1 + " - " + value2);
        } catch (Exception ex) {
            logger.traceException(ex);
            return null;
        }
    }
 
    /**
     * {@inheritDoc}
     * @return DsCacheHits as Long
     */
    @Override
    public Long getDsCacheHits() {
        try {
            Long value = Long.parseLong((String) this.monitor.getAttribute(
              SNMPConnectionHandlerDefinitions.MONITOR_ENTRY_CACHES_OBJECTNANE,
                    "entryCacheHits"));
            return SNMPMonitor.counter32Value(value);
        } catch (Exception ex) {
            logger.traceException(ex);
        }
        return 0L;
    }
 
    /**
     * {@inheritDoc}
     * @return DsCacheEntries as Long
     */
    @Override
    public Long getDsCacheEntries() {
        try {
            Long value = Long.parseLong((String) this.monitor.getAttribute(
              SNMPConnectionHandlerDefinitions.MONITOR_ENTRY_CACHES_OBJECTNANE,
                    "currentEntryCacheCount"));
            return SNMPMonitor.gauge32Value(value);
        } catch (Exception ex) {
            logger.traceException(ex);
        }
        return 0L;
    }
 
    /**
     * {@inheritDoc}
     * @return DsMasterEntries as Long
     */
    @Override
    public Long getDsMasterEntries() {
        Set monitorBackends = null;
        Long result = 0L;
        try {
            monitorBackends = this.server.queryNames(SNMPMonitor.pattern, null);
            for (Iterator iter = monitorBackends.iterator(); iter.hasNext();) {
                ObjectName name = (ObjectName) iter.next();
                Object value = this.monitor.getAttribute(name,
                        "ds-backend-entry-count");
                if (value != null && value instanceof String) {
                    result = result + new Long((String) value);
                }else if (value != null && value instanceof Long) {
                    result = result + (Long)value;
                }
            }
            return SNMPMonitor.gauge32Value(result);
        } catch (Exception ex) {
            logger.traceException(ex);
        }
        return 0L;
    }
 
    /**
     * {@inheritDoc}
     * @return DsServerDescription as String
     */
    @Override
    public String getDsServerDescription() {
        return Utilities.getServerRootDirectory().getAbsolutePath();
    }
 
    /**
     * Gets the object of the entry.
     * @return ObjectName of the entry
     */
    public ObjectName getObjectName() {
        if (this.entryName == null) {
            try {
                this.entryName = new ObjectName(
                        SNMPConnectionHandlerDefinitions.SNMP_DOMAIN +
                        "type=DsTableEntry,name=" +
        SNMPConnectionHandlerDefinitions.MONITOR_SYSTEM_INFORMATION_OBJECTNAME);
            } catch (Exception ex) {
              logger.traceException(ex);
            }
        }
        return this.entryName;
    }
}