-
Notifications
You must be signed in to change notification settings - Fork 1
/
nomad_job_collect
executable file
·105 lines (87 loc) · 3.79 KB
/
nomad_job_collect
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
#!/usr/bin/env bash
job=${1}
hostname=`hostname -f`
export NOMAD_ADDR=${NOMAD_ADDR:-"http://127.0.0.1:4646"}
if [ -z "${job}" ]; then
echo -e "\nNo job name specified -- only collecting general/agent information"
echo -e "\n\t Use the syntax 'nomad_job_collect JOBNAME' to gather details of a specific job"
echo -e "\n"
jobDir='general'
else
jobDir=`echo ${job}|sed 's/\//_/g'`
fi
curDir=`pwd`
tmpDir=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
outDir=${tmpDir}/${jobDir}
echo "# Creating temp directory ${outDir}"
mkdir ${outDir}
cd ${outDir}
echo "# Collecting general information (hostname = ${hostname})"
echo " - all members"
curl -s ${NOMAD_ADDR}/v1/agent/members > members.json
echo " - all nodes"
curl -s ${NOMAD_ADDR}/v1/nodes > nodes.json
echo " - all evaluations"
curl -s ${NOMAD_ADDR}/v1/evaluations > evaluations.json
echo " - all deployments"
curl -s ${NOMAD_ADDR}/v1/deployments > deployments.json
# Allow existing environment variables to override
export NOMAD_SERVERS=${NOMAD_SERVERS:-`cat members.json | jq -r --sort-keys .Members[].Addr`}
export NOMAD_CLIENTS=${NOMAD_CLIENTS:-`cat nodes.json | jq -r --sort-keys .[].Address`}
echo "# Collecting server agent information"
for server in ${NOMAD_SERVERS}
do
echo " - Server = ${server}"
serverFileName=`echo ${server}|sed 's/\./-/g'`
curl -s http://${server}:4646/v1/agent/self | jq --sort-keys . > agent-self-server-${serverFileName}.json
done
echo "# Collecting client agent information"
for client in ${NOMAD_CLIENTS}
do
echo " - Client = ${client}"
clientFileName=`echo ${client}|sed 's/\./-/g'`
curl -s http://${client}:4646/v1/agent/self | jq --sort-keys . > agent-self-client-${clientFileName}.json
done
if [ -n "${job}" ]; then
response=`curl -s -w %{http_code} ${NOMAD_ADDR}/v1/job/${job} -o job.${job}.json`
if [ $response -eq 200 ]; then
echo "# Collecting information about job ${job}"
echo " - allocations"
curl -s ${NOMAD_ADDR}/v1/job/${job}/allocations > job.${job}.allocations.json
echo " - versions"
curl -s ${NOMAD_ADDR}/v1/job/${job}/versions > job.${job}.versions.json
echo " - summary"
curl -s ${NOMAD_ADDR}/v1/job/${job}/summary > job.${job}.summary.json
echo "# Collecting job allocation details for ${job}"
for alloc in `curl -s ${NOMAD_ADDR}/v1/job/${job}/allocations | jq -r '.[].ID'`
do
echo " - allocation ${alloc}"
curl -s ${NOMAD_ADDR}/v1/allocation/${alloc} > job.${job}.allocation.${alloc}.json
for task in $(nomad alloc status -json ${alloc} | jq -r ' .TaskStates|keys[]')
do
echo -e " - Gathering logs for task ${task}"
curl -s "${NOMAD_ADDR}/v1/client/fs/logs/${alloc}?plain=true&task=${task}&type=stdout" > "job.${job}.allocation.${alloc}.${task}.stdout.log"
curl -s "${NOMAD_ADDR}/v1/client/fs/logs/${alloc}?plain=true&task=${task}&type=stderr" > "job.${job}.allocation.${alloc}.${task}.stderr.log"
# nomad alloc logs ${alloc} ${task} > "job.${job}.allocation.${alloc}.${task}.stdout-CLI.log"
# nomad alloc logs -stderr ${alloc} ${task} > "job.${job}.allocation.${alloc}.${task}.stderr-CLI.log"
done
done
else
echo -e "\n\n\t *** Job ${job} not found *** (HTTP response code $response)\n\n"
fi
fi
cd ${tmpDir}
if [ "${job}" == "general" ]; then
tarballFileName=${curDir}/nomad-general-bundle_`date +%F-%H%M%S`.tgz
else
tarballFileName=${curDir}/nomad-job-bundle_${jobDir}_`date +%F-%H%M%S`.tgz
fi
echo "# Creating archive ${tarballFileName}"
echo '```'
tar --sort=name -zcvf ${tarballFileName} ${jobDir}
echo '```'
echo "${tarballFileName}"
echo "# Cleaning up temp directory"
cd ${curDir}
rm -rf $tmpDir
echo "# Done."