Add macro for reporting the provision result
[releng.git] / utils / slave-monitor-0.1.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 #This will put a bunch of files in the pwd. you have been warned.
12 #Counts how long slaves have been online or offline
13
14
15 #Yes I know about jq
16 curlcommand() {
17 curl -s "https://build.opnfv.org/ci/computer/api/json?tree=computer\[displayName,offline\]" \
18     | awk -v k=":" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' \
19     | grep -v "_class" \
20     | awk 'NR%2{printf "%s ",$0;next;}1'  \
21     | awk -F":" '{print $2,$3}' \
22     | awk '{print $1,$3}' \
23     | sed s,\},,g \
24     | sed s,],,g \
25     | sed s,\",,g
26 }
27
28 if [ -f podoutput-current ]; then
29   cp podoutput-current podoutput-lastiteration
30 fi
31
32 curlcommand > podoutput-current
33
34 declare -A slavescurrent slaveslastiteration
35
36 while read -r name status ; do
37             slavescurrent["$name"]="$status"
38 done < <(cat podoutput-current)
39
40 while read -r name status ; do
41             slaveslastiteration["$name"]=$status
42 done < <(cat podoutput-lastiteration)
43
44 main () {
45 for slavename in "${!slavescurrent[@]}"; do
46     #Slave is online. Mark it down.
47     if [ "${slavescurrent[$slavename]}" == "false" ]; then
48
49         if  [ -f "$slavename"-offline ]; then
50             echo "removing offline status from $slavename slave was offline for $(cat "$slavename"-offline ) iterations"
51             rm "$slavename"-offline
52         fi
53
54         if  ! [ -f "$slavename"-online ]; then
55             echo "1" > "$slavename"-online
56         elif [ -f "$slavename"-online ]; then
57             #read and increment slavename
58             read -r -d $'\x04' var < "$slavename"-online
59             ((var++))
60             echo -n "ONLINE $slavename "
61             echo "for $var iterations"
62             echo "$var" > "$slavename"-online
63         fi
64     fi
65
66     #went offline since last iteration.
67     if [ "${slavescurrent[$slavename]}" == "false" ] && [ "${slaveslastiteration[$slavename]}" == "true" ];  then
68         echo "JUST WENT OFFLINE $slavename "
69         if  [ -f "$slavename"-online ]; then
70             echo "removing online status from $slavename. slave was online for $(cat "$slavename"-online ) iterations"
71             rm "$slavename"-online
72         fi
73
74     fi
75
76     #slave is offline
77     if [ "${slavescurrent[$slavename]}" == "true" ]; then
78         if  ! [ -f "$slavename"-offline ]; then
79             echo "1" > "$slavename"-offline
80         fi
81
82         if [ -f "$slavename"-offline ]; then
83             #read and increment slavename
84             read -r -d $'\x04' var < "$slavename"-offline
85             ((var++))
86             echo "$var" > "$slavename"-offline
87                 if  [ "$var" -gt "30" ]; then
88                     echo "OFFLINE FOR $var ITERATIONS REMOVE $slavename "
89                 else
90                     echo "OFFLINE $slavename FOR $var ITERATIONS "
91                 fi
92         fi
93     fi
94
95 done
96 }
97
98 main