-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmake_stats.sh
executable file
·296 lines (236 loc) · 8.2 KB
/
make_stats.sh
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
STATSCRIPT="./DataHubStats_TiB.sh"
XLS="./DHRNAME_report_referenceperiod.xlsx"
XLSPREFIX="CRDR"
REMOTES="./.remotes"
NDAYS=6
TILL=`date -d "yesterday" +%Y-%m-%d`
WRKD="/tmp"
DRY=0
JPROJECT="https://serco-copernicus.atlassian.net/rest/servicedeskapi/servicedesk/CRDR/attachTemporaryFile"
JISSUE="https://serco-copernicus.atlassian.net/rest/servicedeskapi/request/CRDR-1/attachment"
XTRAARG=""
while getopts "hdc:o:l:n:f:w:t:j:p:x:" opt; do
case $opt in
h)
printf "Collect, run and export DHuS Relay statistics\n\nUsage:\n
\t-c <str>\tPath to the statistics script\n\t\t\t(Default \"${STATSCRIPT}\")\n \
\t-o <str>\tPath to the output spreadsheet\n\t\t\t(Default \"${XLS}\")\n \
\t-l <str>\tPath to a file containing remote paths\n\t\t\t(Default \"${REMOTES}\")\n \
\t-w <str>\tPath to the working directory (Default \"${WRKD}\")\n \
\t-n <num>\tStart reporting period <num> days\n\t\t\tBEFORE the final date (Default ${NDAYS})\n \
\t-d \tDry run. Do everything but do not upload to Jira.\n \
\t-p <url>\tUpload URL (default \"${JPROJECT}\")\n \
\t-j <url>\tAttach URL (default \"${JISSUE}\")\n \
\t-x <str>\tAny extra arguments to be handed over to curl\n \
\t-t <Y-M-D>\t\"Till\" Date (Default \"${TILL}\")\n \
\n"
exit 0
;;
c)
STATSCRIPT=$OPTARG
;;
d)
DRY=1
;;
o)
XLS=$OPTARG
;;
l)
REMOTES=$OPTARG
;;
w)
WRKD=$OPTARG
;;
n)
NDAYS=$OPTARG
;;
t)
TILL=$OPTARG
;;
p)
JPROJECT=$OPTARG
;;
j)
JISSUE=$OPTARG
;;
x)
XTRAARG=" $OPTARG "
;;
esac
done
function check_exec()
{
local ret=`which $1 2> /dev/null`
if [ -n "$ret" -a -x "$ret" ]; then
return $TEST_OK
else
return $TEST_ERROR
fi
}
function check_binaries()
{
for file in $@; do
ret=`which $file 2> /dev/null`
if [ -n "$ret" -a -x "$ret" ]; then
echo $ret
else
echo "command $file not found" >&2
exit 1
fi
done
}
function anycat()
{
mimetype=$(file -bi $1 | awk -F ";" '{print $1}')
case $mimetype in
"application/gzip") gunzip -c $1 ;;
"text/plain") cat $1 ;;
*) cat $1 ;;
esac
}
FROM=`date -d "${TILL}-${NDAYS} days" +%Y-%m-%d`
WEEKTILL=`date -d "${TILL}" +%V`
WEEKFROM=`date -d "${FROM}" +%V`
if [ "${WEEKFROM}" == "${WEEKTILL}" ]; then
WEEK="w${WEEKTILL}"
else
WEEK="w${WEEKFROM}to${WEEKTILL}"
fi
YEAR=`date -d "${FROM}" +%Y`
XLSTARGET="${XLSPREFIX}_report_${FROM}_to_${TILL}_DataHubStats.xlsx"
# Use special name if report between 1st and last day of month
if [ `date -d ${FROM} +%m` -eq `date -d ${TILL} +%m` ] && [ `date -d ${FROM} +%d` -eq 1 ] && [ `date -d "${TILL} + 1 day" +%d` -eq 1 ]; then
XLSTARGET="${XLSPREFIX}_Annual_report_`date -d ${FROM} +%m`_`date -d ${FROM} +%Y`.xlsx"
WEEK="m`date -d ${FROM} +%m`"
fi
##########################################
# Step 02: Test prerequisites
echo Checking prerequisites
check_binaries sed libreoffice scp tar gzip basename date cat cp curl grep gunzip
if [ ! -f $HOME/.netrc ]; then
echo File "$HOME/.netrc" not found. Upload to Jira would fail
exit 1
fi
##########################################
# Step 03: Atrribute constraints check
if [ ! -d "$WRKD" ]; then
mkdir -p "$WRKD"
if [ ! -d "$WRKD" ]; then
echo Working directory \"$WRKD\" does not exist! >&2
exit 1
fi
fi
if [ ! -f "$STATSCRIPT" ]; then
echo Script \"$STATSCRIPT\" not found! >&2
exit 1
fi
if [ ! -f "$XLS" ]; then
echo Spreadsheet \"$XLS\" not found! >&2
exit 1
fi
if [ ! -f "$REMOTES" ]; then
echo Remote locations list in \"$REMOTES\" not found! >&2
exit 1
fi
##########################################
# Step 06: Generate target days list
echo Compiling statistics between ${FROM} and ${TILL}.
for i in `seq 0 $NDAYS`; do
LDAYS+=(`date -d "${TILL}-${i} days" +%Y-%m-%d`)
done
#for DATE in "${LDAYS[@]}"; do
# echo $DATE
#done
##########################################
# Step 10: Download all logs
mkdir -p "$WRKD/logs.$$"
WRKLOGS="$WRKD/logs.$$"
while read remote; do
remtok=(${remote//:/ })
echo Downloading logs from ${remtok[0]} \(remote path ${remtok[1]}\)
for DATE in "${LDAYS[@]}"; do
scp $remote/dhus-${DATE}.log* "${WRKLOGS}/dhus-${DATE}.log.${remtok[0]}"
# TODO anycat "${WRKLOGS}/dhus-${DATE}.log.${remtok[0]}" >> "${WRKLOGS}/dhus-${DATE}.log" # This line is more generic but the `egrep` version bellow reduces disk usage considerably.
anycat "${WRKLOGS}/dhus-${DATE}.log.${remtok[0]}" | grep -E '(download.*by.*user.*completed)|(successfully.*synchronized.*from.*http.*)' >> "${WRKLOGS}/dhus-${DATE}.log"
rm "${WRKLOGS}/dhus-${DATE}.log.${remtok[0]}"
# ssh ${remtok[0]} "cat ${remtok[1]}/dhus-${DATE}.log" >> "${WRKLOGS}/dhus-${DATE}.log"
done
done < ${REMOTES}
##########################################
# Step 20: Combine logs
##########################################
# Step 30: Run Statistics
SAVEPWD=`pwd`
cp -v "${STATSCRIPT}" "${WRKLOGS}/"
cp -v "${XLS}" "${WRKLOGS}/${XLSTARGET}"
STATBASE=`basename "${STATSCRIPT}"`
XLSBASE=`basename "${XLSTARGET}"`
cd ${WRKLOGS}
echo Running ${STATSCRIPT} in ${WRKLOGS}
sed --in-place 's/^log_dir=.*$/log_dir=".\/"/' "./${STATBASE}"
${STATSCRIPT} "${FROM}" "${TILL}" "./"
##########################################
# Step 35: Convert to HTML :-(
echo Generating HTML
CSVS=("client_Bandwith_usage_report" "distributed_report" "retrieved_report")
for file in "${CSVS[@]}"
do
printf "Making ${WRKLOGS}/${file}.html "
echo "<table>" >> ${file}.html
while read INPUT ; do
echo "<tr><td>${INPUT//;/</td><td>}</td></tr>" >> ${file}.html
done < ${file}_${FROM}_${TILL}.csv
echo "</table>" >> ${file}.html
echo '[done]'
done
##########################################
# Step 40: Import into spreadsheet
MACROLOC="$HOME/.config/libreoffice/4/user/basic/Standard"
mkdir -p "$MACROLOC"
cat <<EOF > "$MACROLOC/CollGS.xba"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">REM ***** BASIC *****
Sub Main
Dim sheet As Object
Dim document As Object
Dim Dummy()
document = StarDesktop.loadComponentFromURL("file://${WRKLOGS}/${XLSBASE}", "_blank", 0, Dummy)
sheet = thisComponent.getSheets.getByName("RetrieveStats")
sheet.link("file://${WRKLOGS}/retrieved_report.html", "", "", "", com.sun.star.sheet.SheetLinkMode.NORMAL)
sheet = thisComponent.getSheets.getByName("DistributedStats")
sheet.link("file://${WRKLOGS}/distributed_report.html", "", "", "", com.sun.star.sheet.SheetLinkMode.NORMAL)
sheet = thisComponent.getSheets.getByName("BandwidthStats")
sheet.link("file://${WRKLOGS}/client_Bandwith_usage_report.html", "", "", "", com.sun.star.sheet.SheetLinkMode.NORMAL)
document.store()
document.close(True)
end sub
</script:module>
EOF
printf "Running LibreOffice to update the document at ${WRKLOGS}/${XLSBASE} "
libreoffice --invisible --nofirststartwizard --headless --norestore "macro:///Standard.CollGS.Main"
echo '[done]'
##########################################
# Step 45: Encrypt
mkdir "${YEAR}${WEEK}_reports"
cp "${XLSBASE}" "${YEAR}${WEEK}_reports/"
tar cvvf ./${YEAR}${WEEK}_reports.tar ${YEAR}${WEEK}_reports
gzip ${YEAR}${WEEK}_reports.tar
##########################################
# Step 50: Upload to Jira
if [ $DRY -eq 0 ]; then
TEMPID=`curl -n -X POST -H "X-Atlassian-Token: no-check" -F "file=@${WRKLOGS}/${YEAR}${WEEK}_reports.tar.gz" ${XTRAARG} "$JPROJECT" | sed 's/.*"temporaryAttachmentId":"\([^"]*\).*/\1/' | sed 's/.*"temporaryAttachmentId":"\([^"]*\).*/\1/'`
if [ "${TEMPID}" != "" ]; then
echo TEMP file ID ${TEMPID}
curl -D- -n -X POST -H "X-Atlassian-Token: no-check" ${XTRAARG} "$JISSUE" --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{ "temporaryAttachmentIds": [ "'${TEMPID}'" ], "public": true }'
else
>&2 echo TEMP file ID empty
fi
fi
##########################################
# Step 60: Cleanup
echo Removing temporary files from ${WRKLOGS}
#TODO: Actually remove files
cd "${WRKD}"
#rm -rf logs.$$