-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhsm_api.sh
executable file
·146 lines (115 loc) · 3.6 KB
/
rhsm_api.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
#!/usr/bin/bash
FULL_LOG="/tmp/full_list.log"
PRE_PROC="/tmp/full_pre.log"
POS_PROC="/tmp/full_pos.log"
FINAL="/tmp/rhsm_report.log"
DELETED_LOG="/tmp/uuid_deleted.log"
RHSM_USERNAME="rhn-support-wpinheir"
>$PRE_PROC
>$POS_PROC
sync_data()
{
echo "Syncing data from RHSM"
> $FULL_LOG
TOKEN="took token -e rhsm-auth $RHSM_USERNAME"
URL="https://api.access.redhat.com/management/v1/systems"
echo $token
actual_token=$(echo $TOKEN)
if [ $? -ne 0 ]; then
echo "## ATTENTION ##"
echo "Open a second terminal and type the command below. After that, rerun this script once."
echo "took decrypt -t 1440m0s"
exit 1
fi
initial_run=$(curl -s -H "$($TOKEN)" $URL?limit=1\&offset=0)
echo "First touch ... waiting 2 minutes"
sleep 120
cont=0
while true
do
# sleep 5
check_system=$(curl -s -H "$($TOKEN)" $URL?limit=1\&offset=$cont | json_reformat | grep count | awk '{print $2}')
echo "#: $cont, Status: $check_system"
if [ $check_system -ne 1 ]; then
echo "Done"
exit
else
curl -s -H "$($TOKEN)" $URL?limit=100\&offset=$cont | json_reformat >> $FULL_LOG
#curl -s -H "$($TOKEN)" $URL?limit=100\&offset=$cont | json_reformat | tee -a $FULL_LOG
fi
(( cont = cont + 100 ))
done
}
sync_data_temp()
{
echo "Sync Data Temp"
}
process_file()
{
echo "Processing Files"
cat $FULL_LOG | jq '.body[] | .name + " " + .uuid + " " + .type + " " + .lastCheckin' > $PRE_PROC
while read line
do
hostname=$(echo $line | awk '{print $1'} | sed -e 's/^"//g')
uuid=$(echo $line | awk '{print $2'})
mtype=$(echo $line | awk '{print $3'})
ldate=$(echo $line | awk '{print $4'} | sed -e 's/"$//g')
epoch_date=$(date +%s -d"$ldate" 2>/dev/null)
echo "$hostname $uuid $mtype $ldate $epoch_date" >> $POS_PROC
done < $PRE_PROC
echo "fqdn,uuid,type,lastcheckin,epoch_lastcheckin" > $FINAL
cat $POS_PROC | sort -nrk5 | sed -e 's/ /,/g' >> $FINAL
count_uuid=$(wc -l $POS_PROC | awk '{print $1}')
echo "There are $count_uuid UUID on the list"
echo "Please check the file $FINAL"
}
remove_uuid_list()
{
filename=$1
TOKEN="took token -e rhsm-auth $RHSM_USERNAME"
URL="https://api.access.redhat.com/management/v1/systems"
if [ "$filename" == "" ]; then
echo "Please inform the path to the file with uuid to be removed"
exit 1
fi
echo "Removing UUID related to the file $filename"
count_uuid_to_be_removed=$(cat $filename | cut -d, -f2 | wc -l | awk '{print $1}')
echo "There are $count_uuid_to_be_removed on the list to be removed"
echo
echo -e "Are you sure you would like to remove all of them? (y/n):"
read opc
if [ $opc == "n" ]; then
echo "Exiting ..."
exit
elif [ $opc == "y" ]; then
echo "Removing ..."
while read line
do
uuid=$(echo $line | cut -d, -f2)
echo "Removing uuid: $uuid" | tee -a $DELETED_LOG
echo "curl -s -H \"$($TOKEN)\" -X DELETE $URL/$uuid" >> $DELETED_LOG
curl -s -H "$($TOKEN)" -X DELETE $URL/$uuid | tee -a $DELETED_LOG
echo | tee -a $DELETED_LOG
done < $filename
fi
}
# Main
if [ "$1" == "" ]; then
echo "## Please call this script passing the parameter"
echo "#"
echo "# $0 sync_data"
echo " or"
echo "# $0 process_file"
echo " or"
echo "# $0 remove_uuid_list <path to the file with uuid to be removed>"
echo ""
echo "## ATTENTION: Before you start, type on the console the command \"took token -e rhsm-auth <rhsm_username>\""
exit 1
fi
case $1 in
'sync_data') sync_data
;;
'process_file') process_file
;;
'remove_uuid_list') remove_uuid_list $2
esac