From 4bbd7aa5290891e84a74d19400f626069933ef2d Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Wed, 6 Jul 2011 13:01:39 +0200 Subject: [PATCH 1/2] feature #696: Add a bash script to launch the acctd ruby daemon --- src/acct/acctd | 66 +++++++++++++++++++++ src/acct/acctd.rb | 124 ++++++++++++++++++++++++++++++++++++++++ src/acct/etc/acctd.conf | 27 +++++++++ 3 files changed, 217 insertions(+) create mode 100755 src/acct/acctd create mode 100755 src/acct/acctd.rb create mode 100644 src/acct/etc/acctd.conf diff --git a/src/acct/acctd b/src/acct/acctd new file mode 100755 index 0000000000..52b7382767 --- /dev/null +++ b/src/acct/acctd @@ -0,0 +1,66 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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. # +#--------------------------------------------------------------------------- # + +if [ -z "$ONE_LOCATION" ]; then + ACCTD_CMD=/usr/lib/one/ruby/acct/acctd.rb + ACCTD_LOG=/var/log/one/acctd.log + ACCTD_PID_FILE=/var/run/one/acctd.pid +else + ACCTD_CMD=$ONE_LOCATION/lib/ruby/acct/acctd.rb + ACCTD_LOG=$ONE_LOCATION/var/acctd.log + ACCTD_PID_FILE=$ONE_LOCATION/var/acctd.pid +fi + +function acctd_running { + ACCTD_PID=`cat $ACCTD_PID_FILE 2>/dev/null` + ps "$ACCTD_PID" &> /dev/null +} + +COMMAND=$1 + +case $COMMAND in +start) + # check if running + acctd_running + if [ "$?" = "0" ]; then + echo "acctd already running." + exit 1 + fi + + # acctd not running, safe to start + $ACCTD_CMD &> $ACCTD_LOG & + echo $! > $ACCTD_PID_FILE + ;; +stop) + # check if running + acctd_running + if [ "$?" != "0" ]; then + echo "acctd not running." + exit 1 + fi + + # acctd running, safe to stop + ACCTD_PID=`cat $ACCTD_PID_FILE 2>/dev/null` + kill $ACCTD_PID &> /dev/null + rm -f $ACCTD_PID_FILE &> /dev/null + ;; +*) + echo "Usage: acctd {start|stop}" >&2 + exit 3 + ;; +esac diff --git a/src/acct/acctd.rb b/src/acct/acctd.rb new file mode 100755 index 0000000000..0b83239a83 --- /dev/null +++ b/src/acct/acctd.rb @@ -0,0 +1,124 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" + ACCTD_CONF="/etc/one/acctd.conf" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" + ACCTD_CONF=ONE_LOCATION+"/etc/acctd.conf" +end + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/acct" + +require 'yaml' + +require 'OpenNebula' +require 'sequel' +require 'watch_helper' + +CONF = YAML.load_file(ACCTD_CONF) + +class Watcher + def initialize + @monitors = Array.new + end + + def add(resource, steps, pools) + @monitors << { :resource => resource, + :steps => steps, + :pools => [pools].flatten + } + end + + def log(resource,msg=nil) + msg ||= "watched" + STDERR.puts "#{Time.now} [#{resource.class}] #{msg}" + end + + def update(step) + # clear pool cache + @pool_cache = Hash.new + + @monitors.each do |monitor| + if monitor[:steps] > 0 and step % monitor[:steps] == 0 + monitor[:pools].each do |pool| + if pool_hash = @pool_cache[pool] + else + pool.info + pool_hash = pool.to_hash + @pool_cache[pool] = pool_hash + end + + resource = monitor[:resource] + resource.insert(pool_hash) + log(resource) + end + end + end + end +end + +watcher = Watcher.new + +# OpenNebula variables +one_client = OpenNebula::Client.new +vm_pool = nil # common for accounting and monitoring +host_pool = nil + +# Initialize accounting +if CONF[:ACCOUNTING_STEPS] > 0 + require 'accounting' + + accounting = OneWatch::Accounting.new(one_client) + + vm_pool ||= OpenNebula::VirtualMachinePool.new(one_client, -2) + + watcher.add(accounting, CONF[:ACCOUNTING_STEPS], vm_pool) +end + +# Initialize monitoring +if CONF[:MONITORING_STEPS] > 0 + require 'monitoring' + + vm_monitoring = OneWatch::VmMonitoring.new(one_client) + host_monitoring = OneWatch::HostMonitoring.new(one_client) + + vm_pool ||= OpenNebula::VirtualMachinePool.new(one_client, -2) + host_pool ||= OpenNebula::HostPool.new(one_client) + + watcher.add(vm_monitoring, CONF[:MONITORING_STEPS], vm_pool) + #watcher.add(host_monitoring, CONF[:MONITORING_STEPS], host_pool) +end + +step = 0 +loop do + start_time = Time.now + step = step + 1 + + watcher.update(step) + + diff_time = Time.now - start_time + + if diff_time < CONF[:STEP] + sleep (CONF[:STEP] - diff_time) + end +end diff --git a/src/acct/etc/acctd.conf b/src/acct/etc/acctd.conf new file mode 100644 index 0000000000..1eb11c4a58 --- /dev/null +++ b/src/acct/etc/acctd.conf @@ -0,0 +1,27 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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. # +#--------------------------------------------------------------------------- # + +# Duration of each daemon loop in seconds +:STEP: 1 + +# Number of daemon loops until an accounting watch +:ACCOUNTING_STEPS: 1 + +# Number of daemon loops until a monitiroing watch +:MONITORING_STEPS: 1 + +# Number of records to preserve +:WINDOW_SIZE: 5 From 3fe28c5dd2bce7497176323d3fdf14c4688e2ef8 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Wed, 6 Jul 2011 13:14:14 +0200 Subject: [PATCH 2/2] feature #696: enable host monitoring and fix minor bugs in watch_helper --- src/acct/acctd.rb | 2 +- src/acct/watch_helper.rb | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/acct/acctd.rb b/src/acct/acctd.rb index 0b83239a83..265d2925ae 100755 --- a/src/acct/acctd.rb +++ b/src/acct/acctd.rb @@ -106,7 +106,7 @@ if CONF[:MONITORING_STEPS] > 0 host_pool ||= OpenNebula::HostPool.new(one_client) watcher.add(vm_monitoring, CONF[:MONITORING_STEPS], vm_pool) - #watcher.add(host_monitoring, CONF[:MONITORING_STEPS], host_pool) + watcher.add(host_monitoring, CONF[:MONITORING_STEPS], host_pool) end step = 0 diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb index e355f9df35..a04e7301fc 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -121,6 +121,7 @@ module WatchHelper foreign_key :host_id, :hosts Integer :last_poll Integer :timestamp + Integer :state HOST_SAMPLE.each { |key,value| column key, value[:type] @@ -286,9 +287,9 @@ module WatchHelper set_schema do Integer :id, :primary_key=>true String :name - Integer :im_mad - Integer :vm_mad - Integer :tm_mad + String :im_mad + String :vm_mad + String :tm_mad end create_table unless table_exists? @@ -303,7 +304,7 @@ module WatchHelper h.name = host['NAME'] h.im_mad = host['IM_MAD'] h.vm_mad = host['VM_MAD'] - h.tm_nad = host['TM_MAD'] + h.tm_mad = host['TM_MAD'] } end