<commit_before>#!/bin/bash
# nagios-aware

NAG_OK=0
NAG_WARNING=1
NAG_CRITITCAL=2
NAG_UNKNOWN=3

[[ -z "$1" ]] && {
  echo "usage: $(basename $0 .sh) node" >&2
  exit $NAG_UNKNOWN
}

queues="$(qselect | grep "@$1$")"

# no queues - bail out
if [[ -z $queues ]] ; then
  echo "no queue instances registered"
  exit $NAG_WARNING
fi

for qi in $queues ; do
  # setup qis string for output
  [[ -z $qis ]] && qis="$qi" || qis="$qis, $qi"

  qi_state="$(qstat -f | awk "\$1 ~ /^$qi$/ { print \$6 }")"

  if [[ -n "$qi_state" ]] ; then
    # humanize queue state
    for (( i=0; i < ${#qi_state}; i++ )) ; do
      case ${qi_state:$i:1} in
        a ) msg="load threshold exceeded (a)larm"

            node=$(echo $qi | cut -d@ -f2)
            users=$(qstat -u \* -xml -f -l hostname=$node | sed '/JB_owner/!d; s?^[ \t]*\|<JB_owner>\|</JB_owner>??g' | sort -u | sed ':a;N;$!ba;s/\n/, /g')
            [[ -n "$users" ]] && msg="$msg ($users)"

            reason=$(qstat -xml -q $qi -explain a | sed -n '/<load-alarm-reason>/,/<\/load-alarm-reason>/p' | sed 's?^[ \t]*\|<load-alarm-reason>\|</load-alarm-reason>??g; s/&quot;/"/g; s/alarm //g; s/ load-threshold=/ threshold=/g' | sed ':a;N;$!ba;s/\n/, /g')
            [[ -n "$reason" ]] && msg="$msg ($reason)"

            warn=y
            ;;
        A ) msg="suspend threshold exceeded (A)larm"
            reason=$(qstat -xml -q $qi -explain A | sed '/<load-alarm-reason>/!d; s?^[ \t]*\|<load-alarm-reason>\|</load-alarm-reason>??g; s/&quot;/"/g' | sed ':a;N;$!ba;s/\n/, /g')
            [[ -n "$reason" ]] && msg="$msg ($reason)"
            warn=y
            ;;
        s ) msg="(s)uspended"                        ; warn=y    ;;
        d ) msg="(d)isabled"                         ; warn=y    ;;
        D ) msg="calendar disabled (D)"                          ;;
        C ) msg="calendar suspended (C)"                         ;;
        S ) msg="subordinate suspended (S)"                      ;;
        E ) msg="error (E)"
            reason=$(qstat -xml -q $qi -explain E | sed '/<load-alarm-reason>/!d; s?^[ \t]*\|<load-alarm-reason>\|</load-alarm-reason>??g; s/&quot;/"/g' | sed ':a;N;$!ba;s/\n/, /g')
            [[ -n "$reason" ]] && msg="$msg ($reason)"
            error=y
            ;;
        c ) msg="(c)onfiguration ambiguous"
            reason=$(qstat -xml -q $qi -explain c | sed '/<load-alarm-reason>/!d; s?^[ \t]*\|<load-alarm-reason>\|</load-alarm-reason>??g; s/&quot;/"/g' | sed ':a;N;$!ba;s/\n/, /g')
            [[ -n "$reason" ]] && msg="$msg ($reason)"
            warn=y
            ;;
        o ) msg="(o)rphaned"                         ; warn=y    ;;
        u ) msg="(u)nknown"                          ; unknown=y ;;
        * ) msg="${qi_state:$1:1}"                   ; error=y
      esac
      [[ -z $msgs ]] && msgs="$msg" || msgs="$msgs, $msg"
    done

    [[ -z $err_qis ]] && err_qis="$qi: $msgs" || err_qis="$err_qis; $qi: $msgs"
  fi
done

[[ -n $unknown ]] && echo "$err_qis" && exit $NAG_UNKNOWN
[[ -n $error   ]] && echo "$err_qis" && exit $NAG_CRITICAL
[[ -n $warn    ]] && echo "$err_qis" && exit $NAG_WARNING

echo "$qis ALL OK" && exit $NAG_OK<commit_msg>redesigns qcheckqis<commit_after>#!/bin/bash
# nagios-aware

# nagios-based exit status
nagios_exit_good=0
nagios_exit_warn=1
nagios_exit_crit=2
nagios_exit_unkn=3

# ------------------------------------------------------------------------------
# command line arguments
# ------------------------------------------------------------------------------

if [[ -z $1 ]] ; then
  echo "usage: $(basename $0 .sh) host" >&2
  exit $nagios_exit_crit
fi

host=$1

# ------------------------------------------------------------------------------
# determine registered queue instances
# ------------------------------------------------------------------------------

queue_instances="$(qlsqis $host)"

if [[ -z $queue_instances ]] ; then
  echo "no queue instances registered at host $host"
  exit $nagios_exit_warn
fi

# ------------------------------------------------------------------------------
# check queue instances
#
# - get queue instance state and humanize it
# - try to get some more information about the reason of the state
# ------------------------------------------------------------------------------

for qi in $queue_instances ; do
  # setup qis string for output
  if [[ -z $qis ]] ; then
    qis="$qi"
  else
    qis+=", $qi"
  fi

  qi_state="$(qstat -f | awk "/$qi/ { print \$6 }")"

  if [[ -n $qi_state ]] ; then

    # humanize queue state
    for (( i=0; i < ${#qi_state}; i++ )) ; do

      case ${qi_state:$i:1} in

        a)
          msg="load threshold exceeded (a)larm"

          users=$(qstat -u \* -xml -f -l hostname=$host |
                  sed '/JB_owner/!d; s?^[ \t]*\|<JB_owner>\|</JB_owner>??g' |
                  sort -u | sed ':a;N;$!ba;s/\n/, /g')

          [[ -n $users ]] && msg+=" ($users)"

          reason=$(qstat -xml -q $qi -explain a |
                   sed -e '/<load-alarm-reason>/!d' \
                       -e 's/^[ \t]*//' \
                       -e 's?<load-alarm-reason>??g' \
                       -e 's?</load-alarm-reason>??g' \
                       -e "s/&apos;/'/g" \
                       -e "s/&quot;/'/g" \
                       -e 's/alarm //g' \
                       -e 's/ load-threshold=/ threshold=/g' |
                   sort -u | sed ':a;N;$!ba;s/\n/, /g')
          [[ -n $reason ]] && msg+=" ($reason)"

          warn=y
          ;;

        A)
          msg="suspend threshold exceeded (A)larm"

          reason=$(qstat -xml -q $qi -explain A |
                   sed -e '/<load-alarm-reason>/!d' \
                       -e 's/^[ \t]*//' \
                       -e 's?<load-alarm-reason>??g' \
                       -e 's?</load-alarm-reason>??g' \
                       -e "s/&apos;/'/g" \
                       -e "s/&quot;/'/g" |
                   sort -u | sed ':a;N;$!ba;s/\n/, /g')

          [[ -n $reason ]] && msg+=" ($reason)"

          warn=y
          ;;

        d)
          msg="(d)isabled"

          jobs=$(qlsjobs $host | sed ':a;N;$!ba;s/\n/, /g')

          admin_msg=$(qstat -explain m -q $qi | grep -oE 'admin msg:.+')

          [[ -n $admin_msg ]] && msg+=" ($admin_msg)"

          if [[ -n $jobs ]] ; then
            msg+=", jobs: $jobs"
            warn=y
          else
            if [[ $qi_state =~ u ]] ; then
              msg+=", it is already down, check why it's not up"
            else
              msg+=", there are no jobs running: reboot now"
            fi
            error=y
          fi
          ;;

        E)
          msg="error (E)"

          reason=$(qstat -xml -q $qi -explain E |
                   sed -e '/<message>/!d' \
                       -e 's/^[ \t]*//' \
                       -e 's?<message>??g' \
                       -e 's?</message>??g' \
                       -e "s/&apos;/'/g" \
                       -e "s/&quot;/'/g" |
                   sort -u | sed ':a;N;$!ba;s/\n/, /g')

          [[ -n $reason ]] && msg+=" ($reason)"

          [[ -z $(qlsjobs $host) ]] &&
          msg+=" there are no jobs running: you might as well reboot"

          error=y
          ;;

        c)
          msg="(c)onfiguration ambiguous"

          reason=$(qstat -xml -q $qi -explain c |
                   sed -e '/<load-alarm-reason>/!d' \
                       -e 's/^[ \t]*//' \
                       -e 's?<load-alarm-reason>??g' \
                       -e 's?</load-alarm-reason>??g' \
                       -e "s/&apos;/'/g" \
                       -e "s/&quot;/'/g" |
                   sort -u | sed ':a;N;$!ba;s/\n/, /g')

          [[ -n $reason ]] && msg+=" ($reason)"

          warn=y
          ;;

        s)
          msg="(s)uspended"
          warn=y
          ;;

        D)
          msg="calendar disabled (D)"
          ;;

        C)
          msg="calendar suspended (C)"
          ;;

        S)
          msg="subordinate suspended (S)"
          ;;

        o)
          msg="(o)rphaned"
          warn=y
          ;;

        u)
          msg="(u)nknown"
          unknown=y
          ;;

        *)
          msg="${qi_state:$host:1}"
          error=y

      esac

      if [[ -z $msgs ]] ; then
        msgs="$msg"
      else
        msgs+=", $msg"
      fi
    done

    if [[ -z $err_qis ]] ; then
      err_qis="$qi: $msgs"
    else
      err_qis+="; $qi: $msgs"
    fi
  fi
done

[[ -n $unknown ]] && echo "$err_qis" && exit $nagios_exit_unkn
[[ -n $error   ]] && echo "$err_qis" && exit $nagios_exit_crit
[[ -n $warn    ]] && echo "$err_qis" && exit $nagios_exit_warn

echo "$qis ALL OK" && exit $nagios_exit_good
{'fraction_non_alphanumeric': 0.24740604104219507, 'fraction_numerical': 0.003112750749365921, 'mean_word_length': 1.235248647255862, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3278491', 'n_tokens_mistral': 3162, 'n_tokens_neox': 2965, 'n_words': 676}