| # constructs ansii color code escape sequence |
| def ansi(code): "\u001b[" + code + "m" + . + "\u001b[0m"; |
| |
| # Colorize every array element based on the severity and cleared status |
| def colorize(severity; cleared): |
| if cleared == true then . # keep default color |
| elif severity == "critical" then . | map(ansi("31;1")) # red bold |
| elif severity == "major" then . | map(ansi("31")) # red |
| elif severity == "minor" then . | map(ansi("33")) # yellow |
| elif severity == "warning" then . | map(ansi("33;1")) # yellow bold |
| elif severity == "indeterminate" then . | map(ansi("31")) # white |
| else . # default color |
| end; |
| |
| # Extracts resource name from /ietf-hardware xpath, if possible. In other cases, returns the input. |
| def formatResource: |
| if(test("/ietf-hardware:hardware/component*")) then |
| capture("/ietf-hardware:hardware/component\\[name='(?<resource>.*)']") | .resource |
| else |
| . |
| end; |
| |
| # Formats the alarm type and qualifier |
| def formatAlarmType($type;$qualifier): |
| if($type == "velia-alarms:sensor-low-value-alarm") then |
| "\u23f7" # ⏷ (upwards triangle) |
| elif($type == "velia-alarms:sensor-high-value-alarm") then |
| "\u23f6" # ⏶ (downwards triangle) |
| elif($type == "velia-alarms:systemd-unit-failure") then |
| "\u23f8" # ⏸ (pause) |
| elif($type == "velia-alarms:sensor-missing-alarm") then |
| "\u2715" # ✕ (multiplication X) |
| else |
| $type + " " + $qualifier |
| end; |
| |
| def formatIsCleared($isCleared): |
| if $isCleared then |
| "cleared" |
| else |
| "active" |
| end; |
| |
| def filterCleared($config): |
| if $config | has("cleared") then |
| . | map(select(.["is-cleared"] == $config.cleared)) |
| else |
| . |
| end; |
| |
| def parseTimestamp: |
| . | strptime("%Y-%m-%dT%H:%M:%S%Z"); |
| |
| def filterTimestamp($config): |
| if $config | has("until") then |
| . | map(select((.["last-raised"] | parseTimestamp ) <= ($config.until | parseTimestamp))) |
| else |
| . |
| end | |
| if $config | has("since") then |
| . | map(select((.["last-raised"] | parseTimestamp ) >= ($config.since | parseTimestamp))) |
| else |
| . |
| end; |
| |
| # https://stackoverflow.com/questions/76476166/jq-sorting-by-value |
| def sortBySeverity: |
| . | sort_by(.["perceived-severity"] != ("critical", "major", "minor", "warning", "indeterminate", "cleared")); |
| |
| def main: |
| (["", "Resource", "Severity", "Detail", "Last raised", "Status"]), |
| ( . |
| | if . == null then halt else . end # guard against null object (no alarms) |
| | filterCleared($config) |
| | filterTimestamp($config) |
| | sortBySeverity |
| | .[] |
| | .["perceived-severity"] as $severity |
| | .["is-cleared"] as $cleared |
| | [formatAlarmType(.["alarm-type-id"]; .["alarm-type-qualifier"]), |
| (.["resource"] | formatResource), |
| .["perceived-severity"], |
| .["alarm-text"], |
| .["last-raised"], |
| formatIsCleared(.["is-cleared"]) |
| ] |
| | colorize($severity; $cleared) |
| ) | @tsv; |
| |
| .["ietf-alarms:alarms"]?.["alarm-list"]?.["alarm"]? | main |