# Path: bts_tools/notification.py
# def send_notification(nodes, node_msg, alert=False):
#     msg = '[%s] %s: %s' % (nodes[0].type(), ', '.join({n.name for n in nodes}), node_msg)
#     if alert:
#         log.warning(msg)
#     else:
#         log.info(msg)
# 
#     for ntype, notify in [('email', send_notification_email),
#                           ('boxcar', send_notification_boxcar),
#                           ('telegram', send_notification_telegram)]:
#         notify_nodes = [node for node in nodes if ntype in node.notification]
#         if notify_nodes:
#             node_names = ', '.join(n.name for n in notify_nodes)
#             msg = '[%s] %s: %s' % (notify_nodes[0].type(), node_names, node_msg)
#             if core.config.get('hostname'):
#                 msg = '[{}] {}'.format(core.config['hostname'], msg)
#             try:
#                 notify(msg, alert)
#             except Exception as e:
#                 log.warning('Failed sending %s notification to %s: %s' % (ntype, node_names, node_msg))
#                 log.exception(e)
# 
# Path: bts_tools/monitor.py
# class StableStateMonitor(object):
#     """Monitor a sequence of states, and is able to compute a "stable" state value when N
#     consecutive same states have happened.
#     For example, we only want to decide the client is offline after 3 consecutive 'offline' states
#     to absorb transient errors and avoid reacting too fast on wrong alerts.
#     """
#     def __init__(self, n):
#         self.n = n
#         self.states = deque(maxlen=n+1)
#         self.last_stable_state = None
# 
#     def push(self, state):
#         stable_state = self.stable_state()
#         if stable_state is not None:
#             self.last_stable_state = stable_state
#         self.states.append(state)
# 
#     def stable_state(self):
#         size = len(self.states)
#         if size < self.n:
#             return None
#         last_state = self.states[-1]
#         if all(state == last_state for state in islice(self.states, size-self.n, size)):
#             return last_state
#         return None
# 
#     def just_changed(self):
#         stable_state = self.stable_state()
#         if stable_state is None or self.last_stable_state is None:
#             return False
#         return stable_state != self.last_stable_state
# 
# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r
# 
# BTS_TOOLS_CONFIG_FILE = join(BTS_TOOLS_HOMEDIR, 'config.yaml')


from ..notification import send_notification
from ..monitor import StableStateMonitor
from ..core import run, BTS_TOOLS_CONFIG_FILE
import logging
#
# 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; either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
#


log = logging.getLogger(__name__)


# def find_mount_point(path):
#     path = os.path.abspath(path)
#     while not os.path.ismount(path):
#         path = os.path.dirname(path)
#     return path


def to_int(s):
    if s[-1].lower() == 'g':
        return int(s[:-1]) * 1024 * 1024 * 1024
    elif s[-1].lower() == 'm':

# Path: bts_tools/notification.py
# def send_notification(nodes, node_msg, alert=False):
#     msg = '[%s] %s: %s' % (nodes[0].type(), ', '.join({n.name for n in nodes}), node_msg)
#     if alert:
#         log.warning(msg)
#     else:
#         log.info(msg)
# 
#     for ntype, notify in [('email', send_notification_email),
#                           ('boxcar', send_notification_boxcar),
#                           ('telegram', send_notification_telegram)]:
#         notify_nodes = [node for node in nodes if ntype in node.notification]
#         if notify_nodes:
#             node_names = ', '.join(n.name for n in notify_nodes)
#             msg = '[%s] %s: %s' % (notify_nodes[0].type(), node_names, node_msg)
#             if core.config.get('hostname'):
#                 msg = '[{}] {}'.format(core.config['hostname'], msg)
#             try:
#                 notify(msg, alert)
#             except Exception as e:
#                 log.warning('Failed sending %s notification to %s: %s' % (ntype, node_names, node_msg))
#                 log.exception(e)
# 
# Path: bts_tools/monitor.py
# class StableStateMonitor(object):
#     """Monitor a sequence of states, and is able to compute a "stable" state value when N
#     consecutive same states have happened.
#     For example, we only want to decide the client is offline after 3 consecutive 'offline' states
#     to absorb transient errors and avoid reacting too fast on wrong alerts.
#     """
#     def __init__(self, n):
#         self.n = n
#         self.states = deque(maxlen=n+1)
#         self.last_stable_state = None
# 
#     def push(self, state):
#         stable_state = self.stable_state()
#         if stable_state is not None:
#             self.last_stable_state = stable_state
#         self.states.append(state)
# 
#     def stable_state(self):
#         size = len(self.states)
#         if size < self.n:
#             return None
#         last_state = self.states[-1]
#         if all(state == last_state for state in islice(self.states, size-self.n, size)):
#             return last_state
#         return None
# 
#     def just_changed(self):
#         stable_state = self.stable_state()
#         if stable_state is None or self.last_stable_state is None:
#             return False
#         return stable_state != self.last_stable_state
# 
# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r
# 
# BTS_TOOLS_CONFIG_FILE = join(BTS_TOOLS_HOMEDIR, 'config.yaml')


# Path: bts_tools/monitoring/free_disk_space.py
from ..notification import send_notification
from ..monitor import StableStateMonitor
from ..core import run, BTS_TOOLS_CONFIG_FILE
import logging

#
# 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; either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
#


log = logging.getLogger(__name__)


# def find_mount_point(path):
#     path = os.path.abspath(path)
#     while not os.path.ismount(path):
#         path = os.path.dirname(path)
#     return path


def to_int(s):
    if s[-1].lower() == 'g':
        return int(s[:-1]) * 1024 * 1024 * 1024
    elif s[-1].lower() == 'm':

        return int(s[:-1]) * 1024 * 1024