1752794 Members
6190 Online
108789 Solutions
New Discussion юеВ

Re: Perl script needed

 
SOLVED
Go to solution
Allanm
Super Advisor

Perl script needed

Hi!

I have a curl call which gets me the following output:

curl http://hostname1.qa.com:8888/getlist

OUTPUT -

{
"token" = "201119041112";
"apps" = {
"PaymentApp" = "http://hostname1.qa.com:2333/cgi-bin/Jboss/PaymentApp-EMEA.woa/paymentInfo";
"OrderConsumer" = "http://hostname.qa2.com:2345/apps/orders";
...
};
}

Based on the curl call which is being made to a specific URL , I need to parse out URL entries which are of similar pattern as the URL to which the call was made (http://hostname1.qa.com) -

I need to scrub out only entries which have hostname1 in them.

Then I need to populate a couple of properties files which look like this with the appname (e.g. PaymentApp) , URL(http://hostname1.qa.com:2333), host(hostname1.qa.com) , port(e.g. 2333) , env (e.g. qa) information that I gathered from above -

file1 -

define service{
host_name $hostname
use generic-service
servicegroups preprod apps
service_description $appname
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 2
retry_check_interval 1
contact_groups admin
notification_interval 120
notification_period 24x7
notification_options w,u,c,r
process_perf_data 0
check_command url-check_$env_$port
}

file2 -

define command{
command_name url-check_$env_$port
command_line $USER1$/url-check -H $hostname -p $port -r added --regex=alive -t 4
}


I have a way of doing this in shell and now looking for a way to do this in perl.

Thanks,
Allan.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Perl script needed

Hi Allan:

> I have a way of doing this in shell and now looking for a way to do this in perl.

OK, so...

If I had to guess, I'd guess that this is all about Nagios and a plan to refactor shell scripts into pure Perl. Correct?

It's not going to help you (or me) if I provide full-blown solutions for you. You will be better served by trying and learning by trail-and-error. If you are serious, and you haven't already looked here, do so:

http://www.perl.org/learn.html

Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: Perl script needed

Hi Allan,

although the curl output already seems to be quite ordered which maybe suggests to better make up some parsing grammar and feed modules like Parse::RecDescent or similar with it, one could tinker up some weird regexp like this to capture the items you are interested in:

open CURL, "/usr/bin/curl $fetchurl |"
or die "cannot pipe from curl\n";
while (<>) {
if (my ($app, $url, $host, $env, $port) = $_ =~
m{"([^"]+)"\s+=\s+"?(http://(hostname1)\.([^.]+)\.[^:]+:(\d+)/.*)}) {

# process $app, $host, $url, $port, $env etc. here


}
close CURL;



However, I would reconsider if you really want to define a separate Nagios service object for each parsed URL.
I probably would rather define a single service object and use a check_multi check command on it.
(see http://my-plugin.de/wiki/projects/check_multi/start , sorry not clickable to preserve code spacing)

You could also define custom variable macros which you could refer to as e.g. $_HOSTMY_CUSTOM_MACRO$ in a single service definition.
See http://nagios.sourceforge.net/docs/3_0/customobjectvars.html

And don't forget that you can pass arguments to your check command as well like

define service {
...
check_command url-check!$_HOSTENV$!$_HOSTPORT$
}


But probably there isn't much gain because this way you would have a host of host definitions.
Madness, thy name is system administration
Allanm
Super Advisor

Re: Perl script needed

Hi All!

Here is what I have currently -

curl http://host.domain.com:8000/Jboss/Service.jbs/america?method=geturls|grep host.domain.com |awk '{print $1,$3}'

"OrderApp" "http://host.domain.com:8100/apps/Orderinfo";
"EmailAppTest" "http://host.domain.com:5100/apps/email";
...
...

I need to strip out the following words -

OrderApp host.domain.com 8100
EmailAppTest host.domain.com 5100
...
...


So appname , host & port from above.
Then once I have that I can echo that to generate properties files through a while loop.

Thanks,
Allan.

James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl script needed

Hi Allan:

# perl -nle 'm{(OrderApp|EmailAppTest)".*?//(.*):(\d+)/} and print "$1 $2 $3"' file

...or from a pipe, of course, in lieu of a file...

Regards!

...JRF...
Allanm
Super Advisor

Re: Perl script needed

Thanks JRF!

The matching can differ so I need a generic solution.

Regards,
Allan.

James R. Ferguson
Acclaimed Contributor

Re: Perl script needed

Hi (again) Allan:

> The matching can differ so I need a generic solution.

Of course, you said that. Try:

# perl -nle 'm{"(.*?)".*?//(.*):(\d+)/} and print "$1 $2 $3"' file

Regards!

...JRF...