Re: Container Uptime Discussion
"Craig Small" <[email protected]> ("csmall") Thu, 8 Feb 2024 16:51:29 +1100
| Newsgroups | gmane.linux.procps.devel |
|---|---|
| Message-ID | <CALy8Cw7m8kqD+AKx8-jN0N596gK1n4cX4pwOTm3BbsQw0h5big@mail.gmail.com> |
--000000000000d53dc70610d86781 Content-Type: text/plain; charset="UTF-8" On Tue, 30 Jan 2024 at 21:36, Dr. Werner Fink <[email protected]> wrote: > ps o etimes= 1 > > to get the uptime of the container in seconds. I have effectively written procps_container_uptime() to do that. Interesting would be if > the uptime command would accept an further option to specify the namespace > pid of the container to do this from outside aka the host system. This > Strangely enough its not the pid namespace that matters. You (and I originally) would think ok this is a program about PIDS, pids namespace is what I need. But actually what you want is the mount namespace to get to "their" /proc. "nsenter -t <docker_bash_pid> -p ps ax" shows all the host processes, because that's what is in /proc Anyway, it seems to work. Starting in the top directory of procps and making sure the container glibc is the same, or close enough to the host's glibc version. $ docker run -it --mount type=bind,source="$(pwd)",target=/mnt debian:latest root@c00eb009173d:/# LD_PRELOAD=/mnt/library/.libs/libproc2.so /mnt/src/.libs/uptime 22:19:37 up 5 days, 17:04, 0 users, load average: 0.30, 0.28, 0.21 root@c00eb009173d:/# LD_PRELOAD=/mnt/library/.libs/libproc2.so /mnt/src/.libs/uptime --container 22:19:41 up 0 min, 0 users, load average: 0.28, 0.27, 0.21 (wait 2 minutes) root@c00eb009173d:/# LD_PRELOAD=/mnt/library/.libs/libproc2.so /mnt/src/.libs/uptime --container 22:21:29 up 2 min, 0 users, load average: 0.54, 0.38, 0.26 Both w and uptime have this change. I haven't attempted to do any comparison between variables and uptime (e.g. vmstat's initial set of numbers) because there doesn't seem to be a clear way of doing it. But at least now you can work out from inside the container how long its been running. - Craig --000000000000d53dc70610d86781 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr">On Tue, 30 Jan 2024 at 21:36, Dr. Werner = Fink <<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]= </a>> wrote:</div><div class=3D"gmail_quote"><blockquote class=3D"gmail_= quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,= 204);padding-left:1ex">=C2=A0 ps o etimes=3D 1<br> <br> to get the uptime of the container in seconds.=C2=A0 </blockquote><div>=C2= =A0I have effectively written procps_container_uptime() to do that.</div><d= iv><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px = 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Interesting = would be if <br></blockquote><blockquote class=3D"gmail_quote" style=3D"mar= gin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1= ex"> the uptime command would accept an further option to specify the namespace<= br> pid of the container to do this from outside aka the host system.=C2=A0 Thi= s<br></blockquote><div>Strangely enough its not the pid namespace that matt= ers. You (and I originally) would think ok this</div><div>is a program abou= t PIDS, pids namespace is what I need. But actually what you want is the mo= unt</div><div>namespace to get to "their" /proc.</div><div><br></= div><div>"nsenter -t <docker_bash_pid>=C2=A0 -p ps ax" show= s all the host processes, because that's what is in /proc</div><div><br= ></div><div>Anyway, it seems to work.</div><div>Starting in the top directo= ry of procps and making sure the container glibc is the same, or close enou= gh to the host's glibc version.<br></div><div>$ docker run -it --mount = type=3Dbind,source=3D"$(pwd)",target=3D/mnt debian:latest<br>root= @c00eb009173d:/# LD_PRELOAD=3D/mnt/library/.libs/libproc2.so /mnt/src/.libs= /uptime<br>=C2=A022:19:37 up 5 days, 17:04, =C2=A00 users, =C2=A0load avera= ge: 0.30, 0.28, 0.21<br>root@c00eb009173d:/# LD_PRELOAD=3D/mnt/library/.lib= s/libproc2.so /mnt/src/.libs/uptime --container<br>=C2=A022:19:41 up 0 min,= =C2=A00 users, =C2=A0load average: 0.28, 0.27, 0.21</div><div>(wait 2 minu= tes)</div><div>root@c00eb009173d:/# LD_PRELOAD=3D/mnt/library/.libs/libproc= 2.so /mnt/src/.libs/uptime --container<br>=C2=A022:21:29 up 2 min, =C2=A00 = users, =C2=A0load average: 0.54, 0.38, 0.26</div><div><br></div><div>Both w= and uptime have this change. I haven't attempted to do any comparison = between variables and uptime</div><div>(e.g. vmstat's initial set of nu= mbers) because there doesn't seem to be a clear way of doing it.</div><= div><br></div><div>But at least now you can work out from inside the contai= ner how long its been running.<br></div><div>=C2=A0- Craig</div><div><br></= div></div></div> --000000000000d53dc70610d86781--