# Path: canaryd/log.py
# STDOUT_LOG_LEVELS = (logging.DEBUG, logging.INFO)
# STDERR_LOG_LEVELS = (logging.WARNING, logging.ERROR, logging.CRITICAL)
# class LogFilter(logging.Filter):
# class LogFormatter(logging.Formatter):
#     def __init__(self, *levels):
#     def filter(self, record):
#     def format(self, record):
# def setup_logging(verbose, debug, default_level=logging.CRITICAL):
# def setup_logging_from_settings(settings):
# def print_exception(debug_only=False):
# 
# Path: canaryd/subprocess.py
# def ensure_command_tuple(command):
# def decode_output(output):
# def get_command_output(command, *args, **kwargs):


import re
from collections import defaultdict
from os import listdir, path, sep as os_sep
from canaryd.log import logger
from canaryd.subprocess import CalledProcessError, get_command_output


def _get_lsof_pid_to_listens(timeout):
    output = get_command_output(
        'lsof -i -n -P -b -l -L -s TCP:LISTEN',
        timeout=timeout,
    )

    for line in output.splitlines():
        # Skip bad/error lines
        if 'no pwd entry' in line:
            continue

        try:
            _, pid, _, _, ip_type, _, _, _, ip_host, _ = line.split(None, 9)

            ip_type = ip_type.lower()
            pid = int(pid)

            # Work out the host:port bit
            host, port = ip_host.rsplit(':', 1)
            port = int(port)

            host_port_tuple = (ip_type, host, port)
            PID_TO_PORTS[pid].add(host_port_tuple)
        except ValueError:
            logger.warning('Dodgy lsof line ignored: "{0}"'.format(line))


def _get_netstat_pid_to_listens(timeout):

# Path: canaryd/log.py
# STDOUT_LOG_LEVELS = (logging.DEBUG, logging.INFO)
# STDERR_LOG_LEVELS = (logging.WARNING, logging.ERROR, logging.CRITICAL)
# class LogFilter(logging.Filter):
# class LogFormatter(logging.Formatter):
#     def __init__(self, *levels):
#     def filter(self, record):
#     def format(self, record):
# def setup_logging(verbose, debug, default_level=logging.CRITICAL):
# def setup_logging_from_settings(settings):
# def print_exception(debug_only=False):
# 
# Path: canaryd/subprocess.py
# def ensure_command_tuple(command):
# def decode_output(output):
# def get_command_output(command, *args, **kwargs):


# Path: canaryd/plugins/services_util.py
import re
from collections import defaultdict
from os import listdir, path, sep as os_sep
from canaryd.log import logger
from canaryd.subprocess import CalledProcessError, get_command_output



def _get_lsof_pid_to_listens(timeout):
    output = get_command_output(
        'lsof -i -n -P -b -l -L -s TCP:LISTEN',
        timeout=timeout,
    )

    for line in output.splitlines():
        # Skip bad/error lines
        if 'no pwd entry' in line:
            continue

        try:
            _, pid, _, _, ip_type, _, _, _, ip_host, _ = line.split(None, 9)

            ip_type = ip_type.lower()
            pid = int(pid)

            # Work out the host:port bit
            host, port = ip_host.rsplit(':', 1)
            port = int(port)

            host_port_tuple = (ip_type, host, port)
            PID_TO_PORTS[pid].add(host_port_tuple)
        except ValueError:
            logger.warning('Dodgy lsof line ignored: "{0}"'.format(line))


def _get_netstat_pid_to_listens(timeout):

    output = get_command_output(