From 48abdf13622f92aa6857b701191592682d62bfc7 Mon Sep 17 00:00:00 2001 From: Cyber-SiKu Date: Mon, 23 Oct 2023 15:52:39 +0800 Subject: [PATCH] [feat]playbook memcached add exporter Signed-off-by: Cyber-SiKu --- playbook/memcached/hosts.yaml | 4 +++ playbook/memcached/scripts/clean.sh | 16 +++++++++++ playbook/memcached/scripts/deploy.sh | 42 +++++++++++++++++++++++++--- playbook/memcached/scripts/status.sh | 21 ++++++++++++-- playbook/memcached/scripts/stop.sh | 12 ++++++++ 5 files changed, 88 insertions(+), 7 deletions(-) diff --git a/playbook/memcached/hosts.yaml b/playbook/memcached/hosts.yaml index ee69994cc..81ac0eaed 100644 --- a/playbook/memcached/hosts.yaml +++ b/playbook/memcached/hosts.yaml @@ -12,8 +12,10 @@ hosts: - SUDO_ALIAS=sudo - ENGINE=docker - IMAGE=memcached:1.6.17 + - EXPORTER_IMAGE=quay.io/prometheus/memcached-exporter:v0.13.0 - LISTEN=10.0.1.1 - PORT=11211 + - EXPORTER_PORT=9150 - USER=root - MEMORY_LIMIT=32768 # item memory in megabytes - MAX_ITEM_SIZE=8m # adjusts max item size (default: 1m, min: 1k, max: 1024m) @@ -33,6 +35,7 @@ hosts: - IMAGE=memcached:1.6.17 - LISTEN=10.0.1.2 - PORT=11211 + - EXPORTER_PORT=9150 - USER=root - MEMORY_LIMIT=32768 # item memory in megabytes - MAX_ITEM_SIZE=8m # adjusts max item size (default: 1m, min: 1k, max: 1024m) @@ -52,6 +55,7 @@ hosts: - IMAGE=memcached:1.6.17 - LISTEN=10.0.1.3 - PORT=11211 + - EXPORTER_PORT=9150 - USER=root - MEMORY_LIMIT=32768 # item memory in megabytes - MAX_ITEM_SIZE=8m # adjusts max item size (default: 1m, min: 1k, max: 1024m) diff --git a/playbook/memcached/scripts/clean.sh b/playbook/memcached/scripts/clean.sh index 84e0f2a6a..f72a801f2 100644 --- a/playbook/memcached/scripts/clean.sh +++ b/playbook/memcached/scripts/clean.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash g_container_name="memcached-"${PORT} +g_expoter_container_name="memcached-exporter-"${EXPORTER_PORT} g_docker_cmd="${SUDO_ALIAS} ${ENGINE}" g_rm_cmd="${SUDO_ALIAS} rm -rf" @@ -23,6 +24,13 @@ precheck() { die "container [${g_container_name}] not exists!!!\n" exit 1 fi + if [ "${EXPORTER_PORT}" ];then + container_id=`${g_docker_cmd} ps --all --format "{{.ID}}" --filter name=${g_expoter_container_name}` + if [ -z ${container_id} ]; then + die "container [${g_expoter_container_name}] not exists!!!\n" + exit 1 + fi + fi } stop_container() { @@ -32,6 +40,14 @@ stop_container() { exit 1 fi success "rm container[${g_container_name}]\n" + if [ "${EXPORTER_PORT}" ];then + msg=`${g_docker_cmd} rm ${g_expoter_container_name}` + if [ $? -ne 0 ];then + die "${msg}\n" + exit 1 + fi + success "rm container[${g_expoter_container_name}]\n" + fi } rm_cachefile() { diff --git a/playbook/memcached/scripts/deploy.sh b/playbook/memcached/scripts/deploy.sh index f2c48b165..66d4eb2c4 100644 --- a/playbook/memcached/scripts/deploy.sh +++ b/playbook/memcached/scripts/deploy.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash g_container_name="memcached-"${PORT} +g_expoter_container_name="memcached-exporter-"${EXPORTER_PORT} g_start_args="" +g_expoter_start_args="" g_docker_cmd="${SUDO_ALIAS} ${ENGINE}" g_lsof_cmd="${SUDO_ALIAS} lsof" g_rm_cmd="${SUDO_ALIAS} rm -rf" @@ -28,7 +30,21 @@ precheck() { container_id=`${g_docker_cmd} ps --format "{{.ID}}" --filter name=${g_container_name} --all` if [ "${container_id}" ]; then success "container [${g_container_name}] already exists, skip\n" - exit 0 + exit 1 + fi + + if [ "${EXPORTER_PORT}" ];then + container_id=`${g_docker_cmd} ps --format "{{.ID}}" --filter name=${g_expoter_container_name} --all` + if [ "${container_id}" ]; then + success "container [${g_expoter_container_name}] already exists, skip\n" + exit 1 + fi + + ${g_lsof_cmd} -i:${EXPORTER_PORT} >& /dev/null + if [ $? -eq 0 ];then + die "port[${EXPORTER_PORT}] is in use!\n" + exit 1 + fi fi # check port @@ -79,14 +95,25 @@ init() { if [ "${VERBOSE}" ];then g_start_args="${g_start_args} -${VERBOSE}" fi + + + if [ "${EXPORTER_PORT}" ];then + g_expoter_start_args="${g_expoter_start_args} --memcached.address=${LISTEN}:${PORT}" + g_expoter_start_args="${g_expoter_start_args} --web.listen-address=${LISTEN}:${EXPORTER_PORT}" + fi } create_container() { - success "create container [${g_container_name}]\n" ${g_docker_cmd} create --name ${g_container_name} ${g_user} --network host ${g_volume_bind} ${IMAGE} memcached ${g_start_args} >& /dev/null - - success "start container [${g_container_name}]\n" + success "create container [${g_container_name}]\n" + if [ "${EXPORTER_PORT}" ];then + ${g_docker_cmd} create --name ${g_expoter_container_name} --network host ${EXPORTER_IMAGE} ${g_expoter_start_args} >& /dev/null + success "create container [${g_container_name}]\n" + fi ${g_docker_cmd} start ${g_container_name} >& /dev/null + success "start container [${g_container_name}]\n" + ${g_docker_cmd} start ${g_expoter_container_name} >& /dev/null + success "start container [${g_expoter_container_name}]\n" success "wait 3 seconds, check container status...\n" sleep 3 @@ -95,14 +122,21 @@ create_container() { if [ ${g_status} != "running" ]; then exit 1 fi + if [ "${EXPORTER_PORT}" ];then + if [ ${g_expoter_status} != "running" ]; then + exit 1 + fi + fi } get_status_container() { g_status=`${g_docker_cmd} inspect --format='{{.State.Status}}' ${g_container_name}` + g_expoter_status=`${g_docker_cmd} inspect --format='{{.State.Status}}' ${g_expoter_container_name}` } show_info_container() { ${g_docker_cmd} ps --all --filter "name=${g_container_name}" --format="table {{.ID}}\t{{.Names}}\t{{.Status}}" + ${g_docker_cmd} ps --all --filter "name=${g_expoter_container_name}" --format="table {{.ID}}\t{{.Names}}\t{{.Status}}" } precheck diff --git a/playbook/memcached/scripts/status.sh b/playbook/memcached/scripts/status.sh index ed875ec85..fcc971f62 100644 --- a/playbook/memcached/scripts/status.sh +++ b/playbook/memcached/scripts/status.sh @@ -1,11 +1,11 @@ #!/usr/bin/env bash g_container_name="memcached-"${PORT} +g_expoter_container_name="memcached-exporter-"${EXPORTER_PORT} g_start_args="" g_docker_cmd="${SUDO_ALIAS} ${ENGINE}" g_volume_bind="" g_container_id="" -g_status="running" function msg() { printf '%b' "$1" >&2 @@ -26,20 +26,35 @@ precheck() { success "container [${g_container_name}] not exists!!!" exit 1 fi + if [ "${EXPORTER_PORT}" ];then + g_container_id=`${g_docker_cmd} ps --all --format "{{.ID}}" --filter name=${g_expoter_container_name}` + if [ -z ${g_container_id} ]; then + success "container [${g_expoter_container_name}] not exists!!!" + exit 1 + fi + fi } show_info_container() { ${g_docker_cmd} ps --all --filter "name=${g_container_name}" --format="table {{.ID}}\t{{.Names}}\t{{.Status}}" + ${g_docker_cmd} ps --all --filter "name=${g_expoter_container_name}" --format="table {{.ID}}\t{{.Names}}\t{{.Status}}" } show_ip_port() { printf "memcached addr:\t%s:%d\n" ${LISTEN} ${PORT} + printf "memcached-expoter addr:\t%s:%d\n" ${LISTEN} ${EXPORTER_PORT} } get_status_container() { - g_status=`${g_docker_cmd} inspect --format='{{.State.Status}}' ${g_container_name}` - if [ ${g_status} != "running" ]; then + status=`${g_docker_cmd} inspect --format='{{.State.Status}}' ${g_container_name}` + if [ ${status} != "running" ]; then + exit 1 + fi + if [ "${EXPORTER_PORT}" ];then + status=`${g_docker_cmd} inspect --format='{{.State.Status}}' ${g_expoter_container_name}` + if [ ${status} != "running" ]; then exit 1 + fi fi } diff --git a/playbook/memcached/scripts/stop.sh b/playbook/memcached/scripts/stop.sh index 2dc84e53f..ebd7ee87d 100644 --- a/playbook/memcached/scripts/stop.sh +++ b/playbook/memcached/scripts/stop.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash g_container_name="memcached-"${PORT} +g_expoter_container_name="memcached-exporter-"${EXPORTER_PORT} g_docker_cmd="${SUDO_ALIAS} ${ENGINE}" function msg() { @@ -22,11 +23,22 @@ precheck() { die "container [${g_container_name}] not exists!!!\n" exit 1 fi + if [ "${EXPORTER_PORT}" ];then + container_id=`${g_docker_cmd} ps --all --format "{{.ID}}" --filter name=${g_expoter_container_name}` + if [ -z ${container_id} ]; then + die "container [${g_expoter_container_name}] not exists!!!\n" + exit 1 + fi + fi } stop_container() { ${g_docker_cmd} stop ${g_container_name} >& /dev/null success "stop container[${g_container_name}]\n" + if [ "${EXPORTER_PORT}" ];then + ${g_docker_cmd} stop ${g_expoter_container_name} >& /dev/null + success "stop container[${g_expoter_container_name}]\n" + fi } precheck