Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The idea of data Correction of Monitoring Command in Docker Container in linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the idea of correcting the monitoring command data in the Docker container in linux, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

Summary of ideas: write linux c code, generate the corresponding dynamic link library (so file), and hijack the access to / proc file system through LDPRELOAD. After hijacking, the correct data calculation logic in the container is implemented, and the corresponding / proc file is generated and placed in the container / tmp/proc directory. After hijacking, the data source of the corresponding command is obtained from / tmp/proc/*.

Following is an example of hijacking / proc/meminfo:

# write hijack_meminfo.c to hijack the open operation of / proc/meminfo, thereby hijacking the output of the free command.

# define _ GNU_SOURCE#include # include int open (const char * pathname, int flags) {typeof (open) * old_open; old_open = dlsym (RTLD_NEXT, "open"); / / printf ("in my hijack open!"); / / open char * s_meminfo = strstr (pathname, "/ proc/meminfo") of / hijack / proc/meminfo If (s! = NULL) {/ * TODO: call the container meminfo information collection function developed by yourself to generate meminfo to the container directory / tmp/proc/meminfo * / (* old_open) ("/ tmp/proc/meminfo", flags);} else {(* old_open) (pathname, flags);}}

# compile hijack_meminfo.c to generate dynamic link library libhijack_meminfo.so.

$gcc-shared-fPIC hijack_meminfo.c-o libhijack_meminfo.so-ldl

# configure the full path of libhijack_meminfo.so to the environment variable LD_PRELOAD, and the system will hijack the open function.

$export LD_PRELOAD=pwd/libhijack_meminfo.so

# for testing and observation, we will generate the file / tmp/proc/meminfo manually.

MemTotal: 999 kBMemFree: 999 kBMemAvailable: 999 kBBuffers: 999 kBSwapCached: 0 kBActive: 999 kBInactive: 999 kBActive (anon): 999 kBInactive (anon): 999 kBActive (file): 999 kBInactive (file): 999 kBUnevictable: 999 kBMlocked: 999 kBSwapTotal: 999 kBSwapFree : 999 kBDirty: 999 kBWriteback: 999 kBAnonPages: 999 kBMapped: 999 kBShmem: 999 kBSlab: 999 kBSReclaimable: 999 kBSUnreclaim: 999 kBKernelStack: 999 kBPageTables: 999 kBNFS_Unstable: 999 kBBounce: 999 kBWritebackTmp: 999 KBCommitLimit: 999 kBCommitted_AS: 999 kBVmallocTotal: 999 kBVmallocUsed: 999 kBVmallocChunk: 999 kBHardwareCorrupted: 999 kBAnonHugePages: 999 kBCmaTotal: 999 kBCmaFree: 999 kBHugePages_Total: 999HugePages_Free: 999HugePages_Rsvd: 999HugePages_Surp: 999Hugepagesize: 999 kBDirectMap4k: 999 kBDirectMap2M: 999 kB

# execute the free/top command to see whether the hijacking of / proc/meminfo is successful.

According to this idea, we analyze the commonly used data calculation methods of monitoring commands (free, top, iostat, vmstat, sar, df, uptime, etc.), hijack the related / proc files by open/fopen, and recalculate the monitoring data of these monitoring commands according to the corresponding cgroup fs data in the container.

Because some of our scenarios are fat containers, OPS needs to monitor docker containers like VM/PM, so we need to modify the monitoring commands in the container.

Here, I would like to recommend an open source project docker-monitor-injector of Ctrip, and we also refer to its ideas for development. Why not use it directly? Because there are not enough monitoring commands modified by docker-monitor-injector to meet our needs, we have carried out incremental development on this basis.

Thank you for reading this article carefully. I hope the article "ideas for correcting Monitoring Command data in the Docker Container in linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report