码迷,mamicode.com
首页 > 其他好文 > 详细

check_monitor

时间:2019-10-17 01:34:58      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:显示   data   config   bee   pack   关键字   ice   绝对路径   配置文件   

#! /bin/bash

# 声明agent配置文件路径
CONF=/etc/sdata/zabbix/zabbix_agentd.conf
#CONF=/tmp/zabbix_agentd.conf

# -F指定=号切割,寻找关键字段Hostname打头的,用=切割后显示第二行的数值, 最后一位指定文件绝对路径
config_hostname=`awk -F "=" '/Hostname/{print $2}' $CONF`
# 执行hostname命令并将结果赋值给host
host=`hostname`
# cat redhat-release文件,使用 . 进行切割, 打印第一行,
osmaster=`cat /etc/redhat-release |awk -F '.' '{print $1}' |awk '{print $NF}'`

# -qa查询rpm安装的agent信息
pkg=`rpm -qa zabbix-agent| wc -l`

echo "############## Package #################"
# 如果小于1,说明没有安装
if [ $pkg -lt 1 ];then  #回显信息
   echo 'Need install monitor agent, Please excute command: "curl http://10.10.238.114:4507/repo/install.sh -o /tmp/install.sh && sh /tmp/install.sh"' 
   echo "########################################"
   exit 1
else
   echo "Monitor agent has been installed."
fi

# check zabbix status
echo "############## Status ##################"
# ps 查询进程信息
proc_num=`ps -ef|grep zabbix |wc -l`
# 大于1,说明已运行
if [ $proc_num -gt 1 ];then
   STATUS=1
   echo 'Monitor agent is running.'
else
   STATUS=0
   echo 'Monitor agent is stopped.'
fi

# check conf Hostname
echo "############## Config ##################"
echo -ne 'Check monitor agent conf Hostname: '

# 比较两个主机名是否相同
if [ "$config_hostname" != "$host" ];then
    MODIFY=1
    echo "Modify Monitor Agent Hostname: ${config_hostname} --> $host."
    # 对值进行替换,在 $CONF指定的文件中, 寻找已Hostname=开头的字段,替换为$host指定的值, g全局
    sed -i 's/^\(Hostname=\).*$/\1'$host'/g' $CONF
else
    MODIFY=0
    echo -ne "Hostname has no change.\n"
fi

# Start monitor agent
echo "############## Restart #################"
if [ $MODIFY -eq 1 ];then
    if [ $STATUS -eq 1 ];then
        echo "Modify config file, Restart Monitor Agent."
        if [ $osmaster -eq 7 ];then
            systemctl restart zabbix-agent
        else
            service zabbix-agent restart
        fi
    else
        echo "Start Monitor Agent."
        if [ $osmaster -eq 7 ];then
            systemctl start zabbix-agent
        else
            service zabbix-agent start
        fi
    fi
else
    if [ $STATUS -eq 0 ];then
        echo "Start Monitor Agent."
        if [ $osmaster -eq 7 ];then
            systemctl start zabbix-agent
        else
            service zabbix-agent start
        fi
    else
        echo "Monitor agent service is running normally."
        echo "########################################"
        exit 0
    fi
fi

sleep 1


echo "############## Status ##################"
# check agent status
# 查看zabbix的进程数量
proc_num=`ps -ef|grep zabbix |wc -l`
如果大于1条,说明agent正常运行
if [ $proc_num -gt 1 ];then
   echo 'Monitor Agent is running.'
else
   echo 'Monitor Agent is stopped.'
   if [ $osmaster -eq 7 ];then   # 如果centos是7版本,回显命令让用户执行
      echo "Please excute command: 'systemctl start zabbix-agent'"
   else
      echo "Please excute command: 'service zabbix-agent start'" # 回显非7版本的centos启动命令
      
   fi
fi
echo "########################################"

check_monitor

标签:显示   data   config   bee   pack   关键字   ice   绝对路径   配置文件   

原文地址:https://www.cnblogs.com/0916m/p/11689434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!