Skip to content

Commit

Permalink
Merge pull request #2 from tuxudo/patch-1
Browse files Browse the repository at this point in the history
Update for Python 3 and Darwin version checks
  • Loading branch information
tuxudo authored Dec 12, 2022
2 parents 8037708 + 31f10d7 commit 75b2461
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions scripts/usage_stats
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#!/usr/local/munkireport/munkireport-python2
#!/usr/local/munkireport/munkireport-python3
# Script by Bochoven

import os, sys, plistlib, subprocess, platform

def get_usage_metrics():
cmd = ['/usr/bin/powermetrics', '--show-initial-usage', ' -s', 'network,disk', '-f', 'plist', '-n', '0']
cmd = ['/usr/bin/powermetrics', '--show-initial-usage', ' -s', "network,disk", '-f', 'plist', '-n', '0']

proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
plist = plistlib.readPlistFromString(output.rstrip(' \t\r\n\0'))

# Need to remove a broken key "<key>all_tasks</key>" that Python doesn't like
output = output.decode().replace("<key>all_tasks</key>","")

try:
plist = plistlib.readPlistFromString(output.rstrip(' \t\r\n\0').strip())
except AttributeError as e:
try:
plist = plistlib.loads(output.rstrip(' \t\r\n\0').encode())
except:
plist = {}

cmd = ['/usr/bin/powermetrics', '-s', 'cpu_power,gpu_power,thermal,battery', '-n', '1', '-f', 'plist']
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
plist.update(plistlib.readPlistFromString(output.rstrip(' \t\r\n\0')))

try:
plist.update(plistlib.readPlistFromString(output.rstrip(' \t\r\n\0')))
except AttributeError as e:
plist.update(plistlib.loads(output.decode().rstrip(' \t\r\n\0').encode()))

return plist

Expand Down Expand Up @@ -165,10 +180,8 @@ def parse_usage_plist(plist):
def getDarwinVersion():
"""Returns the Darwin version."""
# Catalina -> 10.15.7 -> 19.6.0 -> 19
# os_version_tuple = platform.mac_ver()[0].split('.')
# return int(os_version_tuple[1])
darwin_version_tuple = platform.release().split('.')
return int(darwin_version_tuple[0])
return int(darwin_version_tuple[0])

def main():
"""Main"""
Expand All @@ -177,21 +190,20 @@ def main():
if getDarwinVersion() < 14:
result = dict()

# Write results to cache
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
output_plist = os.path.join(cachedir, 'usage_stats.plist')
plistlib.writePlist(result, output_plist)

else :
# Get results
result = dict()
info = get_usage_metrics()
result = parse_usage_plist(info)

# Write results to cache
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
output_plist = os.path.join(cachedir, 'usage_stats.plist')
# Write results to cache
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
output_plist = os.path.join(cachedir, 'usage_stats.plist')
try:
plistlib.writePlist(result, output_plist)
except:
with open(output_plist, 'wb') as fp:
plistlib.dump(result, fp, fmt=plistlib.FMT_XML)

if __name__ == "__main__":
main()
main()

0 comments on commit 75b2461

Please sign in to comment.