Feature request: monit command with regex or wildcard

Issue #915 resolved
ktoy created an issue

Example of command with regex or wildcard

$ monit restart abc*
$ monit restart abc_\d+

Comments (4)

  1. Henning Bopp

    I like the idea, so I wrote a little bash function that handles this:

    #!/usr/bin/env bash
    function emonit() {
      local available matching action failed
      action="$1"
      shift
      available="$(sudo monit -B summary | awk 'FNR > 2 && /^ /{ print $1 }')"
      matching="$(grep -P "^$*\$" <<< "$available")"
    
      if [ "$matching" = "" ]; then
        echo "Cannot find matching monit services with $*" >&2
        return 1
      elif [ "$action" = "list" ]; then
        echo "$matching"
      else
        failed="false"
        while read -r service; do
          sudo monit "${action}" "${service}" || failed="true"
        done <<< "$matching"
        $failed && return 1 || return 0
      fi
    }
    

    You can source this file in your login-script (~/.bashrc or similar) with . ~/.emonit.sh if you stored it in ~/.emonit.sh. The script uses some non-sh-compatible syntax. So if you only have sh or ash at hand, some changes are needed.

    Syntax:

    $ emonit list 'b.*'
    bin
    bash
    boppy
    
    $ emonit restart 'b.*'
    # Will restart the three services listed above (without providing feedback)
    

    One important thing to notice: Mind the glob! ;)

    $ emonit restart .*             # will expand before it is passed to the script, meaning that the script call will be something like
    $ emonit restart . .. .bashrc   # what will surely fail. I recommend to always single-quote any input, like
    $ emonit restart '.*'           # what will simply restart any service available. This also makes use of character classes easier
    $ emonit restart 'abc_\d+'      # this is equal to the next line
    $ emonit restart abc_\\d+
    

    Hope that helps.

  2. Tildeslash repo owner

    Fix Issue #915: Add support for sevice name regular expressions in monit CLI status, summary, start, stop, restart, unmonitor, monitor commands. Examples: monit status myservice # exact match monit status "data.*" # pattern which will match all services whos name contain "data" substring

    → <<cset 869a3015cdb5>>

  3. Log in to comment