LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-16-2009, 07:08 AM   #1
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466
Blog Entries: 6

Rep: Reputation: 51
Understanding this Pelr Script..


Can anyone pls help me in understanding the following command:

Code:
perl -ne 'next if !/GET \/server_busy\.php/; $|=1; chomp(); print $_ x 10; print "\n"' > /tmp/busy_count
 
Old 11-16-2009, 07:16 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by your_shadow03 View Post
Can anyone pls help me in understanding the following command:

Code:
perl -ne 'next if !/GET \/server_busy\.php/; $|=1; chomp(); print $_ x 10; print "\n"' > /tmp/busy_count
This line is taking input from a file..possibly log file and printing 10x every line that is not a get request for server_busy.php...so everything else into a /tmp/busy_count....i think....

any perl guru feel free to correct me if i am wrong

Last edited by centosboy; 11-16-2009 at 07:17 AM.
 
Old 11-16-2009, 07:25 AM   #3
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
ok..correction....
the oneliner checks through an input file and prints every instance of a get request to /server_busy.php and prints on one line 10 times to the /tmp/busy_count file

Last edited by centosboy; 11-16-2009 at 07:42 AM.
 
Old 11-16-2009, 11:05 AM   #4
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
Yes You are right its taking from logs but the file seems to be empty.
any idea why?
 
Old 11-16-2009, 03:11 PM   #5
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by your_shadow03 View Post
Yes You are right its taking from logs but the file seems to be empty.
any idea why?
/tmp/busy_count is empty because no instances of GET server_busy.php had been found.


the perl one liner says skip lines that do NOT contain GET server_busy.php
either the one liner is incorrectly written or it is not finding what it is trying to match.
best thing to do is test the one liner out by passing an input file to it

Last edited by centosboy; 11-16-2009 at 03:13 PM.
 
Old 11-16-2009, 10:10 PM   #6
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
I have a command which goes like this:
Code:
ssh user@<ip address of server> "perl ./monitor-my-system.pl countmybusypages" | /home/rick/graphs/mailme.pl
I am executing this command which will execute the perl script at remote machine./home/rick/graphs/mailme.pl is a directory in local machine.
Its throwing error like:

Code:
ERROR: expected 18 data source readings (got 19) from 1258370768:1.04:0.90:0.78
Any Idea what may go wrong?
 
Old 11-17-2009, 06:55 AM   #7
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by your_shadow03 View Post
I have a command which goes like this:
Code:
ssh user@<ip address of server> "perl ./monitor-my-system.pl countmybusypages" | /home/rick/graphs/mailme.pl
I am executing this command which will execute the perl script at remote machine./home/rick/graphs/mailme.pl is a directory in local machine.
Its throwing error like:

Code:
ERROR: expected 18 data source readings (got 19) from 1258370768:1.04:0.90:0.78
Any Idea what may go wrong?
not unless i see the content of monitor-my-system.pl and mailme.pl
 
Old 11-19-2009, 11:43 PM   #8
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
Let me explain you what issue I am facing.
I have some scripts to monitor the Web and DB Servers remotely.How it works is We have 5 web servers and 7 DB Servers.
There is a command like:

tail -f /var/logs/expo.txt | perl -ne 'next if !/GET \/server_busy\.php/; $|=1; chomp(); print $_ x 10; print "\n"' > /tmp/busy_count

Now That should be running on the server all the time.
I checked in the midnight and it does collect some 10-20 entry repeatedly.

Next, I have been provided with some script to run from one of centralized server(MontoringServer) which will ssh to the Machine. Th Script is monitor-my-system.pl.
It is run as:

ssh user@<webip> "perl ./monitor-my-system count_busy_pages" | /home/user/graphs/mailme.pl

What I understand it collects data from remote web server and create .rrd file through rrdtool.
Now it should redirect to DOCROOT of apache.
what the issue is I m unable to see the Graph and also DOCROOT is not able to get update.
Its all on RHEL Machine and that too with PHP4.
Is there anything to do with PHP4 and RRDtool.
What packages do we need with RRDtool to function properly.
 
Old 11-20-2009, 02:26 AM   #9
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by your_shadow03 View Post
Let me explain you what issue I am facing.
I have some scripts to monitor the Web and DB Servers remotely.How it works is We have 5 web servers and 7 DB Servers.
There is a command like:

tail -f /var/logs/expo.txt | perl -ne 'next if !/GET \/server_busy\.php/; $|=1; chomp(); print $_ x 10; print "\n"' > /tmp/busy_count

Now That should be running on the server all the time.
I checked in the midnight and it does collect some 10-20 entry repeatedly.

Next, I have been provided with some script to run from one of centralized server(MontoringServer) which will ssh to the Machine. Th Script is monitor-my-system.pl.
It is run as:

ssh user@<webip> "perl ./monitor-my-system count_busy_pages" | /home/user/graphs/mailme.pl

What I understand it collects data from remote web server and create .rrd file through rrdtool.
Now it should redirect to DOCROOT of apache.
what the issue is I m unable to see the Graph and also DOCROOT is not able to get update.
Its all on RHEL Machine and that too with PHP4.
Is there anything to do with PHP4 and RRDtool.
What packages do we need with RRDtool to function properly.
Again, the clue to why this is not working properly lies in the content or location of the scripts.
You can find out more about rrdtool on their homepage.
Why dont you try running the script on the remote machine, and not piping to the mailme.pl script, just to see what kind of output is generated
 
Old 11-20-2009, 11:58 AM   #10
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
I tried running the script and its displaying statistics of web server like:
Code:
time, Load, Load, Load, Memory used, Memory free, Cached, Swap used, Swap Free, Apache processes, Load Manager factor, Max apache,  Busy
vl10we13:1258739458:0.79:0.83:0.78:2:151:23064:3830812:259252:3117900:19904:4076088:1866349575:2606643889:82714932:600908373:16:20:250:0
Its working fine when run in remote Machine.
But when its completely run as:
Code:
ssh user@<webip> "perl ./monitor-my-system count_busy_pages" | /home/user/graphs/mailme.pl
Code:
ERROR: expected 18 data source readings (got 19) from 1258370768...
The Mailme.pl File:
Code:
$| = 1;

while(<STDIN>){
    last if /^\s*$/;
    chomp();
    s/^\s*subject\s*:\s*//i;
    $subject = $_;
}

open( LOG, ">>$subject.log" );

$subject.='.rrd' if !/\.rrd$/;
my $filename = $subject;

print STDERR "$subject\n";

$last_reading = `rrdtool last $filename`;
chomp($last_reading);

while ($line =  <>) {
    $orig = $line;
    chomp($line);
    $line =~ s/^.+?://;
    print LOG $line."\n";
    @data = split /:/, $line;
    next if(!$data[0]);
    next if($data[0]<=$last_reading);
    $cmd = "rrdtool update $filename $line";
    #print $cmd;
    print `$cmd`;
    print "$orig"   
}
Now The issue is it somehow adding this to DOCROOT and graph and statistic with RRDtool is getting prepared.
Can yu find out what this error all about?
 
Old 11-20-2009, 01:45 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by your_shadow03 View Post
Can anyone pls help me in understanding the following command:

Code:
perl -ne 'next if !/GET \/server_busy\.php/; $|=1; chomp(); print $_ x 10; print "\n"' > /tmp/busy_count
I think you are addressing your problem from a wrong side. One-liners are quick to write but hard to debug - because they are one liners.

So, whenever I can't get a one-liner working in 60 seconds of my time, I write a normal Perl script instead.

I.e. your question about unexpected data at output is answered by checking data at input and at every processing step afterwards.

For starters:

'if' is a Perl keyword;
'/ .... /' is match operator;
'!' is logical negation;
'$|' is a Perl predefined variable;
'$_' - ditto;
'chomp' is a Perl built-in function;
'print' - ditto.

Now you have clues what to look for in the documentation:

perldoc.perl.org
.
 
Old 11-20-2009, 11:48 PM   #12
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
I tried couldnt understand. anyone who can make me understand the mailme perl script.

Last edited by your_shadow03; 11-21-2009 at 12:41 AM.
 
Old 11-21-2009, 04:37 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by your_shadow03 View Post
I tried couldnt understand. anyone who can make me understand the mailme perl script.
Sorry, but it looks more like intellectual laziness.

You were given explanations what is what, i.e. which items are keywords, which items are built-in functions, which items are built-in variables.

You were also given pointer to online Perl documentation

For every kind of the above items there is good documentation.

Had you tried to read the documentation and had you posted here pieces of text from the documentation which you don't understand, I would have believed you.
 
  


Reply



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
Help understanding a bash script cylon Linux - Newbie 4 09-16-2009 10:04 AM
Problem understanding line in script snowman81 Programming 3 02-09-2008 09:58 PM
Help understanding a script Gins Programming 11 04-12-2007 02:13 AM
Need help understanding script....? jmr0311 Mandriva 3 08-08-2004 07:42 AM
Startup script, help understanding it..? jmr0311 Linux - Software 1 08-07-2004 05:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration