shell脚本监控

一、进程服务检测自动拉取

运用知识点

. /etc/bashrc   加载用户的环境变量
打点到falcon 里面
执行的日志输出

脚本内容

#!/bin/bash
. /etc/bashrc
region=shdl
host=`hostname`
end_timestamp=`date +%s`
status_num=`yarn application --list|grep AppPre|wc -l`
status=`yarn application --list|grep Pre|awk '{print $6}'`
stream_lock=`cat /home/eos/monitor/stream_lock.log`

if [ $status_num = '1' ] ;then
    > /home/eos/monitor/stream_lock.log    
    if [ $status = 'RUNNING' ] ;then    
        echo -e "33[32mss_pre process is running33[0m"
        curl -X POST -d "[{"metric": "eniot_${region}_pre_status", "endpoint": "eniot_${region}_pre_status", "timestamp": $end_timestamp,"step": 60,"value": 1,"counterType": "GAUGE","tags": "region=$region,host=${host}"}]" http://127.0.0.1:1988/v1/push &> /dev/null
        
    else
        echo -e "33[32mss_pre process is error33[0m"
    curl -X POST -d "[{"metric": "eniot_${region}_pre_status", "endpoint": "eniot_${region}_pre_status", "timestamp": $end_timestamp,"step": 60,"value": 2,"counterType": "GAUGE","tags": "region=$region,host=${host}"}]" http://127.0.0.1:1988/v1/push &> /dev/null
    fi    
    
else
    if [ $stream_lock = 0 ] ;then
        echo -e "33[32mss_pre process is error33[0m"
        curl -X POST -d "[{"metric": "eniot_${region}_pre_status", "endpoint": "eniot_${region}_pre_status", "timestamp": $end_timestamp,"step": 60,"value": 2,"counterType": "GAUGE","tags": "region=$region,host=${host}"}]" http://127.0.0.1:1988/v1/push &> /dev/null
    else

        echo -e "33[32mss_pre process not running33[0m"
        curl -X POST -d "[{"metric": "eniot_${region}_pre_status", "endpoint": "eniot_${region}_pre_status", "timestamp": $end_timestamp,"step": 60,"value": 0,"counterType": "GAUGE","tags": "region=$region,host=${host}"}]" http://127.0.0.1:1988/v1/push &> /dev/null
        cd /home/eos/energy-os/ss_pre ;sh bin/submit_yarn.sh
        echo 0 > /home/eos/monitor/stream_lock.log
    fi
fi

定时任务中写入

[root@streaming-app0001 cron.d]# cat stream_pre 
*/2 * * * * eos /bin/sh /home/eos/monitor/watchdog_stream.sh >> /home/eos/monitor/watchdog_stream.log 2>&1

二、针对一些特殊输出,如java 版本的输出

java输出的问题

java -version >> 1.log 不输出到文件
java -version >> 1.log 2>&1    会输出到文件

 三、shell的dubug调试模式

1)在指定shell运行版本时加上 '-x'

如。#!/bin/bash   -x

测试脚本如下

demo git:(master) ✗ cat debug.sh
#!/bin/bash -x
 
 
echo "hi"
date
sleep 1
echo "hi"
date

调试结果如下

➜  demo git:(master) ✗ ./debug.sh
+ echo hi
hi
+ date
2019年 1月 7日 星期一 13时24分18秒 CST
+ sleep 1
+ echo hi
hi
+ date
原文地址:https://www.cnblogs.com/linu/p/12452529.html