Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Check for data in array

by fazedandconfused (Novice)
on Feb 08, 2012 at 12:08 UTC ( #952469=perlquestion: print w/ replies, xml ) Need Help??
fazedandconfused has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm trying to get a perl script working and having a little trouble. If anyone knows of the Xymon monitoring tool, this is a server side script that I am working on.

I'm looking to check the Xymon client data to see if a partcular service is running but I can seem to get the if to work.

It's always dropping through to the "else" section whether "service.exe" is in $sections{"procs"} or not.

Any help would be very much appreciated. This is the particular piece of code,
if ( $sections{"procs"} !~ m/service.exe/ ) { $color = "yellow"; $summary = "`/bin/date` CAWA Server service is not run +ning."; $statusmsg = "&yellow service.exe not running. Possibl +e failover to ukclasce002. Inform OSG before 8:30am.\n\n" . $s ections{"procs"}; } else { $color = "green"; $summary = "`/bin/date` CAWA Server service OK $os"; $statusmsg = "&green service.exe running ok.\n\n" . $s +ections{"procs"}; }
and this is that data contained in $sections{"procs"}
PID Name 0 Idle 4 System 352 smss.exe 400 csrss.exe 424 winlogon.exe 472 services.exe 484 lsass.exe 644 vmacthlp.exe 684 svchost.exe 752 svchost.exe 816 svchost.exe 840 svchost.exe 856 svchost.exe 996 spoolsv.exe 1196 cagent32.exe 1228 xferwan.exe 1252 svchost.exe 1400 FrameworkService.exe 1832 sqlservr.exe 1868 svchost.exe 1916 sqlwriter.exe 2076 VMwareService.exe 2156 VMUpgradeHelper.exe 2208 tlmagent.exe 2320 wmiprvse.exe 2948 svchost.exe 3380 dllhost.exe 3812 wmiprvse.exe 4040 svchost.exe 2972 lpx86.exe 9900 mfevtps.exe 7904 mcshield.exe 7984 VsTskMgr.exe 4912 mfeann.exe 7112 naPrdMgr.exe 9348 explorer.exe 3176 VMwareTray.exe 3656 VMwareUser.exe 9988 UdaterUI.exe 6696 VProTray.exe 8868 ctfmon.exe 8296 WindowsSearch.exe 6656 McTray.exe 5476 VProSvc.exe 8316 SymSnapService.exe 3292 logon.scr 7876 csrss.exe 360 winlogon.exe 180 rdpclip.exe 9580 explorer.exe 1368 VMwareTray.exe 1512 VMwareUser.exe 6440 VProTray.exe 1332 shstat.exe 1552 ctfmon.exe 140 WindowsSearch.exe 5284 mmc.exe 9324 mmc.exe 8872 BBWin.exe 9712 service.exe

Comment on Check for data in array
Select or Download Code
Re: Check for data in array
by choroba (Abbot) on Feb 08, 2012 at 12:22 UTC
    How is the data stored in the hash value? As a multi-line string? As an array reference? Hash reference, hashed by PID?

      I'm not sure as I didn't write all this code. Most of it is taken from a Xymon sample.

      Here is the peice of code that builds the Hash,

      while ($line = <STDIN>) { if ($line =~ /^\@\@client\#/) { # It's the start of a new client message - the header +looks like this: # @@client#830759/HOSTNAME|1169985951.340108|10.60.65. +152|HOSTNAME|sunos|sunos # Grab the hostname field from the header @hdrfields = split(/\|/, $line); $hostname = $hdrfields[3]; # Clear the variables we use to store the message in $msgtxt = ""; %sections = (); } elsif ($line =~ /^\@\@/) { # End of a message. Do something with it. processmessage(); } elsif ($line =~ /^\[(.+)\]/) { # Start of new message section. $cursection = $1; $sections{ $cursection } = "\n"; } else { # Add another line to the entire message text variable +, # and the the current section. $msgtxt = $msgtxt . $line; $sections{ $cursection } = $sections{ $cursection } . +$line; } }
        Use Data::Dumper, try
        warn Dumper \%sections;
        before matching the regexp. With your sample data contained as string in the hash, everything should work.
Re: Check for data in array
by thezip (Vicar) on Feb 08, 2012 at 15:28 UTC

    If I find that something that "something absolutely *should* work", yet doesn't, I create an override for the data by replacing the normal input with hardcoded values that test all possible conditions.

    For example, just before entering into the "if" statement, hardcode %sections to contain:

    $sections{'procs'} = 'service.exe'; # Should result in success #$sections{'procs'} = 'failure.exe'; # Should now result in failure

    If the successful hardcode fails, then you have half-split the problem to determine that your logic is incorrect, rather than the data than is being supplied to it. If it works as expected, then your data is the problem.

    Pretty basic stuff, but tricks like this allow you to remove all assumptions about the data you think you are processing.


    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011
Re: Check for data in array
by fazedandconfused (Novice) on Feb 08, 2012 at 16:49 UTC

    I've found the solution. If I remove the warn Dumper \%sections; and then change the comaprison to add the "m" flag after the pattern as below,

    if ( $sections{"procs"} !~ /service.exe/m ) { $color = "yellow"; $summary = "`/bin/date` CAWA Server service is not run +ning."; $statusmsg = "&yellow service.exe not running. Possibl +e failover to ukclasce002. Inform OSG before 8:30am.\n\n" . $s ections{"procs"}; } else { $color = "green"; $summary = "`/bin/date` CAWA Server service OK $os"; $statusmsg = "&green service.exe running ok.\n\n" . $s +ections{"procs"}; }

    This now works as expected. Thanks for the suggestions anyway.

      Very strange. /m only affects ^ and $ that you do not use in the regular expression!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://952469]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-08-28 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls