-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponses.sh
More file actions
151 lines (131 loc) · 4.83 KB
/
responses.sh
File metadata and controls
151 lines (131 loc) · 4.83 KB
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
#!/bin/bash
_username=$(whoami);
echo $_username;
user_count=0
sudo service redis_6379 start
function status() {
while IFS=$'\n' read -r user || [[ -n "$user" ]]; do
redis-cli HDEL user:"$user" url
redis-cli HDEL user:$user:$url_count timestamp url header response parent
redis-cli HGETALL user:$user
url_count=(`redis-cli HGET registered_user:$user url_count`)
name=`redis-cli HGET registered_user:$user user_name`;
redis-cli HGETALL user:"$user"
redis-cli HGETALL user:$user:$url_count
((user_count+=1))
echo "user:$user_count"
echo "user:$user"
i=0;
for (( i = 0; i < $url_count ; i++ )); do
url=`redis-cli HGET registered_user:$user url:"$i"`;
echo $url;
let count=check;
#
# count==false => cURL doesn't exist. Continue with wget.
# count==true => cURL exists but wget doesn't exist. Continue with cURL.
# else, both exist so continue with cURL
#
if [[ "$count" = true ]] ; then
timestamp=$(date +'%d/%m/%Y %H:%M:%S:%3N')
header=$(curl -sL -w "%{http_code} %{url_effective}" $url -o /dev/null);
response="$(curl -sL -w %{time_total} $url -o /dev/null)";
redis-cli HMSET user:"$user" name:"$name" url "user:$user:$i";
redis-cli HMSET user:$user:$i timestamp "$timestamp" url "$url" header "$header" response "$response" parent "user:$user_count";
redis-cli RPUSH response:"$user":"$i" $response;
list_len=`redis-cli LLEN response:"$user":"$i"`;
echo $list_len;
last_index=$((list_len-1));
redis-cli LRANGE response:"$user":"$i" 0 $last_index;
elif [[ "$count" = false ]]; then
timestamp=$(date +'%d/%m/%Y %H:%M:%S:%3N')
header=$(wget -S --spider $url 2>&1 | awk '/^ /'); #not sure about the wget command. CHECK.
response=$(wget -S --spider $url 2>&1 | awk '/^ /'); #not sure about the wget command. CHECK.
redis-cli HMSET user:"$user" name:"$name" url "user:$user:$i";
redis-cli HMSET user:$user:$i timestamp "$timestamp" url "$url" header "$header" response "$response" parent "user:$user_count";
redis-cli RPUSH response:"$user":"$i" $response;
list_len=`redis-cli LLEN response:"$user":"$i"`;
echo $list_len;
last_index=$((list_len-1));
redis-cli LRANGE response:"$user":"$i" 0 $last_index;
else
timestamp=$(date +'%d/%m/%Y %H:%M:%S:%3N')
header=$(curl -sL -w "%{http_code} %{url_effective}" $url -o /dev/null)
response=$(curl -sL -w %{time_total} $url -o /dev/null);
redis-cli HMSET user:"$user" name:"$name" url "user:$user:$i";
redis-cli HMSET user:$user:$i timestamp "$timestamp" url "$url" header "$header" response "$response" parent "user:$user_count";
redis-cli RPUSH response:"$user":"$i" $response;
list_len=`redis-cli LLEN response:"$user":"$i"`;
echo $list_len;
last_index=$((list_len-1));
redis-cli LRANGE response:"$user":"$i" 0 $last_index;
fi
echo "Status Recorded."
done
redis-cli HGETALL user:$user:0
redis-cli HGETALL user:$user:1
done </home/pragya/projects/status-smart-2/hashes.txt
}
#
# Checks the existance of ping, cURL, wget
# return count.
# count, false => cURL doesn't exist.
# count, true => cURL exists but wget doesn't.
#
function check() {
#
# Check if ping is installed in the system or not.
# Exit code if ping doesn't exist.
#
if [ ! -x /bin/ping ] ; then
command -v ping >/dev/null 2>&1 || { echo >&2 "Please install ping or set it in your path. Aborting"; exit 1; }
echo "ping doesn't exist.";
fi
#
# Check for curl and wget in the system.
# Inform if curl doesn't exist and proceed with wget.
# Exit if both do not exist.
#
if [ ! -x /usr/bin/curl ] ; then
command -v curl >/dev/null 2>&1 || { echo >&2 "Please install curl or set it in your path."; }
echo "curl doesn't exist.";
let count=false;
elif [ ! -x /usr/bin/wget ] ; then
command -v wget >/dev/null 2>&1 || { echo >&2 "Please install wget or set it in your path. Aborting."; exit 1; }
echo "curl exists but wget doesn't exist";
let count=true;
elif [[ ! -x /usr/bin/curl && ! -x /usr/bin/wget ]]; then
echo "Cannot proceed without cURL and wget. Please install either to continue. Aborting."
exit 1;
else
echo "Program can proceed";
fi
return $count;
}
function mean() {
list_len=0.0;
sum=0.0;
ele=0.0;
while IFS=$'\n' read -r user || [[ -n "$user" ]]; do
url_count=`redis-cli HGET registered_user:$user url_count`;
for (( i = 0; i < $url_count; i++ )); do
sum=0.0;
list_len=`redis-cli LLEN response:"$user":"$i"`;
echo $list_len;
j=0;
for (( j = 0; j < $list_len; j++ )); do
ele=`redis-cli LPOP response:$user:$i`;
echo $ele;
sum=`bc <<< $sum+$ele`
echo $sum;
done
mean=`bc <<< $sum/$list_len`
echo $mean
redis-cli HSET user:$user:$i mean_response "$mean";
done
redis-cli HGETALL user:$user:0;
redis-cli HGETALL user:$user:1;
done </home/pragya/projects/status-smart-2/hashes.txt
}
status
echo "Process Completed."
mean