forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagespeed.py
More file actions
executable file
·62 lines (51 loc) · 1.6 KB
/
Copy pathpagespeed.py
File metadata and controls
executable file
·62 lines (51 loc) · 1.6 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
#!/usr/bin/env python
import urllib2
import optparse
try:
import json
except ImportError:
import simplejson as json
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2
API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=%s&key=%s'
API_KEY = ''
HOSTNAME = ''
WARNING_SCORE = 0
CRITICAL_SCORE = 0
def main():
try:
request = urllib2.urlopen(API_URL % (HOSTNAME, API_KEY))
except urllib2.HTTPError, e:
print 'Error! %s' % e
raise SystemExit(UNKNOWN)
data = json.loads(request.read())
if data.get('error'):
print 'Error! %s' % data.get('error').get('errors')[0].get('reason')
raise SystemExit(UNKNOWN)
_score = data.get('score')
if _score <= CRITICAL_SCORE:
print 'Critical! Current score: %s/100' % _score
raise SystemExit(CRITICAL)
if _score <= WARNING_SCORE:
print 'Warning! Current score: %s/100' % _score
raise SystemExit(WARNING)
print 'OK! Current score: %s/100' % _score
raise SystemExit(OK)
parser = optparse.OptionParser()
parser.add_option('-H', '--hostname', dest='hostname')
parser.add_option('-K', '--key', dest='key')
parser.add_option('-w', '--warning', type=float, dest='warning',
default=0)
parser.add_option('-c', '--critical', type=float, dest='critical',
default=0)
options, args = parser.parse_args()
if not options.hostname or not options.key:
print 'Critical! Missing parameters'
raise SystemExit(CRITICAL)
HOSTNAME = options.hostname
API_KEY = options.key
WARNING_SCORE = options.warning
CRITICAL_SCORE = options.critical
main()