Last active
June 10, 2019 17:19
-
-
Save boppy/5ee265ec67b771f4fd402cf82857d727 to your computer and use it in GitHub Desktop.
Results of `monit summary` rendered in json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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