Help in Perl script (newbiew)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlexMahmood
    New Member
    • Aug 2008
    • 4

    Help in Perl script (newbiew)

    The soruce is the url to get the status of the server page
    Output is as following between lines
    ------------------------------------------------------------------------------------------
    Apache Server Status for test01

    Server Version: IBM_HTTP_Server
    Server Built: Apr 7 2003 13:38:46

    Current Time: Tuesday, 19-Aug-2008 16:11:09 EDT
    Restart Time: Sunday, 17-Aug-2008 07:44:21 EDT
    Parent Server Generation: 0
    Server uptime: 2 days 8 hours 26 minutes 48 seconds
    Total accesses: 980721 - Total Traffic: 3.9 GB
    CPU Usage: u25654.1 s5329.57 cu0 cs0 - 15.2% CPU load
    4.83 requests/sec - 20.2 kB/second - 4285 B/request
    6 requests currently being processed, 69 idle workers

    ------------------------------------------------------------------------------------------
    I can get abve ouput in variable

    I want to grab the underline and bold values in 4 variables and print those on screen

    Thank you in advance.
    Alex
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    What have you tried? How much perl do you know. Please show use the code.

    --Kevin

    Comment

    • AlexMahmood
      New Member
      • Aug 2008
      • 4

      #3
      I know the khs, I understand programming but very basic perl.

      my code

      Code:
      #!/usr/bin/perl
      #
      $output = `wget -q -O- --bind-address=localhost http://test:64999/server-status`; || die "Problems: $!"
      $output =~ /CPU Usage: u/;
      print "$1 ";
      I tried for 1 value to print but no output on the screen.
      If I get this working I'll try more in perl.

      TIA

      Originally posted by eWish
      What have you tried? How much perl do you know. Please show use the code.

      --Kevin
      Last edited by eWish; Aug 20 '08, 12:27 AM. Reason: Added Code tags

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by AlexMahmood
        I know the khs, I understand programming but very basic perl.

        my code

        Code:
        #!/usr/bin/perl
        #
        $output = `wget -q -O- --bind-address=localhost http://test:64999/server-status`; || die "Problems: $!"
        $output =~ /CPU Usage: u/;
        print "$1 ";
        I tried for 1 value to print but no output on the screen.
        If I get this working I'll try more in perl.

        TIA
        You're trying to print $1, but you haven't captured anything in the previous regexp. In fact, your regexp on line 4 doesn't do anything except match for "CPU Usage: u", and doesn't capture, change, or affect anything else.

        Also, the "|| die" portion of your assignment in line 3 is after a semicolon, which should be giving you syntax errors (or would be if you had use strict and use warnings enabled).

        Comment

        • eWish
          Recognized Expert Contributor
          • Jul 2007
          • 973

          #5
          Currently you are not capturing the data in your regex. If you want to capture then you will need to use a set of ().

          Code:
          $output =~ /(CPU Usage: u)/;
          print $1;
          I would suggest that you check out perlre.

          --Kevin

          Comment

          • AlexMahmood
            New Member
            • Aug 2008
            • 4

            #6
            thanks, I'll read the link you posted, meanwhile if you could pls show how to do for 1 var and then I'll try for others.

            TIA

            Comment

            • eWish
              Recognized Expert Contributor
              • Jul 2007
              • 973

              #7
              Here is an example to help you get started. In this example I am opening a file and reading it line by line and looking for a match according to the regex. If it finds a match then print it what was captured in $1.

              Code:
              open(my $FILE, '<', $test_file) || die "Can't open $test_file: $!\n";
              while(<$FILE>) {
              	chomp;
              	print $1 if /^(CPU Usage:.*?\w\d{4}.\d)/g;	
              }
              close($FILE);
              Since regular expressions are not my strong point someone may point out a better solution.

              --Kevin

              Comment

              • AlexMahmood
                New Member
                • Aug 2008
                • 4

                #8
                Thanks I'll try in the morning.

                Originally posted by eWish
                Here is an example to help you get started. In this example I am opening a file and reading it line by line and looking for a match according to the regex. If it finds a match then print it what was captured in $1.

                Code:
                open(my $FILE, '<', $test_file) || die "Can't open $test_file: $!\n";
                while(<$FILE>) {
                	chomp;
                	print $1 if /^(CPU Usage:.*?\w\d{4}.\d)/g;	
                }
                close($FILE);
                Since regular expressions are not my strong point someone may point out a better solution.

                --Kevin

                Comment

                Working...