Skip to content

Instantly share code, notes, and snippets.

@boppy
Last active June 10, 2019 17:19
Show Gist options
  • Save boppy/5ee265ec67b771f4fd402cf82857d727 to your computer and use it in GitHub Desktop.
Save boppy/5ee265ec67b771f4fd402cf82857d727 to your computer and use it in GitHub Desktop.
Results of `monit summary` rendered in json
#!/bin/bash
summary=$(monit summary -B)
numLines=$(wc -l <<< "$summary")
[[ $1 = "-h" ]] \
&& compact=0 \
|| compact=1
awk \
-v lastLineNR=$numLines \
-v compact=$compact \
-f monsum2json.awk \
<<< "$summary"
function trim(s) { sub(/(^\s+|\s+$)/, "", s); return s }
function indent(num) {
if ( compact == 1 ) {
return " "
} else {
printf "%*s", num, ""
}
}
BEGIN {
if ( compact == 1 ) {
ORS=""
}
FS="\\s{2,}"
print "{"
}
FNR == 1 {
split($0, a, "uptime:")
print indent(2) "\"system\": {"
print indent(4) "\"version\": \"" trim(a[1]) "\","
print indent(4) "\"uptime\": \"" trim(a[2]) "\""
print indent(2) "},"
print indent(2) "\"entries\": ["
}
FNR > 2 {
print indent(4) "{"
print indent(6) "\"type\": \"" $3 "\","
print indent(6) "\"name\": \"" trim($1) "\","
print indent(6) "\"status\": \"" $2 "\""
if ( lastLineNR == NR ) {
print indent(4) "}"
} else {
print indent(4) "},"
}
}
END {
print indent(2) "]"
print "}"
if ( compact == 1 ) {
print "\n"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment