Last active
August 7, 2021 10:33
-
-
Save dylan4224/e44a508b8cce683347e4090303327b7f to your computer and use it in GitHub Desktop.
website response speed monitor with collectd
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
# file: curl.conf | |
LoadPlugin curl | |
<Plugin curl> | |
<Page "bing_cn"> | |
URL "https://cn.bing.com" | |
MeasureResponseTime true | |
MeasureResponseCode true | |
<Statistics> | |
TotalTime true | |
NamelookupTime true | |
SpeedDownload true | |
</Statistics> | |
</Page> | |
</Plugin> |
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
# file: website_speed.conf | |
LoadPlugin exec | |
<Plugin exec> | |
Exec "nobody:nogroup" "/etc/collectd/collectd.conf.d/website_speed.sh" | |
</Plugin> | |
# file: /etc/collectd/collectd.conf.d/website_speed.sh | |
#!/usr/bin/env bash | |
# set -o pipefail | |
# See https://starbeamrainbowlabs.com/blog/article.php?article=posts/361-Monitoring-Http-Response-Times-Collectd.html | |
# Variables: | |
# COLLECTD_INTERVAL Interval at which to collect data | |
# COLLECTD_HOSTNAME The hostname of the local machine | |
HOSTNAME="${COLLECTD_HOSTNAME:-localhost}" | |
INTERVAL="${COLLECTD_INTERVAL:-60}" | |
declare -A targets=( | |
["bing_en"]="https://www2.bing.com" | |
["bing_cn"]="https://cn.bing.com" | |
["google"]="https://www.google.com/ncr" | |
["facebook"]="https://www.facebook.com" | |
["youtube"]="https://www.youtube.com" | |
) | |
# Pure-bash alternative to sleep. | |
# Source: https://blog.dhampir.no/content/sleeping-without-a-subprocess-in-bash-and-how-to-sleep-forever | |
snore() { | |
local IFS; | |
[[ -n "${_snore_fd:-}" ]] || exec {_snore_fd}<> <(:); | |
read ${1:+-t "$1"} -u $_snore_fd || :; | |
} | |
function check_target() { | |
target_name="$1" | |
target_url="$2" | |
readarray -t result < <(curl -sS -o /dev/null -w "%{http_code}\n%{time_namelookup}\n%{time_connect}\n%{time_total}\n%{speed_download}" "${target_url}") | |
echo "PUTVAL \"${HOSTNAME}/website_speed/gauge-${target_name}-http_code\" interval=${INTERVAL} N:${result[0]}" | |
echo "PUTVAL \"${HOSTNAME}/website_speed/gauge-${target_name}-time_namelookup\" interval=${INTERVAL} N:${result[1]}" | |
echo "PUTVAL \"${HOSTNAME}/website_speed/gauge-${target_name}-time_connect\" interval=${INTERVAL} N:${result[2]}" | |
echo "PUTVAL \"${HOSTNAME}/website_speed/gauge-${target_name}-time_total\" interval=${INTERVAL} N:${result[3]}" | |
echo "PUTVAL \"${HOSTNAME}/website_speed/gauge-${target_name}-speed_download\" interval=${INTERVAL} N:${result[4]}" | |
} | |
while :; do | |
for target in "${!targets[@]}"; do | |
check_target "${target}" "${targets[${target}]}" | |
done | |
snore "${INTERVAL}"; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment