[virt-who/systemd] Force using background mode when starting from systemd
by Radek Novacek
commit 7a2fc3e2a84608c55a312717f9be01dbfe6ad78a
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Dec 11 09:05:38 2013 +0100
Force using background mode when starting from systemd
virt-who has "forking" type of systemd service, so it must fork itself when
starting. The VIRTWHO_BACKGROUND config option was removed from the
config file (but is still recognized) and is overriden when starting
through systemd.
virt-who.conf | 5 -----
virt-who.service | 2 +-
2 files changed, 1 insertions(+), 6 deletions(-)
---
diff --git a/virt-who.conf b/virt-who.conf
index 2c117d2..95a4cc1 100644
--- a/virt-who.conf
+++ b/virt-who.conf
@@ -3,11 +3,6 @@
# These enviromental variables are only used when starting virt-who as service,
# otherwise you must specify them manually.
-# Start virt-who on background, perform doublefork and monitor for virtual guest
-# events (if possible). It is NOT recommended to turn off this option for
-# starting virt-who as service.
-VIRTWHO_BACKGROUND=1
-
# Enable debugging output.
VIRTWHO_DEBUG=0
diff --git a/virt-who.service b/virt-who.service
index 74982c5..1066de0 100644
--- a/virt-who.service
+++ b/virt-who.service
@@ -5,7 +5,7 @@ After=syslog.target libvirtd.service network.target
[Service]
Type=forking
PIDFile=/var/run/virt-who.pid
-ExecStart=/usr/bin/virt-who
+ExecStart=/usr/bin/virt-who -b
EnvironmentFile=-/etc/sysconfig/virt-who
[Install]
10 years, 4 months
[virt-who/systemd] Check if process defined by PID file exists
by Radek Novacek
commit 15abd71138e7421f7ab476027e74747e2115b9ea
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Dec 11 08:43:24 2013 +0100
Check if process defined by PID file exists
When virt-who is starting it now checks if the previous virt-who is
running (defined by PID file) and if not, deletes the pid file and
starts normally.
virt-who.py | 19 ++++++++++++++++---
1 files changed, 16 insertions(+), 3 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index 52ec318..32e3c05 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -289,6 +289,21 @@ def daemonize(debugMode):
os.chdir("/")
return True
+def checkPidFile():
+ try:
+ f = open(PIDFILE, "r")
+ pid = int(f.read().strip())
+ try:
+ os.kill(pid, 0)
+ print >>sys.stderr, "virt-who seems to be already running. If not, remove %s" % PIDFILE
+ sys.exit(1)
+ except OSError:
+ # Process no longer exists
+ print >>sys.stderr, "PID file exists but associated process does not, deleting PID file"
+ os.remove(PIDFILE)
+ except Exception:
+ pass
+
def createPidFile(logger=None):
atexit.register(cleanup)
signal.signal(signal.SIGINT, cleanup)
@@ -313,9 +328,7 @@ def cleanup(sig=None, stack=None):
sys.exit(0)
def main():
- if os.access(PIDFILE, os.F_OK):
- print >>sys.stderr, "virt-who seems to be already running. If not, remove %s" % PIDFILE
- sys.exit(1)
+ checkPidFile()
createPidFile()
parser = OptionParserEpilog(description="Agent for reporting virtual guest IDs to subscription-manager",
10 years, 4 months
[virt-who/systemd] vsphere: reconnect before each scan
by Radek Novacek
commit 15bb845a3a8dd8765fb4949c033e89693f58e571
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Dec 11 08:36:50 2013 +0100
vsphere: reconnect before each scan
With long scan interval (3600 seconds) the login session expires
and virt-who can't read host/guest association from the ESX.
This commit forces virt-who to relogin each time before the scanning.
vsphere.py | 8 +++++---
1 files changed, 5 insertions(+), 3 deletions(-)
---
diff --git a/vsphere.py b/vsphere.py
index 46b5fb3..415df65 100644
--- a/vsphere.py
+++ b/vsphere.py
@@ -108,6 +108,8 @@ class VSphere:
def __init__(self, logger, url, username, password):
self.logger = logger
self.url = url
+ self.username = username
+ self.password = password
# Url must contain protocol (usualy https://)
if not "://" in self.url:
@@ -125,9 +127,6 @@ class VSphere:
# Service Content object defines properties of the ServiceInstance object
self.sc = self.client.service.RetrieveServiceContent(_this=self.moRef)
- # Login to server using given credentials
- self.client.service.Login(_this=self.sc.sessionManager, userName=username, password=password)
-
self.clusters = {}
self.hosts = {}
self.vms = {}
@@ -138,6 +137,9 @@ class VSphere:
all ComputeResources, Hosts and VirtualMachines.
"""
+ # Login to server using given credentials
+ self.client.service.Login(_this=self.sc.sessionManager, userName=self.username, password=self.password)
+
# Clear results from last run
self.clusters = {}
self.hosts = {}
10 years, 4 months
[virt-who] vdsm: Handle also NoSectionError in vdsm config file
by Radek Novacek
commit 059644af4eb37e789419e0a40edf15760b16b6b3
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 6 10:56:27 2013 +0100
vdsm: Handle also NoSectionError in vdsm config file
vdsm.py | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
---
diff --git a/vdsm.py b/vdsm.py
index e4a6b9e..957ea55 100644
--- a/vdsm.py
+++ b/vdsm.py
@@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import xmlrpclib
-from ConfigParser import SafeConfigParser, NoSectionError
+from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
import subprocess
class VdsmError(Exception):
@@ -55,7 +55,7 @@ class VDSM:
else:
self.trust_store_path = None
self.management_port = parser.get("addresses", "management_port")
- except NoSectionError, e:
+ except (NoSectionError, NoOptionError), e:
raise VdsmError("Error in vdsm configuration file: %s" % str(e))
def _getLocalVdsName(self, tsPath):
10 years, 4 months
[virt-who] Query virtual guest per host, not all at once.
by Radek Novacek
commit e2b0dac515943e387b532cee5304f3a2db8b78fb
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 6 10:53:50 2013 +0100
Query virtual guest per host, not all at once.
Querying all at once fails for lots of guests, this way should be more
error prone.
vsphere.py | 38 +++++++++++++++++++-------------------
1 files changed, 19 insertions(+), 19 deletions(-)
---
diff --git a/vsphere.py b/vsphere.py
index ae81e73..84eca50 100644
--- a/vsphere.py
+++ b/vsphere.py
@@ -195,8 +195,8 @@ class VSphere:
# Get list of host uuids, names and virtual machines
object_contents = self.RetrieveProperties('HostSystem', ['vm', 'hardware'], hostObjs)
- vmObjs = [] # List of objs for 'VirtualMachine' query
for host in object_contents:
+ vmObjs = [] # List of objs for 'VirtualMachine' query
if not hasattr(host, 'propSet'):
continue
for propSet in host.propSet:
@@ -209,28 +209,28 @@ class VSphere:
v = VM()
self.vms[vm.value] = v
self.hosts[host.obj.value].vms.append(v)
+
+ # Get list of virtual machine uuids
+ vm_object_contents = self.RetrieveProperties('VirtualMachine', ['config'], vmObjs)
+ for obj in vm_object_contents:
+ if len(obj) > 1:
+ for propSet in obj.propSet:
+ if propSet.name == 'config':
+ # We need some uuid, let's try a couple of options
+ if propSet.val.uuid is not None:
+ self.vms[obj.obj.value].uuid = propSet.val.uuid
+ elif propSet.val.instanceUuid is not None:
+ self.vms[obj.obj.value].uuid = propSet.val.instanceUuid
+ else:
+ self.logger.error("No UUID for virtual machine %s", self.vms[obj.obj.value].name)
+ self.vms[obj.obj.value].uuid = None
+ else:
+ self.logger.error("Cannot read VM list from ESX for host %s (len(obj) <= 1)" % self.hosts[host.obj.value].uuid)
+
except AttributeError:
# This means that there is no guest on given host
pass
- if len(vmObjs) == 0:
- return
-
- # Get list of virtual machine uuids
- object_contents = self.RetrieveProperties('VirtualMachine', ['config'], vmObjs)
- for obj in object_contents:
- if not hasattr(obj, 'propSet'):
- continue
- for propSet in obj.propSet:
- if propSet.name == 'config':
- # We need some uuid, let's try a couple of options
- if propSet.val.uuid is not None:
- self.vms[obj.obj.value].uuid = propSet.val.uuid
- elif propSet.val.instanceUuid is not None:
- self.vms[obj.obj.value].uuid = propSet.val.instanceUuid
- else:
- self.logger.error("No UUID for virtual machine %s", self.vms[obj.obj.value].name)
- self.vms[obj.obj.value].uuid = None
def ping(self):
return True
10 years, 4 months