zoukankan      html  css  js  c++  java
  • shell脚本检测网络是否畅通

    shell初始化安装脚本执行时,需从网络上安装一些rpm包,所有需要先检测网络的畅通性, 

    代码

    #检测网络链接&&ftp上传数据  
    function networkAndFtp()  
    {  
        #超时时间  
        timeout=5  
          
        #目标网站  
        target=www.baidu.com  
      
        #获取响应状态码  
        ret_code=`curl -I -s --connect-timeout $timeout $target -w %{http_code} | tail -n1`  
      
        if [ "x$ret_code" = "x200" ]; then  
            #网络畅通  
        else  
            #网络不畅通  
        fi  
    }  

    实际脚本:

    #判断网络是否配置正确,如果网络不通,退出安装程序
     
    net_status=`curl -I -s --connect-timeout 5 www.baidu.com -w %{http_code} |tail -n1`
     
    if [ $net_status -eq 200 ];then
          echo -e "33[32m[ #########the network connecting is normal, installing now############ ]33[0m"
          sleep 1
    else 
         echo -e "33[31m33[01m[ ##########the network connecting is unstable, please check the network firstly ,then start the install again . 网络连接有问题,安装即将退出,请检查网络后再次安装##########  ]33[0m"
         sleep 1
         exit 1
    fi

    IDC机房到阿里云vpc网络ping网络连通性

    配置zabbix客户端配置文件

    vim /etc/zabbix/zabbix_agentd.conf

    添加  Include=/etc/zabbix/zabbix_agentd.d/

    cat net_status.sh 
    #!/bin/bash
    
    #判断网络延时 idc机房到阿里云vpc网络
    
    #设置环境变量
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
    export PATH
    
    while getopts "c:i:" opt; do
        case $opt in
            c)
                count=$OPTARG
                ;;
            i)
                ips=$OPTARG
                ;;
            ?)
                ;;
        esac
    done
    
    tmp_fifofile="/tmp/ping.fifo"
    mkfifo $tmp_fifofile
    exec 6<>$tmp_fifofile
    rm $tmp_fifofile
    
    for ((i=0;i<20;i++));do
        echo
    done >&6
    
    for ip in `echo $ips |awk -F'|' '{  for(i=1;i<=NF;i++) {print $i} }'`
    do
    read -u 6
    {
        rtt=`ping -c $count $ip |grep rtt |awk '{print $4}' |awk -F'/' '{print $2}'`
        rtt=${rtt:-'0'}
        echo  $rtt
    }&
    echo >&6
    done
    
    wait
    exec 6>&-
    

      

    [root@gpu007 zabbix_agentd.d]# cat zhiyi.conf 
    
    UserParameter=net_status,/etc/zabbix/scripts/net_status.sh -c 4 -i 192.168.0.167
    

      

    zabbix 客户端重新启动 zabbix-agent

    [root@compute zabbix_agentd.d]# systemctl restart zabbix-agent

    在 zabbix server 端添加 相应的item  

    添加相应的trigger

    添加相应的graphs

  • 相关阅读:
    Python Socket传输文件
    docker-compose使用volume部署mysql时permission deny问题解决
    Docker-compose ports和expose的区别
    Docker Compose
    Docker Compose 配置文件详解
    SynergyS7G2RTC时钟模块的使用
    Maven 之多模块构建
    Dockerfile 中的 COPY 与 ADD 命令
    Docker Dockerfile 一
    Docker镜像构建上下文(Context)
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/8949813.html
Copyright © 2011-2022 走看看