<commit_before>#!/usr/bin/env ruby

if RUBY_VERSION =~ /^1.8/
  require 'rubygems'
  require 'net/https'
end
require 'optparse'
require 'uri'
require 'net/http'
require 'json'

CONN_TIMEOUT = 2  # http connection timeout (seconds)
READ_TIMEOUT = 2  # http read timeout (seconds)
DEFAULT_API_URL = "https://api.opsmatic.com/webhooks/events"

# map nagios statuses to opsmatic statuses
status_map = {
  # host states
  "UP"       => "ok",
  "DOWN"     => "failure",
  # service states
  "OK"       => "ok",
  "WARNING"  => "warning",
  "CRITICAL" => "failure",
  "UNKNOWN"  => "warning"
}

## process command line
options = {
  :api_url => DEFAULT_API_URL,
  :name_pref => "name",
}

OptionParser.new do |opts|
  opts.on "-t", "--token TOKEN", "Opsmatic Integration Token" do |token|
    options[:token] = token
  end
  opts.on "-u", "--url URL", "Opsmatic API URL (default: #{options[:api_url]})" do |url|
    options[:api_url] = url
  end
  opts.on "-n", "--hostname_pref NAME_PREF", "Which Nagios field to use for hostname 'name' or 'alias'" do |name_pref|
    options[:name_pref] = name_pref.downcase
  end
end.parse!

if !options.has_key?(:token)
  abort "ERROR: you must provide your opsmatic integration token with the --token parameter"
end

if options[:name_pref] !~ /name|alias/
  abort "ERROR: hostname prefernce must be 'name' or 'alias'"
end

# cherry pick the nagios details we need
data = {
  :notification_type => ENV['NAGIOS_NOTIFICATIONTYPE'],
  :notification_time => ENV['NAGIOS_TIMET'].to_i,
  :host => {
    :name        => ENV['NAGIOS_HOSTNAME'],
    :alias       => ENV['NAGIOS_HOSTALIAS'],
    :address     => ENV['NAGIOS_HOSTADDRESS'],
    :state       => ENV['NAGIOS_HOSTSTATE'],
    :output      => ENV['NAGIOS_HOSTOUTPUT'],
  },
  :service => {
    :desc        => ENV['NAGIOS_SERVICEDESC'],
    :state       => ENV['NAGIOS_SERVICESTATE'],
    :output      => ENV['NAGIOS_SERVICEOUTPUT'],
  },
}

## process the event
# no service state, we must be a host event
event = {
  :timestamp    => data[:notification_time],
  :source       => "nagios",
  :type         => "notifications/nagios",
  :data         => {},
  :scopes       => {
    :hostname => data[:host][:name]
  }
}
<commit_msg>simplify with ternary<commit_after>#!/usr/bin/env ruby

if RUBY_VERSION =~ /^1.8/
  require 'rubygems'
  require 'net/https'
end
require 'optparse'
require 'uri'
require 'net/http'
require 'json'

CONN_TIMEOUT = 2  # http connection timeout (seconds)
READ_TIMEOUT = 2  # http read timeout (seconds)
DEFAULT_API_URL = "https://api.opsmatic.com/webhooks/events"

# map nagios statuses to opsmatic statuses
status_map = {
  # host states
  "UP"       => "ok",
  "DOWN"     => "failure",
  # service states
  "OK"       => "ok",
  "WARNING"  => "warning",
  "CRITICAL" => "failure",
  "UNKNOWN"  => "warning"
}

## process command line
options = {
  :api_url => DEFAULT_API_URL,
  :name_pref => "name",
}

OptionParser.new do |opts|
  opts.on "-t", "--token TOKEN", "Opsmatic Integration Token" do |token|
    options[:token] = token
  end
  opts.on "-u", "--url URL", "Opsmatic API URL (default: #{options[:api_url]})" do |url|
    options[:api_url] = url
  end
  opts.on "-n", "--hostname_pref NAME_PREF", "Which Nagios field to use for hostname 'name' or 'alias'" do |name_pref|
    options[:name_pref] = name_pref.downcase
  end
end.parse!

if !options.has_key?(:token)
  abort "ERROR: you must provide your opsmatic integration token with the --token parameter"
end

if options[:name_pref] !~ /name|alias/
  abort "ERROR: hostname prefernce must be 'name' or 'alias'"
end

# cherry pick the nagios details we need
data = {
  :notification_type => ENV['NAGIOS_NOTIFICATIONTYPE'],
  :notification_time => ENV['NAGIOS_TIMET'].to_i,
  :host => {
    :name        => ENV['NAGIOS_HOSTNAME'],
    :alias       => ENV['NAGIOS_HOSTALIAS'],
    :address     => ENV['NAGIOS_HOSTADDRESS'],
    :state       => ENV['NAGIOS_HOSTSTATE'],
    :output      => ENV['NAGIOS_HOSTOUTPUT'],
  },
  :service => {
    :desc        => ENV['NAGIOS_SERVICEDESC'],
    :state       => ENV['NAGIOS_SERVICESTATE'],
    :output      => ENV['NAGIOS_SERVICEOUTPUT'],
  },
}

## process the event
# no service state, we must be a host event
event = {
  :timestamp    => data[:notification_time],
  :source       => "nagios",
  :type         => "notifications/nagios",
  :data         => {},
  :scopes       => {
    :hostname => data[:host][:name] ? options[:name_pref] == "name" : data[:host][:alias]
  }
}

{'fraction_non_alphanumeric': 0.15975935828877005, 'fraction_numerical': 0.0017825311942959, 'mean_word_length': 3.3624878522837705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3187972', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1599, 'n_words': 393}