Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. import docker
  2. import re
  3. import pprint
  4. import datetime,time
  5. from urllib.request import urlopen, Request, HTTPError, URLError
  6. import json
  7.  
  8. #instantiate a client, and connect to the main manager in the swarm
  9. #Since I am connecting to a remote host, I am using the below.
  10. client = docker.DockerClient(base_url='tcp://192.168.5.114:2375')
  11.  
  12. #But if you intend to run this on the main manager, uncomment the below line
  13. #client = docker.from_env()
  14.  
  15. node = client.api.nodes() #docker node ls - only on swarm
  16.  
  17. #get current time in seconds since the Epoch
  18. unix_seconds = time.mktime(datetime.datetime.now().timetuple())
  19.  
  20. def calculateCPUPercentUnix(previousCPU, previousSystem,v):
  21. cpuPercent = 0.0
  22. #calculate the change for the cpu usage of the container in between readings
  23. cpuDelta = v['cpu_stats']['cpu_usage']['total_usage'] - previousCPU
  24. #calculate the change for the entire system between readings
  25. systemDelta = v['cpu_stats']['system_cpu_usage'] - previousSystem
  26. if systemDelta > 0.0 and cpuDelta > 0.0:
  27. cpuPercent = (cpuDelta / systemDelta) * len(v['cpu_stats']['cpu_usage']['percpu_usage']) * 100
  28. return cpuPercent
  29. else:
  30. return cpuPercent
  31.  
  32. def systemDf(cli):
  33. size = []
  34. df = cli.api.df()['Containers'] #docker system df -v and get only container info
  35. for df_len in range(len(df)):
  36. if df[df_len]['State'] != 'exited':
  37. iD = df[df_len]['Id'][:10]
  38. size.append({iD+'-Image':re.sub(r'(.*)(@.*)',r'\1',df[df_len]['Image']),
  39. iD+'-Size':(df[df_len].get('SizeRw', 0)/1000000),
  40. iD+'-VirtualSize':(df[df_len].get('SizeRootFs', 0)/1000000000),
  41. iD+'-Status':df[df_len]['Status'],
  42. iD+'-Created':str(int((unix_seconds-df[df_len]['Created'])/(24*60*60)))+' Days ago'
  43. if int((unix_seconds-df[df_len]['Created'])/(24*60*60)) <= 10 else str(int((unix_seconds-df[df_len]['Created'])/(7*24*60*60)))+' Weeks Ago',
  44. iD+'-State':df[df_len]['State']
  45. })
  46. return size
  47.  
  48. def containerMetrics(cli):
  49. mem = []
  50. container_node = cli.api.containers() #docker ps
  51. cont = [container_node[i]['Id'][:10] for i in range(len(container_node))]
  52. for cont_id in range(len(cont)):
  53. #Get container stats
  54. stats = cli.api.stats(cont[cont_id],stream=False)
  55. name = re.sub(r'(\W*)(\w*)(\..*)?',r'\2',str(stats['name']))
  56. previousCPU = stats['precpu_stats']['cpu_usage']['total_usage']
  57. previousSystem = stats['precpu_stats']['system_cpu_usage']
  58. cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem,stats)
  59. service = cli.api.inspect_container(cont[cont_id])['Config']['Labels']
  60. mem.append({cont[cont_id]+'-ContainerName':name,cont[cont_id]+'-ContainerId':cont[cont_id],
  61. cont[cont_id]+'-MemUsage':(stats['memory_stats']['usage']-stats['memory_stats']['stats']['cache'])/1048576, #Convert to MiB
  62. cont[cont_id]+'-MaxMem':stats['memory_stats']['limit']/1073741824,cont[cont_id]+'-CpuPercent':cpuPercent,
  63. cont[cont_id]+'-ContainerNode':node_ip,cont[cont_id]+'-ServiceName':service['com.docker.swarm.service.name']
  64. if 'com.docker.swarm.service.name' in service else 'None',cont[cont_id]+'-ServiceID':service['com.docker.swarm.service.id'][:10]
  65. if 'com.docker.swarm.service.id' in service else 'None' })
  66. return mem
  67.  
  68. def imageInfo(cli):
  69. image_info = []
  70. #Get image info, when you do a docker system df -v, Containers per image/created/size/unique size/name of each image
  71. image = cli.api.df()['Images'] #docker system df -v and get only image info
  72. for i in range(len(image)):
  73. image_iD = re.sub(r'(.*:)(.*)',r"\2",image[i]['Id'])[:10]
  74. image_info.append({image_iD+'-Name':str(image[i]['RepoTags']).strip("['']"),
  75. image_iD+'-Containers':image[i]['Containers'],
  76. image_iD+'-Size':image[i]['Size']/1000000,
  77. #as per docker docs UNIQUE SIZE is the amount of space that is only used by a given image
  78. #UNIQUE SIZE = SIZE - SHARED SIZE
  79. image_iD+'-UniqueSize':(image[i]['Size']-image[i]['SharedSize'])/1000000,
  80. image_iD+'-Created':str(int((unix_seconds-image[i]['Created'])/(24*60*60)))+' Days ago'
  81. if int((unix_seconds-image[i]['Created'])/(24*60*60)) <= 10 else str(int((unix_seconds-image[i]['Created'])/(7*24*60*60)))+' Weeks Ago',
  82. image_iD+'-ImageNode':node_ip
  83. })
  84. return image_info
  85.  
  86. #get the system info
  87. NodeInfo = []
  88. SystemDF = []
  89. ContainerMetrics = []
  90. ImageInfo = []
  91.  
  92.  
  93. for node_index in range(len(node)):
  94. node_ip = node[node_index]['Status']['Addr']
  95. #connect to all nodes in the swarm and pull info
  96. cli = docker.DockerClient(base_url='tcp://'+node_ip+':2375')
  97. #docker info
  98. sys = cli.api.info()
  99. node_addr=sys['Swarm']['NodeAddr']
  100. #NodeInfo = []
  101. NodeInfo.append({node_addr+'-NodeName':sys['Name'],node_addr+'-NodeID':sys['Swarm']['NodeID'][:10],
  102. node_addr+'-Containers':sys['Containers'],node_addr+'-ContainersRunning':sys['ContainersRunning'],
  103. node_addr+'-Images':sys['Images'],
  104. node_addr+'-OperatingSystem':sys['OperatingSystem'],
  105. node_addr+'-State':str(node[node_index]['Status']['State']).capitalize(),
  106. node_addr+'-Role':str(node[node_index]['Spec']['Role']).capitalize(),
  107. node_addr+'-Availability':str(node[node_index]['Spec']['Availability']).capitalize()})
  108. #If node is a manager add the following details
  109. if node[node_index]['Spec']['Role'] == 'manager':
  110. NodeInfo[node_index][node_addr+'-Managers'] = sys['Swarm']['Managers']
  111. NodeInfo[node_index][node_addr+'-Nodes'] = sys['Swarm']['Nodes']
  112.  
  113. ContainerMetrics.extend(containerMetrics(cli))
  114. SystemDF.extend(systemDf(cli))
  115.  
  116. # ** is called dictionary unpacking.
  117. #It splits the contents of the dictionary into its respective key-value pairs and allows you to merge them into one dictionary.
  118. ContainerMetrics = [{**a, **b} for a, b in zip(ContainerMetrics,SystemDF)]
  119.  
  120. ImageInfo.extend(imageInfo(cli))
  121.  
  122.  
  123. '''
  124. pprint.pprint(ContainerMetrics)
  125. print('\n')
  126. pprint.pprint(NodeInfo)
  127. print('\n')
  128. pprint.pprint(ImageInfo)
  129. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement