Regex to split up string of CPU usage percentages?

284 views Asked by At

We have an HP 1920S switch and the only way to get the CPU usage via SNMP is an OID I found here: https://community.hpe.com/t5/Web-and-Unmanaged/HP-1920s-OID-CPU-Utilization-and-memory/td-p/7001413

OID: .1.3.6.1.4.1.11.5.7.5.7.1.1.1.1.4.9.0

When queried via SNMP that OID returns values like this:

 5 Secs ( 22.3463%) 60 Secs ( 25.677%) 300 Secs ( 21.3522%)
 5 Secs ( 13.6713%) 60 Secs ( 15.3520%) 300 Secs ( 15.9640%)

So the 5 second CPU utilization average is 22%, 60 second was 25%, and 5 minute was 21%. Then the next time I queried it was 13, 15, and 15 percent.

Is there a way via regex to discretely match the 5 second percentage or discretely match the 60 second percentage or the 5 minute percentage? The goal is to wire this up to a few SNMP Custom String sensors in PRTG, so then it can alert if the 5 minute average is above 80%, for example. I did fine this regex here: \d+(\%|\s\bpercent\b) via https://www.regextester.com/95112 but I also can't figure out how to modify it for my needs and can't get it to work on regex101.com

Thank you!

1

There are 1 answers

2
The fourth bird On BEST ANSWER

Using \d+(\%|\s\bpercent\b) will not match the decimal part in the example string.

You could add matching an optional decimal part \d+(?:\.\d+)?(?:\%|\s\bpercent\b) , but you will get 3 matches and the pattern does not match Secs

Your query returned 2 times the same structure for the data. One way could be using a pattern that matches the full line and use 3 capturing groups so you know which group has which value.

There is not word percentage in the example data, so you could just use % instead.

^\s*\d+\s+Secs\s*\(\s*(\d+(?:\.\d+)?%)\)\s+\d+\s+Secs\s+\(\s+(\d+(?:\.\d+)?%)\)\s+\d+\s+Secs\s+\(\s+(\d+(?:\.\d+)?%)\)$

Regex demo