-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteStatus.coffee
More file actions
83 lines (68 loc) · 1.91 KB
/
siteStatus.coffee
File metadata and controls
83 lines (68 loc) · 1.91 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
# INSTRUCTIONS:
#
# Install Coffeescript:
# npm install -g coffee-script
#
# Run from the terminal:
# coffee siteStatus.coffee http://xyzzy.com 20 2
#
# The first number defines how many times you want to make the request
# The second number defines the interval between requests (in seconds)
# Both are optional
http = require 'http'
if process.argv[2]?
# Tests the URL's spelling just in case
regex = /((http|https):\/\/)(www\.)?(.+)(\.\w+)\/?(\w+)?/g
par = process.argv[2]
if par.match(regex)
url = par
else
console.log "Check the URL spelling!"
return
else
url = 'http://xyzzy.com/'
# Request amount (defaut is 10)
if process.argv[3]?
repeat = process.argv[3]
else
repeat = 10
# Request interval (default is 2 seconds)
if process.argv[4]?
interval = process.argv[4] * 1000
else
interval = 2000
request = ->
# Takes the starting time for the request
startTime = new Date().getTime()
req = http.get url, (res) ->
# If status is 200, prints OK, else fail and the status code
status = res.statusCode
if status is 200
requestTime = new Date().getTime() - startTime
process.stdout.write timestamp() + " OK! " + "(" + requestTime + "ms)\n"
res.on 'data', (data) ->
body = data.toString()
repeat--
else
requestTime = new Date().getTime() - startTime
process.stdout.write timestamp() + " Fail! " + "[" + status + "] " + "(" + requestTime + "ms)\n"
repeat--
# Then the request has been repeated the specified times, stops the timer
if repeat < 1
console.log "Loppu!"
clearInterval(Timer)
return
# If something goes wrong, print the error message
req.on 'error', (e) ->
console.log e.message
return
# Turns the timestamp into a readable form
# TODO Insert a zero before the single numbers
timestamp = ->
now = new Date()
h = now.getHours()
m = now.getMinutes()
s = now.getSeconds()
return h + ":" + m + ":" + s
# Creates a timer
Timer = setInterval request, interval