Skip to content

Commit

Permalink
[feat]playbook memcached add exporter
Browse files Browse the repository at this point in the history
Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu authored and caoxianfei1 committed Oct 23, 2023
1 parent 279c590 commit 48abdf1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
4 changes: 4 additions & 0 deletions playbook/memcached/hosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions playbook/memcached/scripts/clean.sh
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
42 changes: 38 additions & 4 deletions playbook/memcached/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 18 additions & 3 deletions playbook/memcached/scripts/status.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions playbook/memcached/scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit 48abdf1

Please sign in to comment.