Package org.graylog2.plugin
Class Tools
java.lang.Object
org.graylog2.plugin.Tools
Utility class for various tool/helper functions.
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final org.joda.time.format.DateTimeFormatter
static final org.joda.time.format.DateTimeFormatter
static final org.joda.time.format.DateTimeFormatter
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Comparable<? super T>>
SortedSet<T>asSortedSet
(Collection<T> c) static String
buildElasticSearchTimeFormat
(org.joda.time.DateTime timestamp) static String
bytesToHex
(byte[] a) static org.joda.time.DateTime
dateTimeFromDouble
(double x) The double representation of a UNIX timestamp with milliseconds is a strange, human readable format.static org.joda.time.DateTime
Parse the string representation of an ISO 8601 date/timestamp with milliseconds and timezone.static String
decodeBase64
(String what) static String
decompressGzip
(byte[] compressedData) Decompress GZIP (RFC 1952) compressed datastatic String
decompressGzip
(byte[] compressedData, long maxBytes) Decompress GZIP (RFC 1952) compressed datastatic String
decompressZlib
(byte[] compressedData) Decompress ZLIB (RFC 1950) compressed datastatic String
decompressZlib
(byte[] compressedData, long maxBytes) Decompress ZLIB (RFC 1950) compressed datastatic String
Try to parse a date in ES_DATE_FORMAT format considering it is in UTC and convert it to an ISO8601 date.static String
encodeBase64
(String what) static Optional<AbsoluteRange>
extractHistogramBoundaries
(String query) static String
static Double
Convert something to a double in a fast way having a good guess that it is a double.static String
getISO8601String
(org.joda.time.DateTime time) static <T,
E> T getKeyByValue
(Map<T, E> map, E value) static String
static String
static Number
static String
getPID()
Get the own PID of this process.static String
Get a String containing version information of JRE, OS, ...static int
getTimestampDaysAgo
(int ts, int days) static URI
getUriWithDefaultPath
(URI uri, String path) static URI
getUriWithPort
(URI uri, int port) static URI
getUriWithScheme
(URI uri, String scheme) static int
static double
Get the current UNIX epoch with milliseconds of the systemstatic double
getUTCTimestampWithMilliseconds
(long timestamp) Get the UNIX epoch with milliseconds of the provided millisecond timestampstatic InetAddress
guessPrimaryNetworkAddress
(boolean preferIPv4) Try to get the primaryInetAddress
of the primary network interface with fallback to the local loopback address (usually127.0.0.1
or::1
.static org.joda.time.DateTime
iso8601()
Deprecated.static boolean
isWildcardInetAddress
(InetAddress inetAddress) static URI
normalizeURI
(URI uri, String scheme, int port, String path) static org.joda.time.DateTime
now
(org.joda.time.DateTimeZone dateTimeZone) static org.joda.time.DateTime
nowUTC()
static String
rdnsLookup
(InetAddress socketAddress) static String
safeSubstring
(String target, int start, int end) static void
The default uncaught exception handler will print to STDERR, which we don't always want for threads.static String
syslogFacilityToReadable
(int facility) Converts integer syslog facility to human readable stringstatic String
syslogLevelToReadable
(int level) Converts integer syslog loglevel to human readable stringstatic org.joda.time.format.DateTimeFormatter
Accepts our ElasticSearch time formats without milliseconds.static URI
uriWithTrailingSlash
(URI uri)
-
Field Details
-
ES_DATE_FORMAT_FORMATTER
public static final org.joda.time.format.DateTimeFormatter ES_DATE_FORMAT_FORMATTER -
ES_DATE_FORMAT_NO_MS_FORMATTER
public static final org.joda.time.format.DateTimeFormatter ES_DATE_FORMAT_NO_MS_FORMATTER -
ISO_DATE_FORMAT_FORMATTER
public static final org.joda.time.format.DateTimeFormatter ISO_DATE_FORMAT_FORMATTER
-
-
Method Details
-
getPID
Get the own PID of this process.- Returns:
- PID of the running process
-
syslogLevelToReadable
Converts integer syslog loglevel to human readable string- Parameters:
level
- The level to convert- Returns:
- The human readable level
-
syslogFacilityToReadable
Converts integer syslog facility to human readable string- Parameters:
facility
- The facility to convert- Returns:
- The human readable facility
-
getSystemInformation
Get a String containing version information of JRE, OS, ...- Returns:
- Descriptive string of JRE and OS
-
decompressZlib
Decompress ZLIB (RFC 1950) compressed data- Parameters:
compressedData
- A byte array containing the ZLIB-compressed data.- Returns:
- A string containing the decompressed data
- Throws:
IOException
-
decompressZlib
Decompress ZLIB (RFC 1950) compressed data- Parameters:
compressedData
- A byte array containing the ZLIB-compressed data.maxBytes
- The maximum number of uncompressed bytes to read.- Returns:
- A string containing the decompressed data
- Throws:
IOException
-
decompressGzip
Decompress GZIP (RFC 1952) compressed data- Parameters:
compressedData
- A byte array containing the GZIP-compressed data.- Returns:
- A string containing the decompressed data
- Throws:
IOException
-
decompressGzip
Decompress GZIP (RFC 1952) compressed data- Parameters:
compressedData
- A byte array containing the GZIP-compressed data.maxBytes
- The maximum number of uncompressed bytes to read.- Returns:
- A string containing the decompressed data
- Throws:
IOException
-
getUTCTimestamp
public static int getUTCTimestamp()- Returns:
- The current UTC UNIX timestamp.
-
getUTCTimestampWithMilliseconds
public static double getUTCTimestampWithMilliseconds()Get the current UNIX epoch with milliseconds of the system- Returns:
- The current UTC UNIX timestamp with milliseconds.
-
getUTCTimestampWithMilliseconds
public static double getUTCTimestampWithMilliseconds(long timestamp) Get the UNIX epoch with milliseconds of the provided millisecond timestamp- Parameters:
timestamp
- a millisecond timestamp (milliseconds since UNIX epoch)- Returns:
- The current UTC UNIX timestamp with milliseconds.
-
getLocalHostname
-
getLocalCanonicalHostname
-
getTimestampDaysAgo
public static int getTimestampDaysAgo(int ts, int days) -
encodeBase64
-
decodeBase64
-
rdnsLookup
- Throws:
UnknownHostException
-
generateServerId
-
asSortedSet
-
buildElasticSearchTimeFormat
-
dateTimeFromDouble
public static org.joda.time.DateTime dateTimeFromDouble(double x) The double representation of a UNIX timestamp with milliseconds is a strange, human readable format. This sucks and no format should use the double representation. Change GELF to use long. (zomg) -
dateTimeFromString
Parse the string representation of an ISO 8601 date/timestamp with milliseconds and timezone. -
timeFormatterWithOptionalMilliseconds
public static org.joda.time.format.DateTimeFormatter timeFormatterWithOptionalMilliseconds()Accepts our ElasticSearch time formats without milliseconds.- Returns:
- A DateTimeFormatter suitable to parse an ES_DATE_FORMAT formatted string to a DateTime Object even if it contains no milliseconds.
-
nowUTC
public static org.joda.time.DateTime nowUTC() -
now
public static org.joda.time.DateTime now(org.joda.time.DateTimeZone dateTimeZone) -
iso8601
Deprecated.UsenowUTC()
instead.- Returns:
- The current date with timezone UTC.
-
getISO8601String
-
elasticSearchTimeFormatToISO8601
Try to parse a date in ES_DATE_FORMAT format considering it is in UTC and convert it to an ISO8601 date. If an error is encountered in the process, it will return the original string. -
safeSubstring
- Parameters:
target
- String to cut.start
- Character position to start cutting at. Inclusive.end
- Character position to stop cutting at. Exclusive!- Returns:
- Extracted/cut part of the string or null when invalid positions where provided.
-
getDouble
Convert something to a double in a fast way having a good guess that it is a double. This is perfect for MongoDB data that *should* have been stored as doubles already so there is a high probability of easy converting.- Parameters:
x
- The object to convert to a double- Returns:
- Converted object, 0 if empty or something went wrong.
-
getNumber
-
guessPrimaryNetworkAddress
Try to get the primaryInetAddress
of the primary network interface with fallback to the local loopback address (usually127.0.0.1
or::1
.- Returns:
- The primary
InetAddress
of the primary network interface or the loopback address as fallback. - Throws:
SocketException
- if the list of network interfaces couldn't be retrieved
-
isWildcardInetAddress
-
getUriWithPort
-
getUriWithScheme
-
getUriWithDefaultPath
-
uriWithTrailingSlash
-
normalizeURI
-
getKeyByValue
-
bytesToHex
-
silenceUncaughtExceptionsInThisThread
public static void silenceUncaughtExceptionsInThisThread()The default uncaught exception handler will print to STDERR, which we don't always want for threads. Using this utility method you can avoid writing to STDERR on a per-thread basis -
extractHistogramBoundaries
-
nowUTC()
instead.