The example of this article is to share the Python script monitoring Docker container method, for your reference, the specific content is as follows
Scripting Features:
1. Monitor CPU Usage
2. Monitor Memory usage Status
3. Monitor network traffic
Specific code:
#!/usr/bin/env python#--*--coding:utf-8--*--import sysimport tabimport reimport osimport timefrom Docker import Clienti Mport 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.clo Se () def monitor_docker (Monitor_item,merit): if merit = = ' Mem_use_percent ': Start (container_name) Mem_usage = Contai ner_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_perce NT ': 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_byte S ': command= ' Docker exec-itApi1 ifconfig eth1 | grep "bytes" | awk ' {print $} ' | Awk-f ': ' {print $} ' Result_one = commands.getoutput (command) time.sleep (1) command= ' Docker exec-it API 1 Ifconfig eth1 | grep "bytes" | awk ' {print $} ' | Awk-f ': ' {print $} ' 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 $} ' Result_one = commands.getoutput (command) time.sleep (1) command= ' Docker exec-it API 1 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_c Ontainer_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 entry above. ' 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 have entered are not in the scope of monitoring, please re-execute the script and enter the correct detailed monitoring metrics above." Else:merit = sys.argv[3] Monitor_docker (Monitor_item,merit)
The above is the Python script to monitor the Docker container all the code, I hope that everyone's learning is helpful.