From 4bbd7aa5290891e84a74d19400f626069933ef2d Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Wed, 6 Jul 2011 13:01:39 +0200 Subject: [PATCH] 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