Skip to content

Commit 93a5f05

Browse files
committed
Added log utility
1 parent 9556e22 commit 93a5f05

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Simple Log Utility to fetch/copy Logs from CBCD components from deployed namespaces.
2+
Pre-Requisite:
3+
kubectl installed and configured.
4+
5+
6+
➜ logs git:(master) ✗ ./get-logs.sh -help
7+
8+
Usage:
9+
-n Namespace: The name of the kubernetes namespace/project where CBCD/SDA is deployed
10+
-c (Optional) Component: The name of component to fetch logs. defaults to all
11+
all, flow-server, flow-web, flow-devopsinsight, flow-bound-agent, flow-agent
12+
-o (Optional)Output path to copy logs. default is /tmp.
13+
-k (Optional)Kubeconfig file path to connect to k8s cluster.
14+
15+
Examples:
16+
./get-logs.sh -n flow-demo
17+
./get-logs.sh -n flow-demo -c all -o /tmp/flow-logs
18+
./get-logs.sh -n flow-demo -c flow-server -o /tmp/flow-logs
19+
./get-logs.sh -n flow-demo -c flow-server -k /home/foo/kubeconfigfile
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#! /bin/bash
2+
3+
4+
kctl() {
5+
if [ ! -z $KUBE_CONFIG_PATH ] ; then
6+
kubectl --kubeconfig $KUBE_CONFIG_PATH -n $NAMESPACE "$@"
7+
else
8+
kubectl -n $NAMESPACE "$@"
9+
fi
10+
}
11+
12+
function get_logs
13+
{
14+
C_FETCH="flow"
15+
if [ $COMPONENT == "all" ] ; then
16+
C_FETCH="flow"
17+
else
18+
C_FETCH=$COMPONENT
19+
fi
20+
for i in $(kctl get po|awk '{print $1}'|awk 'NR!=1'|grep $C_FETCH)
21+
do
22+
echo "Fetching logs for $i"
23+
echo "Creating Directory $OUTPUT/$i/logs"
24+
mkdir -p $OUTPUT/$i/logs
25+
echo "Copying logs from $i to $OUTPUT/$i/logs"
26+
kctl cp $i:/opt/cbflow/logs $OUTPUT/$i/logs
27+
done
28+
29+
}
30+
function initScript
31+
{
32+
NAMESPACE=""
33+
COMPONENT="all"
34+
OUTPUT="/tmp"
35+
KUBE_CONFIG_PATH=""
36+
while getopts h:n:c:o:k: opt
37+
do
38+
case "$opt" in
39+
h) usage "";exit 1;;
40+
n) NAMESPACE=$OPTARG;;
41+
c) COMPONENT=$OPTARG;;
42+
o) OUTPUT=$OPTARG;;
43+
k) KUBE_CONFIG_PATH=$OPTARG;;
44+
\?) usage "";exit 1;;
45+
esac
46+
done
47+
if [ -z $NAMESPACE ] ;
48+
then
49+
echo "$(date) Make sure you provide valid -n NAMESPACE"
50+
usage ""
51+
exit 1;
52+
fi
53+
54+
}
55+
56+
function usage
57+
{
58+
cat <<EOF
59+
60+
Usage:
61+
-n Namespace: The name of the kubernetes namespace/project where CBCD/SDA is deployed
62+
-c (Optional) Component: The name of component to fetch logs. defaults to all
63+
all, flow-server, flow-web, flow-devopsinsight, flow-bound-agent, flow-agent
64+
-o (Optional)Output path to copy logs. default is /tmp.
65+
-k (Optional)Kubeconfig file path to connect to k8s cluster.
66+
67+
Examples:
68+
./get-logs.sh -n flow-demo
69+
./get-logs.sh -n flow-demo -c all -o /tmp/flow-logs
70+
./get-logs.sh -n flow-demo -c flow-server -o /tmp/flow-logs
71+
./get-logs.sh -n flow-demo -c flow-server -k /home/foo/kubeconfigfile
72+
EOF
73+
}
74+
75+
function main
76+
{
77+
initScript "$@"
78+
kctl get ns $NAMESPACE
79+
exit_code=$?
80+
if [ $exit_code != 0 ];
81+
then
82+
echo "$(date) Please set valid namespace "
83+
echo "$(date) Please check kubectl configuration is set correctly. Logging utility not able to reach to k8s cluster"
84+
echo "$(date) Please set valid kubeconfig file if exist "
85+
exit 1;
86+
fi
87+
get_logs "$@"
88+
}
89+
90+
main "$@"

0 commit comments

Comments
 (0)