From 525b741b6fbb178b11c6a2b3ccd4bf9ff9e98e0c Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Tue, 28 Oct 2014 14:43:15 -0400 Subject: Added Docker image build stuff --- .../files/monitoring_container/Dockerfile | 26 ++++++++ .../monitoring_container/register-with-zabbix.rb | 36 +++++++++++ .../files/monitoring_container/start.rb | 73 ++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 roles/docker_img_monitoring/files/monitoring_container/Dockerfile create mode 100755 roles/docker_img_monitoring/files/monitoring_container/register-with-zabbix.rb create mode 100755 roles/docker_img_monitoring/files/monitoring_container/start.rb (limited to 'roles/docker_img_monitoring/files') diff --git a/roles/docker_img_monitoring/files/monitoring_container/Dockerfile b/roles/docker_img_monitoring/files/monitoring_container/Dockerfile new file mode 100644 index 000000000..cd1651bc1 --- /dev/null +++ b/roles/docker_img_monitoring/files/monitoring_container/Dockerfile @@ -0,0 +1,26 @@ +# This FROM gives us the proper oo-rhui certs, basic runtime env vars, basic repos, etc. +# Otherwise we can't install anything +FROM rhel6ops + +MAINTAINER Thomas Wiest + +RUN yum -y update ; yum clean all + +# Container Specific RPMs +RUN yum -y install ruby193-rubygem-zbxapi cronie zabbix-sender ruby openshift-origin-util-scl ruby193-facter socat ; yum clean all + +# Setup ctr-ipc dir +RUN ln -s /shared/var/run/ctr-ipc /var/run/ctr-ipc + +# TEMP WORKAROUND: until zbxapi rpm is updated to work with zbx 2.4 +RUN ruby -i -ane 'if $_ =~ /APIInfo.version/ ; puts " @major,@minor=2,0" ; else puts $_ ; end' /opt/rh/ruby193/root/usr/share/gems/gems/zbxapi-0.3.3/zbxapi.rb + + +# Container specific files +ADD start.rb /start.rb +ADD register-with-zabbix.rb /register-with-zabbix.rb + +# TEMP WORKAROUND: until cron-send-haproxy-status.rb doesn't check service to see if haproxy is running +RUN ln -sf /bin/true /etc/init.d/haproxy + +CMD ["/start.rb"] diff --git a/roles/docker_img_monitoring/files/monitoring_container/register-with-zabbix.rb b/roles/docker_img_monitoring/files/monitoring_container/register-with-zabbix.rb new file mode 100755 index 000000000..02659228f --- /dev/null +++ b/roles/docker_img_monitoring/files/monitoring_container/register-with-zabbix.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env oo-ruby + +require 'optparse' +require '/usr/local/lib/zabbix_helper' + + +if __FILE__ == $0 + $stdout.sync = true + $stderr.sync = true + + opt_name = nil + opt_hostgroup = [] + opt_template = [] + + optparse = OptionParser.new do |opts| + opts.banner = "\nUsage: #{File.basename $0}\n\n" + + opts.on('--name NAME', '[REQUIRED] The host name to register') { |value| opt_name = value } + opts.on('--hostgroup GROUP', '[REQUIRED] The hostgroup(s) with which to register') { |value| opt_hostgroup << value } + opts.on('--template TEMPLATE', '[REQUIRED] The template with which to register') { |value| opt_template << value } + end + + optparse.parse! + + abort optparse.help if opt_name.nil? || opt_hostgroup.empty? || opt_template.empty? + + puts "Adding host [#{opt_name}] to zabbix..." + + zh = ZabbixHelper.new() + result = zh.create_agentless_host(opt_name, opt_hostgroup, opt_template) + if result['hostids'].nil? + raise "failed to add #{opt_name}" + else + puts "Successfully registered host with hostid [#{result['hostids'].first}]" + end +end diff --git a/roles/docker_img_monitoring/files/monitoring_container/start.rb b/roles/docker_img_monitoring/files/monitoring_container/start.rb new file mode 100755 index 000000000..7cd713707 --- /dev/null +++ b/roles/docker_img_monitoring/files/monitoring_container/start.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +require 'fileutils' + + +# TODO: These should be passed in as env vars. When we're in a POD, make sure to do this. +# WORKAROUND: ^^ +OO_ENV = 'stg' +OO_CTR_TYPE = 'proxy' +HOSTGROUPS = ['STG Environment'] +TEMPLATES = ['Template OpenShift Proxy Ctr'] +CTR_NAME = "ctr-#{OO_CTR_TYPE}-#{OO_ENV}-#{ENV['container_uuid'][0..6]}" + + +CTR_CONFIG_FLAG = '/shared/var/run/ctr-ipc/flag/ctr_configured' + + +class Start + def self.wait_for_ctr_configured + while ! File.exist?(CTR_CONFIG_FLAG) + puts "Sleeping 10 seconds, waiting for #{CTR_CONFIG_FLAG}" + sleep 10 + end + end + + def self.add_to_zabbix + # Need to do this as a separate script because /usr/local gets changed after this script starts. + # FIXME: we can change this once we aren't using the puppet container anymore + cmd = "/register-with-zabbix.rb --name #{CTR_NAME}" + cmd += ' ' + HOSTGROUPS.collect() { |a| "--hostgroup '#{a}'" }.join(' ') + cmd += ' ' + TEMPLATES.collect() { |a| "--template '#{a}'" }.join(' ') + puts "Running: #{cmd}" + system(cmd) + raise "failed" unless $?.exitstatus == 0 + end + + def self.setup_shared_dirs + puts '_' + ['/usr/local', '/etc/openshift', '/var/lib/haproxy', '/etc/haproxy'].each do |shared_dir| + puts "Setting up /shared#{shared_dir}..." + FileUtils.rm_rf(shared_dir) + FileUtils.ln_s("/shared#{shared_dir}", shared_dir) + end + puts '_' + end + + def self.setup_cron() + File.open('/etc/crontab', 'a') do |f| + # FIXME: on failure, this should e-mail, not log to a file. Not sure how best to do that in a 1 service per container way. + f.write("30 12 * * * root /usr/bin/flock -n /var/tmp/cron-send-cert-expiration.lock -c '/usr/bin/timeout -s9 30s /usr/local/bin/cron-send-cert-expiration.rb --server noc2.ops.rhcloud.com --zbx-host #{CTR_NAME}' &>> /var/log/cron-send-cert-expiration.log\n") + f.write("*/2 * * * * root /usr/local/bin/cron-send-haproxy-status.rb --server noc2.ops.rhcloud.com --zbx-host #{CTR_NAME} &>> /var/log/cron-send-haproxy-status.log\n") + end + end + + def self.exec_cron() + puts '_' + puts 'Exec-ing cron' + puts '-------------' + puts "Starting cron..." + exec("/usr/sbin/crond -n") + end +end + +if __FILE__ == $0 + $stdout.sync = true + $stderr.sync = true + + Start.setup_shared_dirs() + Start.wait_for_ctr_configured + Start.add_to_zabbix() + Start.setup_cron() + Start.exec_cron() +end -- cgit v1.2.3