From 05cf77eab18b11271056d0d36aaa350b0ef7ddc8 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Sun, 13 Oct 2013 21:34:17 +0200 Subject: [PATCH] im-collectd: collectd-client agent --- src/im_mad/im_exec/one_im_exec.rb | 3 +- .../{kvm.d => kvm-probes.d}/architecture.sh | 0 .../remotes/{kvm.d => kvm-probes.d}/cpu.sh | 0 .../remotes/{kvm.d => kvm-probes.d}/kvm.rb | 0 .../remotes/{kvm.d => kvm-probes.d}/name.sh | 0 .../remotes/{kvm.d => kvm-probes.d}/poll.sh | 0 src/im_mad/remotes/kvm.d/collectd-client.rb | 120 ++++++++++++++++++ .../remotes/kvm.d/collectd-client_control.sh | 62 +++++++++ 8 files changed, 184 insertions(+), 1 deletion(-) rename src/im_mad/remotes/{kvm.d => kvm-probes.d}/architecture.sh (100%) rename src/im_mad/remotes/{kvm.d => kvm-probes.d}/cpu.sh (100%) rename src/im_mad/remotes/{kvm.d => kvm-probes.d}/kvm.rb (100%) rename src/im_mad/remotes/{kvm.d => kvm-probes.d}/name.sh (100%) rename src/im_mad/remotes/{kvm.d => kvm-probes.d}/poll.sh (100%) create mode 100644 src/im_mad/remotes/kvm.d/collectd-client.rb create mode 100755 src/im_mad/remotes/kvm.d/collectd-client_control.sh diff --git a/src/im_mad/im_exec/one_im_exec.rb b/src/im_mad/im_exec/one_im_exec.rb index 1b2f8e5f12..42828b6f8d 100755 --- a/src/im_mad/im_exec/one_im_exec.rb +++ b/src/im_mad/im_exec/one_im_exec.rb @@ -73,7 +73,8 @@ class InformationManagerDriver < OpenNebulaDriver end end - do_action("#{@hypervisor}", number, host, :MONITOR, + # TODO: Do not hard-code the port + do_action("#{@hypervisor} #{number} 9876", number, host, :MONITOR, :script_name => 'run_probes', :base64 => true) end end diff --git a/src/im_mad/remotes/kvm.d/architecture.sh b/src/im_mad/remotes/kvm-probes.d/architecture.sh similarity index 100% rename from src/im_mad/remotes/kvm.d/architecture.sh rename to src/im_mad/remotes/kvm-probes.d/architecture.sh diff --git a/src/im_mad/remotes/kvm.d/cpu.sh b/src/im_mad/remotes/kvm-probes.d/cpu.sh similarity index 100% rename from src/im_mad/remotes/kvm.d/cpu.sh rename to src/im_mad/remotes/kvm-probes.d/cpu.sh diff --git a/src/im_mad/remotes/kvm.d/kvm.rb b/src/im_mad/remotes/kvm-probes.d/kvm.rb similarity index 100% rename from src/im_mad/remotes/kvm.d/kvm.rb rename to src/im_mad/remotes/kvm-probes.d/kvm.rb diff --git a/src/im_mad/remotes/kvm.d/name.sh b/src/im_mad/remotes/kvm-probes.d/name.sh similarity index 100% rename from src/im_mad/remotes/kvm.d/name.sh rename to src/im_mad/remotes/kvm-probes.d/name.sh diff --git a/src/im_mad/remotes/kvm.d/poll.sh b/src/im_mad/remotes/kvm-probes.d/poll.sh similarity index 100% rename from src/im_mad/remotes/kvm.d/poll.sh rename to src/im_mad/remotes/kvm-probes.d/poll.sh diff --git a/src/im_mad/remotes/kvm.d/collectd-client.rb b/src/im_mad/remotes/kvm.d/collectd-client.rb new file mode 100644 index 0000000000..bf332972ca --- /dev/null +++ b/src/im_mad/remotes/kvm.d/collectd-client.rb @@ -0,0 +1,120 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'socket' +require 'base64' + +DIRNAME = File.dirname(__FILE__) + +CYCLE = 20 +TOTAL_HOSTS = 1000 + +SLEEP = 1 + +class CollectdClient + def initialize(hypervisor, number, host, port) + # Arguments + @hypervisor = hypervisor + @number = number.to_i + @host = host + @port = port + + # Monitorization slot + hosts_per_cycle = TOTAL_HOSTS / CYCLE + @my_slot = (@number % TOTAL_HOSTS) / hosts_per_cycle + + # Probes + run_probes_cmd = File.join(DIRNAME, '..', "run_probes") + @run_probes_cmd = "#{run_probes_cmd} #{@hypervisor}-probes" + + # Socket + @s = UDPSocket.new + end + + def run_probes + data = `#{@run_probes_cmd}` + data64 = Base64::encode64(data).strip.delete("\n") + + return data64 + end + + def send(data) + @s.send("MONITOR SUCCESS #{@number} #{data}\n", 0, @host, @port) + end + + def do_send?(current, last_send) + current_cycle = current[0] + current_slot = current[1] + + last_cycle = last_send[0] + last_slot = last_send[1] + + if last_slot < @my_slot + min_cycle = last_cycle + else + min_cycle = last_cycle + 1 + end + + if current_cycle > min_cycle + return true + elsif current_cycle < min_cycle + return false + else + return current_slot >= @my_slot + end + end + + def monitor + initial = true + data = run_probes + + last_send = nil + + loop do + t = Time.now.to_i + + current_cycle = t / CYCLE + current_slot = t % CYCLE + + current = [current_cycle, current_slot] + + if initial + last_send = current + initial = false + end + + if do_send?(current, last_send) + send data + last_send = current + + data = run_probes + end + + sleep SLEEP + end + end +end + +hypervisor = ARGV[0] +number = ARGV[1] +port = ARGV[2] + +host = ENV['SSH_CLIENT'].split.first + +client = CollectdClient.new(hypervisor, number, host, port) +client.monitor diff --git a/src/im_mad/remotes/kvm.d/collectd-client_control.sh b/src/im_mad/remotes/kvm.d/collectd-client_control.sh new file mode 100755 index 0000000000..485d802152 --- /dev/null +++ b/src/im_mad/remotes/kvm.d/collectd-client_control.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ARGV=$* + +# Directory that contains this file +DIR=$(pwd) + +# Basename +BASENAME=$(basename $0 _control.sh) + +# Collectd client (Ruby) +CLIENT=$DIR/${BASENAME}.rb + +# Collectd client PID +CLIENT_PID_FILE=/tmp/one-collectd-client.pid + +# Launch the client +function start_client() { + /usr/bin/env ruby $CLIENT $ARGV & +} + +# Write the PID +function write_pid() { + echo $1 > $CLIENT_PID_FILE +} + +# Check if running process +function check_running() { + # Assume the process is not running if there is no pid file + test ! -f $CLIENT_PID_FILE && return 1 + + PID=$(cat $CLIENT_PID_FILE) + + if ps --no-headers -o command $PID 2>/dev/null | grep -q $BASENAME; then + return 0 + else + # Stale PID file + rm -f $CLIENT_PID_FILE + return 1 + fi +} + +if ! check_running; then + start_client + write_pid $! +fi