Big news! The community will be moving to a new platform April 21. Read more.
Big news! The community will be moving to a new platform April 21. Read more.
Lieutenant Commander
Lieutenant Commander
551 views

Measurement Threshold - Folder Size Template

Hello,

So i was trying to create a template which receives parameters and monitor a folder using perl. it appears like everything works perfectly apart when it comes to close the alert!

either the "if" statement is wrong because of my variables or something is wrong with my close correlation (i did check the script on my machine locally and its working...)

please ignore that there is no validation check etc.

thought someone would be able to help me with that:

Script:

use constant TRUE => 1;

use constant FALSE => 0;

 

                my $strHost;

                $strHost = $OVOSystem->ParameterGetString("host","%%host%%");

                $Session->Value('host', $strHost);

 

                my $path;

                $path = $OVOSystem->ParameterGetString("filesname","%%filesname%%");

                $Session->Value('filesname', $path);

 

                my $foldersize;

                $foldersize = $OVOSystem->ParameterGetString("foldersize","%%foldersize%%");

                $Session->Value('foldersize', $foldersize);

 

sub main

{

                my $GetFolderSize = `du -b -s $path | awk '{print $1}'`;

                my $MyFolderSize = $GetFolderSize; #/ 1024 / 1024;

                if($MyFolderSize gt $FolderSize)

                {

                                $Rule->Status(TRUE);

                }

}

&main();

 

Picures were also added.

 

Thanks in advance!

0 Likes
12 Replies
Fleet Admiral Fleet Admiral
Fleet Admiral

Cant help you with that as my approch will be different but much simpler may be not that elegant.

Use configfile policy or paramters to configure the paths.

loop through it with your script that does  du -b -s .$PATH for each and print it into a (overwrite on each run) into a log like timestamp hostname path size

then use a structured logfile policy for metrics that writes the data into CODA (read from begin always)

Then use a seperate treshold ppolicy to create events of CODA data.

for this only a schedule policy, structured logfile policy and the monitor policy is needed.  with perls script you need to create instrumentation as well but with bash its possible to do it as one liner in scheduler policy.

This embedded perl policy stuff is really hard to debug and not easy to see whats going on. because of this i do policy development the old fashioned way.

Micro Focus Expert
Micro Focus Expert

Hello,

I don't think that you can assume Rule->Status to be 0 (FALSE) by default.
It probably keeps its value across script executions (though I have not tested).
You should add an "else" statement to your main sub that will set Rule->Status to 0 (FALSE).

Regards,
Thierry.

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
Lieutenant Commander
Lieutenant Commander

Hello Rotax,
I've also added else option and when ive seen it's doing nothing i removed it, a friend of mine told me it's default is 0 unless told different.
0 Likes
Micro Focus Expert
Micro Focus Expert

Hello,

OK, still I recommend that you initialize variables as good practice. Don't assume that something will do it the way you hope.

So then having a closer look at your script, I notice two problems:

1. Perl variable names are case sensitive (you should use consistently "$foldersize" or "$FolderSize")

2. The numerical comparison operator for "greater than" is ">" ("gt" is for strings)

Hope it helps.

Regards,
Thierry.

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
Lieutenant Commander
Lieutenant Commander

Hello Rotax,
I did checked it it worked with $foldersize and $Foldersize combined, but i tried and changed them to make sure they are all the same.
also i changed the "gt" to ">" i tried that couple of time before i wrote this post and still didnt work (or atleast didnt had an effect on the outcome)

i've also tried to cast the variable to "int" like this:
if(int($MyFolderSize) > int($FolderSize))

and still didnt work.
0 Likes
Micro Focus Expert
Micro Focus Expert

Hello,

If you mix case or if you use the wrong operator, it might "work" but not do what you expect. For instance:

# perl -e 'my $Size=100; my $Threshold=10; if ( $Size gt $Threshold ) { print "yes\n"; } else { print "no\n" }'
yes
# perl -e 'my $Size=100; my $Threshold=19; if ( $Size gt $Threshold ) { print "yes\n"; } else { print "no\n" }'
no

The reason for the above result is that "gt" will do an alphabetical comparison, and 100 comes before 19 indeed in alphabetical order.

Have you created an "End action" in the policy?
Also I hope you are passing only one path in the %%filesname%%, because your script contains no logic to parse multi-line output of 'df'.

At this stage, I recommend that:

  • You implement tracing in your script by printing intermediate results into a file
  • Upload the policy definition file to this thread (I may not have the bandwidth to check it, but maybe somebody else can)

To locate the policy definition file, go to /var/opt/OV/datafiles/policies/monitor on the node where the policy was deployed and identify the file like ###_data that contains the definition of this policy (these files are ascii so you can run 'cat' on them to find your specific policy).

Regards,
Thierry.

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
Lieutenant Commander
Lieutenant Commander

Hey Rotax,
I couldnt really understand your example:
howcome that 100 us gt than 10 but not gt than 19?
or you used the wrong operator in the example and it should be like this:
100 > 10 = yes
100 gt 19 = no

0 Likes
Micro Focus Expert
Micro Focus Expert

Hello,

I already gave the answer in my previous post.

The operator 'gt' performs an alphabetical comparison. It doesn't interpret 10, 19 and 100 as number, but considers them like strings. When comparing alphabetically, all the below are true:

"z" gt "a"
"abc" gt "aaa"
"a2" gt "a1"
"a2" gt "a10"
"9" gt "1"
"19" gt "100"

This was just to explain that when you use the wrong operator, you still get something, but not necessarily what you expect.

Since you need the numerical operator, you should use ">".

Regards,
Thierry.

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
Micro Focus Expert
Micro Focus Expert

Hello,

I just noticed you attached some screenshots of the policy (sorry for missing it initially).

The event keys look OK, but:

  • You probably wanted to set BADSTATE in the start action and GOODSTATE in the end action
  • You should anchor the event key relations (in field "Close Events with key"), e.g.:

    ^<$MSG_NODE_NAME>:<$NAME>:<$SESSION(filesname)>:<*>$

    Notice the leading caret (^) and the trailing dollar ($) that respectively mean that the event key should match from the beginning to the end (no extra characters at the beginning or at the end of the event key are allowed).

Not anchoring the relations can lead to (severe) performance problems on OBM and occasionally to excess correlation.

But the above does not explain the problem that you described.
You need to apply all the corrections that I mentioned and implement tracing in your script.
It may also still be useful to upload the policy definition file as explained earlier (there might be some wrong setting in some other place which I can't think of now).

Regards,
Thierry.

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
Lieutenant Commander
Lieutenant Commander

Hello Rotax,
Thanks for your reply, I started to debug my script and it appears like $MyFolderSize was holding some text with the number, something like this (60 /tmp/moo.txt)
i fixed it and now i print to a file if the value is bigger or smaller (i get true or false to logfile as well) however it seems like my problem is with the "Close Events with Key"

here is the policy itself:
SYNTAX_VERSION 10





ADVMONITOR "taltest"

DESCRIPTION ""

INTERVAL "1m0s"

SCRIPTTYPE "Perl"

INSTANCEMODE SAME

MAXTHRESHOLD

SEPARATORS " "

SEVERITY Unknown

PROGRAM "Mooo"

DESCRIPTION ""

MONPROG "<$NOPROGRAM>"

MSGCONDITIONS

DESCRIPTION "TRUE"

CONDITION_ID "45122C46-E891-AB57-2FAA-E59681E61C3C"

CONDITION

THRESHOLD

SCRIPT "# use strict;

# use warnings;



my $strHost;

$strHost = $OVOSystem->ParameterGetString(\"host\",\"%%host%%\");

$Session->Value('host', $strHost);



my $path;

$path = $OVOSystem->ParameterGetString(\"filesname\",\"%%filesname%%\");

$Session->Value('filesname', $path);



my $FolderSize;

$FolderSize = $OVOSystem->ParameterGetNum(\"FolderSize\",\"%%FolderSize%%\");

$Session->Value('FolderSize', $FolderSize);



sub main

{

my $GetFolderSize = `du -b -s $path`;

my @GetFolderSizeArray = split(/\\s+/, $GetFolderSize);

my $MyFolderSize = $GetFolderSizeArray[0]; #/ 1024 / 1024;

open my $file, \">>\", \"/tmp/moo/moo.txt\" or die \"can't open!\";

if($MyFolderSize > $FolderSize)

{

$Rule->Status(1);

print $file \"True - $MyFolderSize is bigger than the amount of $FolderSize \\n\";

}

else

{

$Rule->Status(0);

print $file \"False - $MyFolderSize is bigger than the amount of $FolderSize \\n\";

}

}

&main();"

SETSTART

SEVERITY Major

MSGKEY "<$MSG_NODE_NAME>:<$NAME>:<$SESSION(filesname)>:Open"

MSGKEYRELATION ACK "^<$MSG_NODE_NAME>:<$NAME>:<$SESSION(filesname)>:<*>$" SEPARATORS " "

CUSTOM "Description" "hello world"

TEXT "%%filesname%% is larger than %%FolderSize%%"

SETEND

SEVERITY Normal

OBJECT "<$SESSION(filesname)>"

MSGKEY "<$MSG_NODE_NAME>:<$NAME>:<$SESSION(filesname)>:Closed"

MSGKEYRELATION ACK "^<$MSG_NODE_NAME>:<$NAME>:<$SESSION(filesname)>:<*>$" SEPARATORS " "

TEXT "%%filesname%% is no longer larger than %%FolderSize%%"
0 Likes
Micro Focus Expert
Micro Focus Expert

Hello,

On first quick look, I can't spot the reason for the problem now.
I would look at an "open" and a "closed" event in the event perspective and check the values of the attributes "Key" and "Close Events with Key". Maybe they don't resolve to the expected value for some reason.

You should also check the setting of "Detected Related Events by Key Matching Pattern" in the Infrastructure settings / OPERATIONS MANAGEMENT - Change State Of Related Events Settings. It is "true" by default, but perhaps it was changed to "false" in your environment.

And finally, I assume that you checked again a few minutes later whether the "open" event was closed automatically. There can be some latency and sometimes even more than a few minutes, especially if you have lots of events with relations that are not anchored with "^" and "$".

Regards,
Thierry.

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.