12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #!/bin/sh
- # {{{ Variables
- URL=""
- INFO=""
- CRAWLER=""
- INFOS="active accepted handled requests reading writing waiting"
- # }}}
- # {{{ usage()
- usage() {
- cat <<EOH
- USAGE: $(basename "$0") -u URL -i STATUS_INFO
- Get Nginx status
- Options:
- -u Nginx status URL (e.g: http://localhost/nginx_status
- -i Status info in $(echo "${INFOS}" | sed 's/ /, /g')
- EOH
- }
- # }}}
- # {{{ main()
- while getopts "u:i:" OPT ; do
- case "$OPT" in
- \?|h)
- usage
- exit 0
- ;;
- u)
- URL="$OPTARG"
- ;;
- i)
- INFO="$OPTARG"
- ;;
- esac
- done
- if which curl >/dev/null 2>&1 ; then
- CRAWLER="curl -s"
- elif which wget >/dev/null 2>&1 ; then
- CRAWLER="wget -q -O -"
- else
- printf "[ERR] wget or curl not found\n"
- usage
- exit 1
- fi
- if [ -z "${URL}" ]; then
- printf "[ERR] URL not defined or empty\n"
- usage
- exit 1
- fi
- if ! echo "${INFO}" | egrep -q "^($(echo "${INFOS}" | sed 's/ /|/g'))$"; then
- printf "[ERR] info '${INFO}' not available in ${INFOS}\n"
- usage
- exit 1
- fi
- case "${INFO}" in
- #INFOS="active accepted handled requests reading writing waiting"
- active)
- $CRAWLER "${URL}" | sed -r -n 's/^Active connections:\s+//p'
- ;;
- accepted)
- $CRAWLER "${URL}" | sed -r -n 's/^\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)/\1/p'
- ;;
- handled)
- $CRAWLER "${URL}" | sed -r -n 's/^\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)/\2/p'
- ;;
- requests)
- $CRAWLER "${URL}" | sed -r -n 's/^\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)/\3/p'
- ;;
- reading)
- $CRAWLER "${URL}" | sed -r -n 's/^Reading:\s+([0-9]+)\s+Writing:\s+([0-9]+)\s+Waiting:\s+([0-9]+)\s+$/\1/p'
- ;;
- writing)
- $CRAWLER "${URL}" | sed -r -n 's/^Reading:\s+([0-9]+)\s+Writing:\s+([0-9]+)\s+Waiting:\s+([0-9]+)\s+$/\2/p'
- ;;
- waiting)
- $CRAWLER "${URL}" | sed -r -n 's/^Reading:\s+([0-9]+)\s+Writing:\s+([0-9]+)\s+Waiting:\s+([0-9]+)\s+$/\3/p'
- ;;
- esac
- exit 0
- # }}}
- # vim: foldmethod=marker foldlevel=0 foldenable
|