<commit_before>#
# Cookbook Name:: wt_streaminglogreplayer
# Recipe:: default
#
# Copyright 2012, Webtrends
#
# All rights reserved - Do Not Redistribute
#

# include runit so we can create a runit service
include_recipe "runit"

# install package for mounting nfs volumes
package "nfs-common"

log "Deploy build is #{ENV["deploy_build"]}"
if ENV["deploy_build"] == "true" then 
    log "The deploy_build value is true so un-deploy first"
    include_recipe "wt_streaminglogreplayer::undeploy"
else
    log "The deploy_build value is not set or is false so we will only update the configuration"
end

log_dir      = File.join("#{node['wt_common']['log_dir_linux']}", "streaminglogreplayer")
install_dir  = File.join("#{node['wt_common']['install_dir_linux']}", "streaminglogreplayer")

java_home    = node['java']['java_home']
download_url = node['wt_streaminglogreplayer']['download_url']
user = node['wt_streaminglogreplayer']['user']
group = node['wt_streaminglogreplayer']['group']
java_opts = node['wt_streaminglogreplayer']['java_opts']
tarball = "streaminglogreplayer-bin.tar.gz"

log "Install dir: #{install_dir}"
log "Log dir: #{log_dir}"
log "Java home: #{java_home}"

# create the log directory
directory "#{log_dir}" do
owner   user
group   group
mode    00755
recursive true
action :create
end

# create the bin directory
directory "#{install_dir}/bin" do
owner "root"
group "root"
mode 00755
recursive true
action :create
end

# create the conf directory
directory "#{install_dir}/conf" do
owner "root"
group "root"
mode 00755
recursive true
action :create
end

# create the share mount dir
directory "#{node['wt_streaminglogreplayer']['share_mount_dir']}" do
    action :create
end

# mount the NFS mount and add to /etc/fstab
mount "#{node['wt_streaminglogreplayer']['share_mount_dir']}" do
    device "#{node['wt_streaminglogreplayer']['log_share_mount']}"
    fstype "nfs"
    options "rw"
    action [:mount, :enable]
end

def processTemplates (install_dir, node, user, group)

    log "Updating the template files"
    # get the correct environment for the zookeeper nodes
    zookeeper_port = node['zookeeper']['client_port']
    zookeeper_env = "#{node.chef_environment}"
    unless node[:wt_streaminglogreplayer][:zookeeper_env].nil? || node[:wt_streaminglogreplayer][:zookeeper_env].empty?
        zookeeper_env = node['wt_streaminglogreplayer']['zookeeper_env']
    end

    # grab the zookeeper nodes that are currently available
    zookeeper_pairs = Array.new
    if not Chef::Config.solo
        search(:node, "role:zookeeper AND chef_environment:#{zookeeper_env}").each do |n|
            zookeeper_pairs << n[:fqdn]
        end
    end

	# fall back to attribs if search doesn't come up with any zookeeper roles
	if zookeeper_pairs.count == 0
		node[:zookeeper][:quorum].each do |i|
			zookeeper_pairs << i
		end
	end

    # append the zookeeper client port (defaults to 2181)
    i = 0
    while i < zookeeper_pairs.size do
        zookeeper_pairs[i] = zookeeper_pairs[i].concat(":#{zookeeper_port}")
        i += 1
    end

    %w[monitoring.properties producer.properties logconverter.properties].each do |template_file|
    template "#{install_dir}/conf/#{template_file}" do
            source	"#{template_file}.erb"
            owner user
            group group
            mode  00755
            variables({ 
                :wt_streaminglogreplayer => node[:wt_streaminglogreplayer],
                :zookeeper_pairs => zookeeper_pairs,
                :wt_monitoring => node[:wt_monitoring]
            })
        end
    end
end


if ENV["deploy_build"] == "true" then 
    log "The deploy_build value is true so we will grab the tar ball and install"

    # download the application tarball
    remote_file "#{Chef::Config[:file_cache_path]}/#{tarball}" do
    source download_url
    mode 00644
    end

    # uncompress the application tarball into the install dir
    execute "tar" do
    user  "root"
    group "root" 
    cwd install_dir
    command "tar zxf #{Chef::Config[:file_cache_path]}/#{tarball}"
    end

    template "#{install_dir}/bin/service-control" do
        source  "service-control.erb"
        owner "root"
        group "root"
        mode  00755
        variables({
            :log_dir => log_dir,
            :install_dir => install_dir,
            :java_home => java_home,
            :user => user,
            :java_class => "com.webtrends.streaming.LogReplayer",
            :java_jmx_port => node['wt_monitoring']['jmx_port'],
            :java_opts => java_opts
        })
    end

    processTemplates(install_dir, node, user, group)

    # delete the application tarball
    execute "delete_install_source" do
        user "root"
        group "root"
        command "rm -f #{Chef::Config[:file_cache_path]}/#{tarball}"
        action :run
    end

    # create a runit service
    runit_service "streaminglogreplayer" do
    options({
        :log_dir => log_dir,
        :install_dir => install_dir,
        :java_home => java_home,
        :user => user
    })
    end
else
    processTemplates(install_dir, node, user, group)
end

#Create collectd plugin for streaminglogreplayer JMX objects if collectd has been applied.
if node.attribute?("collectd")
  template "#{node[:collectd][:plugin_conf_dir]}/collectd_kafka-producer.conf" do
    source "collectd_kafka-producer.conf.erb"
    owner "root"
    group "root"
    mode 00644
    notifies :restart, resources(:service => "collectd")
  end
end<commit_msg>Define a NRPE check for old file on the streaming log replayer share<commit_after>#
# Cookbook Name:: wt_streaminglogreplayer
# Recipe:: default
#
# Copyright 2012, Webtrends
#
# All rights reserved - Do Not Redistribute
#

# include runit so we can create a runit service
include_recipe "runit"

# install package for mounting nfs volumes
package "nfs-common"

log "Deploy build is #{ENV["deploy_build"]}"
if ENV["deploy_build"] == "true" then 
    log "The deploy_build value is true so un-deploy first"
    include_recipe "wt_streaminglogreplayer::undeploy"
else
    log "The deploy_build value is not set or is false so we will only update the configuration"
end

log_dir      = File.join("#{node['wt_common']['log_dir_linux']}", "streaminglogreplayer")
install_dir  = File.join("#{node['wt_common']['install_dir_linux']}", "streaminglogreplayer")

java_home    = node['java']['java_home']
download_url = node['wt_streaminglogreplayer']['download_url']
user = node['wt_streaminglogreplayer']['user']
group = node['wt_streaminglogreplayer']['group']
java_opts = node['wt_streaminglogreplayer']['java_opts']
tarball = "streaminglogreplayer-bin.tar.gz"

log "Install dir: #{install_dir}"
log "Log dir: #{log_dir}"
log "Java home: #{java_home}"

# create the log directory
directory "#{log_dir}" do
owner   user
group   group
mode    00755
recursive true
action :create
end

# create the bin directory
directory "#{install_dir}/bin" do
owner "root"
group "root"
mode 00755
recursive true
action :create
end

# create the conf directory
directory "#{install_dir}/conf" do
owner "root"
group "root"
mode 00755
recursive true
action :create
end

# create the share mount dir
directory "#{node['wt_streaminglogreplayer']['share_mount_dir']}" do
    action :create
end

# mount the NFS mount and add to /etc/fstab
mount "#{node['wt_streaminglogreplayer']['share_mount_dir']}" do
    device "#{node['wt_streaminglogreplayer']['log_share_mount']}"
    fstype "nfs"
    options "rw"
    action [:mount, :enable]
end

def processTemplates (install_dir, node, user, group)

    log "Updating the template files"
    # get the correct environment for the zookeeper nodes
    zookeeper_port = node['zookeeper']['client_port']
    zookeeper_env = "#{node.chef_environment}"
    unless node[:wt_streaminglogreplayer][:zookeeper_env].nil? || node[:wt_streaminglogreplayer][:zookeeper_env].empty?
        zookeeper_env = node['wt_streaminglogreplayer']['zookeeper_env']
    end

    # grab the zookeeper nodes that are currently available
    zookeeper_pairs = Array.new
    if not Chef::Config.solo
        search(:node, "role:zookeeper AND chef_environment:#{zookeeper_env}").each do |n|
            zookeeper_pairs << n[:fqdn]
        end
    end

	# fall back to attribs if search doesn't come up with any zookeeper roles
	if zookeeper_pairs.count == 0
		node[:zookeeper][:quorum].each do |i|
			zookeeper_pairs << i
		end
	end

    # append the zookeeper client port (defaults to 2181)
    i = 0
    while i < zookeeper_pairs.size do
        zookeeper_pairs[i] = zookeeper_pairs[i].concat(":#{zookeeper_port}")
        i += 1
    end

    %w[monitoring.properties producer.properties logconverter.properties].each do |template_file|
    template "#{install_dir}/conf/#{template_file}" do
            source	"#{template_file}.erb"
            owner user
            group group
            mode  00755
            variables({ 
                :wt_streaminglogreplayer => node[:wt_streaminglogreplayer],
                :zookeeper_pairs => zookeeper_pairs,
                :wt_monitoring => node[:wt_monitoring]
            })
        end
    end
end


if ENV["deploy_build"] == "true" then 
    log "The deploy_build value is true so we will grab the tar ball and install"

    # download the application tarball
    remote_file "#{Chef::Config[:file_cache_path]}/#{tarball}" do
    source download_url
    mode 00644
    end

    # uncompress the application tarball into the install dir
    execute "tar" do
    user  "root"
    group "root" 
    cwd install_dir
    command "tar zxf #{Chef::Config[:file_cache_path]}/#{tarball}"
    end

    template "#{install_dir}/bin/service-control" do
        source  "service-control.erb"
        owner "root"
        group "root"
        mode  00755
        variables({
            :log_dir => log_dir,
            :install_dir => install_dir,
            :java_home => java_home,
            :user => user,
            :java_class => "com.webtrends.streaming.LogReplayer",
            :java_jmx_port => node['wt_monitoring']['jmx_port'],
            :java_opts => java_opts
        })
    end

    processTemplates(install_dir, node, user, group)

    # delete the application tarball
    execute "delete_install_source" do
        user "root"
        group "root"
        command "rm -f #{Chef::Config[:file_cache_path]}/#{tarball}"
        action :run
    end

    # create a runit service
    runit_service "streaminglogreplayer" do
    options({
        :log_dir => log_dir,
        :install_dir => install_dir,
        :java_home => java_home,
        :user => user
    })
    end
else
    processTemplates(install_dir, node, user, group)
end

#Create collectd plugin for streaminglogreplayer JMX objects if collectd has been applied.
if node.attribute?("collectd")
  template "#{node[:collectd][:plugin_conf_dir]}/collectd_kafka-producer.conf" do
    source "collectd_kafka-producer.conf.erb"
    owner "root"
    group "root"
    mode 00644
    notifies :restart, resources(:service => "collectd")
  end
end

#Create a nagios nrpe check for old files in the logshare
if node.attribute?("nagios")
	nagios_nrpecheck "wt_streaming_logreplayer_oldfiles" do
		command "#{node['nagios']['plugin_dir']}/check_file_ages_in_dirs"
		warning_condition "15"
		critical_condition "20"
		parameters "-a '-d #{node['wt_streaminglogreplayer']['share_mount_dir']} -t minutes'"
		action :add
	end
{'fraction_non_alphanumeric': 0.10990359333917617, 'fraction_numerical': 0.008413672217353198, 'mean_word_length': 3.5031570639305447, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '821617', 'n_tokens_mistral': 3746, 'n_tokens_neox': 3605, 'n_words': 972}