Python Script monitoring Docker container

Source: Internet
Author: User
Tags python script

Scripting Features:

    1. Monitor CPU Usage

    2. Monitor Memory usage

    3. Monitor network traffic

#!/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 ())     except 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)    &NBsp;    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  $ '   '         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 wrong, please re-execute the script and enter the correct container name above."         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  ' YouThe input container monitoring item is not in the monitoring range, please re-execute the script and enter the correct monitoring item above. '             sys.exit (1)          else:            monitor_ item = sys.argv[2]            if  sys.argv[3] not in merit_list:                 print merit_list                 print  "The detail of the container monitoring details you entered is not in the range of monitoring, please re-execute the script and enter the correct detailed monitoring metrics."             else:                 merit = sys.argv[3]                 monitor_docker (monitor_ ItemMerit 



This article from the "Do not ask for the best, only better" blog, please be sure to keep this source http://yujianglei.blog.51cto.com/7215578/1767908

Python Script monitoring Docker container

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.