Skip to content

Commit

Permalink
Switch to 'logging' module instead of 'syslog'
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaeon committed Sep 13, 2013
1 parent b64eb20 commit 6d0bcff
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
14 changes: 11 additions & 3 deletions src/vmpoller-client
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ vmpoller-client is an application used for polling objects' information from a V
It is intended to be integrated into a Zabbix template for polling of ESX hosts and Datastores properties.
"""

import logging

from vmpoller.core import VMPollerClient
from docopt import docopt

def main():

usage="""
Usage:
vmpoller-client [-r <retries>] [-t <timeout>] (-D|-H) -c discover -V <vcenter> -e <endpoint>
vmpoller-client [-r <retries>] [-t <timeout>] -H -n <name> -p <property> -c poll -V <vcenter> -e <endpoint>
vmpoller-client [-r <retries>] [-t <timeout>] -D -n <name> -p <property> -u <datastore-url> -c poll -V <vcenter> -e <endpoint>
vmpoller-client [-r <retries>] [-t <timeout>] [-l <logfile>] (-D|-H) -c discover -V <vcenter> -e <endpoint>
vmpoller-client [-r <retries>] [-t <timeout>] [-l <logfile>] -H -n <name> -p <property> -c poll -V <vcenter> -e <endpoint>
vmpoller-client [-r <retries>] [-t <timeout>] [-l <logfile>] -D -n <name> -p <property> -u <datastore-url> -c poll -V <vcenter> -e <endpoint>
vmpoller-client --help
vmpoller-client --version
Expand All @@ -56,11 +58,17 @@ Options:
-r <retries>, --retries <retries> Number of time to retry if a request times out [default: 3]
-t <timeout>, --timeout <timeout> Timeout after that period of milliseconds [default: 3000]
-e <endpoint>, --endpoint <endpoint> Endpoint of ZeroMQ Proxy/Broker the client connects to
-l <logfile>, --logfile <logfile> Specify the log file to use when performing the poll
[default: /var/log/vmpoller/vmpoller-client.log]
"""

args = docopt(usage, version="1.0.0")

logging.basicConfig(filename=args["--logfile"],
format='%(asctime)s - %(levelname)s - vmpoller-client[%(process)s]: %(message)s',
level=logging.DEBUG)

client = VMPollerClient(endpoint=args["--endpoint"], retries=args["--retries"], timeout=args["--timeout"])

msg = { "type": "hosts" if args["--hosts"] else "datastores",
Expand Down
10 changes: 9 additions & 1 deletion src/vmpoller-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ between a pool of ZeroMQ workers.
"""

import logging

from vmpoller.core import VMPollerProxy, VMPollerClient
from docopt import docopt

Expand Down Expand Up @@ -78,7 +80,7 @@ def status(endpoint):
def main():

usage="""
Usage: vmpoller-proxy [-d] [-p <pidfile>] [-f <config-file>] start
Usage: vmpoller-proxy [-d] [-p <pidfile>] [-f <config-file>] [-l <logfile>] start
vmpoller-proxy -e <endpoint> stop
vmpoller-proxy -e <endpoint> status
vmpoller-proxy --help
Expand All @@ -99,10 +101,16 @@ Options:
-f <config-file>, --config <config-file> Specify config file to use
[default: /etc/vmpoller/vmpoller-proxy.conf]
-e <endpoint>, --endpoint <endpoint> Specify the endpoint we connect to
-l <logfile>, --logfile <logfile> Specify the logfile to use
[default: /var/log/vmpoller/vmpoller-proxy.log]
"""

args = docopt(usage, version="1.0.0")

logging.basicConfig(filename=args["--logfile"],
format='%(asctime)s - %(levelname)s - vmpoller-proxy[%(process)s]: %(message)s',
level=logging.DEBUG)

if args["start"]:
start(args["--pidfile"], args["--config"], args["--daemon"])
Expand Down
10 changes: 9 additions & 1 deletion src/vmpoller-worker
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ performing the actual polling from a vCenter server.
"""

import logging

from vmpoller.core import VMPollerWorker, VMPollerClient
from docopt import docopt

Expand Down Expand Up @@ -80,7 +82,7 @@ def status(endpoint):

def main():
usage="""
Usage: vmpoller-worker [-d] [-p <pidfile>] [-f <config-file>] start
Usage: vmpoller-worker [-d] [-p <pidfile>] [-f <config-file>] [-l <logfile>] start
vmpoller-worker -e <endpoint> stop
vmpoller-worker -e <endpoint> status
vmpoller-worker --help
Expand All @@ -101,10 +103,16 @@ Options:
-f <config-file>, --config <config-file> Specify config file to use
[default: /etc/vmpoller/vmpoller-worker.conf]
-e <endpoint>, --endpoint <endpoint> Specify the endpoint we connect to
-l <logfile>, --logfile <logfile> Specify the logfile to use
[default: /var/log/vmpoller/vmpoller-worker.log]
"""

args = docopt(usage, version="1.0.0")

logging.basicConfig(filename=args["--logfile"],
format='%(asctime)s - %(levelname)s - vmpoller-worker[%(process)s]: %(message)s',
level=logging.DEBUG)

if args["start"]:
start(args["--pidfile"], args["--config"], args["--daemon"])
Expand Down
54 changes: 27 additions & 27 deletions src/vmpoller/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
import glob
import json
import time
import syslog
import logging
import threading
import ConfigParser
from time import sleep
Expand Down Expand Up @@ -142,7 +142,7 @@ def run(self, config_file, start_agents=True):
"""
if not os.path.exists(config_file):
syslog.syslog("Configuration file does not exists: %s" % config_file)
logging.error("Configuration file does not exists: %s", config_file)
raise VMPollerException, "Configuration file does not exists: %s" % config_file

config = ConfigParser.ConfigParser()
Expand All @@ -154,7 +154,7 @@ def run(self, config_file, start_agents=True):
self.vcenter_configs = config.get('Default', 'vcenters')
self.threads_num = int(config.get('Default', 'threads'))
except ConfigParser.NoOptionError as e:
syslog.syslog("Configuration issues detected in %s: %s" % (config_file, e))
logging.error("Configuration issues detected in %s: %s" , config_file, e)
raise

# A flag to signal that our threads and daemon should be terminated
Expand Down Expand Up @@ -185,17 +185,17 @@ def run(self, config_file, start_agents=True):
try:
self.mgmt.bind(self.mgmt_endpoint)
except zmq.ZMQError as e:
syslog.syslog("Cannot bind management socket: %s" % e)
logging.error("Cannot bind management socket: %s", e)
raise VMPollerException, "Cannot bind management socket: %s" % e

# Create a ROUTER socket for forwarding client requests to our worker threads
syslog.syslog("Connecting to the VMPoller Proxy server")
logging.info("Connecting to the VMPoller Proxy server")
self.router = self.zcontext.socket(zmq.ROUTER)

try:
self.router.connect(self.proxy_endpoint)
except zmq.ZMQError as e:
syslog.syslog("Cannot connect worker to proxy: %s" % e)
logging.error("Cannot connect worker to proxy: %s", e)
raise VMPollerException, "Cannot connect worker to proxy: %s" % e

# Create a DEALER socket for passing messages from our worker threads back to the clients
Expand Down Expand Up @@ -297,15 +297,15 @@ def get_vcenter_configs(self, config_dir):
"""
if not os.path.exists(config_dir) or not os.path.isdir(config_dir):
syslog.syslog("%s does not exists or is not a directory" % config_dir)
logging.error("%s does not exists or is not a directory", config_dir)
raise VMPollerException, "%s does not exists or is not a directory" % config_dir

# Get all *.conf files for the different vCenters
path = os.path.join(config_dir, "*.conf")
confFiles = glob.glob(path)

if not confFiles:
syslog.syslog("No vCenter config files found in %s" % config_dir)
logging.error("No vCenter config files found in %s", config_dir)
raise VMPollerException, "No vCenter config files found in %s" % config_dir

return confFiles
Expand All @@ -319,7 +319,7 @@ def start_agents(self):
try:
self.agents[eachAgent].connect()
except Exception as e:
print 'Cannot connect to %s: %s' % (eachAgent, e)
logging.error('Cannot connect to %s: %s', eachAgent, e)

def shutdown_agents(self):
"""
Expand Down Expand Up @@ -376,7 +376,7 @@ def process_mgmt_message(self, msg):

if msg["cmd"] == "shutdown":
self.time_to_die = True
syslog.syslog("VMPoller Worker is shutting down")
logging.info("VMPoller Worker is shutting down")
return "Shutting down VMPoller Worker"
elif msg["cmd"] == "status":
header = "VMPoller Worker"
Expand Down Expand Up @@ -454,15 +454,15 @@ def get_host_property(self, msg):
'runtime.bootTime': lambda p: time.strftime('%Y-%m-%d %H:%M:%S', p),
}

syslog.syslog('[%s] Retrieving %s for host %s' % (self.vcenter, msg['property'], msg['name']))
logging.info('[%s] Retrieving %s for host %s', self.vcenter, msg['property'], msg['name'])

# Get the properties for all registered hosts
try:
results = self.viserver._retrieve_properties_traversal(property_names=property_names,
obj_type=MORTypes.HostSystem)
except Exception as e:
sleep(1) # Settle down for a moment
syslog.syslog("Cannot get property for host %s: %s" % (msg["name"], e))
logging.warning("Cannot get property for host %s: %s", msg["name"], e)
return "Cannot get property for host %s: %s" % (msg["name"], e)

# Do we have something to return?
Expand Down Expand Up @@ -545,14 +545,14 @@ def get_datastore_property(self, msg):
property_names.extend(custom_zbx_properties[msg["property"]])
property_names.remove(msg["property"])

syslog.syslog('[%s] Retrieving %s for datastore %s' % (self.vcenter, msg['property'], msg['name']))
logging.info('[%s] Retrieving %s for datastore %s', self.vcenter, msg['property'], msg['name'])

try:
results = self.viserver._retrieve_properties_traversal(property_names=property_names,
obj_type=MORTypes.Datastore)
except Exception as e:
sleep(1) # Settle down for a moment
syslog.syslog("Cannot get property for datastore %s: %s" % (msg["name"], e))
logging.warning("Cannot get property for datastore %s: %s" % (msg["name"], e))
return "Cannot get property for datastore %s: %s" % (msg["name"], e)

if not results:
Expand Down Expand Up @@ -602,15 +602,15 @@ def discover_hosts(self):
# Property <name>-<macros> mappings that Zabbix uses
property_macros = { 'name': '{#ESX_NAME}', 'runtime.powerState': '{#ESX_POWERSTATE}' }

syslog.syslog('[%s] Discovering ESX hosts' % self.vcenter)
logging.info('[%s] Discovering ESX hosts', self.vcenter)

# Retrieve the data
try:
results = self.viserver._retrieve_properties_traversal(property_names=property_names,
obj_type=MORTypes.HostSystem)
except Exception as e:
sleep(1) # Settle down for a moment
syslog.syslog("Cannot discover hosts: %s" % e)
logging.warning("Cannot discover hosts: %s", e)
return "Cannot discover hosts: %s" % e

# Iterate over the results and prepare the JSON object
Expand Down Expand Up @@ -655,15 +655,15 @@ def discover_datastores(self):
'summary.accessible': '{#DS_ACCESSIBLE}',
}

syslog.syslog('[%s] Discovering datastores' % self.vcenter)
logging.info('[%s] Discovering datastores', self.vcenter)

# Retrieve the data
try:
results = self.viserver._retrieve_properties_traversal(property_names=property_names,
obj_type=MORTypes.Datastore)
except Exception as e:
sleep(1) # Settle down for a moment
syslog.syslog("Cannot discover datastores: %s" % e)
logging.warning("Cannot discover datastores: %s", e)
return "Cannot discover datastores: %s" % e

# Iterate over the results and prepare the JSON object
Expand Down Expand Up @@ -698,7 +698,7 @@ class VMPollerProxy(Daemon):
"""
def run(self, config_file):
if not os.path.exists(config_file):
syslog.syslog("Cannot read configuration for proxy: %s" % config_file)
logging.error("Cannot read configuration for proxy: %s", config_file)
raise VMPollerException, "Cannot read configuration for proxy: %s" % config_file

config = ConfigParser.ConfigParser()
Expand All @@ -709,7 +709,7 @@ def run(self, config_file):
self.backend_endpoint = config.get('Default', 'backend')
self.mgmt_endpoint = config.get('Default', 'mgmt')
except ConfigParser.NoOptionError as e:
syslog.syslog("Configuration issues detected in %s: %s" % (config_file, e))
logging.error("Configuration issues detected in %s: %s", config_file, e)
raise

# A flag to indicate that the VMPollerProxy daemon should be terminated
Expand All @@ -724,7 +724,7 @@ def run(self, config_file):
try:
self.mgmt.bind(self.mgmt_endpoint)
except zmq.ZMQError as e:
syslog.syslog("Cannot bind management socket: %s" % e)
logging.error("Cannot bind management socket: %s", e)
raise VMPollerException, "Cannot bind management socket: %s" % e

# Socket facing clients
Expand All @@ -733,7 +733,7 @@ def run(self, config_file):
try:
self.frontend.bind(self.frontend_endpoint)
except zmq.ZMQError as e:
syslog.syslog("Cannot bind frontend socket: %s" % e)
logging.error("Cannot bind frontend socket: %s", e)
raise VMPollerException, "Cannot bind frontend socket: %s" % e

# Socket facing workers
Expand All @@ -742,7 +742,7 @@ def run(self, config_file):
try:
self.backend.bind(self.backend_endpoint)
except zmq.ZMQError as e:
syslog.syslog("Cannot bind backend socket: %s" % e)
logging.error("Cannot bind backend socket: %s", e)
raise VMPollerException, "Cannot bind backend socket: %s" % e

# Create a poll set for our sockets
Expand All @@ -751,7 +751,7 @@ def run(self, config_file):
self.zpoller.register(self.backend, zmq.POLLIN)
self.zpoller.register(self.mgmt, zmq.POLLIN)

syslog.syslog("Starting the VMPoller Proxy")
logging.info("Starting the VMPoller Proxy")

# Enter the daemon loop from here
while not self.time_to_die:
Expand Down Expand Up @@ -802,7 +802,7 @@ def process_mgmt_message(self, msg):

if msg["cmd"] == "shutdown":
self.time_to_die = True
syslog.syslog("VMPoller Proxy is shutting down")
logging.info("VMPoller Proxy is shutting down")
return "Shutting down VMPoller Proxy"
elif msg["cmd"] == "status":
header = "VMPoller Proxy"
Expand Down Expand Up @@ -872,7 +872,7 @@ def run(self, msg):
else:
# We didn't get a reply back from the server, let's retry
self.retries -= 1
syslog.syslog("Did not receive reply from server, retrying...")
logging.warning("Did not receive reply from server, retrying...")

# Socket is confused. Close and remove it.
self.zclient.close()
Expand All @@ -891,7 +891,7 @@ def run(self, msg):

# Did we have any result reply at all?
if not result:
syslog.syslog("Did not receive a reply from the server, aborting...")
logging.error("Did not receive a reply from the server, aborting...")
return "Did not receive reply from the server, aborting..."

return result

0 comments on commit 6d0bcff

Please sign in to comment.