Skip to content

Commit

Permalink
container status function
Browse files Browse the repository at this point in the history
  • Loading branch information
usmannasir committed Dec 21, 2023
1 parent ab6af43 commit 7baa302
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions plogical/DockerSites.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,35 @@ def ContainerLogs(self):
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)

### pass container id and number of lines to fetch from logs
def ContainerInfo(self):
try:
import docker
# Create a Docker client
client = docker.from_env()

# Get the container by ID
container = client.containers.get(self.data['containerID'])

# Fetch container stats
stats = container.stats(stream=False)

dic = {
'id': container.short_id,
'name': container.name,
'status': container.status,
'volumes': container.attrs['HostConfig']['Binds'] if 'HostConfig' in container.attrs else [],
'logs_50': container.logs(tail=50).decode('utf-8'),
'ports': container.attrs['HostConfig']['PortBindings'] if 'HostConfig' in container.attrs else {},
'memory': stats['memory_stats']['usage'],
'cpu' : stats['cpu_stats']['cpu_usage']['total_usage']
}

return 1, dic
except BaseException as msg:
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)


def Main():
try:
Expand Down

0 comments on commit 7baa302

Please sign in to comment.