Python scripts monitor docker containers and pythondocker containers

Source: Internet
Author: User
Tags docker ps

Python scripts monitor docker containers and pythondocker containers

This article provides an example of how to monitor docker containers using python scripts for your reference. The details are as follows:

Script Function:

1. Monitor CPU usage

2. Monitor memory usage

3. Monitor network traffic

Code:

#! /Usr/bin/env python # -- * -- coding: UTF-8 -- * -- import sysimport tabimport reimport osimport timefrom docker import Clientimport commandskeys_container_stats_list = ['blkio _ stats ', 'precpu _ stats', 'network', 'read', 'memory _ stats', 'cpu _ stats'] merit_list = ['usage', 'limit ', 'mem _ use_percent ', 'total _ cpu_usage', 'System _ cpu_usage ', 'cpu _ usage_percent', 'rx _ bytes ', 'tx _ bytes '] returnval = Nonedef start (container_name): global container_stats conn = Client (base_url = 'unix: // run/docker. sock ', version = '1. 19 ') generator = conn. stats (container_name) try: container_stats = eval (generator. next () handle T NameError, error_msg: pass # print error_msg container_stats = eval (generator. next () finally: conn. close () def monitor_docker (monitor_item, merit): if merit = 'mem _ use_percent ': start (container_name) mem_usage = container_stats ['memory _ stats'] ['usage'] mem_limit = container_stats ['memory _ stats'] ['limit'] returnval = round (float (mem_usage) /float (mem_limit), 2) print returnval elif merit = 'System _ cpu_usage ': start (container_name) first_result = container_stats ['cpu _ stats'] ['System _ cpu_usage '] start (container_name) second_result = container_stats ['cpu _ stats'] ['System _ cpu_usage '] returnval = second_result-first_result print returnval elif merit = 'total _ cpu_usage': start (container_name) first_result = container_stats ['cpu _ stats'] ['cpu _ usage'] ['total _ usage'] start (container_name) second_result = container_stats ['cpu _ stats'] ['cpu _ usage'] ['total _ usage'] returnval = second_result-first_result print returnval elif merit = 'cpu _ usage_percent': start (container_name) system_use = container_stats ['cpu _ stats'] ['System _ cpu_usage'] total_use = container_stats ['cpu _ stats'] ['cpu _ usage'] ['total _ usage'] cpu_count = len (container_stats ['cpu _ stats'] ['cpu _ usage'] ['percpu _ usage']) returnval = round (float (total_use)/float (system_use) * cpu_count * 100.0, 2) print returnval elif merit = 'rx _ bytes ': command = '''docker exec-it api1 ifconfig eth1 | grep "bytes" | awk '{print $2}' | awk-F ': ''{print $2} ''' result_one = commands. getoutput (command) time. sleep (1) command = '''docker exec-it api1 ifconfig eth1 | grep "bytes" | awk '{print $2}' | awk-F ': ''{print $2} ''' result_second = commands. getoutput (command) returnval = round (int (result_second)-int (result_one)/1024,2) print returnval elif merit = 'tx _ bytes ': command = '''docker exec-it api1 ifconfig eth1 | grep "bytes" | awk '{print $6}' | awk-F ': ''{print $2} ''' result_one = commands. getoutput (command) time. sleep (1) command = '''docker exec-it api1 ifconfig eth1 | grep "bytes" | awk '{print $6}' | awk-F ': ''{print $2} ''' result_second = commands. getoutput (command) returnval = round (int (result_second)-int (result_one)/1024,2) print returnvalif _ name _ = '_ main __': command = '''docker ps | awk' {print $ NF} '| grep-v "NAMES" ''' str = commands. getoutput (command) container_counts_list = str. split ('\ n') if sys. argv [1] not in container_counts_list: print container_counts_list print "the container name you entered is incorrect. Please run the script again and enter the correct container name. "sys. exit (1) else: container_name = sys. argv [1] if sys. argv [2] not in keys_container_stats_list: print keys_container_stats_list print 'The Container Monitoring item you entered is not in the monitoring range. Please re-execute the script and enter the correct monitoring item. 'sys. exit (1) else: monitor_item = sys. argv [2] if sys. argv [3] not in merit_list: print merit_list print "the details of the container monitoring you entered are not within the monitoring range. Please re-execute the script and enter the correct detailed monitoring metrics. "else: merit = sys. argv [3] monitor_docker (monitor_item, merit)

 

The above is all the code of the python script to monitor the docker container. I hope it will be helpful for you to learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.