Skip to content
Snippets Groups Projects
Select Git revision
  • 1abd4b0d2c9b089fb571be160aee15c738b9760d
  • master default protected
  • simple-task/7248-eol-check-add-node-22
  • 6877_check_iml_deployment
4 results

check_opencpu

Blame
  • check_couchdb 5.25 KiB
    #!/bin/bash
    # ======================================================================
    #
    # Icinga/ Nagios Check
    # COUCHDB
    #
    # ----------------------------------------------------------------------
    #
    # REQUIREMENTS:
    #   - curl 
    #
    # SYNTAX:
    #   - check_couchdb [-h] [-c CFGFILE] -m MODE
    #
    # ----------------------------------------------------------------------
    # 2023-08-28  v0.1  <axel.hahn@unibe.ch>  first lines
    # 2023-08-28  v0.2  <axel.hahn@unibe.ch>  first check "up"
    # 2023-08-28  v0.3  <axel.hahn@unibe.ch>  add check "replication"
    # 2023-08-28  v0.4  <axel.hahn@unibe.ch>  add check "pending"
    # ======================================================================
    
    . $(dirname $0)/inc_pluginfunctions
    
    export self_APPVERSION=0.4
    
    cfgfile=/etc/icingaclient/.couchdb
    export RESPONSE
    
    # ----------------------------------------------------------------------
    # FUNCTIONS
    # ----------------------------------------------------------------------
    
    # show help
    function showHelp(){
        local _self; _self=$(basename $0)
        cat <<EOF
    $( ph.showImlHelpHeader )
    
    Show couchdb status.
    
    SYNTAX:
    $_self [-h] [-t FILE] -m MODE
    
    OPTIONS:
        -h or --help   show this help.
        -c CFGFILE     set a custom config file
                       default: ${cfgfile}
        -m MODE        test a value; for debugging purposes the full json 
                       response will be shown
    
        MODE is one of
          up           show general couchdb status
          replication  show last replication status
          pending      show count of pending updates for nodes, dbs and users
    
    EXAMPLE:
    $_self -m up
        Check if couchdb is up and running
    
    EOF
    }
    
    # get couchdb status by given url and a filter
    # The check aborts here if no data were found.
    # Response is written into global var RESPONSE
    #
    # param  string  url to request; the part behind couchdb base url
    # param  string  string to search for in the content
    function abortOnWrongResponse(){
            _path="$1"
            _filter="$2"
    
            RESPONSE=$( curl -s "${COUCH_URL}${_path}" )
            if ! grep "$_filter" <<< "$RESPONSE" >/dev/null ; then
                    echo "ERROR: Wrong response from $_path - it does not contain $_filter"
                    curl -si "${COUCH_URL}${_path}"
                    ph.abort
            fi
    }
    
    # ----------------------------------------------------------------------
    # MAIN
    # ----------------------------------------------------------------------
    
    # --- check param -h
    case "$1" in
        "--help"|"-h")
            showHelp
            exit 0
            ;;
        *)
    esac
    
    ph.require jq
    
    sMode=$(ph.getValueWithParam '' "m" "$@")
    cfgfile=$(ph.getValueWithParam "${cfgfile}" "c" "$@")
    
    if [ ! -f "$cfgfile" ]; then
        echo "ERROR: Config file [${cfgfile}] does not exist."
        ph.abort
    fi
    
    . "$cfgfile" || exit 1
    
    if [ -z "$COUCH_URL" ]; then
        echo "ERROR: I have no couchdb url + authentication yet."
        echo "set 'export COUCH_URL=http://USER:PW@localhost:5984' in $cfgfile"
        ph.abort
    fi
    
    # ----------------------------------------------------------------------
    
    case "${sMode}" in
        # ............................................................
        "up")
            REQ=/_up
            abortOnWrongResponse "${REQ}" '"status":"'
    
            _status=$( jq '.status' <<< "${RESPONSE}" | tr -d '"')
            if ! echo "$_status" | grep "ok" >/dev/null; then
                    ph.setStatus critical
            fi
            ph.status "Couchdb :: health status (value 'status' in ${REQ} is '$_status')"
            echo "Reponse of ${REQ}: "; echo "${RESPONSE}" | jq
            ;;
        # ............................................................
        "replication")
            REQ=/_up
            abortOnWrongResponse "${REQ}" '"status":"'
    
            _status=$( jq '.seeds[] | .last_replication_status' <<< "${RESPONSE}" | grep -v "null" | tr -d '"')
    
            # there can be multiple sections "seeds" and multiple line responses.
            # remove all lines with "ok" and check if there is any "bad content" left
            _nonok=$( echo "$_status" | grep -v "ok" )
            if [ -n "$_nonok" ]; then
                    ph.setStatus critical
            fi
    
            ph.status "Couchdb :: replication (values 'last_replication_status' in ${REQ} are '$_status')"
            echo "Reponse: of ${REQ}"; echo "${RESPONSE}" | jq
            ;;
        # ............................................................
        "pending")
            REQ=/_up
            abortOnWrongResponse "${REQ}" '"status":"'
    
            _status=$( jq '.seeds[] | .pending_updates' <<< "${RESPONSE}" | grep -v "null" | tr -d '"')
            
            typeset -i _iSumme
            typeset -i _iTotal
    
            _iTotal=0
            for myvar in _nodes _dbs _users
            do
                _iSumme=0
                for myvalue in $( grep "$myvar" <<< "$_status" | cut -f 2 -d ':' | tr -d ',')
                do
                    _iSumme+=$myvalue
                    test "$myvalue" -gt 0  && ph.setStatus warning
                done
                ph.perfadd "$myvar" "$_iSumme"    "" ""
                _iTotal+=$_iSumme
            done
    
            ph.status "Couchdb :: pending updates: $_iTotal (values below 'pending_updates' in ${REQ})"
            echo "Reponse: of ${REQ}"; echo "${RESPONSE}" | jq
            ;;
        # ............................................................
        *)
            echo "ERRROR: [${sMode}] is an INVALID mode"
            showHelp
            ph.abort
    
    esac
    
    ph.exit
    
    # ----------------------------------------------------------------------