Class Tools

java.lang.Object
org.graylog2.plugin.Tools

public final class Tools extends Object
Utility class for various tool/helper functions.
  • 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

      public static String getPID()
      Get the own PID of this process.
      Returns:
      PID of the running process
    • syslogLevelToReadable

      public static String syslogLevelToReadable(int level)
      Converts integer syslog loglevel to human readable string
      Parameters:
      level - The level to convert
      Returns:
      The human readable level
    • syslogFacilityToReadable

      public static String syslogFacilityToReadable(int facility)
      Converts integer syslog facility to human readable string
      Parameters:
      facility - The facility to convert
      Returns:
      The human readable facility
    • getSystemInformation

      public static String getSystemInformation()
      Get a String containing version information of JRE, OS, ...
      Returns:
      Descriptive string of JRE and OS
    • decompressZlib

      public static String decompressZlib(byte[] compressedData) throws IOException
      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

      public static String decompressZlib(byte[] compressedData, long maxBytes) throws IOException
      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

      public static String decompressGzip(byte[] compressedData) throws IOException
      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

      public static String decompressGzip(byte[] compressedData, long maxBytes) throws IOException
      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

      public static String getLocalHostname()
    • getLocalCanonicalHostname

      public static String getLocalCanonicalHostname()
    • getTimestampDaysAgo

      public static int getTimestampDaysAgo(int ts, int days)
    • encodeBase64

      public static String encodeBase64(String what)
    • decodeBase64

      public static String decodeBase64(String what)
    • rdnsLookup

      public static String rdnsLookup(InetAddress socketAddress) throws UnknownHostException
      Throws:
      UnknownHostException
    • generateServerId

      public static String generateServerId()
    • asSortedSet

      public static <T extends Comparable<? super T>> SortedSet<T> asSortedSet(Collection<T> c)
    • buildElasticSearchTimeFormat

      public static String buildElasticSearchTimeFormat(org.joda.time.DateTime timestamp)
    • 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

      public static org.joda.time.DateTime dateTimeFromString(String s)
      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 public static org.joda.time.DateTime iso8601()
      Deprecated.
      Use nowUTC() instead.
      Returns:
      The current date with timezone UTC.
    • getISO8601String

      public static String getISO8601String(org.joda.time.DateTime time)
    • elasticSearchTimeFormatToISO8601

      public static String elasticSearchTimeFormatToISO8601(String time)
      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

      public static String safeSubstring(String target, int start, int end)
      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

      public static Double getDouble(Object x)
      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

      public static Number getNumber(Object o, Number defaultValue)
    • guessPrimaryNetworkAddress

      public static InetAddress guessPrimaryNetworkAddress(boolean preferIPv4) throws SocketException
      Try to get the primary InetAddress of the primary network interface with fallback to the local loopback address (usually 127.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

      public static boolean isWildcardInetAddress(@Nullable InetAddress inetAddress)
    • getUriWithPort

      @Nullable public static URI getUriWithPort(@Nullable URI uri, int port)
    • getUriWithScheme

      @Nullable public static URI getUriWithScheme(@Nullable URI uri, String scheme)
    • getUriWithDefaultPath

      @Nullable public static URI getUriWithDefaultPath(@Nullable URI uri, String path)
    • uriWithTrailingSlash

      @Nullable public static URI uriWithTrailingSlash(@Nullable URI uri)
    • normalizeURI

      @Nullable public static URI normalizeURI(@Nullable URI uri, String scheme, int port, String path)
    • getKeyByValue

      @Nullable public static <T, E> T getKeyByValue(Map<T,E> map, E value)
    • bytesToHex

      public static String bytesToHex(byte[] a)
    • 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

      public static Optional<AbsoluteRange> extractHistogramBoundaries(String query)