简易监控ceph集群状态变化脚本

有时候需要实时监控并显示集群状态变化情况,所以直接写了个脚本,脚本的功能比较简单就是对比集群状态的变化,然后将变化打印出来。

将下面的代码复制后,保存为check.sh即可:

 1 #!/bin/bash
 2 num=0
 3 cmd='ceph -s'
 4 echo -e '脚本开始运行,退出请按Ctrl+c,现在时间:'`date '+%Y-%m-%d %H:%M:%S'`
 5 while true
 6 do
 7  $cmd &>/tmp/oldstatus
 8  echo $cmd'目前的状态为:'
 9  grep -n '. *' /tmp/oldstatus
10  sleep 1
11  $cmd &>/tmp/newstatus
12  diff /tmp/oldstatus /tmp/newstatus >/dev/null
13  while [ $? -eq 0 ]
14  do
15   $cmd &>/tmp/oldstatus
16   sleep 1
17   $cmd &>/tmp/newstatus
18   diff /tmp/oldstatus /tmp/newstatus >/dev/null
19  done
20  num=$[$num+1]
21  echo '现在时间:'`date '+%Y-%m-%d %H:%M:%S'`','$cmd'的状态第'$num'次发生变化:'
22  echo '--------------------'
23  diff /tmp/oldstatus /tmp/newstatus | sed "s/^-/$(tput setaf 5)&/; s/^</$(tput setaf 2)&/; s/^>/$(tput setaf 1)&/; s/$/$(tput sgr0)/"
24  echo '--------------------'
25  echo -e '...
'
26 done
27 exit 0

执行脚本,从下图脚本执行的结果中可以实时查看集群的状态发生了哪些变化。

 jihuo

原文地址:https://www.cnblogs.com/xzy186/p/14240037.html