Status beacon should raise proper exception

Also have it check for the status exec module in __virtual__()
This commit is contained in:
Mike Place 2016-12-06 11:19:49 -07:00
parent 1b52289508
commit a8ce153252
No known key found for this signature in database
GPG Key ID: 9136F4F13705CFD3
2 changed files with 21 additions and 3 deletions

View File

@ -87,6 +87,7 @@ markers for specific list items:
from __future__ import absolute_import
import logging
import datetime
import salt.exceptions
log = logging.getLogger(__name__)
@ -99,6 +100,12 @@ def __validate__(config):
return False, ('Configuration for status beacon must be a dictionary.')
return True, 'Valid beacon configuration'
def __virtual__():
# TODO Find a way to check the existence of the module itself, not just a single func
if 'status.w' not in __salt__:
return (False, 'The \'status\' execution module is not available on this system')
else:
return True
def beacon(config):
'''
@ -118,7 +125,12 @@ def beacon(config):
ret = {}
for func in config:
data = __salt__['status.{0}'.format(func)]()
try:
data = __salt__['status.{0}'.format(func)]()
except salt.exceptions.NotImplemented as exc:
log.error('Status beacon attempted to process function {0} \
but encountered error: {1}'.format(func, exc))
continue
ret[func] = {}
for item in config[func]:
if item == 'all':

View File

@ -27,7 +27,7 @@ import salt.utils.event
from salt.utils.network import host_to_ips as _host_to_ips
from salt.utils.network import remote_port_tcp as _remote_port_tcp
from salt.ext.six.moves import zip
from salt.exceptions import CommandExecutionError
from salt.exceptions import CommandExecutionError, NotImplemented
__virtualname__ = 'status'
__opts__ = {}
@ -210,8 +210,14 @@ def loadavg():
.. code-block:: bash
salt '*' status.loadavg
:raises NotImpelemnted: If the system cannot report loadaverages to Python
'''
load_avg = os.getloadavg()
try:
load_avg = os.getloadavg()
except AttributeError:
# Some UNIX-based operating systems do not have os.getloadavg()
raise salt.exceptions.CommandExecutionError('status.loadavag is not available on your platform')
return {'1-min': load_avg[0],
'5-min': load_avg[1],
'15-min': load_avg[2]}