Beispiel #1
0
##################

splunk_docker_utils.do_setup()

# We need to loop running containers, getting stats

hosts = open (splunk_docker_utils.RUN_DIR + '/docker_hosts.dat','r')

for host in hosts:

    host=host.rstrip()
    conn_string="tcp://" + host +":2375"        
    #cli = Client(base_url='tcp://192.168.160.166:2375', version='auto')

    try:
        cli = Client(base_url=conn_string, version='auto')
        containers=cli.containers()
        count=0
        for container in containers:
            #print "container is "+str(container)
            name_attr=re.search( r'\[u\'\/(\w+)\'\]',str(containers[count]['Names']))
            container_name=name_attr.group(1)	
            count +=1
            inspect=cli.inspect_container(container_name)
            # We need hostname, unfortunately we have to inspect container for it
            hostname=inspect['Config']['Hostname']
            stats=cli.stats(container_name,decode=None, stream=False)

            # Network

            #networks": {
Beispiel #2
0
	def __init__(self,args=None):
		## Init Thread 
		threading.Thread.__init__(self)
		self.wait=threading.Event()
		## Init Logs
		self._logger = logging.getLogger("MAIN")
		FORMAT = '%(asctime)s %(levelname)s %(threadName)s %(name)s %(message)s'
		LOGFILE='logs/docker-zabbix-sender.log'
		logFormatter = logging.Formatter(FORMAT)
		console = logging.StreamHandler(sys.stdout)
		console.setFormatter(logFormatter)
		fileHandler = logging.handlers.TimedRotatingFileHandler(LOGFILE, when='D', interval=1, backupCount=7)
		fileHandler.setFormatter(logFormatter)
		self._logger.addHandler(fileHandler)
		self._logger.setLevel(logging.INFO)
		self._logger.addHandler(console)
		
		## Parse the command line
		parser = argparse.ArgumentParser(
			description="""Provides Zabbix Docker containers statistics running on a Docker daemon."""
		)
		parser.add_argument('-V', '--version',
    	        	action='version',
	    	        version='%(prog)s ' + version
    		)
		parser.add_argument('-v', '--verbose',
	    	        action='count',
    		        help='Verbose mode, -vv for more details'
		)
		parser.add_argument("--tlsverify",
			action='store',
	    	        choices=["true", "false"],
    		        default='true',
	    	        help="Use TLS and verify the remote Docker daemon. Default is %(default)s"
    		)
	    	parser.add_argument('-c', '--config',
    		        metavar="<file>",
	    	        help="Absolute path to the zabbix agent configuration file"
    		)
		parser.add_argument('-z', '--zabbix-server',
    	        	metavar='<server>',
	    	        help='Hostname or IP address of Zabbix server'
    		)
	    	parser.add_argument('-p', '--port',
    		        metavar='<server port>',
    	        	help='Specify port number of server trapper running on the server. Default is 10051'
		)
	    	parser.add_argument('-i', '--interval',
    		        metavar='<sec>',
    	        	default=1800,
	    	        type=int,
    		        help='Specify Zabbix update interval (in sec). Default is %(default)s'
    	    	)
	    	parser.add_argument('-r', '--real-time',
    		        action='store_true',
    	        	help="zabbix_sender push metrics to Zabbix one by one as soon as they are sent."
		)
	    	parser.add_argument('-s', '--host',
    		        metavar='<hostname>',
	    	        help='Specify host name. Host IP address and DNS name will not work'
    		)
	   	self.args = parser.parse_args(args)
    		kwargs  = kwargs_from_env()
	    	if not self.args.tlsverify.lower() in ("yes", "true", "t", "1"):
    		        kwargs['tls'].assert_hostname = False
	    	kwargs['version'] = '1.17'
	    	if self.args.zabbix_server is None:
    		        self.args.zabbix_server = os.environ['ZABBIX_SERVER']
	    	if self.args.host is None:
    		        self.args.host = os.environ['ZABBIX_HOST']
		if "INTERVAL" in os.environ :
			self.args.interval = os.environ['INTERVAL']
		## Hostame in environment varaiable
		if "HOSTNAME" in os.environ :
			self.hostname=os.environ['HOSTNAME']
		## The configuration file
		self.configFilename = "Default.ini"
		## Open the Config File
		self.json = self.openConfigFile(self.configFilename)
		if self.json.cp == None :
	                self._logger.info("PARSE ERROR : Please Check the Configuration File %s", self.configFilename )

			
			
		## Init de l'API docker && Param to stop the thread && the zabbix_sender subProcess && Thread Array
    		self.docker_client = Client(**kwargs)
	    	self.running = True
    		self.zabbixPipe = TrapSender(
          		config_file = self.args.config,
	    		zabbix_server = self.args.zabbix_server,
    			host = self.args.host,
    	       		port = self.args.port,
	    		real_time = self.args.real_time,
    			verbose = self.args.verbose if self.args.verbose is not None else 0
		);
		self.emitter={}
Beispiel #3
0
from docker import Client

#cli = Client(base_url="tcp://192.168.0.2:2376")
cli = Client(base_url='tcp://192.168.0.2:2376', version='auto')


class DockerManager:
    def __init__(self):
        try:
            self.cli = Client(base_url='tcp://192.168.0.2:2376',
                              version='auto')
        except Exception as e:
            raise Exception(e)

    def getContainers(self, todos=True):
        try:

            return [c for c in self.cli.containers(all=todos)]
        except Exception as e:
            raise Exception(e)

    def createContainer(self, nome, hn):
        try:
            container = self.cli.create_container(name=nome,
                                                  hostname=hn,
                                                  image="debian",
                                                  detach=True,
                                                  stdin_open=True,
                                                  tty=True)
            self.cli.start(container=container.get('Id'))
            return container
Beispiel #4
0
# -*- coding: utf-8 -*-

from docker import Client
from docker import errors
import base64
import binascii




def sss(ddd):
    ddd = [9,9,9,9]

cli = Client(version='auto', base_url='tcp://127.0.0.1:2375')
result =  cli.containers()
print '\n\n'
print result

zzz = 'sh /home/run.sh /home/libg/Jx3Robot/Jx3Robot Li9KeDNSb2JvdEQgLVQgU3VwZXJSb2JvdDo1MCAtYSBsYWl0dWFuIC1yIMC0zcWwySAtbCAtZSAtayDL5rv6IC1qIMvmu/ogLWcgy+a7+iAtcyAxMC4yMC45Ni4xNTUgLXAgMTAw'
ppp = zzz.split(' ')

print ppp

print ppp[2]

print ppp[3]

#procmd.decode('utf8').encode('gb2312')

rrr = base64.b64decode(ppp[3])
#		except NameError,error_msg:
#			pass
#		finally:
#			old_result = eval(generator.next())
#			new_result = eval(generator.next())
#			c.close()
#		cpu_total_usage=new_result['cpu_stats']['cpu_usage']['total_usage'] - old_result['cpu_stats']['cpu_usage']['total_usage']
#		cpu_system_uasge=new_result['cpu_stats']['system_cpu_usage'] - old_result['cpu_stats']['system_cpu_usage']
#		cpu_num=len(old_result['cpu_stats']['cpu_usage']['percpu_usage'])
#		cpu_percent=round((float(cpu_total_usage)/float(cpu_system_uasge))*cpu_num*100.0,2)
#		print cpu_percent
#	else:
#		print 0

if __name__ == '__main__':
    c = Client(base_url='unix://var/run/docker.sock', version='1.22')
    if len(sys.argv) < 3:
        #print "error! please %s [container_name] [up|cpu_per_usage|memory_usage|rx_bytes|tx_bytes]" %sys.argv[0]
        print "error! please %s [container_name] [up|cpu_per_usage|usage_mem|limit_max_mem|rx_bytes|tx_bytes]" % sys.argv[
            0]

    elif sys.argv[2] == "up":
        container_status(sys.argv[1])

    elif sys.argv[2] == "cpu_per_usage":
        display_cpu(sys.argv[1])

    elif sys.argv[2] == "limit_max_mem":
        display_memory(sys.argv[1], "limit_max_mem")

    elif sys.argv[2] == "usage_mem":
Beispiel #6
0
#!usr/bin/python
# -*- coding: utf-8 -*-
from docker import Client

cli = Client(base_url="http://192.168.0.2:2376", version="auto")


class DockerModulo:
    def __init__(self):
        try:
            self.cli = Client(base_url="http://192.168.0.2:2376",
                              version="auto")
        except Exception as e:
            raise Exception(e)

    def getContainers(self, todos=True):
        try:
            return [c for c in self.cli.containers(all=todos)]
        except Exception as e:
            raise Exception(e)

    def startContainer(self, id):
        return self.cli.start(container=id)

    def stopContainer(self, id):
        return self.cli.stop(container=id)

    def removeContainer(self, id):
        self.cli.stop(container=id)
        return self.cli.remove_container(id)
Beispiel #7
0
from services import Services
from docker import Client
from flask import Flask

services = Services(Client(base_url='unix://var/run/docker.sock'),
                    "config.yml")
app = Flask(__name__, static_url_path='')

import overview.frontend
import overview.api
import overview.helpers
    def get_client(self, daemon_client):

        return Client(**daemon_client)
Beispiel #9
0
#!/usr/bin/env python

import requests
import time
from docker import Client
from docker import utils as dockerutils

c = Client(base_url="unix://var/run/docker.sock")

while True:
    res = requests.get('http://192.168.33.10/nginx_status')
    active_conn = int(res.text.split('\n')[0].split()[2])
    num_backend = len(
        filter(lambda c: c['Image'] == 'runseb/hostname', c.containers()))

    print num_backend, active_conn

    conn_threshold = 10

    if (num_backend <= active_conn / conn_threshold):
        new_backend = c.create_container(image='runseb/hostname', detach=True, ports=[5000], \
                                         host_config=dockerutils.utils.create_host_config(port_bindings={5000: None }))
        res = c.start(container=new_backend.get('Id'))
    elif (num_backend > 1 and num_backend > active_conn / conn_threshold):
        backends = filter(lambda c: c['Image'] == 'runseb/hostname',
                          c.containers())
        c.kill(container=backends[0]['Id'])
        c.remove_container(container=backends[0]['Id'])

    time.sleep(6)
Beispiel #10
0
 def cleanup():
     client = Client(version='auto', **kwargs_from_env())
     test_images = client.images(TEST_ORG + "/*")
     for image in test_images:
         client.remove_image(image)
 def __init__(self, containerObject):
     self.containerObject = containerObject
     self.dockerCli = Client(base_url='unix://var/run/docker.sock')
Beispiel #12
0
def template_queue(path, base_dir="/var/lib/docker/data/"):
    """
    Processes template files that have been added or changed or deleted from
    the rq-worker. Then, removes exited containers, kills and removes disabled containers,
    and starts/restarts enabled containers. Only restarts enabled containers which were
    running at the time of the configuration change.
    """
    import ast
    import os
    import sys
    import time

    import ConfigParser
    config = ConfigParser.RawConfigParser()
    # needed to preserve case sensitive options
    config.optionxform = str

    from docker import Client
    from docker.utils.types import LogConfig
    from subprocess import check_output
    c = Client(base_url='unix://var/run/docker.sock')

    try:
        # keep track of running containers to restart
        running_containers = c.containers()

        # first some cleanup
        containers = c.containers(quiet=True,
                                  all=True,
                                  filters={'status': "exited"})
        for cont in containers:
            try:
                # will only remove containers that aren't running
                c.remove_container(cont['Id'])
            except Exception as e:
                pass
        try:
            data_dir = "/scripts/"
            if base_dir != "/var/lib/docker/data/":
                data_dir = base_dir
            enabled, disabled = ast.literal_eval(
                check_output("python2.7 " + data_dir +
                             "info_tools/get_status.py enabled -b " + base_dir,
                             shell=True))
        except Exception as e:
            pass
        # remove disabled running containers
        for container in containers:
            for name in container["Names"]:
                if name in disabled:
                    try:
                        c.kill(container["Id"])
                        c.remove_container(container["Id"])
                        continue
                    except Exception as e:
                        pass

        core_containers = c.containers(all=True, filters={'name': "core-"})

        # remove core containers, so when restarted, behavior matches current configuration
        for container in core_containers:
            try:
                if container["Status"] == "exited":
                    c.remove_container(container["Id"])
                elif container["Id"].startswith(os.environ.get('HOSTNAME')):
                    # skip this container until the end
                    this_container = container["Id"]
                else:
                    c.kill(container["Id"])
                    c.remove_container(container["Id"])
            except Exception as e:
                pass

        # restart this container last
        try:
            c.kill(this_container)
            c.remove_container(this_container)
        except Exception as e:
            pass
        # start enabled containers
        data_dir = "/vent/"
        if base_dir != "/var/lib/docker/data/":
            data_dir = base_dir
        os.system('python2.7 ' + data_dir + 'template_parser.py core start')

        active_started = False
        passive_started = False
        vis_started = False

        for container in running_containers:
            # usually containers have one name, but container["Names"] is a list, so we have to be sure
            for name in container["Names"]:
                if active_started and passive_started and vis_started:
                    break
                elif "active-" in name and not active_started:
                    os.system('python2.7 ' + base_dir +
                              'template_parser.py active start')
                    active_started = True
                elif "passive-" in name and not passive_started:
                    os.system('python2.7 ' + base_dir +
                              'template_parser.py passive start')
                    passive_started = True
                elif "visualization-" in name and not vis_started:
                    os.system('python2.7 ' + base_dir +
                              'template_parser.py visualization start')
                    vis_started = True
                else:
                    pass
    except Exception as e:
        pass
    return
Beispiel #13
0
def file_queue(path, base_dir="/var/lib/docker/data/"):
    """
    Processes files that have been added from the rq-worker, and tells
    vent-management to start plugins that match the mime type for the new file.
    """
    import ConfigParser
    import magic
    import os
    import time

    from docker import Client
    from docker.utils.types import LogConfig

    c = Client(base_url='unix://var/run/docker.sock')

    # first some cleanup
    containers = c.containers(quiet=True,
                              all=True,
                              filters={'status': "exited"})
    for cont in containers:
        try:
            # will only remove containers that aren't running
            c.remove_container(cont['Id'])
        except Exception as e:
            pass

    template_dir = base_dir + "templates/"
    plugins_dir = base_dir + "plugins/"
    vent_dir = "/vent/"
    if base_dir != "/var/lib/docker/data/":
        vent_dir = base_dir
    try:
        config = ConfigParser.RawConfigParser()
        # needed to preserve case sensitive options
        config.optionxform = str
        # Check file exists
        if os.path.isfile(template_dir + 'modes.template'):
            config.read(template_dir + 'modes.template')
        plugin_array = []
        # Check section exists and has options
        if config.has_section("plugins") and config.options("plugins"):
            plugin_array = config.options("plugins")
        plugins = {}
        for plug in plugin_array:
            if plug not in ["core", "visualization", "collectors"]:
                plugins[plug] = config.get("plugins", plug)

        container_count = 0
        container_max = 50
        try:
            config = ConfigParser.RawConfigParser()
            # needed to preserve case sensitive options
            config.optionxform = str
            if os.path.isfile(template_dir + 'core.template'):
                config.read(template_dir + 'core.template')
            if config.has_section("active-containers") and config.has_option(
                    "active-containers", "count"):
                container_max = int(config.get("active-containers", "count"))
        except Exception as e:
            print(str(e))

        file_mime = None
        try:
            f_path = path.split("_", 1)[1]
            file_mime = magic.from_file(f_path, mime=True)
        except Exception as e:
            print(str(e))

        for plugin in plugins:
            # check mime types
            mime_types = []
            try:
                config = ConfigParser.RawConfigParser()
                # needed to preserve case sensitive options
                config.optionxform = str
                if os.path.isfile(template_dir + plugin + '.template'):
                    config.read(template_dir + plugin + '.template')
                if config.has_section("service") and config.has_option(
                        "service", "mime_types"):
                    mime_types = config.get("service", "mime_types").split(",")
            except Exception as e:
                print(str(e))
            # check file extensions
            ext_types = []
            try:
                config = ConfigParser.RawConfigParser()
                # needed to preserve case sensitive options
                config.optionxform = str
                if os.path.isfile(template_dir + plugin + '.template'):
                    config.read(template_dir + plugin + '.template')
                if config.has_section("service") and config.has_option(
                        "service", "ext_types"):
                    ext_types = config.get("service", "ext_types").split(",")
            except Exception as e:
                print(str(e))
            if len(mime_types) == 0 or file_mime in mime_types or path.split(
                    ".", -1)[-1] in ext_types:
                # check resources before creating container
                # wait until there are resources available
                container_count = len(
                    c.containers(filters={'status': 'running'}))
                while container_count >= container_max:
                    time.sleep(5)
                    container_count = len(
                        c.containers(filters={'status': 'running'}))
                cmd = "python2.7 " + vent_dir + "template_parser.py " + plugin + " start " + path
                os.system(cmd)
    except Exception as e:
        print(str(e))
    return
Beispiel #14
0
 def __init__(self):
     self.client = Client(**kwargs_from_env(assert_hostname=False))
     try:
         self.client.info()
     except Exception as e:
         self.client = None
Beispiel #15
0
def attach_to_docker_client():
    if os.getenv('DOCKER_HOST') == 'tcp://192.168.59.103:2376':
        c = Client(**kwargs_from_env(assert_hostname=False))
    else:
        c = Client()
    return c
Beispiel #16
0
 def client(self):
     return Client(**self.settings)
Beispiel #17
0
#!/usr/bin/python

from ansible.module_utils.basic import *
import docker
try:
    import docker
    from docker import Client
except ImportError:
    from docker import APIClient as Client

if __name__ == '__main__':
    module = AnsibleModule(argument_spec=dict(
        id=dict(required=True),
        type=dict(default='container', choises=['container', 'image'])),
                           supports_check_mode=True)

    id = module.params['id']
    type = module.params['type']

    client = Client()

    try:
        if type == 'image':
            attrs = client.inspect_image(id)
        elif type == 'container':
            attrs = client.inspect_container(id)
    except docker.errors.APIError as e:
        module.fail_json(attrs={}, msg=str(e))
    else:
        module.exit_json(failed=False, changed=False, attrs=attrs)
Beispiel #18
0
def _get_client(timeout=None):
    client = Client(base_url='unix://var/run/docker.sock', version='1.19')
    return client
Beispiel #19
0
 def __init__(self):
     try:
         self.cli = Client(base_url="http://192.168.0.2:2376",
                           version="auto")
     except Exception as e:
         raise Exception(e)
    def get_pid(ptf_name):
        cli = Client(base_url='unix://var/run/docker.sock')
        result = cli.inspect_container(ptf_name)

        return result['State']['Pid']
Beispiel #21
0
 def create_connection(self, url):
     return Client(base_url=url)
Beispiel #22
0
def main():
    global docker_api
    docker_api = Client(base_url='unix://var/run/docker.sock', version='auto')
    ui = Ui()
    gevent.spawn(event_watcher, ui.update)
    ui.run()
Beispiel #23
0
            stdout=subprocess.PIPE)).stdout.readlines()[0]).strip('\n'))
        time.sleep(1)
        network_new_result = eval(((subprocess.Popen(
            network_check_command, shell=True,
            stdout=subprocess.PIPE)).stdout.readlines()[0]).strip('\n'))
        result = int(network_new_result['rx']) - int(network_old_result['rx'])
    elif collect_item == 'network_tx_bytes':
        network_check_command = """docker exec %s awk '/eth0/ {print "{\\"rx\\":"$2",\\"tx\\":"$10"}"}' /proc/net/dev""" \
                                % container_name
        network_old_result = eval(((subprocess.Popen(
            network_check_command, shell=True,
            stdout=subprocess.PIPE)).stdout.readlines()[0]).strip('\n'))
        time.sleep(1)
        network_new_result = eval(((subprocess.Popen(
            network_check_command, shell=True,
            stdout=subprocess.PIPE)).stdout.readlines()[0]).strip('\n'))
        result = int(network_new_result['tx']) - int(network_old_result['tx'])
    return result


if __name__ == "__main__":
    client = Client(base_url='unix:///var/run/docker.sock')
    container = None
    item = None
    if sys.argv[1] is not None and sys.argv[2] is not None:
        container = sys.argv[1]
        item = sys.argv[2]
    else:
        exit(1)
    print check_container_stats(container, item)
    def execute(self, context):
        self.log.info('Starting docker container from image %s', self.image)

        tls_config = None
        if self.tls_ca_cert and self.tls_client_cert and self.tls_client_key:
            tls_config = tls.TLSConfig(ca_cert=self.tls_ca_cert,
                                       client_cert=(self.tls_client_cert,
                                                    self.tls_client_key),
                                       verify=True,
                                       ssl_version=self.tls_ssl_version,
                                       assert_hostname=self.tls_hostname)
            self.docker_url = self.docker_url.replace('tcp://', 'https://')

        self.cli = Client(base_url=self.docker_url,
                          version=self.api_version,
                          tls=tls_config)

        if ':' not in self.image:
            image = self.image + ':latest'
        else:
            image = self.image

        if self.force_pull or len(self.cli.images(name=image)) == 0:
            self.log.info('Pulling docker image %s', image)
            for l in self.cli.pull(image, stream=True):
                output = json.loads(l.decode('utf-8'))
                self.log.info("%s", output['status'])

        cpu_shares = int(round(self.cpus * 1024))

        with TemporaryDirectory(prefix='airflowtmp') as host_tmp_dir:
            self.environment['AIRFLOW_TMP_DIR'] = self.tmp_dir
            self.volumes.append('{0}:{1}'.format(host_tmp_dir, self.tmp_dir))

            self.container = self.cli.create_container(
                command=self.get_command(),
                cpu_shares=cpu_shares,
                environment=self.environment,
                host_config=self.cli.create_host_config(
                    binds=self.volumes, network_mode=self.network_mode),
                image=image,
                mem_limit=self.mem_limit,
                user=self.user,
                working_dir=self.working_dir)
            self.cli.start(self.container['Id'])

            line = ''
            for line in self.cli.logs(container=self.container['Id'],
                                      stream=True):
                line = line.strip()
                if hasattr(line, 'decode'):
                    line = line.decode('utf-8')
                self.log.info(line)

            exit_code = self.cli.wait(self.container['Id'])
            if exit_code != 0:
                raise AirflowException('docker container failed')

            if self.xcom_push_flag:
                return self.cli.logs(container=self.container['Id']
                                     ) if self.xcom_all else str(line)
Beispiel #25
0
 def __init__(self):
     self.cli = Client(base_url="tcp://10.100.0.116:2376", version="auto")
Beispiel #26
0
 def __init__(self):
     self.client = Client(base_url="tcp://192.168.0.2:2376")
Beispiel #27
0
 def __init__(self, target, auth, options, pkey):
     self.target = settings.SWARM_HOST
     # single global connection
     self.registry = settings.REGISTRY_HOST + ':' + settings.REGISTRY_PORT
     self.docker_cli = Client("tcp://{}:2395".format(self.target),
                              timeout=1200, version='1.17')
Beispiel #28
0
#!/usr/local/miniconda/bin/python3
__author__ = 'matthieu'

import argparse
from io import StringIO

from docker import Client
dcl = Client()

parser = argparse.ArgumentParser(description='PGXL startpack')
parser.add_argument('--ip',
                    dest="ip",
                    action="store_true",
                    help="print container IP")
parser.add_argument('--ncontainers',
                    dest="ncontainers",
                    action="store",
                    type=int,
                    default=4,
                    help="N containers")
parser.add_argument('--conf',
                    dest='conf',
                    action="store",
                    type=str,
                    help="generate configuration file")
parser.add_argument('--static',
                    dest='static',
                    action="store",
                    type=str,
                    default=None,
                    help="generate configuration file")
Beispiel #29
0
 def __init__(self):
     try:
         self.cli = Client(base_url='tcp://192.168.0.2:2376',
                           version='auto')
     except Exception as e:
         raise Exception(e)
Beispiel #30
0
def docker_is_available(context):
    base_docker_url = get_docker_host()
    docker_client = Client(base_url=base_docker_url)
    assert docker_client.ping()
    context.docker_client = docker_client