PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/opennms-rrd/opennms-rrd-api/src/main/java/org/opennms/netmgt/rrd/RrdFileConstants.java

https://gitlab.com/th/opennms
Java | 389 lines | 204 code | 58 blank | 127 comment | 75 complexity | b20b7c625fbbcc8338ed972b32fae6ae MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0
  1. /*******************************************************************************
  2. * This file is part of OpenNMS(R).
  3. *
  4. * Copyright (C) 2007-2012 The OpenNMS Group, Inc.
  5. * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
  6. *
  7. * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
  8. *
  9. * OpenNMS(R) is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published
  11. * by the Free Software Foundation, either version 3 of the License,
  12. * or (at your option) any later version.
  13. *
  14. * OpenNMS(R) is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with OpenNMS(R). If not, see:
  21. * http://www.gnu.org/licenses/
  22. *
  23. * For more information contact:
  24. * OpenNMS(R) Licensing <license@opennms.org>
  25. * http://www.opennms.org/
  26. * http://www.opennms.com/
  27. *******************************************************************************/
  28. package org.opennms.netmgt.rrd;
  29. import java.io.File;
  30. import java.io.FileFilter;
  31. import java.io.FilenameFilter;
  32. import java.util.regex.Matcher;
  33. import java.util.regex.Pattern;
  34. /**
  35. * A convenience class containing RRD file and directory related constants.
  36. *
  37. * @author <a href="mailto:mike@opennms.org">Mike Davidson </a>
  38. * @author <a href="mailto:larry@opennms.org">Lawrence Karnowski </a>
  39. */
  40. public class RrdFileConstants extends Object {
  41. private static final Pattern GRAPHING_ESCAPE_PATTERN;
  42. static {
  43. // IPv6: ':' and '%'
  44. if (File.separatorChar == '\\') {
  45. // If Windows, escape '\' as well
  46. GRAPHING_ESCAPE_PATTERN = Pattern.compile("([\\:\\%\\\\])");
  47. } else {
  48. GRAPHING_ESCAPE_PATTERN = Pattern.compile("([\\:\\%])");
  49. }
  50. }
  51. /** The longest an RRD filename can be, currently 1024 characters. */
  52. public static final int MAX_RRD_FILENAME_LENGTH = 1024;
  53. /** Convenience filter that matches only RRD files. */
  54. public static final FilenameFilter RRD_FILENAME_FILTER = new FilenameFilter() {
  55. @Override
  56. public boolean accept(final File file, final String name) {
  57. return name.endsWith(getRrdSuffix());
  58. }
  59. };
  60. /** Convenience filter that matches directories with RRD files in them. */
  61. public static final FileFilter INTERFACE_DIRECTORY_FILTER = new FileFilter() {
  62. @Override
  63. public boolean accept(final File file) {
  64. return isValidRRDInterfaceDir(file);
  65. }
  66. };
  67. /** Convenience filter that matches directories with RRD files in them. */
  68. public static final FileFilter DOMAIN_INTERFACE_DIRECTORY_FILTER = new FileFilter() {
  69. @Override
  70. public boolean accept(final File file) {
  71. return isValidRRDDomainInterfaceDir(file);
  72. }
  73. };
  74. /**
  75. * Convenience filter that matches integer-named directories that either
  76. * contain RRD files or directories that contain RRD files.
  77. */
  78. public static final FileFilter NODE_DIRECTORY_FILTER = new FileFilter() {
  79. @Override
  80. public boolean accept(File file) {
  81. return isValidRRDNodeDir(file);
  82. }
  83. };
  84. /**
  85. * <p>isValidRRDNodeDir</p>
  86. *
  87. * @param file a {@link java.io.File} object.
  88. * @return a boolean.
  89. */
  90. public static final boolean isValidRRDNodeDir(final File file) {
  91. if (!file.isDirectory()) {
  92. return false;
  93. }
  94. try {
  95. // if the directory name is an integer
  96. Long.valueOf(file.getName());
  97. } catch (final Throwable e) {
  98. return false;
  99. }
  100. // if the node dir contains RRDs, then it is queryable
  101. final File[] nodeRRDs = file.listFiles(RRD_FILENAME_FILTER);
  102. if (nodeRRDs != null && nodeRRDs.length > 0) {
  103. return true;
  104. }
  105. // if the node dir contains queryable interface directories, then
  106. // it is queryable
  107. final File[] intfDirs = file.listFiles(INTERFACE_DIRECTORY_FILTER);
  108. if (intfDirs != null && intfDirs.length > 0) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * Convenience filter that matches integer-named directories that either
  115. * contain RRD files or directories that contain RRD files.
  116. */
  117. public static final FileFilter NODESOURCE_DIRECTORY_FILTER = new FileFilter() {
  118. @Override
  119. public boolean accept(File file) {
  120. return isValidRRDNodeSourceDir(file);
  121. }
  122. };
  123. /**
  124. * <p>isValidRRDNodeSourceDir</p>
  125. *
  126. * @param file a {@link java.io.File} object.
  127. * @return a boolean.
  128. */
  129. public static final boolean isValidRRDNodeSourceDir(final File file) {
  130. if (!file.isDirectory()) {
  131. return false;
  132. }
  133. // if the nodeSource dir contains RRDs, then it is queryable
  134. final File[] nodeRRDs = file.listFiles(RRD_FILENAME_FILTER);
  135. if (nodeRRDs != null && nodeRRDs.length > 0) {
  136. return true;
  137. }
  138. // if the nodeSource dir contains queryable interface directories, then
  139. // it is queryable
  140. final File[] intfDirs = file.listFiles(INTERFACE_DIRECTORY_FILTER);
  141. if (intfDirs != null && intfDirs.length > 0) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Convenience filter that matches non-integer-named directories that
  148. * contain directories that contain RRD files.
  149. */
  150. public static final FileFilter DOMAIN_DIRECTORY_FILTER = new FileFilter() {
  151. @Override
  152. public boolean accept(final File file) {
  153. return isValidRRDDomainDir(file);
  154. }
  155. };
  156. public static final FileFilter SOURCE_DIRECTORY_FILTER = new FileFilter() {
  157. @Override
  158. public boolean accept(final File file) {
  159. return isValidRRDSourceDir(file);
  160. }
  161. };
  162. // FIXME This is not working and it is not being used
  163. public static final boolean isValidRRDSourceDir(final File file) {
  164. if (!file.isDirectory()) {
  165. return false;
  166. }
  167. try {
  168. // if the directory name is an integer
  169. Integer.parseInt(file.getName());
  170. } catch (final Throwable e) {
  171. // if the source dir contains integer-named directories, then
  172. // it is queryable
  173. final File[] idDirs = file.listFiles(NODE_DIRECTORY_FILTER);
  174. if (idDirs != null && idDirs.length > 0) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. /**
  181. * <p>isValidRRDDomainDir</p>
  182. *
  183. * @param file a {@link java.io.File} object.
  184. * @return a boolean.
  185. */
  186. public static final boolean isValidRRDDomainDir(final File file) {
  187. if (!file.isDirectory()) {
  188. return false;
  189. }
  190. try {
  191. // if the directory name is an integer
  192. Integer.parseInt(file.getName());
  193. } catch (final Throwable e) {
  194. // if the domain dir contains queryable interface directories, then
  195. // it is queryable
  196. final File[] intfDirs = file.listFiles(INTERFACE_DIRECTORY_FILTER);
  197. if (intfDirs != null && intfDirs.length > 0) {
  198. for (File intfDir : intfDirs) {
  199. try {
  200. // if the interface directory name is an integer (Long)
  201. Long.valueOf(intfDir.getName());
  202. } catch (final Throwable ee) {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * <p>isValidRRDInterfaceDir</p>
  213. *
  214. * @param file a {@link java.io.File} object.
  215. * @return a boolean.
  216. */
  217. public static final boolean isValidRRDInterfaceDir(final File file) {
  218. if (!file.isDirectory()) {
  219. return false;
  220. }
  221. final File[] intfRRDs = file.listFiles(RRD_FILENAME_FILTER);
  222. if (intfRRDs != null && intfRRDs.length > 0) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. public static final boolean isValidRRDDomainInterfaceDir(final File file) {
  228. if (!file.isDirectory()) {
  229. return false;
  230. }
  231. try {
  232. // if the interface directory name is an integer (Long) its not part of a domain
  233. Long.valueOf(file.getName());
  234. } catch (final Throwable ee) {
  235. final File[] intfRRDs = file.listFiles(RRD_FILENAME_FILTER);
  236. if (intfRRDs != null && intfRRDs.length > 0) {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. /**
  243. * Determines if the provided File object represents a valid RRD latency
  244. * directory.
  245. *
  246. * @param file a {@link java.io.File} object.
  247. * @return a boolean.
  248. */
  249. public static final boolean isValidRRDLatencyDir(final File file) {
  250. if (!file.isDirectory()) {
  251. return false;
  252. }
  253. // if the directory contains RRDs, then it is queryable
  254. final File[] nodeRRDs = file.listFiles(RRD_FILENAME_FILTER);
  255. if (nodeRRDs != null && nodeRRDs.length > 0) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Checks an RRD filename to make sure it is of the proper length and does
  262. * not contain any unexpected charaters.
  263. *
  264. * The maximum length is specified by the
  265. * {@link #MAX_RRD_FILENAME_LENGTH MAX_RRD_FILENAME_LENGTH}constant. The
  266. * only valid characters are letters (A-Z and a-z), numbers (0-9), dashes
  267. * (-), dots (.), and underscores (_). These precautions are necessary since
  268. * the RRD filename is used on the commandline and specified in the graph
  269. * URL.
  270. *
  271. * @param rrd a {@link java.lang.String} object.
  272. * @return a boolean.
  273. */
  274. public static boolean isValidRRDName(final String rrd) {
  275. if (rrd == null) {
  276. throw new IllegalArgumentException("Cannot take null parameters.");
  277. }
  278. final int length = rrd.length();
  279. if (length > MAX_RRD_FILENAME_LENGTH) {
  280. return false;
  281. }
  282. // cannot contain references to higher directories for security's sake
  283. if (rrd.indexOf("..") >= 0) {
  284. return false;
  285. }
  286. for (int i = 0; i < length; i++) {
  287. final char c = rrd.charAt(i);
  288. if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || (c == '_') || (c == '.') || (c == '-') || (c == '/'))) {
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. /**
  295. * Note this method will <strong>not </strong> handle references to higher
  296. * directories ("..").
  297. *
  298. * @param rrd a {@link java.lang.String} object.
  299. * @return a {@link java.lang.String} object.
  300. */
  301. public static String convertToValidRrdName(final String rrd) {
  302. if (rrd == null) {
  303. throw new IllegalArgumentException("Cannot take null parameters.");
  304. }
  305. final StringBuffer buffer = new StringBuffer(rrd);
  306. // truncate after the max length
  307. if (rrd.length() > MAX_RRD_FILENAME_LENGTH) {
  308. buffer.setLength(MAX_RRD_FILENAME_LENGTH - 1);
  309. }
  310. final int length = buffer.length();
  311. for (int i = 0; i < length; i++) {
  312. char c = buffer.charAt(i);
  313. if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || (c == '_') || (c == '.') || (c == '-') || (c == '/'))) {
  314. buffer.setCharAt(i, '_');
  315. }
  316. }
  317. return buffer.toString();
  318. }
  319. public static String escapeForGraphing(final String path) {
  320. final Matcher matcher = GRAPHING_ESCAPE_PATTERN.matcher(path);
  321. return matcher.replaceAll("\\\\$1");
  322. }
  323. /**
  324. * <p>getRrdSuffix</p>
  325. *
  326. * @return a {@link java.lang.String} object.
  327. */
  328. public static String getRrdSuffix() {
  329. return RrdUtils.getExtension();
  330. }
  331. }