[Ovirt-devel] [PATCH] Add load average to hosts table

Hugh O. Brock hbrock at redhat.com
Thu Jun 5 21:46:22 UTC 2008


On Thu, Jun 05, 2008 at 02:13:12PM -0700, Ian Main wrote:
> This patch adds a new daemon, 'host-collect', which uses a unixsocket
> to talk to collectd to grab the latest load average values from the host
> and updates the host table with this value.
> ---
>  wui/conf/ovirt-host-collect            |   49 ++++++++++++++
>  wui/ovirt-wui.spec                     |    4 +
>  wui/scripts/ovirt-wui-install          |    2 +-
>  wui/src/db/migrate/002_create_hosts.rb |    2 +
>  wui/src/host-collect/host-collect.rb   |  110 ++++++++++++++++++++++++++++++++
>  5 files changed, 166 insertions(+), 1 deletions(-)
>  create mode 100755 wui/conf/ovirt-host-collect
>  create mode 100755 wui/src/host-collect/host-collect.rb
> 
> diff --git a/wui/conf/ovirt-host-collect b/wui/conf/ovirt-host-collect
> new file mode 100755
> index 0000000..2d8ffcd
> --- /dev/null
> +++ b/wui/conf/ovirt-host-collect
> @@ -0,0 +1,49 @@
> +#!/bin/bash
> +#
> +#
> +# ovirt-host-collect       startup script for ovirt-host-collect
> +#
> +# chkconfig: - 97 03
> +# description: ovirt-host-collect is an essential component of the \
> +#    ovirt VM manager.
> +#
> +
> +DAEMON=/usr/share/ovirt-wui/host-collect/host-collect.rb
> +
> +. /etc/init.d/functions
> +
> +start() {
> +    echo -n "Starting ovirt-host-collect: "
> +    daemon $DAEMON
> +    RETVAL=$?
> +    echo
> +}
> +
> +stop() {
> +    echo -n "Shutting down ovirt-host-collect: "
> +    killproc host-collect.rb
> +    RETVAL=$?
> +    echo
> +}
> +
> +case "$1" in
> +    start)
> +	start
> +	;;
> +    stop)
> +	stop
> +	;;
> +    restart)
> +	stop
> +	start
> +	;;
> +    status)
> +	status $DAEMON
> +	RETVAL=$?
> +	;;
> +    *)
> +      echo "Usage: ovirt-host-collect {start|stop|restart|status}"
> +      exit 1
> +  ;;
> +esac
> +exit $RETVAL
> diff --git a/wui/ovirt-wui.spec b/wui/ovirt-wui.spec
> index 426c551..7b16667 100644
> --- a/wui/ovirt-wui.spec
> +++ b/wui/ovirt-wui.spec
> @@ -74,6 +74,7 @@ touch %{buildroot}%{_localstatedir}/log/%{name}/host-status.log
>  
>  %{__install} -Dp -m0755 %{pbuild}/conf/ovirt-host-browser %{buildroot}%{_initrddir}
>  %{__install} -Dp -m0755 %{pbuild}/conf/ovirt-host-status %{buildroot}%{_initrddir}
> +%{__install} -Dp -m0755 %{pbuild}/conf/ovirt-host-collect %{buildroot}%{_initrddir}
>  %{__install} -Dp -m0755 %{pbuild}/conf/ovirt-mongrel-rails %{buildroot}%{_initrddir}
>  %{__install} -Dp -m0755 %{pbuild}/conf/ovirt-taskomatic %{buildroot}%{_initrddir}
>  
> @@ -99,6 +100,7 @@ rm -rf $RPM_BUILD_ROOT
>  %{_bindir}/ovirt-add-host
>  %{_initrddir}/ovirt-host-browser
>  %{_initrddir}/ovirt-host-status
> +%{_initrddir}/ovirt-host-collect
>  %{_initrddir}/ovirt-mongrel-rails
>  %{_initrddir}/ovirt-taskomatic
>  %config(noreplace) %{_sysconfdir}/httpd/conf.d/%{name}.conf
> @@ -133,10 +135,12 @@ exit 0
>  if [ "$1" = 0 ] ; then
>    /sbin/service ovirt-host-browser stop > /dev/null 2>&1
>    /sbin/service ovirt-host-status stop > /dev/null 2>&1
> +  /sbin/service ovirt-host-collect stop > /dev/null 2>&1
>    /sbin/service ovirt-mongrel-rails stop > /dev/null 2>&1
>    /sbin/service ovirt-taskomatic stop > /dev/null 2>&1
>    /sbin/chkconfig --del ovirt-host-browser
>    /sbin/chkconfig --del ovirt-host-status
> +  /sbin/chkconfig --del ovirt-host-collect
>    /sbin/chkconfig --del ovirt-mongrel-rails
>    /sbin/chkconfig --del ovirt-taskomatic
>  fi
> diff --git a/wui/scripts/ovirt-wui-install b/wui/scripts/ovirt-wui-install
> index 61da1b6..6eb7d8e 100755
> --- a/wui/scripts/ovirt-wui-install
> +++ b/wui/scripts/ovirt-wui-install
> @@ -14,7 +14,7 @@ SASL_FILE=/etc/sasl2/libvirt.conf
>  LDAP_CFG=${OVIRT_DIR}/config/ldap.yml
>  
>  OVIRT_SVCS="ovirt-host-browser ovirt-host-keyadd ovirt-host-status \
> -            ovirt-mongrel-rails ovirt-taskomatic"
> +            ovirt-host-collect ovirt-mongrel-rails ovirt-taskomatic"
>  ENABLE_SVCS="ntpdate ntpd httpd postgresql libvirtd collectd"
>  
>  # This checks to see if we're running on a bundled/developer install.
> diff --git a/wui/src/db/migrate/002_create_hosts.rb b/wui/src/db/migrate/002_create_hosts.rb
> index 3e36738..cef7996 100644
> --- a/wui/src/db/migrate/002_create_hosts.rb
> +++ b/wui/src/db/migrate/002_create_hosts.rb
> @@ -31,6 +31,8 @@ class CreateHosts < ActiveRecord::Migration
>        t.integer :hardware_pool_id, :null => false
>        t.integer :lock_version,     :default => 0
>        t.string  :state
> +      t.float   :load_average
> +      t.timestamps
>      end
>  
>      execute "alter table hosts add constraint fk_host_pools
> diff --git a/wui/src/host-collect/host-collect.rb b/wui/src/host-collect/host-collect.rb
> new file mode 100755
> index 0000000..278449a
> --- /dev/null
> +++ b/wui/src/host-collect/host-collect.rb
> @@ -0,0 +1,110 @@
> +#!/usr/bin/ruby
> +#
> +# Copyright (C) 2008 Red Hat, Inc.
> +# Written by Ian Main <imain at redhat.com>
> +#
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; version 2 of the License.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
> +# MA  02110-1301, USA.  A copy of the GNU General Public License is
> +# also available at http://www.gnu.org/copyleft/gpl.html.
> +
> +$: << File.join(File.dirname(__FILE__), "../app")
> +$: << File.join(File.dirname(__FILE__), "../dutils")
> +$: << File.join(File.dirname(__FILE__), ".")
> +
> +require 'optparse'
> +require 'dutils'
> +require 'models/task'
> +require 'socket'
> +
> +do_daemon = false
> +sleeptime = 30
> +
> +
> +opts = OptionParser.new do |opts|
> +  opts.on("-h", "--help", "Print help message") do
> +    puts opts
> +    exit
> +  end
> +  opts.on("-n", "--nodaemon", "Run interactively (useful for debugging)") do |n|
> +    do_daemon = !n
> +  end
> +  opts.on("-s N", Integer, "--sleep", "Seconds to sleep between iterations (default is 5 seconds)") do |s|
> +    sleeptime = s
> +  end
> +end
> +
> +begin
> +  opts.parse!(ARGV)
> +rescue OptionParser::InvalidOption
> +  puts opts
> +  exit
> +end
> +
> +if do_daemon
> +  daemonize
> +end
> +
> +f = UNIXSocket.new("/var/lib/collectd/unixsock")
> +
> +database_connect
> +
> +loop do
> +  f.write("LISTVAL\n")
> +
> +  count = f.gets
> +  count = count.to_i
> +
> +  vals = []
> +  while count > 0 do
> +    value = f.gets
> +    vals.push(value)
> +    count = count - 1
> +  end
> +
> +  for val in vals do
> +    timestamp, keystring = val.split(" ")
> +
> +    hostname,plugin,type = keystring.split("/")
> +
> +    if plugin == "load" and type == "load"
> +      f.write("GETVAL #{keystring}\n")
> +      valuestring = f.gets
> +
> +      values = valuestring.split("=")
> +      if values.length != 4
> +        puts("GACK! Should have 4 values for load")
> +        next
> +      end
> +      short = values[1].to_f
> +      med = values[2].to_f
> +      long = values[3].to_f
> +
> +      # You only see this in non-daemon mode..
> +      puts("hostname: #{hostname} --> short: #{short}, med: #{med}, long: #{long}")
> +
> +      # We have our values now, just need to update the db.
> +      host = Host.find(:first, :conditions => [ "hostname = ?", hostname])
> +      if host == nil
> +        puts("GACK! No such host in database: #{hostname}")
> +      else
> +        host.load_average = med
> +        host.save
> +      end
> +    end
> +  end
> +
> +  sleep sleeptime
> +
> +end
> +
> -- 

ACK

and I have pushed it.

NOTE TO ALL: This patch requires a schema upgrade. You can reload your
db from scratch, or you can add columns to your database following the
above patch.

--Hugh




More information about the ovirt-devel mailing list