Last active
September 26, 2019 22:03
-
-
Save sunjon/271230450aec29a4ef908ef2da5defaf to your computer and use it in GitHub Desktop.
Shinken stats for TV display
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
#!/usr/bin/env bash | |
SHINKEN_HOST='http://shinken01.se-ix.delta.prod:50000' | |
OUTPUT_FILE='./output.html' | |
CURL_TIMEOUT=1 | |
declare -a COLUMN_KEYS | |
COLUMN_KEYS=( | |
host_name | |
description | |
state | |
plugin_output | |
) | |
# last_check | |
# host_last_time_up | |
# host_current_attempt | |
# host_max_check_attempts | |
LIVESTATUS_QUERY=" | |
GET services\n | |
OutputFormat: json\n | |
ColumnHeaders: -\n | |
Localtime: $(date +%s)\n | |
KeepAlive: off\n | |
Columns: ${COLUMN_KEYS[@]}\n | |
Filter: state = 1\n | |
Filter: state = 2\n | |
Or: 2\n | |
Filter: acknowledged = 0\n | |
" | |
repeat() { | |
local char=$1 | |
local count=$2 | |
printf -- "$char%.0s" $(seq 1 "$count") | |
} | |
format_query() { | |
local content=$1 | |
local output | |
banner=$(repeat '-' 22) | |
output="$banner\n" | |
output+="$content\n\n$banner" | |
printf '%s' "$output" | |
} | |
generate_td() { | |
local cell_column=$1 | |
local cell_content=$2 | |
local table_row | |
printf "<td class='cell100 column$cell_column'>%s</td>\n" "$cell_content" | |
} | |
json_to_htmltable() { | |
local json=$1 | |
local table="<table>\n" | |
local table_row | |
for row in $(echo "${json}" | jq -r '.[] | @base64'); do | |
_jq() { | |
echo "${row}" | base64 --decode | jq -r "${1}" | |
} | |
table_row='<tr>' | |
for c in "${!COLUMN_KEYS[@]}"; do | |
table_row+=$(generate_td $((c+1)) "$(_jq """.[$c]""" )") | |
done | |
table_row+="</tr>\n" | |
table+=$table_row | |
done | |
echo -e "$table</table>" | |
} | |
generate_html_document() { | |
local content=$1 | |
template=$(<"./template.html") | |
echo "${template/--CONTENT_PLACEHOLDER--/$content}" > "$OUTPUT_FILE" | |
} | |
main() { | |
local post_data | |
local curl_output | |
local html_table | |
post_data=$(format_query "$LIVESTATUS_QUERY") | |
curl_output=$(curl -m $CURL_TIMEOUT "$SHINKEN_HOST/query" \ | |
-H 'Content-Type: text/xml' -d "$(echo -e $post_data)" 2>/dev/null) | |
html_table=$(json_to_htmltable "$curl_output") | |
generate_html_document "$html_table" | |
echo "Done!" | |
} | |
main | |
exit 0 |
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
<!DOCTYPE html> | |
<html lang='en'> | |
<head> | |
<meta charset='utf-8'> | |
<title>Shinken TV view</title> | |
<meta name='description' content='Shinken TV view'> | |
<meta name='author' content='Delta Projects'> | |
<meta name='viewport' content='width=device-width, initial-scale=1'> | |
<link rel='icon' type='image/png' href='images/icons/favicon.ico'/> | |
<link rel='stylesheet' type='text/css' href='vendor/bootstrap/css/bootstrap.min.css'> | |
<link rel='stylesheet' type='text/css' href='fonts/font-awesome-4.7.0/css/font-awesome.min.css'> | |
<link rel='stylesheet' type='text/css' href='vendor/animate/animate.css'> | |
<link rel='stylesheet' type='text/css' href='vendor/select2/select2.min.css'> | |
<link rel='stylesheet' type='text/css' href='vendor/perfect-scrollbar/perfect-scrollbar.css'> | |
<link rel='stylesheet' type='text/css' href='css/util.css'> | |
<link rel='stylesheet' type='text/css' href='css/main.css'> | |
</head> | |
<body> | |
<div class='limiter'> | |
<div class='container-table100'> | |
<div class='wrap-table100'> | |
<div class='table100 ver3 m-b-110'> | |
<div class='table100-body js-pscroll'> | |
--CONTENT_PLACEHOLDER-- | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment