Last active
October 2, 2021 16:56
-
-
Save JhoLee/abd29d9f7fbdd3990e133828465ffbcc to your computer and use it in GitHub Desktop.
Inspect Docker containers' info to csv files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python3 | |
import csv | |
from datetime import datetime | |
from os import listdir, path, makedirs, system | |
import json | |
DATE = datetime.now().strftime("%y%m%d") | |
INPUT_JSON_DIR = path.join(".", "docker.inspect", DATE) | |
OUTPUT_CSV = path.join(INPUT_JSON_DIR, "output.csv") | |
if __name__ == "__main__": | |
if not path.exists(INPUT_JSON_DIR): | |
makedirs(INPUT_JSON_DIR) | |
system(f"for d in $(docker ps -a -q); do docker inspect $d > {INPUT_JSON_DIR}/$d.json; done") | |
with open(OUTPUT_CSV, 'w', ) as csv_f: | |
fieldnames = ["Id", "Name", "State.Status", "HostConfig.Privileged", "HostConfig.NetworkMode", | |
"Config.Env.NVIDIA_VISIBLE_DEVICES", | |
"State.Pid", "State.StartedAt", "Created", ] | |
writer = csv.DictWriter(csv_f, fieldnames=fieldnames) | |
writer.writeheader() | |
json_file_path_list = [ | |
path.join(INPUT_JSON_DIR, file_name) | |
for file_name in listdir(INPUT_JSON_DIR) if file_name[-5:] == ".json" | |
] | |
for json_file_path in json_file_path_list: | |
with open(json_file_path) as f : | |
raw_data = json.load(f) | |
if len(raw_data) > 0: | |
raw_data = raw_data[0] | |
data = { | |
"Id": raw_data["Id"], | |
"Name": raw_data["Name"], | |
"State.Status": raw_data["State"]["Status"], | |
"HostConfig.Privileged": raw_data["HostConfig"]["Privileged"], | |
"HostConfig.NetworkMode": raw_data["HostConfig"]["NetworkMode"], | |
"State.Pid": raw_data["State"]["Pid"], | |
"State.StartedAt": raw_data["State"]["StartedAt"], | |
"Created": raw_data["Created"], | |
} | |
envs = raw_data["Config"]["Env"] | |
data["Config.Env.NVIDIA_VISIBLE_DEVICES"] = "" | |
for env in envs: | |
if "NVIDIA_VISIBLE_DEVICES" in env: | |
data["Config.Env.NVIDIA_VISIBLE_DEVICES"] = env.replace("NVIDIA_VISIBLE_DEVICES=", "") | |
writer.writerow(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python3 | |
import csv | |
from datetime import datetime | |
from os import listdir, path, makedirs, system | |
import json | |
DATE = datetime.now().strftime("%y%m%d") | |
INPUT_JSON_DIR = path.join(".", "docker.inspect", DATE) | |
OUTPUT_CSV = path.join(INPUT_JSON_DIR, "output.without_k8s.csv") | |
if __name__ == "__main__": | |
if not path.exists(INPUT_JSON_DIR): | |
makedirs(INPUT_JSON_DIR) | |
system(f"for d in $(docker ps | grep -v k8s | awk '{print $1}'); do docker inspect $d > {INPUT_JSON_DIR}/$d.json; done") | |
with open(OUTPUT_CSV, 'w', ) as csv_f: | |
fieldnames = ["Id", "Name", "State.Status", "HostConfig.Privileged", "HostConfig.NetworkMode", | |
"Config.Env.NVIDIA_VISIBLE_DEVICES", | |
"State.Pid", "State.StartedAt", "Created", ] | |
writer = csv.DictWriter(csv_f, fieldnames=fieldnames) | |
writer.writeheader() | |
json_file_path_list = [ | |
path.join(INPUT_JSON_DIR, file_name) | |
for file_name in listdir(INPUT_JSON_DIR) if file_name[-5:] == ".json" | |
] | |
for json_file_path in json_file_path_list: | |
with open(json_file_path) as f : | |
raw_data = json.load(f) | |
if len(raw_data) > 0: | |
raw_data = raw_data[0] | |
data = { | |
"Id": raw_data["Id"], | |
"Name": raw_data["Name"], | |
"State.Status": raw_data["State"]["Status"], | |
"HostConfig.Privileged": raw_data["HostConfig"]["Privileged"], | |
"HostConfig.NetworkMode": raw_data["HostConfig"]["NetworkMode"], | |
"State.Pid": raw_data["State"]["Pid"], | |
"State.StartedAt": raw_data["State"]["StartedAt"], | |
"Created": raw_data["Created"], | |
} | |
envs = raw_data["Config"]["Env"] | |
data["Config.Env.NVIDIA_VISIBLE_DEVICES"] = "" | |
for env in envs: | |
if "NVIDIA_VISIBLE_DEVICES" in env: | |
data["Config.Env.NVIDIA_VISIBLE_DEVICES"] = env.replace("NVIDIA_VISIBLE_DEVICES=", "") | |
writer.writerow(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment