如上面评论中所述,我为您制作了一些示例代码。它基于Redis,我建议您在集群管理器节点上运行Redis,该节点可能靠近集群的节点并且始终处于运行状态 - 因此是统计信息收集服务的理想选择。
示例代码是一个虚拟作业,用Python编写,监视例程用 编写bash
,但是该作业可以很容易地用 C/C++ 和Perl中的监视例程编写 - Redis有各种绑定- 不要'不要沉迷于一种语言。
即使你不会读Python,也很容易理解。有 3 个线程并行运行。只需使用总经过的处理时间更新Redisstring
中的 a 。另外两个使用时间序列数据更新Redis - 合成三角波 - 一个以 5 Hz 运行,另一个以 1 Hz 运行。 lists
我在变量不需要记录历史的地方使用了Redis字符串,在需要历史的地方使用了Redis列表。其他数据结构可用。
在下面的代码中,唯一有趣的 3 行是:
# Connect to Redis server by IP address/name
r = redis.Redis(host='localhost', port=6379, db=0)
# Set a Redis string called 'processTime' to value `processsTime`
r.set('processTime', processTime)
# Push a value to left end of Redis list
r.lpush(RedisKeyName, value)
这是正在监视的虚拟作业。从它说的地方开始阅读
######
# Main
######
这是代码:
#!/usr/local/bin/python3
import redis
import _thread
import time
import os
import random
################################################################################
# Separate thread periodically updating the 'processTime' in Redis
################################################################################
def processTimeThread():
"""Calculate time since we started and update every so often in Redis"""
start = time.time()
while True:
processTime = int(time.time() - start)
r.set('processTime', processTime)
time.sleep(0.2)
################################################################################
# Separate thread generating a times series and storing in Redis with the given
# name and update rate
################################################################################
def generateSeriesThread(RedisKeyName, interval):
"""Generate a saw-tooth time series and log to Redis"""
# Delete any values from previous runs
r.delete(RedisKeyName)
value = 0
inc = 1
while True:
# Generate next value and store in Redis
value = value + inc
r.lpush(RedisKeyName, value)
if value == 0:
inc = 1
if value == 10:
inc = -1
time.sleep(interval)
################################################################################
# Main
################################################################################
# Connect to Redis on local host - but could just as easily be on another machine
r = redis.Redis(host='localhost', port=6379, db=0)
# Get start time of job in RFC2822 format
startTime=time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
# ... and set Redis string "startTime"
r.set('startTime',startTime)
# Get process id (pid)
pid=os.getpid()
# ... and set Redis string "pid""
r.set('pid',pid)
# Start some threads generating data
_thread.start_new_thread( processTimeThread, () )
_thread.start_new_thread( generateSeriesThread, ('seriesA', 0.2) )
_thread.start_new_thread( generateSeriesThread, ('seriesB', 1) )
# Hang around (with threads still running) till user presses a key
key = input("Press Return/Enter to stop.")
然后我编写了一个bash
连接到 Redis 的监控脚本,获取值并在终端上以 TUI(文本用户界面)的形式显示它们。您可以同样使用 Python、Perl 或 PHP,同样可以编写图形界面或基于 Web 的界面。
#!/bin/bash
################################################################################
# drawGraph
################################################################################
drawGraph(){
top=$1 ; shift
data=( "$@" )
for ((row=0;row<10;row++)) ; do
((y=10-row))
((screeny=top+row))
line=""
for ((col=0;col<30;col++)) ; do
char=" "
declare -i v
v=${data[col]}
[ $v -eq $y ] && char="X"
line="${line}${char}"
done
printf "$(tput cup $screeny 0)%s" "${line}"
done
}
# Save screen and clear and make cursor invisible
tput smcup
tput clear
tput civis
# Trap exit
trap 'exit 1' INT TERM
trap 'tput rmcup; tput clear' EXIT
while :; do
# Get processid from Redis and display
pid=$(redis-cli <<< "get pid")
printf "$(tput cup 0 0)ProcessId: $pid"
# Get process start time from Redis and display
startTime=$(redis-cli <<< "get startTime")
printf "$(tput cup 1 0)Start Time: $startTime"
# Get process running time from Redis and display
processTime=$(redis-cli <<< "get processTime")
printf "$(tput cup 2 0)Running Time: $(tput el)$processTime"
# Display seriesA last few values
seriesA=( $(redis-cli <<< "lrange seriesA 0 30") )
printf "$(tput cup 5 0)seriesA latest values: $(tput el)"
printf "%d " "${seriesA[@]}"
# Display seriesB last few values
seriesB=( $(redis-cli <<< "lrange seriesB 0 30") )
printf "$(tput cup 6 0)seriesB latest values: $(tput el)"
printf "%d " "${seriesB[@]}"
drawGraph 8 "${seriesA[@]}"
drawGraph 19 "${seriesB[@]}"
# Put cursor at bottom of screen and tell user how to quit
printf "$(tput cup 30 0)Hit Ctrl-C to quit"
done
希望您能看到您可以非常轻松地从 Redis 中获取数据结构。这将获取processTime
集群节点上作业中设置的变量:
processTime=$(redis-cli <<< "get processTime")
TUI 如下所示: