From 0b8f11421b3526c219d768290ef7ea71b7ef4dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20S=2E=20Montero?= Date: Thu, 19 Mar 2009 23:25:54 +0000 Subject: [PATCH] The remote dir can be now specified thorugh the command line, this simplifies the driver itself and the replication of IM drivers git-svn-id: http://svn.opennebula.org/one/trunk@426 3034c82b-c49b-4eb3-8279-a7acafdc01c0 --- src/im_mad/im_ssh/one_im_ssh | 10 ++ src/im_mad/im_ssh/one_im_ssh.rb | 207 +++++++++++++++++--------------- src/im_mad/kvm/im_kvm.conf | 6 +- src/im_mad/xen/im_xen.conf | 6 +- 4 files changed, 123 insertions(+), 106 deletions(-) diff --git a/src/im_mad/im_ssh/one_im_ssh b/src/im_mad/im_ssh/one_im_ssh index f8907c24b6..4ea19ec9b2 100755 --- a/src/im_mad/im_ssh/one_im_ssh +++ b/src/im_mad/im_ssh/one_im_ssh @@ -17,6 +17,16 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +#Parse command line arguments +while getopts r: option $@ +do + case $option in + r) export IM_REMOTE_DIR="$OPTARG";; + esac +done +shift $(($OPTIND - 1)) + +#Setup driver variables DRIVER_NAME=`basename $1 | cut -d. -f1` if [ -z "${ONE_LOCATION}" ]; then diff --git a/src/im_mad/im_ssh/one_im_ssh.rb b/src/im_mad/im_ssh/one_im_ssh.rb index 77c55241f2..dd93a5e9c3 100755 --- a/src/im_mad/im_ssh/one_im_ssh.rb +++ b/src/im_mad/im_ssh/one_im_ssh.rb @@ -38,144 +38,138 @@ require 'fileutils' require 'OpenNebulaDriver' require 'CommandManager' -DEBUG_LEVEL=ENV["ONE_MAD_DEBUG"] - -## -# Debug constants -## -ERROR, DEBUG=[0,1] - -# This class holds the information of a IM probe and the methods -# to copy the script to the remote hosts and run it +#------------------------------------------------------------------------------- +# This class holds the information of a IM probe and the methods to copy the +# script to the remote hosts and run it +#------------------------------------------------------------------------------- class Sensor - attr_accessor :remote_dir - attr_reader :name, :script - - def initialize(name, script) - @name=name - @script=script - - gen_identifier + + #--------------------------------------------------------------------------- + # Class constructor init the remote script name and working directory + #--------------------------------------------------------------------------- + def initialize(name, script, remote_dir) + id = Digest::MD5.hexdigest("#{name}#{script}") + + @script = script + @remote_script = "#{remote_dir}/one_im-#{id}" + @remote_dir = remote_dir end - + + #--------------------------------------------------------------------------- + # Sends the monitor probe script to the remote host and execute it + #--------------------------------------------------------------------------- def execute(host, log_proc) - script_text=File.read @script - scr="'mkdir -p #{remote_dir};cat > #{remote_script};if [ \"x$?\" != \"x0\" ]; then exit 42;fi;chmod +x #{remote_script};#{remote_script}'" - - cmd=SSHCommand.run(scr, host, log_proc, script_text) + script_text = File.read @script + + src = "'mkdir -p #{@remote_dir}; cat > #{@remote_script};" \ + " if [ \"x$?\" != \"x0\" ]; then exit -1; fi;" \ + " chmod +x #{@remote_script}; #{@remote_script}'" + + cmd = SSHCommand.run(src, host, log_proc, script_text) + case cmd.code when 0 # Splits the output by lines, strips each line and gets only # lines that have something - value=cmd.stdout.split("\n").collect {|v| - v2=v.strip - if v2=="" + value = cmd.stdout.split("\n").collect {|v| + v2 = v.strip + if v2 == "" nil else v2 end }.compact.join(",") - - when 42 - log_proc.call("Can not send script to remote machine: "+host) - nil else - value="Could not execute remote script in " + - host + ": "+remote_script - log_proc.call(value) nil end end - -private - - # Generates an unique identifier using name of the sensor and its script - def gen_identifier - id=@name+@script - @identifier=Digest::MD5.hexdigest(id) - end - - # Returns the path of the script in the remote machine - def remote_script - @remote_dir+"/ne_im-"+@identifier - end - end +#------------------------------------------------------------------------------- +# This class is an array of sensor probes to be executed by the information +# driver. The class is built on top of the Sensor class +#------------------------------------------------------------------------------- class SensorList < Array - def initialize(config_file=nil) + #--------------------------------------------------------------------------- + # Initialize the class + #--------------------------------------------------------------------------- + def initialize(config_file, remote_dir) super(0) - - @remote_dir='/tmp/ne_im_scripts' - - load_sensors(config_file) if config_file + + @remote_dir = remote_dir + + load_sensors(config_file) end - + + #--------------------------------------------------------------------------- + # Execute all sensors in the list in the given host + #--------------------------------------------------------------------------- def execute_sensors(host, log_proc) - results=Array.new + results = Array.new + self.each {|sensor| - results<