-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhortator.py
120 lines (98 loc) · 3.49 KB
/
hortator.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
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
#
# API Call routines and modification
#
from apiclient.discovery import build
from apiclient.errors import HttpError
# Needed for Stats
from datetime import datetime
from datetime import timedelta
from collections import defaultdict
import re
#
# The query is up and running
#
def GetChromeManifest(decorator):
'Takes care of getting the Chrome Device Manifest. Returns the dict'
service = build('admin', 'directory_v1')
request = service.chromeosdevices().list(customerId='my_customer').execute(decorator.http())
devices = request['chromeosdevices']
while 'nextPageToken' in request:
request = service.chromeosdevices().list(customerId='my_customer', pageToken=request['nextPageToken']).execute()
devices.append(result['chromeosdevices'])
return devices
def BuildChromeManifest(Template, apiResponse):
'Use the supplied template for mapping desired information and returning new manifest'
holder = []
for device_info in apiResponse:
for key in Template:
Template[key] = device_info.get(key, " ")
holder.append(Template.values())
return holder
def StatsFromManifest(apiResponse):
'Perform a series of tests on Chrome Manifest and returns a dict object'
today = datetime.now().date()
LastSyncCount = 0
EnrollCount = 0
Channels = defaultdict(int)
Firmware = defaultdict(int)
OUPath = defaultdict(int)
Version = defaultdict(int)
Status = defaultdict(int)
# Master loop - go through all devices
for device in apiResponse:
# Check for LastSyncTime
try:
if 'lastSync' in device:
if (today - (datetime.strptime(device['lastSync'][:-14], '%Y-%m-%d').date())) <= timedelta(days=7):
LastSyncCount += 1
except:
print "[LastSync]: ", device
# Check for LastEnrollmentDate in the past 7 days ( this would denote devices being wiped and re-enrolled )
try:
if 'lastEnrollmentTime' in device:
if (today - (datetime.strptime(device['lastEnrollmentTime'][:-14], '%Y-%m-%d').date())) <= timedelta(days=7):
EnrollCount += 1
except:
print "[LastSync]: ", device
# Find the channel and increment appropriately
try:
if 'platformVersion' in device:
Channels[re.match(r'.* (.*)-channel', device['platformVersion'], re.U).group(1)] += 1
except:
print "[platformVersion]: ", device
# Find the firmware version
try:
if 'firmwareVersion' in device:
Firmware[re.match(r'(.*?)\.', device['firmwareVersion'], re.U).group(1)] += 1
except:
print "[firmwareVersion]: ", device
# Count of each device in a given OU
try:
if 'orgUnitPath' in device:
OUPath[device['orgUnitPath']] += 1
except:
print "[orgUnitPath]", device
# Count of each OS Version
try:
if 'osVersion' in device:
Version[device['osVersion'][:2]] += 1
except:
print "[osVersion]: ", device
# Count of 'active' / 'shipped' etc.
try:
if 'status' in device:
Status[device['status']] += 1
except:
print "[status]: ", device
# Count of devices that wre
# Once data has been collected bundle up and return
stats = {}
stats['RecentSync'] = LastSyncCount
stats['RecentEnroll'] = EnrollCount
stats['Channel'] = Channels.items()
stats['OUPath'] = OUPath.items()
stats['Version'] = Version.items()
stats['Status'] = Status.items()
stats['ChannelTotal'] = sum(Channels.values())
stats['VersionTotal'] = sum(Version.values())
return stats