Results 1 to 6 of 6

Thread: Compare values of my 2 servers with my code parameters in Perl

  1. #1
    Join Date
    Jan 2010
    Posts
    61

    Compare values of my 2 servers with my code parameters in Perl

    For purposes of supervision with nagios on HP-UX, I started using Perl which I never tried yet. But now I encountered an issue. To explain the context, I need to compare the values of swap used by both of my servers to values that are passed as parameters (to set critical threshold and warning). So I need to retrieve the results of two commands "swapinfo" and put them each in a table to be treated thereafter. This is where I stopped for the moment.

    Here's what I did:
    Code:
    #!/opt/perl/bin/perl
    #
    # check multiswap hpux
    #
    use strict;
    use Getopt::Std;
    use vars qw(@swap1 @swap2 $command_line1 $command_line2 %exit_codes);
     
    # Codes prefer finished for Nagios
    %exit_codes  = ('UNKNOWN',-1,'OK',0,'WARNING',1,'CRITICAL',2);
     
    # Get the info of the swap1 in /dev/vg00/lvol2
    $command_line1 = `swapinfo -tam | grep lvol2`;
     
    # Get the info of the swap2 in /dev/vg00/lvol9
    $command_line2 = `swapinfo -tam | grep lvol9`;
     
    # Put the result into a table for swap1
    chomp $command_line1;
    @swap1 = split(/ /,$command_line1);
     
    # Put the result into a table for swap2
    chomp $command_line2;
    @swap2 = split(/ /, $command_line2);
    With the command "swapinfo -tam," I noted that, for example, the 3rd column (counting from 0) gives the number of MB free, and 4th columns provide the percentage used. Thus, to verify that I could well use the values of the table eventually, I tried to read by adding at the end of the script:

    Code:
    print $swap1[3],"\n";
    print $swap1[4],"\n";
    print $swap2[3],"\n";
    print $swap2[4],"\n";
    But at runtime, nothing appears. After several tries, I realized that only

    Code:
    print $swap1[0],"\n";
    print $swap2[0],"\n";
    actually gives me what I asked. When I try to display the following columns, nothing appears. Did I inevitably missed something? Since I do not know how to do that, could anyone of you help me please?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Compare values of my 2 servers with my code parameters in Perl

    Did you tried to split /\t/ ? This is the best answer to the problem in my opinion, because if the result is returned in columns, it is very likely that these tabs, and not spaces.

  3. #3
    Join Date
    Jan 2010
    Posts
    61

    Re: Compare values of my 2 servers with my code parameters in Perl

    I just tried your solution in my script, but for
    Code:
    print $swap1[0],"\n"
    While the result of my order is displayed (all columns), and
    Code:
    print $swap1[1],"\n"
    again nothing is displayed. In addition, it is indeed the basis of tabs:
    Code:
    swapinfo -tam | grep lvol2
    displays as in attachment. Another idea?
    Attached Images Attached Images

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Compare values of my 2 servers with my code parameters in Perl

    When in doubt use \s instead of \t. It is less suited to my taste but it will take any type of space.

  5. #5
    Join Date
    Jan 2010
    Posts
    61

    Re: Compare values of my 2 servers with my code parameters in Perl

    arf ... the result is the same as starting:
    Code:
    print $swap1[0],"\n"
    displays well the 1st column, "dev", then
    Code:
    print $swap1[1],"\n"
    displays nothing (an empty line):
    root:/opt/nrpe/scripts#./check_multiswap.pl
    dev

    root:/opt/nrpe/scripts#

  6. #6
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Compare values of my 2 servers with my code parameters in Perl

    Add:
    Code:
    $command_line1 =~ s/\s\s/\s/g
    To remove the extra spaces before cutting over

Similar Threads

  1. How to compare two Date values in two different columns of a table
    By Ehtesham SIddiqui in forum Windows Software
    Replies: 1
    Last Post: 06-10-2011, 11:32 AM
  2. W32 registry values are not getting matched by the default values
    By Angrzej in forum Networking & Security
    Replies: 5
    Last Post: 19-05-2011, 12:23 PM
  3. Replies: 1
    Last Post: 19-03-2011, 01:28 AM
  4. What is PERL and what are its uses?
    By Gadin in forum Software Development
    Replies: 10
    Last Post: 08-01-2011, 07:33 PM
  5. Difference between COD4 Steam servers and Normal servers
    By Techno05 in forum Networking & Security
    Replies: 5
    Last Post: 22-01-2010, 10:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,373,930.54160 seconds with 17 queries