如图所示,如何制作服务器正常运行时间和停机时间的图表
How do I make a graph for server uptime and downtime as shown in the image
如何制作如下图 link 中的图表。服务器上每 5 分钟就会有一个脚本 运行 检查服务器是启动还是关闭。我的文件有日期时间戳,状态为 1/0 上下。现在我需要制作一个与图像中相同的图表。
状态文件如下所示:
Wed Mar 4 13:18:42 UTC 2015, Serverup
Wed Mar 4 13:23:42 UTC 2015, Serverup
Wed Mar 4 13:28:42 UTC 2015, Serverdown
Wed Mar 4 13:33:42 UTC 2015, Serverup
Wed Mar 4 13:38:42 UTC 2015, Serverup
Wed Mar 4 13:43:42 UTC 2015, Serverup
每次您使用 ImageMagick 确定其 UP/DOWN 状态时,我都会在服务器上重新生成图像。它安装在大多数 Linux 发行版上,可用于 OSX 和 Windows。
这是制作图像的小脚本,然后您只需在 HTML 中选取该图像即可。它的评论很好,所以你可以看到它在做什么:
#!/bin/bash
blockw=8 # width of up/down block
blockh=20 # height of up/down block
datew=100 # width of field containing date
dateh=60 # height of field containing date
xpos=0 # current output position
# Generate key to colours (BLUE part of explanatory image)
convert -background none -gravity west -pointsize 36 \
-size 60x60 xc:green \
-size 300x60 label:"Server UP" \
-size 60x60 xc:red \
-size 300x60 label:"Server DOWN" +append key.png
# Parse server status file
while read junk day date rest; do
day="$day $date"
# Set colour, green for up, and overwrite with red if down
colour="green"
[[ $rest == *"own"* ]] && colour="red"
# Just output our standard red/green status block (YELLOW part of explanatory image)
convert -size ${blockw}x${blockh} xc:$colour miff:-
# If day has changed, remember day for next time, and output it too
if [ "$day" != "$prevday" ]; then
prevday=$day
# Create our label for today
convert -size ${datew}x${dateh} -background none -pointsize 36 label:"$day" today.png
if [ $xpos -eq 0 ]; then
# Create very first datebar file
mv today.png datebar.png
datex=$datew
else
# Second or subsequent date, append to datebar file at correct position (MAGENTA part of explanatory image)
((xpad=xpos-datex))
convert -background none datebar.png -size ${xpad}x${dateh} xc:none today.png +append datebar.png
# Keep track of width of datebar
((datex=datex+xpad+datew))
fi
fi
# Keep track of our x position
((xpos+=blockw))
done < status.csv | convert -background white - +append \
datebar.png -append \
-gravity center -extent 110x600% \
-gravity South key.png -composite result.png
这是它的作用:
只是为了解释一下,如果您不太了解 ImageMagick,图像由 3 部分组成,在下图中以蓝色、黄色和洋红色着色。我已经标记了代码以显示在每个地方生成的部分。
如何制作如下图 link 中的图表。服务器上每 5 分钟就会有一个脚本 运行 检查服务器是启动还是关闭。我的文件有日期时间戳,状态为 1/0 上下。现在我需要制作一个与图像中相同的图表。
状态文件如下所示:
Wed Mar 4 13:18:42 UTC 2015, Serverup
Wed Mar 4 13:23:42 UTC 2015, Serverup
Wed Mar 4 13:28:42 UTC 2015, Serverdown
Wed Mar 4 13:33:42 UTC 2015, Serverup
Wed Mar 4 13:38:42 UTC 2015, Serverup
Wed Mar 4 13:43:42 UTC 2015, Serverup
每次您使用 ImageMagick 确定其 UP/DOWN 状态时,我都会在服务器上重新生成图像。它安装在大多数 Linux 发行版上,可用于 OSX 和 Windows。
这是制作图像的小脚本,然后您只需在 HTML 中选取该图像即可。它的评论很好,所以你可以看到它在做什么:
#!/bin/bash
blockw=8 # width of up/down block
blockh=20 # height of up/down block
datew=100 # width of field containing date
dateh=60 # height of field containing date
xpos=0 # current output position
# Generate key to colours (BLUE part of explanatory image)
convert -background none -gravity west -pointsize 36 \
-size 60x60 xc:green \
-size 300x60 label:"Server UP" \
-size 60x60 xc:red \
-size 300x60 label:"Server DOWN" +append key.png
# Parse server status file
while read junk day date rest; do
day="$day $date"
# Set colour, green for up, and overwrite with red if down
colour="green"
[[ $rest == *"own"* ]] && colour="red"
# Just output our standard red/green status block (YELLOW part of explanatory image)
convert -size ${blockw}x${blockh} xc:$colour miff:-
# If day has changed, remember day for next time, and output it too
if [ "$day" != "$prevday" ]; then
prevday=$day
# Create our label for today
convert -size ${datew}x${dateh} -background none -pointsize 36 label:"$day" today.png
if [ $xpos -eq 0 ]; then
# Create very first datebar file
mv today.png datebar.png
datex=$datew
else
# Second or subsequent date, append to datebar file at correct position (MAGENTA part of explanatory image)
((xpad=xpos-datex))
convert -background none datebar.png -size ${xpad}x${dateh} xc:none today.png +append datebar.png
# Keep track of width of datebar
((datex=datex+xpad+datew))
fi
fi
# Keep track of our x position
((xpos+=blockw))
done < status.csv | convert -background white - +append \
datebar.png -append \
-gravity center -extent 110x600% \
-gravity South key.png -composite result.png
这是它的作用:
只是为了解释一下,如果您不太了解 ImageMagick,图像由 3 部分组成,在下图中以蓝色、黄色和洋红色着色。我已经标记了代码以显示在每个地方生成的部分。