Skip to content

Commit

Permalink
Add dump logs script
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Sep 22, 2023
1 parent ab86447 commit 8ec4542
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions utils/dump_logs/dump_irodscsidriver_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#! /usr/bin/env python3


### ======================================================================= ###
### A command-line tool to dump irods csi driver logs ###
### Uses: ./dump_irodscsidriver_logs.py ###
### ======================================================================= ###

import os, sys
import argparse
import subprocess
from datetime import datetime

try:
from subprocess import DEVNULL # py3k
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')

def get_kube_controller_pods(kubeconf=""):
kubepods = []
kubecommand = "kubectl get pods -n irods-csi-driver --no-headers --ignore-not-found -l app.kubernetes.io/instance=irods-csi-driver-controller" + kubeconf

pipe = os.popen(kubecommand)
for line in pipe:
fields = line.strip().split()
if len(fields) < 5:
continue

podname = fields[0].strip()
status = fields[2].strip()

if status == "Running":
kubepods.append(podname)

return kubepods

def get_kube_node_pods(kubeconf=""):
kubepods = []
kubecommand = "kubectl get pods -n irods-csi-driver --no-headers --ignore-not-found -l app.kubernetes.io/instance=irods-csi-driver-node" + kubeconf

pipe = os.popen(kubecommand)
for line in pipe:
fields = line.strip().split()
if len(fields) < 5:
continue

podname = fields[0].strip()
status = fields[2].strip()

if status == "Running":
kubepods.append(podname)

return kubepods

def dump_controllers(kubeconf, dumpdir):
pods = get_kube_controller_pods(kubeconf)

for pod in pods:
for container in ["irods-plugin", "csi-provisioner"]:
container_flag = " -c " + container

log_filename = dumpdir + "/" + pod + "_" + container + ".log"
with open(log_filename, "w") as logfile:
kubecommand = "kubectl logs -n irods-csi-driver" + container_flag + kubeconf + " " + pod
p = subprocess.Popen(kubecommand, shell=True, universal_newlines=True, stdout=logfile)
p.wait()

def dump_nodes(kubeconf, dumpdir):
pods = get_kube_node_pods(kubeconf)

for pod in pods:
for container in ["irods-plugin", "csi-driver-registrar", "irods-pool"]:
container_flag = " -c " + container

log_filename = dumpdir + "/" + pod + "_" + container + ".log"
with open(log_filename, "w") as logfile:
kubecommand = "kubectl logs -n irods-csi-driver" + container_flag + kubeconf + " " + pod
p = subprocess.Popen(kubecommand, shell=True, universal_newlines=True, stdout=logfile)
p.wait()

# dump irodsfs /storage/irodsfs
irodsfs_dumpdir = dumpdir + "/" + pod
if not os.path.exists(irodsfs_dumpdir):
os.makedirs(irodsfs_dumpdir)

kubecommand = "kubectl cp -c irods-plugin" + kubeconf + " irods-csi-driver/" + pod + ":/storage/irodsfs " + irodsfs_dumpdir + "/"
p = subprocess.Popen(kubecommand, shell=True, universal_newlines=True, stdout=DEVNULL, stderr=DEVNULL)
p.wait()


parser = argparse.ArgumentParser()
parser.add_argument("--kubeconfig", dest="kubeconfig", type=str, help="kubernetes configuration filepath")
parser.add_argument("-o", dest="output", type=str, help="dump output directory")

args = parser.parse_args()

kubeconf = ""
if args.kubeconfig:
kubeconf = " --kubeconfig=" + args.kubeconfig

now = datetime.now()
dumpdir = now.strftime("irodscsi_log_%Y%m%d_%H%M%S")

if args.output:
dumpdir = args.output

if not os.path.exists(dumpdir):
os.makedirs(dumpdir)

dump_controllers(kubeconf, dumpdir)
dump_nodes(kubeconf, dumpdir)
File renamed without changes.
File renamed without changes.

0 comments on commit 8ec4542

Please sign in to comment.