0

I am running a python script that grabs metrics for our PCF environment. It runs the command "cf nozzle" and saves the output. currently the output is:

origin:"rep" eventType:ContainerMetric timestamp:1549634851407422442 deployment:"cf" job:"diego_cell" index:"133834a1-b38a-41fc-9c86-3fc0433ca25c" ip:"10.54.168.53" tags: containerMetric: a59a-7d65bc026e54" instanceIndex:3 cpuPercentage:0.0002414595272222457 memoryBytes:4562944 diskBytes:10043392 memoryBytesQuota:13 4217728 diskBytesQuota:1073741824 >

origin:"rep" eventType:ContainerMetric timestamp:1549634851407427339 deployment:"cf" job:"diego_cell" index:"133834a1-b38a-41fc-9c86-3fc0433ca25c" ip:"10.54.168.53" tags: containerMetric: 973d-2b929bd948e1" instanceIndex:1 cpuPercentage:0.08058658221147202 memoryBytes:277782528 diskBytes:210866176 memoryBytesQuota:5 36870912 diskBytesQuota:268435456 >

origin:"rep" eventType:ContainerMetric timestamp:1549634851407432095 deployment:"cf" job:"diego_cell" index:"133834a1-b38a-41fc-9c86-3fc0433ca25c" ip:"10.54.168.53" tags: containerMetric: 80f9-3e91490ace2b" instanceIndex:2 cpuPercentage:0.697502774289568 memoryBytes:366649344 diskBytes:189415424 memoryBytesQuota:107 3741824 diskBytesQuota:268435456 >

I want to be able to organize this output better by making each metric block into it's own dictionary/list. That way I'll be able to grab certain metrics for monitoring.

My current code is:

import subprocess


nozzle_cmd = ['timeout', '10', 'cf', 'nozzle', '--filter', 'ContainerMetric']

process_nozzle = subprocess.run(nozzle_cmd, stdout=subprocess.PIPE)
nozzle_bytes = process_nozzle.stdout
nozzle_str = nozzle_bytes.decode("utf-8")
print(nozzle_str)
Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
  • Would splitting each line by the space character and splitting the entire output by line not work? – BoboDarph Feb 08 '19 at 14:30
  • Does `cf nozzle` provide an option to produce more machine-friendly output? This *looks* like it's just a stream of white-space separated key/value pairs, of the form `key:value`, where `value` may or may not be enclosed in double quotes, and that sets of key/value pairs are separated by blank lines. But is that *guaranteed*? If the output were in a standard format like JSON, you wouldn't have to worry about guessing the format, and you wouldn't have to write an ad-hoc parser to consume it. – chepner Feb 08 '19 at 14:30

0 Answers0