Skip to content
Snippets Groups Projects
Select Git revision
  • f2ada9a019107a8779da96665c80777bc836a41e
  • master default protected
  • 7771-harden-postgres-backup
  • pgsql-dump-with-snapshots
  • update-colors
  • update-docs-css
  • usb-repair-stick
  • desktop-notification
  • 7000-corrections
  • db-detector
10 results

style.css

Blame
  • check_conn 3.25 KiB
    #!/usr/bin/env bash
    # ======================================================================
    #
    # Check connection ... is a host available on a given port via tcp or udp
    #
    # requirements:
    # - none
    #
    # ----------------------------------------------------------------------
    # 2021-11-05  v0.0  <axel.hahn@iml.unibe.ch>
    # 2023-07-27  v1.1  <axel.hahn@unibe.ch>      update help page
    # ======================================================================
    
    
    . $(dirname $0)/inc_pluginfunctions
    
    export self_APPVERSION=1.1
    
    cfgfile=$( dirname $0 )/$( basename $0 ).cfg
    
    
    out=""
    
    # ----------------------------------------------------------------------
    # functions
    # ----------------------------------------------------------------------
    
    function getChecks(){
      while [ $# -gt 0 ]; do
        test -r "$1" && grep -E "^(tcp|udp)/" "$1" 2>&1
        shift 1
      done
    }
    
    
    function showHelp(){
        local _self; _self=$(basename $0)
        cat <<EOF
    $( ph.showImlHelpHeader )
    
    SYNTAX:
    $_self OPTIONS
    
    You can create a file named $cfgfile
    and add your connections there. 
    To start you can copy the file $cfgfile.dist
    
    OPTIONS:
    
        -h or --help   show this help.
        -t CONNECTION  test given connection; This param ignores entries
                       in the config file. For multiple connection tests
                       quote the parameter value and set spaces between
                       the connections.
    
    PARAMETERS:
    
        CONNECTION     Connection in the same syntax like in the config:
                       tcp/host/port
                       udp/host/port
    EXAMPLES:
    
    $_self         If no param is given it checks entries in $cfgfile
    
    $_self -t "tcp/api.example.com/443"
                       Check a single connection
    
    $_self -t "tcp/api.example.com/443 tcp/localhost/3306"
                       Check multiple connections.
    
    EOF
    }
    # ----------------------------------------------------------------------
    # MAIN
    # ----------------------------------------------------------------------
    
    # --- check required tools
    # ph.require bc top
    
    
    # --- check param -h
    case "$1" in
        "--help"|"-h")
            showHelp
            exit 0
            ;;
        *)
    esac
    
    configline=$( ph.getValueWithParam "" t "$@" )
    
    test -z "$configline" || cfgfile=""
    
    
    
    typeset -i iWarnings=0
    typeset -i iErrors=0
    typeset -i iOK=0
    typeset -i iCount=0
    
    for myline in $( echo $configline; getChecks $cfgfile )
    do
    
      iCount=$iCount+1
      
      out="$out
    $myline"
    
      # --- syntax check of config entry
      echo "$myline" | grep -E "^(tcp|udp)/[a-z][a-z0-9\.\-]*/[0-9]*$" >/dev/null 2>&1
      if [ $? -ne 0 ]; then
        out="$out SKIP: INVALID ENTRY"
        iWarnings=$iWarnings+1
      else 
        >/dev/$myline
        if [ $? -ne 0 ]; then
            out="$out FAILED"
            iErrors=$iErrors+1
        else
            out="$out OK"
            iOK=$iOK+1
        fi
      fi
    done
    
    # ----- output
    if [ $iCount -eq 0 ]; then
    ph.setStatus "unknown"
    ph.status "tcp check - NO checks"
    test $iCount    -eq 0 && echo "
    No config entry was found. Create a file named $cfgfile
    and add your connections i.e.
    tcp/www.example.com/443"
    else
        test $iWarnings -gt 0 && ph.setStatus "warning"
        test $iErrors   -gt 0 && ph.setStatus "critical"
        ph.status "tcp check - $iCount checks - $iOK OK; $iWarnings warnings; $iErrors errors $out"
    fi
    
    # ----- CLEANUP AND BYE!
    
    ph.exit
    
    # ----------------------------------------------------------------------