forked from netskopeoss/ta_cloud_exchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnose
executable file
·178 lines (152 loc) · 4.92 KB
/
diagnose
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
SUBJECT=some-unique-id
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$SUBJECT.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# --- Output functions --------------------------------------------
out_docker_core_logs() {
echo "----- Collecting Core Logs -----" > /dev/tty
OUT="core.log"
docker-compose logs --no-color core > $OUT
echo $OUT
}
out_docker_ui_logs() {
echo "----- Collecting UI Logs -----" > /dev/tty
OUT="ui.log"
docker-compose logs --no-color ui > $OUT
echo $OUT
}
out_docker_mongo_logs() {
echo "----- Collecting Mongo Logs -----" > /dev/tty
OUT="mongo.log"
docker-compose logs --no-color mongodb-primary > $OUT
echo $OUT
}
out_docker_rabbitmq_logs() {
echo "----- Collecting RabbitMq Logs -----" > /dev/tty
OUT="rabbitmq.log"
docker-compose logs --no-color rabbitmq-stats > $OUT
echo $OUT
}
out_stats() {
echo "----- Collecting Machine Stats -----" > /dev/tty
OUT="system_stats.txt"
echo "> lscpu" > $OUT
lscpu >> $OUT
echo "----------------------------------------------------------------------------------" >> $OUT
echo "> free -h" >> $OUT
free -h >> $OUT
echo "----------------------------------------------------------------------------------" >> $OUT
echo "> df -h" >> $OUT
df -h >> $OUT
echo $OUT
}
out_docker_stats() {
echo "----- Collecting Docker Stats -----" > /dev/tty
OUT="docker_stats.txt"
echo "> docker ps --all" > $OUT
docker ps --all >> $OUT
echo "---------------------------------------------------------------------------------" >> $OUT
echo "> docker system info" >> $OUT
docker system info >> $OUT
echo "---------------------------------------------------------------------------------" >> $OUT
echo "> docker-compose version" >> $OUT
docker-compose version >> $OUT
echo "---------------------------------------------------------------------------------" >> $OUT
echo "> docker system df" >> $OUT
docker system df >> $OUT
echo $OUT
}
out_versions() {
echo "----- Collecting CE Version -----" > /dev/tty
OUT="ce_platform_versions.txt"
if [ -z "$TOKEN" ];
then
echo "==> Error while collecting CE Version. Check hostname, username, or password." > /dev/tty
echo "Error while collecting CE Version. Check hostname, username, or password." > $OUT
else
SETTINGS_RESPONSE=`curl -s -X 'GET' \
"$HOST/api/settings" \
-H 'accept: application/json' \
-H "Authorization: Bearer $TOKEN"`
CORE_VERSION=`echo $SETTINGS_RESPONSE | grep -Po '"version":(\W+)?"\K[a-zA-Z0-9._-]+(?=")'`
echo "> Core-Version: $CORE_VERSION" > $OUT
DB_VERSION=`echo $SETTINGS_RESPONSE | grep -Po '"databaseVersion":(\W+)?"\K[a-zA-Z0-9._-]+(?=")'`
echo "> DB-Version: $DB_VERSION" >> $OUT
fi
echo $OUT
}
out_ce_logs() {
echo "----- Collecting CE Logs -----" > /dev/tty
OUT="ce_platform_logs.log"
if [ -z "$TOKEN" ];
then
echo "==> Error while collecting CE Logs. Check hostname, username, or password." > /dev/tty
echo "Error while collecting CE Logs. Check hostname, username, or password." > $OUT
else
curl -s -X 'GET' \
"$HOST/api/logs/?skip=0&limit=10&ascending=true&aggregate=false&filters=%7B%7D&download=true" \
-H 'accept: application/json' \
-H "Authorization: Bearer $TOKEN" -o "$OUT"
fi
echo $OUT
}
out_docker_images(){
echo "----- Collecting Docker Images -----" > /dev/tty
OUT="docker_images.txt"
docker inspect --format='{{.Id}} {{.Name}} {{.Image}}' $(docker ps -aq) > "$OUT"
echo $OUT
}
# --- Body --------------------------------------------------------
# SCRIPT LOGIC GOES HERE
output_generators=(
out_versions
out_stats
out_docker_stats
out_docker_images
out_docker_core_logs
out_docker_ui_logs
out_docker_mongo_logs
out_docker_rabbitmq_logs
out_ce_logs
)
outputs=()
# check whether zip is installed or not
zip -v 1> /dev/null
if [ $? -eq 127 ]; then
echo "Zip is not installed"
exit 1;
fi
# check whether curl is installed or not
curl --version 1> /dev/null
if [ $? -eq 127 ]; then
echo "curl is not installed"
exit 1;
fi
# Take inputs from user
read -p "Enter CE Host name (e.g. http(s)://<host-name>:<port>): " HOST
read -p "Enter CE Admin Username: " USERNAME
read -sp "Enter CE Admin Password: " PASSWORD
echo ""
# check whether credentials are correct or not by making an API call
RESPONSE=`curl -s -X 'POST' \
"$HOST/api/auth" \
-d "grant_type=password&username=$USERNAME&password=$PASSWORD&scopes=read write me"`
TOKEN=`echo $RESPONSE | grep -Po '"access_token":(\W+)?"\K[a-zA-Z0-9._-]+(?=")'`
for i in ${output_generators[@]}; do
output=`$i`
outputs+="${output} "
done
# create zip file
echo "----- Creating zip file -----"
OUT="`date | tr " " "_"`.zip"
zip -r "$OUT" ${outputs[@]}
echo $OUT
# remove individual files
rm -rf ${outputs[@]}
# -----------------------------------------------------------------