如何使用Shell脚本计算docker容器百分比的cpu,内存和硬盘使用率。
以下是在特定容器上使用此命令的输出
curl -s --unix-socket /var/run/docker.sock -X GET http://v1.24/containers/myNginx/stats | jq。
{
"read": "2020-09-08T17:57:08.612559386Z",
"preread": "0001-01-01T00:00:00Z",
"pids_stats": {
"current": 2
},
"blkio_stats": {
"io_service_bytes_recursive": [
{
"major": 8,
"minor": 0,
"op": "Read",
"value": 17207296
},
{
"major": 8,
"minor": 0,
"op": "Write",
"value": 8192
},
{
"major": 8,
"minor": 0,
"op": "Sync",
"value": 17211392
},
{
"major": 8,
"minor": 0,
"op": "Async",
"value": 4096
},
{
"major": 8,
"minor": 0,
"op": "Discard",
"value": 0
},
{
"major": 8,
"minor": 0,
"op": "Total",
"value": 17215488
}
],
"io_serviced_recursive": [
{
"major": 8,
"minor": 0,
"op": "Read",
"value": 584
},
{
"major": 8,
"minor": 0,
"op": "Write",
"value": 2
},
{
"major": 8,
"minor": 0,
"op": "Sync",
"value": 585
},
{
"major": 8,
"minor": 0,
"op": "Async",
"value": 1
},
{
"major": 8,
"minor": 0,
"op": "Discard",
"value": 0
},
{
"major": 8,
"minor": 0,
"op": "Total",
"value": 586
}
],
"io_queue_recursive": [],
"io_service_time_recursive": [],
"io_wait_time_recursive": [],
"io_merged_recursive": [],
"io_time_recursive": [],
"sectors_recursive": []
},
"num_procs": 0,
"storage_stats": {},
"cpu_stats": {
"cpu_usage": {
"total_usage": 139411120,
"percpu_usage": [
57494949,
42111399,
10313912,
29490860
],
"usage_in_kernelmode": 100000000,
"usage_in_usermode": 50000000
},
"system_cpu_usage": 649165800000000,
"online_cpus": 4,
"throttling_data": {
"periods": 0,
"throttled_periods": 0,
"throttled_time": 0
}
},
"precpu_stats": {
"cpu_usage": {
"total_usage": 0,
"usage_in_kernelmode": 0,
"usage_in_usermode": 0
},
"throttling_data": {
"periods": 0,
"throttled_periods": 0,
"throttled_time": 0
}
},
"memory_stats": {
"usage": 3731456,
"max_usage": 14323712,
"stats": {
"active_anon": 114688,
"active_file": 499712,
"cache": 2445312,
"dirty": 0,
"hierarchical_memory_limit": 9223372036854772000,
"hierarchical_memsw_limit": 0,
"inactive_anon": 159744,
"inactive_file": 2367488,
"mapped_file": 1622016,
"pgfault": 4323,
"pgmajfault": 33,
"pgpgin": 6105,
"pgpgout": 5404,
"rss": 327680,
"rss_huge": 0,
"total_active_anon": 77824,
"total_active_file": 0,
"total_cache": 0,
"total_dirty": 0,
"total_inactive_anon": 73728,
"total_inactive_file": 0,
"total_mapped_file": 0,
"total_pgfault": 4689,
"total_pgmajfault": 294,
"total_pgpgin": 6417,
"total_pgpgout": 6523,
"total_rss": 0,
"total_rss_huge": 0,
"total_unevictable": 0,
"total_writeback": 319488,
"unevictable": 0,
"writeback": 0
},
"limit": 8265834496
},
"name": "/myNginx",
"id": "3c54b9e5991dd57c4e298c95b7201f322d813aab701acf2ed3d5585ff9169200",
"networks": {
"eth0": {
"rx_bytes": 1480757,
"rx_packets": 8331,
"rx_errors": 0,
"rx_dropped": 0,
"tx_bytes": 0,
"tx_packets": 0,
"tx_errors": 0,
"tx_dropped": 0
}
}
}
containerIds = $(sudo docker ps --filter“ status = running” | awk'NR> 1 {print $ 1}')
回显“ $ containerIds”
致谢