"""
  Server Density Plugin
  Temperature measurements

  https://www.serverdensity.com/plugins/temperatures/
  https://github.com/serverdensity/sd-agent-plugins/


  Version: 1.0.0
"""

import json
import logging
import os
import platform
import sys
import subprocess
import time


class Docker(object):

    def __init__(self, agent_config, checks_logger, raw_config):
        self.agent_config = agent_config
        self.checks_logger = checks_logger
        self.raw_config = raw_config
        self.version = platform.python_version_tuple()

        # stats we are interested in
        self.docker_path = {
            '/sys/fs/cgroup/memory/docker/{0}/memory.stat': 'memory',
            '/sys/fs/cgroup/memory/docker/{0}/memory.oom_control': 'memory',
            '/sys/fs/cgroup/cpu/docker/{0}/cpu.stat': 'cpu',
            '/sys/fs/cgroup/cpu/docker/{0}/blkio.sectors': 'blkio',
            '/sys/fs/cgroup/cpu/docker/{0}/blkio.io_service_bytes': 'blkio',
            '/sys/fs/cgroup/cpu/docker/{0}/blkio.io_serviced': 'blkio',
            '/sys/fs/cgroup/blkio/docker/{0}/blkio.io_queued': 'blkio',
        }

    def run(self):

        data = {}

        container_id = None

        names = self.extract_container_names()
        print names

        for container_id, container_name in names.iteritems():
            print container_id

            for path, stat_type in self.docker_path.iteritems():
                try:
                    stat_file = path.format(container_id)
                    if not os.path.isfile(stat_file):
                        continue

                    reading = None
                    with open(stat_file) as temp_file:
                        reading = temp_file.read()
                        temp_file.close()
                    if not reading:
                        continue

                    reading = reading.split('\n')
                    for read in reading:
                        if not read:
                            continue

                        name, value = read.split()
                        data['{0}-{1}-{2}'.format(
                            container_name,
                            stat_type,
                            name.lower())] = value

                except Exception as exception:
                    self.checks_logger.error(
                        'Failed to open file to read stat: {0}'.format(
                        exception.message))

        return data

    def extract_container_names(self):
        """ Iterate over the output of:
            sudo docker ps -l
            CONTAINER ID IMAGE        COMMAND   CREATED      STATUS      PORTS NAMES
            491f2fe3b18f ubuntu:14.04 /bin/bash 40 hours ago Up 40 hours focused_feynman
        """

        names = {}

        proc = subprocess.Popen(
                    ['sudo', 'docker', 'ps', '-l', '--no-trunc'],
                    stdout=subprocess.PIPE,
                    close_fds=True)
        docker_ps = proc.communicate()[0]

        for line in docker_ps.split('\n'):
            if not line or line.startswith('CONTAINER ID'):
                continue
            line = line.split()
            names[line[0]] = line[-1]

        return names



if __name__ == '__main__':
    """Standalone test
    """

    raw_agent_config = {
        'Docker': {
        }
    }

    main_checks_logger = logging.getLogger('Docker')
    main_checks_logger.setLevel(logging.DEBUG)
    main_checks_logger.addHandler(logging.StreamHandler(sys.stdout))
    docker_check = Docker({}, main_checks_logger, raw_agent_config)

    while True:
        try:
            print json.dumps(docker_check.run(), indent=4, sort_keys=True)
        except:
            main_checks_logger.exception("Unhandled exception")
        finally:
            time.sleep(60)
@@ -1158,37 +1158,8 @@
 %7B%7D%0A%0A
-        container_id = None%0A%0A
     
@@ -1204,28 +1204,8 @@
 es()
-%0A        print names
 %0A%0A  
@@ -1269,40 +1269,8 @@
 ():%0A
-            print container_id%0A%0A
     
@@ -2338,24 +2338,28 @@
             
+    
 exception.me
@@ -2529,22 +2529,16 @@
  IMAGE  
-      
 COMMAND 
@@ -2548,20 +2548,16 @@
 REATED  
-    
 STATUS  
@@ -2607,14 +2607,8 @@
 untu
-:14.04
  /bi
@@ -2627,12 +2627,8 @@
 urs 
-ago 
 Up 4
@@ -2729,24 +2729,16 @@
         
-  
-      
 %5B'sudo',
@@ -2787,24 +2787,16 @@
         
-        
 stdout=s
@@ -2811,24 +2811,16 @@
 s.PIPE,%0A
-        
         
@@ -3104,17 +3104,16 @@
 names%0A%0A%0A
-%0A
 if __nam
@@ -3690,8 +3690,9 @@
 leep(60)
+%0A

fix pep8 issues