Skip to content

Instantly share code, notes, and snippets.

@robweber
Created April 20, 2024 17:42
Show Gist options
  • Save robweber/4df52fbfc44cdfe46137a6ac21cfcfd1 to your computer and use it in GitHub Desktop.
Save robweber/4df52fbfc44cdfe46137a6ac21cfcfd1 to your computer and use it in GitHub Desktop.
Different examples of parsing Nagios performance data output with Python
output = "HTTP OK: HTTP/1.1 200 OK - 16499 bytes in 0.019 second response time |time=0.019284s;;;0.000000;10.000000 size=16499B;;;0"
# first split on the pipe
perf_data = output.split("|")
# each data point separated by a space
for p in perf_data[1].split(" "):
result = {}
p_values = p.split(";")
# first key/value pair is required
key_value_array = p_values[0].split("=")
# key is first, data is second with UOM
result['label'] = key_value_array[0]
result['value'] = key_value_array[1]
# get the rest of the values, if they exist
if(1 < len(p_values) and p_values[1].strip() != ''):
result['warn'] = float(p_values[1])
if(2 < len(p_values) and p_values[2].strip() != ''):
result['crit'] = float(p_values[2])
if(3 < len(p_values) and p_values[3].strip() != ''):
result['min'] = float(p_values[3])
if(4 < len(p_values) and p_values[4].strip() != ''):
result['max'] = float(p_values[4])
print(result)
import re
output = "HTTP OK: HTTP/1.1 200 OK - 16499 bytes in 0.019 second response time |time=0.019284s;;;0.000000;10.000000 size=16499B;;;0"
# first split on the pipe
perf_data = output.split("|")
# each data point separated by a space
perf_order = ["value", "warning", "critical", "min", "max"]
for p in perf_data[1].split(" "):
result = {}
# find all the numeric values
p_values = []
for t in re.finditer("(=|;)[+-]?((\\d+(\\.\\d+)?)|(\\.\\d+))|;", p):
p_values.append(t.group()[1:])
# get the rest of the values, if they exist
result = {perf_order[i]: p_values[i] for i in range(0, len(p_values)) if p_values[i].strip() != ''}
# get the label and unit of measure
key_value_array = p.split('=')
unit_of_measure = key_value_array[1][len(result["value"]):key_value_array[1].find(';')]
# convert values to decimals
result = {k: float(v) for k, v in result.items()}
# set label and UOM (if available)
result['label'] = key_value_array[0]
if(unit_of_measure != ''):
result['uom'] = unit_of_measure
print(result)
import re
output = "HTTP OK: HTTP/1.1 200 OK - 16499 bytes in 0.019 second response time |time=0.019284s;;;0.000000;10.000000 size=16499B;;;0 'RAID Status'=1"
# first split on the pipe
perf_data = output.split("|")
# each data point separated by a space
perf_order = ["value", "warning", "critical", "min", "max"]
for p in re.finditer("(\"[^\"]*\"|'[^']*'|[\\S]+)+", perf_data[1].strip()):
result = {}
# find all the numeric values
p_values = []
for t in re.finditer("(=|;)[+-]?((\\d+(\\.\\d+)?)|(\\.\\d+))|;", p.group()):
p_values.append(t.group()[1:])
# get the rest of the values, if they exist
result = {perf_order[i]: p_values[i] for i in range(0, len(p_values)) if p_values[i].strip() != ''}
# get the label and unit of measure
key_value_array = p.group().split('=')
unit_of_measure = key_value_array[1][len(result["value"]):key_value_array[1].find(';')]
# convert values to decimals
result = {k: float(v) for k, v in result.items()}
# set label and UOM (if available)
result['label'] = key_value_array[0].replace("'", "")
if(unit_of_measure != ''):
result['uom'] = unit_of_measure
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment