Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psutil inside docker container reports host stats #1011

Open
avaranovich opened this issue Apr 13, 2017 · 18 comments
Open

psutil inside docker container reports host stats #1011

avaranovich opened this issue Apr 13, 2017 · 18 comments
Labels
docker linux vm any container (e.g. docker) or virtual OS (e.g. VMWare)

Comments

@avaranovich
Copy link

If I use psutil inside a docker container, it reports the host machine stats, i.e. CPU and RAM.
I would expect it to grab a container-level statistic.

@giampaolo
Copy link
Owner

giampaolo commented Apr 13, 2017

You can change /proc filesystem location like this:

import psutil
psutil.PROCFS_PATH = "/something_else"

See:

Please note that most information on Linux is retrieved via /proc but not all of it (e.g. swap_memory, net_if_addrs, net_if_stats, disk_partitions and others) so the PROCFS_PATH trick does not cover all psutil API.

@giampaolo
Copy link
Owner

Update: swap_memory() now relies on /proc, see #1015.

@crazyglasses
Copy link

psutil.PROCFS_PATH = "/something_else"

hey, for my container, what would be the '/something_else' ?

@giampaolo
Copy link
Owner

giampaolo commented Jun 8, 2017

I don't know. I don't have a Docker instance to check against. Try to google for "docker proc filesystem location" to see where it is located.

@keitwb
Copy link

keitwb commented Jun 8, 2017

@crazyglasses You will have to mount the root mount namespace's /proc dir into your container with the -v flag to docker run. E.g. docker run -v /proc:/host-proc ... and then configure PROCFS_PATH to point to that.

@crazyglasses
Copy link

@keitwb Basically what I want to do is run psutil inside a python script inside a container. A few comments that I have gotten is that by default the psutil is pointed towards the containers /proc and that I would get metrics of the container if I use it that way . What you are suggesting seems to be mounting the root inside the container, doesnt that mean I get the host stats only?

@keitwb
Copy link

keitwb commented Jun 9, 2017

@crazyglasses Sorry, misread your question. For docker containers, there isn't a hard distinction between CPU/mem inside or outside the container like there would be on a VM -- all containers share the same kernel and there isn't a namespace for cpus or memory that causes the container to see those differently. There are cgroups, which you can use to limit resource usage of the containers (e.g. certain cores or period/quota of CPU), but that would just show as the processes in the container using a more limited amount of resources, not change what psutil would report as far as I understand it. I know docker stats will give you what it sounds like you want -- I think it relies on cgroup stats somewhere in the proc filesystem, but I'm not sure if psutil can pick up those or not.

@halegreen
Copy link

So psutil run in container collects host stats or container stats ?

@giampaolo
Copy link
Owner

giampaolo commented Jul 22, 2019

Container. (EDIT: host)

@halegreen
Copy link

@giampaolo So how can I collect host stats ? thanks

@giampaolo
Copy link
Owner

giampaolo commented Jul 22, 2019

I don’t know enough about containers but I guess you can mount host’s /proc fs so that it’s visibile from within the container. With that in place you can:

‘’’
import psutil
psutil.PROCFS_PATH = “/path/to/host/procfs”
‘’’

From then on most psutil’s API will return stats about the host. All of this is based on the assumption that host and container’s /proc filesystems are independent, of which I am not completely sure. If they are then querying /proc for things such as system wide stats (CPU, memory, ...) is safe, but querying processes is not.

@halegreen
Copy link

OK, thanks alot.

@jankatins
Copy link

All of this is based on the assumption that host and container’s /proc filesystems are independent, of which I am not completely sure

It seems that this is not correct: This blog posts shows that /proc/meminfo shows the information from the host and not the container: https://ops.tips/blog/why-top-inside-container-wrong-memory/ You can get the memory via the proposed way in #490

@ronytesler
Copy link

I get different results when running on host and inside a docker container. How can I get the host's stats when running inside a container?

@gb160
Copy link

gb160 commented Nov 14, 2023

I don’t know enough about containers but I guess you can mount host’s /proc fs so that it’s visibile from within the container. With that in place you can:

‘’’ import psutil psutil.PROCFS_PATH = “/path/to/host/procfs” ‘’’

From then on most psutil’s API will return stats about the host. All of this is based on the assumption that host and container’s /proc filesystems are independent, of which I am not completely sure. If they are then querying /proc for things such as system wide stats (CPU, memory, ...) is safe, but querying processes is not.

apologies for resurrecting this, but can I use the proc trick to use psutil.net_io_counters() and get the host machines bytes_sent/bytes_recievd ?

@giampaolo
Copy link
Owner

Yes, you should because psutil.net_io_counters() retrieves its data from /proc/net/dev.

@gb160
Copy link

gb160 commented Nov 15, 2023

Yes, you should because psutil.net_io_counters() retrieves its data from /proc/net/dev.

Yeah I thought the same but it doesn't work, psutil.net_io_counters() only returns the data from the container. There is something weird going on that's above my paygrade.

The only way I could get it to work was to start the container using '--net=host', which gets psutil.net_io_counters() retrieving the host network info, but that isn't ideal.

@giampaolo
Copy link
Owner

Also see discussion at #2100.

@giampaolo giampaolo added the vm any container (e.g. docker) or virtual OS (e.g. VMWare) label Nov 17, 2024
@giampaolo giampaolo reopened this Nov 17, 2024
@giampaolo giampaolo added linux and removed doc labels Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker linux vm any container (e.g. docker) or virtual OS (e.g. VMWare)
Projects
None yet
Development

No branches or pull requests

8 participants