LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-23-2010, 10:50 AM   #1
bic
Member
 
Registered: Jan 2005
Distribution: MythDora 3.2, RHEL 6.3
Posts: 95

Rep: Reputation: 16
grep not getting whole string


I am trying to monitor how long an ldap search takes and maybe notify or something that a search takes longer than say 10 seconds.

Code:
tail -n 1000 /var/log/ldap.log

for SRCH in $( cat monitorldap.log |grep 'SRCH'); do


        echo search string is
        echo $SRCH

        connum=`echo $SRCH | cut -d= -f2 |cut -d' ' -f1 `
        starttime=`echo $SRCH| cut -d ' ' -f3 `

        echo connection number $connum
        echo start time $starttime

        success=`cat monitorldap.log |grep 'conn=$connum op=1 SEARCH RESULT' `

        echo $success

        if [ "$success" = "" ] ; then

                echo $connum search result not found
        fi


done
Sample results:

Code:
search string is
Aug
connection number Aug
start time Aug

Aug search result not found
ok, so to start off with it doesn't appear to get the whole line, just a piece "Aug". How can I get the whole line into a variable so I can then cut it up into the pieces I need?
 
Old 08-23-2010, 11:05 AM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Could you please give us sample input? (monitorldap.log)

PS: You should use 'grep' directly instead of 'cat' and pipe to 'grep'.
 
Old 08-23-2010, 11:36 AM   #3
bic
Member
 
Registered: Jan 2005
Distribution: MythDora 3.2, RHEL 6.3
Posts: 95

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by quanta View Post
Could you please give us sample input? (monitorldap.log)

PS: You should use 'grep' directly instead of 'cat' and pipe to 'grep'.
Here's a couple:

Code:
Aug 23 11:53:51 ldap5 slapd[26983]: conn=122965 op=1 SRCH base="o=my,c=US" scope=2 deref=3 filter="(&(objectClass=name)(uid=smith))"
Aug 23 11:53:51 ldap5 slapd[26983]: conn=122966 op=1 SRCH base="Account=123,o=my,c=US" scope=0 deref=3 filter="(objectClass=*)"
 
Old 08-23-2010, 11:48 AM   #4
bic
Member
 
Registered: Jan 2005
Distribution: MythDora 3.2, RHEL 6.3
Posts: 95

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by bic View Post
Here's a couple:

Code:
Aug 23 11:53:51 ldap5 slapd[26983]: conn=122965 op=1 SRCH base="o=my,c=US" scope=2 deref=3 filter="(&(objectClass=name)(uid=smith))"
Aug 23 11:53:51 ldap5 slapd[26983]: conn=122966 op=1 SRCH base="Account=123,o=my,c=US" scope=0 deref=3 filter="(objectClass=*)"
oops, that's just the search entries, here's a tail:

Code:
Aug 18 16:43:38 ldap5 slapd[21231]: conn=397719 fd=136 ACCEPT from IP=192.168.1.5:60704 (IP=0.0.0.0:389)
Aug 18 16:43:38 ldap5 slapd[21231]: conn=397719 op=0 BIND dn="uid=xxx,o=US,c=US" method=128
Aug 18 16:43:38 ldap5 slapd[21231]: conn=397719 op=0 BIND dn="uid=xxx,o=US,c=US" mech=SIMPLE ssf=0
Aug 18 16:43:38 ldap5 slapd[21231]: conn=397719 op=0 RESULT tag=97 err=0 text=
Aug 18 16:43:38 ldap5 slapd[21231]: conn=397719 op=1 SRCH base="o=US,c=US" scope=2 deref=3 filter="(&(personnumber=123)(objectClass=Person))"
Aug 18 16:43:54 ldap5 slapd[21231]: conn=397719 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=
Aug 18 16:43:54 ldap5 slapd[21231]: conn=397719 op=2 UNBIND
Aug 18 16:43:54 ldap5 slapd[21231]: conn=397719 fd=136 closed
This one you can see goes from 38 to 54 seconds, so it ran for quite some time.
 
Old 08-23-2010, 12:07 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,583

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
I would change this:
Code:
        success=`cat monitorldap.log |grep 'conn=$connum op=1 SEARCH RESULT' `

        echo $success

        if [ "$success" = "" ] ; then

                echo $connum search result not found
        fi
to:
Code:
if ! grep -q 'conn=$connum op=1 SEARCH RESULT' monitorldap.log
then
    echo $connum search result not found
fi
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to GREP string? tquang Linux - General 2 07-30-2010 12:51 AM
grep a particular string john83reuben Linux - Newbie 3 12-07-2009 09:34 AM
How to grep a string from all the files under / thomas2004ch Linux - Newbie 23 09-16-2009 08:16 PM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM


All times are GMT -5. The time now is 05:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration