Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
Go Back   Zimbra :: Forums > Zimbra Collaboration Suite > Administrators

Welcome to the Zimbra :: Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-15-2010, 03:12 PM
Partner (VAR/HSP)
 
Posts: 197
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!
__________________

http://agilemail.com.au

Your Australian Zimbra experts
Sales, consulting, installation, support

Last edited by greenrenault; 01-15-2010 at 03:12 PM.. Reason: Adding code tags
Reply With Quote
  #2 (permalink)  
Old 01-15-2010, 10:53 PM
Zimbra Employee
 
Posts: 46
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



Reply With Quote
  #3 (permalink)  
Old 01-17-2010, 11:47 AM
Partner (VAR/HSP)
 
Posts: 197
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
Reply With Quote
  #4 (permalink)  
Old 03-10-2010, 04:51 AM
Starter Member
 
Posts: 2
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
Reply With Quote
  #5 (permalink)  
Old 03-10-2010, 11:22 AM
Partner (VAR/HSP)
 
Posts: 197
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

blog.zimbra.com




 

SEO by vBSEO ©2011, Crawlability, Inc.