# -*- coding: utf-8 -*-
'''
Beacon to emit system load averages
'''

# Import Python libs
from __future__ import absolute_import
import logging
import os

# Import Salt libs
import salt.utils

# Import Py3 compat
from salt.ext.six.moves import zip

log = logging.getLogger(__name__)

__virtualname__ = 'load'


def __virtual__():
    if salt.utils.is_windows():
        return False
    else:
        return __virtualname__


def validate(config):
    '''
    Validate the beacon configuration
    '''

    # Configuration for load beacon should be a list of dicts
    if not isinstance(config, list):
        log.info('Configuration for load beacon must be a list.')
        return False
    else:
        for config_item in config:
            if not isinstance(config_item, dict):
                log.info('Configuration for load beacon must '
                         'be a list of dictionaries.')
                return False
            else:
                if not any(j in ['1m', '5m', '15m'] for j in config_item.keys()):
                    log.info('Configuration for load beacon must '
                             'contain 1m, 5m and 15m items.')
                    return False

            for item in config_item:
                if not isinstance(config_item[item], list):
                    log.info('Configuration for load beacon: '
                             '1m, 5m and 15m items must be '
                             'a list of two items.')
                    return False
                else:
                    if len(config_item[item]) != 2:
                        log.info('Configuration for load beacon: '
                                 '1m, 5m and 15m items must be '
                                 'a list of two items.')
                        return False
    return True


def beacon(config):
    '''
    Emit the load averages of this host.

    Specify thresholds for each load average
    and only emit a beacon if any of them are
    exceeded.

    .. code-block:: yaml

        beacons:
          - load:
            - 1m:
              - 0.0
              - 2.0
            - 5m:
              - 0.0
              - 1.5
            - 15m:
              - 0.1
              - 1.0

    '''
    log.trace('load beacon starting')
    ret = []
    if not os.path.isfile('/proc/loadavg'):
        return ret
    with salt.utils.fopen('/proc/loadavg', 'rb') as fp_:
        avgs = fp_.read().split()[:3]
        avg_keys = ['1m', '5m', '15m']
        avg_dict = dict(zip(avg_keys, avgs))
        # Check each entry for threshold
        if float(avgs[0]) < float(config[0]['1m'][0]) or \
        float(avgs[0]) > float(config[0]['1m'][1]) or \
        float(avgs[1]) < float(config[1]['5m'][0]) or \
        float(avgs[1]) > float(config[1]['5m'][1]) or \
        float(avgs[2]) < float(config[2]['15m'][0]) or \
        float(avgs[2]) > float(config[2]['15m'][1]):
            ret.append(avg_dict)
    return ret

@@ -2040,18 +2040,16 @@
         
- -
  load:%0A 
@@ -2058,18 +2058,16 @@
         
- -
  1m:%0A   
@@ -2114,18 +2114,16 @@
         
- -
  5m:%0A   
@@ -2159,16 +2159,16 @@
   - 1.5%0A
+
         
@@ -2170,18 +2170,16 @@
         
- -
  15m:%0A  
@@ -2596,27 +2596,24 @@
 loat(config%5B
-0%5D%5B
 '1m'%5D%5B0%5D) or
@@ -2657,11 +2657,8 @@
 fig%5B
-0%5D%5B
 '1m'
@@ -2702,27 +2702,24 @@
 loat(config%5B
-1%5D%5B
 '5m'%5D%5B0%5D) or
@@ -2763,11 +2763,8 @@
 fig%5B
-1%5D%5B
 '5m'
@@ -2808,27 +2808,24 @@
 loat(config%5B
-2%5D%5B
 '15m'%5D%5B0%5D) o
@@ -2870,11 +2870,8 @@
 fig%5B
-2%5D%5B
 '15m

Make load beacon config into list