Results 1 to 5 of 5

Thread: Bash: how to send commands to zmprov and zmmailbox instead of launching zm* each time

  1. #1
    greenrenault's Avatar
    greenrenault is offline Partner (VAR/HSP)
    Join Date
    Jul 2006
    Location
    Australia, ACT
    Posts
    197
    Rep Power
    9

    Question Bash: how to send commands to zmprov and zmmailbox instead of launching zm* each time

    Hi Everyone,

    I've got a collection of bash scripts which would execute far faster if I could launch an instance of zmmailbox and zmprov once and then just send commands to those instances. Instead of launching zmmailbox for a single command, getting the result and then destroying it. Which requires initiating a Java environment, executing the command and then tearing Java down again.

    For example: executing this command 1,000 times is very inefficient.
    /opt/zimbra/bin/zmprov ga {account}@test.com

    However, I would like to do this:
    zmprov
    ga {account-1}@test.com | pipe result to calling script
    ga {account-2}@test.com | pipe result to calling script
    ga {account-3}@test.com | pipe result to calling script

    Here is an example bash script I would like to optimise:

    Code:
    ZIMBRA_ACCOUNTS=`su - zimbra --command='/opt/zimbra/bin/zmprov -l gaa'`;
    
    for ZIMBRA_ACCOUNT in $ZIMBRA_ACCOUNTS; do
           # Get all attributes for this account, slow as zmprov has to initiate
           ZIMBRA_ACCOUNT_DETAILS="`su - zimbra --command='/opt/zimbra/bin/zmprov ga '$ZIMBRA_ACCOUNT''`";
           [DO SOME INTERESTING THINGS HERE ...]
    done;
    Any suggestions on best to do this? I appreciate that it may not be a specific Zimbra problem but a general bash question.

    Cheers!
    Last edited by greenrenault; 01-15-2010 at 04:12 PM. Reason: Adding code tags

    http://agilemail.com.au

    Your Australian Zimbra experts
    Sales, consulting, installation, support

  2. #2
    irfan is offline Zimbra Employee
    Join Date
    Aug 2007
    Location
    Pune, India
    Posts
    46
    Rep Power
    7

    Default

    You could add all commands excluding "zmprov" in a file and redirect it to zmprov. For example to add huge number of accounts, you could create a text file following line:

    ca user1@domain.com password
    ca user2@domain.com password

    Then redirect it to zmprov

    zmprov < textfile

    In your case this might help.

    for i in `zmprov -l gaa` ; do
    echo "ga $i" >> /tmp/textfile
    done

    Then redirect the resulting file to zmprov.
    zmprov < /tmp/textfile
    - Irfan




  3. #3
    greenrenault's Avatar
    greenrenault is offline Partner (VAR/HSP)
    Join Date
    Jul 2006
    Location
    Australia, ACT
    Posts
    197
    Rep Power
    9

    Smile Thanks for the suggestion

    Thanks for the suggestion irfan. There are situations where this would work, but in my case, I need to query zmmailbox, test the results and then perform additional zmmailbox queries based on the first results.

    Doing it all in one hit doesn't really help. But thanks anyway!

    So I'm still looking for a way to keep a zmmailbox query open and send it commands from another script. I'm sure this is possible.

    It might be an idea to review Zimbra's install scripts, they might be doing this.

    http://agilemail.com.au

    Your Australian Zimbra experts
    Sales, consulting, installation, support

  4. #4
    Doros Eracledes is offline Starter Member
    Join Date
    Mar 2008
    Location
    Cyprus
    Posts
    2
    Rep Power
    7

    Default

    This is what I've done using file descriptors to search through all archive mailboxes (I know zmmboxsearch does this but it failed MISERABLY) using zmmailbox.
    ...
    ...
    QUERY="search -l 999 -t message ...."
    QUERY_NEXT="search -n"
    ..
    ..
    #return code
    RC=0
    # Use the PID ($$) in the FIFO and remove it on exit:
    FIFO="/tmp/-pipe.$$"
    mkfifo ${FIFO} || exit $?

    # Tie FD3 to the FIFO (only for writing), then start zmmailbox in the
    # background with its input from the FIFO:
    exec 3<>${FIFO}
    echo "sending output to temporary file ${TMP_LIST}"
    zmmailbox -u https://localhost:7071 -z <${FIFO} >> ${TMP_LIST} &
    ZIMBRA_ARCHIVE=$!
    echo "zmmailbox PID is" ${ZIMBRA_ARCHIVE}

    #loop through all archives
    for user in $(zmprov gaa | grep "com.archive")
    do
    THEACCOUNT=${user}

    #wait till zmmailbox give us a prompt
    #sleep 5
    echo "opening mailbox" ${THEACCOUNT}
    echo sm ${THEACCOUNT} >&3
    sleep 5
    echo ${QUERY} >&3
    sleep 10
    is_more
    echo "first check says" $MORE
    #the return code
    RC=3
    #loop through multiple pages of search output
    while [ "$MORE" == "true" ]; do
    echo ${QUERY_NEXT} >&3
    sleep 10
    is_more
    echo "account" ${THEACCOUNT} "there's even more:" $MORE
    done
    done
    ...
    ...
    is_more is a function that checks is there are more pages in the query results
    is_more()
    {
    read MORE < <(sed -n -e '/^mbox\|num|more/p' ${TMP_LIST} | grep -v mailbox | awk '{if ( NF == 6 ) { print $6} }' | tail -1 )
    }

    --------------------------------------------
    I had some problems with timing though - that's why the "sleep" lines when I had to wait for the query to complete before sending "send me the next page" to FD3

    Perhaps someone else can help me out
    Doros

  5. #5
    greenrenault's Avatar
    greenrenault is offline Partner (VAR/HSP)
    Join Date
    Jul 2006
    Location
    Australia, ACT
    Posts
    197
    Rep Power
    9

    Smile Great idea!

    Great idea Doros Eracledes!

    I'll see how this goes in my scripts and report back. Thanks for posting the code example.

    http://agilemail.com.au

    Your Australian Zimbra experts
    Sales, consulting, installation, support

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] can not find the CLI commands like zmprov
    By Gil Bauman in forum General Questions
    Replies: 1
    Last Post: 05-19-2009, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •