Munin Plugin for redis requiring only `redis-cli`
#!/usr/bin/env bash | |
# | |
# Copyright 2013 Robert Allen | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
if [ 4 -gt ${BASH_VERSION:0:1} ]; then | |
echo "" | |
echo "bash version >= 4 required" | |
echo "" | |
exit 1 | |
fi | |
config=false | |
declare -A redis_info | |
if [ -z "${redis_cli}" ]; then | |
redis_cli=$(which redis-cli) | |
fi | |
# Used to setup the script, collect the redis data into an associative array | |
get_opts(){ | |
cmd=$(echo $0 | awk -F_ '{ print $2 }') | |
ip=$(echo $0 | awk -F_ '{ print $3 }') | |
port=$(echo $0 | awk -F_ '{ print $4 }') | |
[ -z "${ip}" ] && ip=127.0.0.1 | |
[ -z "${port}" ] && port=6379 | |
if [ -z "${password}" ]; then | |
password="" | |
else | |
password=" -a ${password}" | |
fi | |
for i in $( ${redis_cli} -h ${ip} -p ${port} ${password} info ); do | |
key=$(echo "$i" | cut -d":" -f1) | |
val=$(echo "$i" | cut -d":" -f2) | |
redis_info["$key"]=$( echo "$val" | tr -d "\r\n ") | |
done | |
} | |
############################################################################################### | |
# | |
# Fetchs the changes since last save | |
# | |
############################################################################################### | |
changes() { | |
if $config; then | |
echo "graph_title Redis Changes since last save" | |
echo "graph_category redis" | |
echo "changes.label changes" | |
else | |
printf "changes.value %s\n" ${redis_info[rdb_changes_since_last_save]} | |
fi | |
} | |
############################################################################################### | |
# | |
# Fetches the redis command execute count | |
# Fetches keyspace hits | |
# Fetches keyspace misses | |
# | |
############################################################################################### | |
commands() { | |
if $config; then | |
echo "graph_order commands hits misses" | |
echo "graph_title Redis Commands rate" | |
echo "graph_category redis" | |
echo "graph_vlabel commands/s" | |
echo "graph_info This graph monitors the commands rate" | |
echo "commands.label commands/s" | |
echo "commands.type COUNTER" | |
echo "commands.min 0" | |
echo "hits.label cache hits" | |
echo "hits.type COUNTER" | |
echo "hits.min 0" | |
echo "misses.label cache misses" | |
echo "misses.type COUNTER" | |
echo "misses.min 0" | |
else | |
printf "commands.value %s\n" ${redis_info[total_commands_processed]} | |
printf "hits.value %s\n" ${redis_info[keyspace_hits]} | |
printf "misses.value %s\n" ${redis_info[keyspace_misses]} | |
fi | |
} | |
############################################################################################### | |
# | |
# Fetches the total connections recieved counter | |
# | |
############################################################################################### | |
connections(){ | |
if $config; then | |
echo "graph_title Redis Connections rate" | |
echo "graph_category redis" | |
echo "graph_vlabel connections/s" | |
echo "graph_info This graph monitors the connctions rate" | |
echo "connections.label connections" | |
echo "connections.type COUNTER" | |
echo "connections.min 0" | |
else | |
printf "connections.value %s\n" ${redis_info[total_connections_received]} | |
fi | |
} | |
############################################################################################### | |
# | |
# Fetches the total background save time for redis | |
# | |
############################################################################################### | |
bg_save(){ | |
if $config; then | |
echo "graph_title Redis background save time" | |
echo "graph_category redis" | |
echo "graph_vlabel save time" | |
echo "graph_info This graph monitors the background save time required" | |
echo "bg_save_time.label seconds" | |
echo "bg_save_time.info Amount of seconds required by redis" | |
echo "bg_save_time.type COUNTER" | |
echo "bg_save_time.min 0" | |
echo "bg_time_since.label seconds" | |
echo "bg_time_since.info Amount of time since the last bg_save" | |
echo "bg_time_since.type COUNTER" | |
echo "bg_time_since.min 0" | |
else | |
printf "bg_save_time.value %s\n" ${redis_info[rdb_current_bgsave_time_sec]} | |
printf "bg_save_since.value %s\n" $(expr `date "+%s"` - ${redis_info[rdb_last_save_time]}) | |
fi | |
} | |
############################################################################################### | |
# | |
# Fetches the total memory in use by redis | |
# | |
############################################################################################### | |
memory(){ | |
if $config; then | |
echo "graph_title Redis Memory" | |
echo "graph_category redis" | |
echo "graph_vlabel mem used" | |
echo "graph_info This graph monitors the commands rate" | |
echo "graph_args --base 1024 -l 0" | |
echo "memory.label memory" | |
echo "memory.info Amount of mem used by redis" | |
echo "memory.type GAUGE" | |
echo "memory.min 0" | |
echo "memory.draw AREA" | |
else | |
printf "memory.value %s\n" ${redis_info[used_memory]} | |
fi | |
} | |
############################################################################################### | |
# | |
# Fetches the total clients connected to redis count | |
# | |
############################################################################################### | |
users(){ | |
if $config; then | |
echo "graph_title Redis Clients" | |
echo "graph_category redis" | |
echo "graph_vlabel connections/s" | |
echo "graph_info This graph monitors the number of clients" | |
echo "clients.label clients" | |
echo "clients.type GAUGE" | |
echo "clients.min 0" | |
else | |
printf "clients.value %s\n" ${redis_info[connected_clients]} | |
fi | |
} | |
############################################################################################### | |
# | |
# Fetches total blocked client(s) count | |
# | |
############################################################################################### | |
blocked(){ | |
if $config; then | |
echo "graph_title Redis Clients Blocked" | |
echo "graph_category redis" | |
echo "graph_vlabel blocked" | |
echo "graph_info This graph monitors the number of clients" | |
echo "blocked_clients.label clients" | |
echo "blocked_clients.type GAUGE" | |
echo "blocked_clients.min 0" | |
else | |
printf "blocked_clients.value %s\n" ${redis_info[blocked_clients]} | |
fi | |
} | |
############################################################################################### | |
# | |
# Internal function to output from the required command | |
# | |
############################################################################################### | |
output_values() { | |
get_opts | |
case $cmd in | |
(changes) | |
changes | |
;; | |
(commands) | |
commands | |
;; | |
(connections) | |
connections | |
;; | |
(memory) | |
memory | |
;; | |
(users) | |
users | |
;; | |
(blocked) | |
blocked | |
;; | |
(bgsave) | |
bg_save | |
;; | |
*) | |
output_usage | |
;; | |
esac | |
} | |
############################################################################################### | |
# | |
# Print the help text | |
# | |
############################################################################################### | |
output_usage() { | |
printf >&2 "%s - munin plugin to graph redis runtime values\n" ${0##*/} | |
printf >&2 "Usage:\n" | |
printf >&2 "\n" | |
printf >&2 "for i in changes commands memory connections users blocked bgsave; do\n" | |
printf >&2 " ln -snf %s /etc/munin/plugins/redis_\${i}_127.0.0.1_6379\n" ${0##*/} | |
printf >&2 "done\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "Config Options:\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "[redis_*]\n" | |
printf >&2 "\n" | |
printf >&2 "user redis\n" | |
printf >&2 "group redis\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "\n" | |
printf >&2 "[redis_127.0.0.1_6379]\n" | |
printf >&2 "\n" | |
printf >&2 "user redis\n" | |
printf >&2 "group redis\n" | |
printf >&2 "env.password my_redis_password # optional\n" | |
printf >&2 "env.redis_cli /usr/local/bin/redis-cli # optional\n" | |
printf >&2 "\n" | |
} | |
# Roll it up and run it | |
case $# in | |
0) | |
output_values | |
;; | |
1) | |
case $1 in | |
config) | |
config=true | |
output_values | |
;; | |
*) | |
output_usage | |
exit 1 | |
;; | |
esac | |
;; | |
*) | |
output_usage | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment