-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathga_realtime.py
executable file
·69 lines (54 loc) · 2.36 KB
/
ga_realtime.py
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
#!/usr/bin/python
__author__ = "David Gunzinger"
__copyright__ = "Copyright 2013, Smooh GmbH"
__credits__ = []
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "David Gunzinger"
__email__ = "[email protected]"
__status__ = "Production"
import argparse
import nagiosplugin
import httplib2
from oauth2client.file import Storage
from apiclient.discovery import build
# data acquisition
class RealtimeVisitors(nagiosplugin.Resource):
def __init__(self, data, view):
self.data = data
self.view = view
def probe(self):
# try:
storage = Storage(self.data)
credentials = storage.get()
http = httplib2.Http()
http = credentials.authorize(http)
# Construct the service object for the interacting with the Google Analytics API.
service = build('analytics', 'v3', http=http)
request = service.data().realtime().get(ids="ga:%s"%(self.view),metrics="ga:activeVisitors")
response = request.execute()
# print response
activeVisitors = int(response["totalsForAllResults"]["ga:activeVisitors"])
# print activeVisitors
return [nagiosplugin.Metric("activeVisitors", activeVisitors, min=0,context='activeVisitors')]
# except:
#return [nagiosplugin.Metric("activeVisitors", None, context='activeVisitors')]
# runtime environment and data evaluation
@nagiosplugin.guarded
def main():
argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument('-w', '--warning', metavar='RANGE', default='',
help='return warning if activeVisitors is outside RANGE')
argp.add_argument('-c', '--critical', metavar='RANGE', default='',
help='return critical if activeVisitors is outside RANGE')
argp.add_argument('-D', '--data', action='store',required=True)
argp.add_argument('-V', '--view', action='store',required=True)
argp.add_argument('-v', '--verbose', action='count', default=0,
help='increase output verbosity (use up to 3 times)')
args = argp.parse_args()
check = nagiosplugin.Check(
RealtimeVisitors(args.data,args.view),
nagiosplugin.ScalarContext('activeVisitors', args.warning, args.critical))
check.main(verbose=args.verbose)
if __name__ == '__main__':
main()