Trying to use two loops in a perl script but not getting results need.

Trying to use two loops in a perl script but not getting results need.

The foreach loop splits the data and working as expected.
the $apps has the first column of apps name.
$everything_else has rest of the columns.

However I add another while loop with an if statement. if ! not contain "requested state: started" then $bad++ and print.
The issue is that the if statement find a issue then stops.
Would like for it to loop through all the $apps first then print "cf apps OK\n" unless $bad; at the end.
Something wrong with the loop and if statement.


#!/usr/bin/perl
use strict;
use warnings;
use Fatal qw(open close);
my $state_critical = 2;
open my $IN, 'sudo /usr/bin/cf apps | perl -ne "s/ // && print if (/name\s+requested state\s+instances\s+memory\s+disk\s+urls/..0)>1" |';
my $bad=0;
my ($apps, $state, $inst, $mem, $disk, $url, $everything_else);
 foreach (<$IN>) {
  #      ( $apps, $state, $inst, $mem, $disk, $url ) = split ( " " );
              ( $apps, $everything_else) = split ( " " , $_, 5 );
        # print "Here is the $apps\n";
    open my $IN, 'sudo /usr/bin/cf app $apps |';
while (<$IN>) {
   if (!(/^\s*requested\s+state:\s+started\b/)) {
        $bad++;
        print "cf app stopped $apps: $_\n";
        exit $state_critical;
    }
    next unless (/^#/ and /\brunning\b/);
    my ($amt, $amt2) = m{\s([\d\.]+)M\s+of\s.*\s([\d\.]+)M\s+of\b};
    if ($amt >= 92 or $amt2 >= 92) {
        print "App memory issue: $_\n";
        $bad++;
        exit $state_critical;
     }
  } 
}
 
print "cf apps OK\n" unless $bad;

Open in new window

Troush2009Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
I'm looking at your earlier question (Q28702906) How-to-parse-stdout-of-a-cammand-in-the-format-I-want

What you have here is quite a bit more complicated than I think it needs to be. Did you really intend to open the cf command twice? I don't think that's necessary.

It quits as soon as it finds the first problem because you used "exit $state_critical" in the first part of the if blocks. Try taking that statement out and moving it to the end, conditionalized on whether the $bad count is greater than zero.

Also, is there a value of "state" other than "running" that you might want to report on? Right now, your script would skip over such a line.
0
wilcoxonCommented:
Also, calling a perl one-liner as part of your first open pipe is far less efficient than just calling cf and looping over the full output in the current program and filtering it there.

@jmcg, I'm not familiar with cf but it looks like the second call is with different arguments (a specific app rather than all apps).  I don't know if that makes sense to do or not though.
0
Troush2009Author Commented:
The below command I am pulling the first column "name" as that is the app name.
# cf apps
Getting apps in org aac / space amecheapps as admin...
OK

name                                                                          requested state   instances   memory   disk   urls
5syu6vbrfgbdrwndceeowhti5-142-265                           started                1/1           1G            1G     5syu6vbrfgbdrwndceeowhti5-142-265.cf.com
cd72pq4f4exkrlesqxksudcmn-39-206                              started               1/1           1G            1G     ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206.cf.com
NICE-nice-com-2erlinu13z2i72ptgs4g8bfmg-148-285       started              1/1           1G            1G     2erlinu13z2i72ptgs4g8bfmg-148-285.cf.com
xyzcorp-8-314                                                              started               1/1           1G            1G     xyzcorp-8-314.cf.com
bryan1-133-307                                                           started                1/1           1G           1G     bryan1-133-307.cf.com

Then I tring to take the app name of each and run below command.
# cf app 5syu6vbrfgbdrwndceeowhti5-142-265
Showing health and status for app 5syu6vbrfgbdrwndceeowhti5-142-265 in org aac / space amecheapps as admin...
OK

requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: 5syu6vbrfgbdrwndceeowhti5-142-265.cf.com

     state     since                    cpu    memory        disk
#0   running   2015-08-12 09:55:25 PM   0.0%   42.7M of 1G   42.5M of 1G
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

wilcoxonCommented:
Here's some modifications that should fix problems and help make more things efficient...
#!/usr/bin/perl
use strict;
use warnings;
use Fatal qw(open close);
my $state_critical = 2;
open my $IN, 'sudo /usr/bin/cf apps |';
my $bad=0;
my ($apps, $state, $inst, $mem, $disk, $url, $everything_else);
foreach (<$IN>) {
    next if /^name\s+requested state/;
    ($apps, $everything_else) = split /\s+/, $_, 2;
    # print "Here is the $apps\n";
    open my $APP, 'sudo /usr/bin/cf app $apps |';
    while (<$APP>) {
        unless (/^\s*requested\s+state:\s+started\b/) {
            $bad++;
            print "cf app stopped $apps: $_\n";
        }
        next unless (/^#/ and /\brunning\b/);
        my ($amt, $amt2) = m{\s([\d\.]+)M\s+of\s.*\s([\d\.]+)M\s+of\b};
        if ($amt >= 92 or $amt2 >= 92) {
            $bad++;
            print "App memory issue: $_\n";
        }
    } 
}
 
exit $state_critical if $bad;
print "cf apps OK\n";

Open in new window

0
Troush2009Author Commented:
There is a issue because the code is addressing number 2 listed below.

Below is a good stdout.
root@stagevm-pdk1:~# cf app xyzcorp-8-314
Showing health and status for app xyzcorp-8-314 in org aac / space amecheapps as admin...
OK

requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: xyzcorp-8-314.cf.pdk1.audcom.cci.att.com

     state     since                    cpu    memory        disk
#0   running   2015-08-14 02:12:30 PM   0.8%   87.8M of 1G   50.7M of 1G


1 Below is a bad stdout:
# cf app xyzcorp-8-314
Showing health and status for app xyzcorp-8-314 in org aac / space amecheapps as admin...
OK

requested state: stopped
instances: 1/1
usage: 1G x 1 instances
urls: xyzcorp-8-314.cf.pdk1.audcom.cci.att.com

     state     since                    cpu    memory        disk



2 Below is bad stdout:
# cf app ag201e51eucwvr5pyr040ikxy-44-56
Showing health and status for app ag201e51eucwvr5pyr040ikxy-44-56 in org attac / space amecheapps as admin...
FAILED
Server error, status code: 504, error code: 170007, message: Staging time expired: cannot get instances since staging failed




Below is the stdout of the script.
# ./tr3.pl
cf app stopped Getting: FAILED

cf app stopped Getting: Incorrect Usage.

cf app stopped Getting:

cf app stopped Getting: NAME:

cf app stopped Getting:    app - Display health and status for app

cf app stopped Getting:

cf app stopped Getting: USAGE:

cf app stopped Getting:    cf app APP

cf app stopped Getting:

cf app stopped OK: FAILED

cf app stopped OK: Incorrect Usage.

cf app stopped OK:

cf app stopped OK: NAME:

cf app stopped OK:    app - Display health and status for app

cf app stopped OK:

cf app stopped OK: USAGE:

cf app stopped OK:    cf app APP

cf app stopped OK:

cf app stopped : FAILED

cf app stopped : Incorrect Usage.

cf app stopped :

cf app stopped : NAME:

cf app stopped :    app - Display health and status for app

cf app stopped :

cf app stopped : USAGE:

cf app stopped :    cf app APP

cf app stopped :

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265: FAILED

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265: Incorrect Usage.

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265:

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265: NAME:

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265:    app - Display health and status for app

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265:

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265: USAGE:

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265:    cf app APP

cf app stopped 5syu6vbrfgbdrwndceeowhti5-142-265:

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206: FAILED

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206: Incorrect Usage.

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206:

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206: NAME:

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206:    app - Display health and status for app

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206:

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206: USAGE:

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206:    cf app APP

cf app stopped ATT-SIT-Entv2--cd72pq4f4exkrlesqxksudcmn-39-206:

cf app stopped NICE-nice-com-2yp7bsu4pyo8lsqxv1qz4kups-10-187: FAILED

cf app stopped NICE-nice-com-2yp7bsu4pyo8lsqxv1qz4kups-10-187: Incorrect Usage.

cf app stopped NICE-nice-com-2yp7bsu4pyo8lsqxv1qz4kups-10-187:
0
wilcoxonCommented:
Try this:
#!/usr/bin/perl
use strict;
use warnings;
use Fatal qw(open close);
my $state_critical = 2;
open my $IN, 'sudo /usr/bin/cf apps |';
my $bad=0;
foreach (<$IN>) {
    next if /^name\s+requested state/;
    my ($apps, $everything_else) = split /\s+/, $_, 2;
    open my $APP, 'sudo /usr/bin/cf app $apps |';
    while (<$APP>) {
        next if 1../^Showing health and status for app/;
        if (/^\s*requested\s+state:\s+(\w+)/ and $1 ne 'started') {
            print "cf app $1 $apps: $_\n";
            $bad++;
        } elsif (/^FAILED\s*$/) {
            my $err = <$APP>;
            print "App failure $apps: $err\n";
            $bad++;
        }
        next unless (/^#/ and /\brunning\b/);
        my ($amt, $amt2) = m{\s([\d\.]+)M\s+of\s.*\s([\d\.]+)M\s+of\b};
        if ($amt >= 92 or $amt2 >= 92) {
            print "App memory issue: $_\n";
            $bad++;
        }
    } 
}
 
exit $state_critical if $bad;
print "cf apps OK\n";

Open in new window


Let me know if there are any issues (if there are, I'll take the time to bootstrap this so I can test it without having cf).
0
Troush2009Author Commented:
wilcoxon,

There seems to be a issue because there is one app that will show FAILED and its not printing that out.
I also changed if ($amt >= 62 or $amt2 >= 62) to test and see it would print out this as well but did not.

# ./tr3.pl
cf apps OK

Below is the one app that show FAILED.
# cf app ag201e51eucwvr5pyr040ikxy-44-56
Showing health and status for app ag201e51eucwvr5pyr040ikxy-44-56 in org aac / space amecheapps as admin...
FAILED
Server error, status code: 504, error code: 170007, message: Staging time expired: cannot get instances since staging failed
0
jmcgOwnerCommented:
Just a context note pointing to Cloud Foundry. The "app" and "apps" subcommands are described on the following page:

cf Troubleshooting Commands

which seems to say that you use the "apps" subcommand to enumerate apps and the "app" subcommand with the name of an app to get details on a particular app.

I'll certainly take back my impression of excess complexity in the script in this respect; it seems to be required by the underlying task.
0
wilcoxonCommented:
Here's a version that works for me.  For some reason (on Strawberry Perl at least) the .. if toggle wasn't working so I kludged a work-around to do the same thing.  If you want to try the builtin .. operator, it would be next if /^/../regex/ where the regex is the third argument to toggle (1../regex/ doesn't work because it seems to get confused if it is a toggle or range operator (at least in Strawberry again)).
#!/usr/bin/perl
use strict;
use warnings;
use Fatal qw(open close);
my $state_critical = 2;
open my $IN, 'sudo /usr/bin/cf apps |';
my $bad=0;
my ($skip_apps, $skip_app) = (1,1);
foreach (<$IN>) {
    next if toggle('apps', 1, /^name\s+requested state/);
    my ($apps, $everything_else) = split /\s+/, $_, 2;
    open my $APP, "sudo /usr/bin/cf app $apps |";
    while (<$APP>) {
        next if toggle($apps, 1, /^Showing health and status for app/);
        if (/^\s*requested\s+state:\s+(\w+)/ and $1 ne 'started') {
            print "cf app $1 $apps: $_\n";
            $bad++;
        } elsif (/^FAILED\s*$/) {
            my $err = <$APP>;
            print "App failure $apps: $err\n";
            $bad++;
        }
        next unless (/^#/ and /\brunning\b/);
        my ($amt, $amt2) = m{\s([\d\.]+)M\s+of\s.*\s([\d\.]+)M\s+of\b};
        if ($amt >= 92 or $amt2 >= 92) {
            print "App memory issue $apps: $_\n";
            $bad++;
        }
    } 
}
 
exit $state_critical if $bad;
print "cf apps OK\n";

{ # closure
my %state;
sub toggle {
    my ($key, $true, $false) = @_;
    if ($true and not exists $state{$key}) {
        if ($false) {
            undef $state{$key};
            return 1;
        } else {
            $state{$key}++;
        }
    } elsif ($false) {
        undef $state{$key};
    }
    return $state{$key};
}
} # closure

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Troush2009Author Commented:
Wilcoxon,

 Yes, that worked as expected. I appreciate your help and want to say thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.