-
Notifications
You must be signed in to change notification settings - Fork 52
/
GetOrgUnitCrOSCounts.py
executable file
·96 lines (87 loc) · 3.69 KB
/
GetOrgUnitCrOSCounts.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
#!/usr/bin/env python3
"""
# Purpose: Show the number of CrOS devices in each Org Unit
# Note: This script can use GAM7 or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Customize: Set SHOW_STATUS,SHOW_TOTALS
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Get OrgUnits
# $ gam redirect csv ./OrgUnits.csv print ous
# 2: Get CrOS devices; omit status if you don't want status info
# $ gam redirect csv ./CrOS.csv print cros ou status
# 3: From those lists of Org Units and CrOS devices, output a CSV file with CrOS device counts for each Org Unit
# $ python3 GetOrgUnitCrOSCounts.py ./OrgUnits.csv ./CrOS.csv ./OrgUnitCrOSCounts.csv
"""
import csv
import sys
SHOW_STATUS = True # False if you don't want status information
SHOW_TOTALS = True # False if you don't want totals
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
orgUnits = {}
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
inputCSV = csv.DictReader(inputFile, quotechar=QUOTE_CHAR)
inputFieldNames = inputCSV.fieldnames
if 'orgUnitPath' not in inputFieldNames:
sys.stderr.write(f'Error: no header orgUnitPath in Org Units file {sys.argv[1]} field names: {",".join(inputFieldNames)}\n')
sys.exit(1)
for row in inputCSV:
orgUnits[row['orgUnitPath']] = {'devices' : 0, 'statusValues': {}}
inputFile.close()
if sys.argv[2] != '-':
inputFile = open(sys.argv[2], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
inputCSV = csv.DictReader(inputFile, quotechar=QUOTE_CHAR)
inputFieldNames = inputCSV.fieldnames
if 'orgUnitPath' not in inputFieldNames:
sys.stderr.write(f'Error: no header orgUnitPath in CrOS file {sys.argv[2]} field names: {",".join(inputFieldNames)}\n')
sys.exit(1)
fieldnames = ['orgUnitPath', 'devices']
checkStatus = SHOW_STATUS and 'status' in inputFieldNames
statusValues = set()
if (len(sys.argv) > 3) and (sys.argv[3] != '-'):
outputFile = open(sys.argv[3], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
totals = {'devices' : 0, 'statusValues': {}}
for row in inputCSV:
orgUnitPath = row['orgUnitPath']
if orgUnitPath not in orgUnits:
orgUnits[orgUnitPath] = {'devices' : 0, 'statusValues': {}}
orgUnits[orgUnitPath]['devices'] += 1
if checkStatus:
statusValue = row['status']
if statusValue not in statusValues:
statusValues.add(statusValue)
orgUnits[orgUnitPath]['statusValues'].setdefault(statusValue, 0)
orgUnits[orgUnitPath]['statusValues'][statusValue] += 1
if checkStatus:
for statusValue in sorted(statusValues):
fieldnames.append(f'status.{statusValue}')
totals['statusValues'][statusValue] = 0
outputCSV = csv.DictWriter(outputFile, fieldnames, lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for orgUnit, counts in sorted(iter(orgUnits.items())):
row = {'orgUnitPath': orgUnit, 'devices': counts['devices']}
totals['devices'] += counts['devices']
if checkStatus:
for statusValue in statusValues:
count = counts['statusValues'].get(statusValue, 0)
row[f'status.{statusValue}'] = count
totals['statusValues'][statusValue] += count
outputCSV.writerow(row)
if SHOW_TOTALS:
row = {'orgUnitPath': 'Totals', 'devices': totals['devices']}
if checkStatus:
for statusValue, count in iter(totals['statusValues'].items()):
row[f'status.{statusValue}'] = count
outputCSV.writerow(row)
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()