/
XmpCollectionResource.java
245 lines (203 loc) · 8 KB
/
XmpCollectionResource.java
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2011-2012 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
/*
* OCA CONTRIBUTION ACKNOWLEDGEMENT - NOT PART OF LEGAL BOILERPLATE
* DO NOT DUPLICATE THIS COMMENT BLOCK WHEN CREATING NEW FILES!
*
* This file was contributed to the OpenNMS(R) project under the
* terms of the OpenNMS Contributor Agreement (OCA). For details on
* the OCA, see http://www.opennms.org/index.php/Contributor_Agreement
*
* Contributed under the terms of the OCA by:
*
* Bobby Krupczak <rdk@krupczak.org>
* THE KRUPCZAK ORGANIZATION, LLC
* http://www.krupczak.org/
*/
/** XmpCollectionResource contains a set of AttributeGroups which
in turn contain actual attributes or XmpVars. Attribute groups
closely mirror the data collection config file.
@author <a href="mailto:rdk@krupczak.org">Bobby Krupczak</a>
@version $Id: XmpCollectionResource.java 38 2008-07-24 13:39:32Z rdk $
**/
package org.opennms.netmgt.protocols.xmp.collector;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.opennms.netmgt.collectd.AbstractCollectionResource;
import org.opennms.netmgt.collectd.CollectionAgent;
import org.opennms.netmgt.config.collector.AttributeGroup;
import org.opennms.netmgt.config.collector.CollectionSetVisitor;
import org.opennms.netmgt.model.RrdRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class XmpCollectionResource extends AbstractCollectionResource
{
/* class variables and methods *********************** */
private static final Logger LOG = LoggerFactory.getLogger(XmpCollectionResource.class);
/* instance variables ******************************** */
private final String m_nodeTypeName;
private final String m_instance;
private final String m_resourceType;
private final int m_nodeType;
private final Set<AttributeGroup> m_listOfGroups;
/* constructors ************************************* */
public XmpCollectionResource(CollectionAgent agent, String resourceType, String nodeTypeName, String instance)
{
super(agent);
// node type can be "node" for scalars or
// "if" for network interface resources and
// "*" for all other resource types
// or anything but 'node' (e.g. table name)
// resourceType tells us if we are writing under a separate RRD
// subdir
this.m_nodeTypeName = nodeTypeName;
if ((resourceType == null) || (resourceType.length() == 0)) {
this.m_resourceType = null;
} else {
this.m_resourceType = resourceType;
}
m_nodeType = -1;
// filter the instance so it does not have slashes (/) nor colons
// in it as they can munge our rrd file layout
// filter so there are not spaces either just so that
// it makes directory structures less annoying to deal with
// rdk - 9/11/2009
if (instance != null) {
String instanceValue = instance.replace('/','_');
instanceValue = instanceValue.replace('\\','_');
instanceValue = instanceValue.replace(':','_');
instanceValue = instanceValue.replace(' ','_');
this.m_instance = instanceValue;
}
else {
this.m_instance = null;
}
m_listOfGroups = new HashSet<AttributeGroup>();
}
/* private methods *********************************** */
/* public methods ************************************ */
// get the location where we are supposed to write our data to
/** {@inheritDoc} */
@Override
public File getResourceDir(RrdRepository repository)
{
// if we are a collection resource for scalars,
// return what our super class would return
if (m_nodeTypeName.equalsIgnoreCase("node")) {
return new File(repository.getRrdBaseDir(), getParent());
}
// we are a collection resource for tabular data
// return essentially share/rrd/snmp/NodeId/resourceType/instance
// for now; the problem with using key/instance is that
// it can change for some tables (e.g. proc table)
// whoever instantiates this object is responsible for
// passing in an instance that will be unique;
// if we want a specific instance of a table, we will use
// the instance/key that was used for the query; if not,
// we will use the key returned per table row
File instDir, rtDir;
File rrdBaseDir = repository.getRrdBaseDir();
File nodeDir = new File(rrdBaseDir, getParent());
// if we have a resourceType, put instances under it
if (m_resourceType != null) {
rtDir = new File(nodeDir,m_resourceType);
instDir = new File(rtDir,m_instance);
}
else {
instDir = new File(nodeDir,m_instance);
}
return instDir;
}
/**
* <p>addAttributeGroup</p>
*
* @param aGroup a {@link org.opennms.netmgt.config.collector.AttributeGroup} object.
*/
public void addAttributeGroup(AttributeGroup aGroup)
{
m_listOfGroups.add(aGroup);
}
/**
* <p>Getter for the field <code>instance</code>.</p>
*
* @return a {@link java.lang.String} object.
*/
@Override
public String getInstance()
{
// for node level resources, no instance
return m_instance;
}
/**
* <p>getResourceTypeName</p>
*
* @return a {@link java.lang.String} object.
*/
@Override
public String getResourceTypeName() { return m_nodeTypeName; };
/**
* <p>getType</p>
* return -1 for non-tabular; what do we return for
* for interface or tabular data?
*
* @return a int.
*/
@Override
public int getType() { return m_nodeType; }
/**
* <p>getGroups</p>
*
* @return a {@link java.util.Collection} object.
*/
public Collection<AttributeGroup>getGroups() { return m_listOfGroups; }
/**
* <p>toString</p>
*
* @return a {@link java.lang.String} object.
*/
@Override
public String toString() { return "XmpCollectionResource for "+m_agent+" resType="+m_resourceType+" instance="+m_instance+" nodeType="+m_nodeTypeName+" nodeType="+m_nodeType; }
/**
* @deprecated This class should be changed to store its {@link AttributeGroup}
* collection in {@link #m_attributeGroups} like all of the other implementations do.
*/
@Override
public void visit(CollectionSetVisitor visitor)
{
LOG.debug("XmpCollectionResource: visit starting with {} attribute groups", getGroups().size());
visitor.visitResource(this);
// visit the attribute groups one at a time
for (AttributeGroup ag: getGroups()) {
ag.visit(visitor);
}
visitor.completeResource(this);
LOG.debug("XmpCollectionResource: visit finished for {}", m_agent);
} /* visit */
} /* class XmpCollectionResource */