View Full Version : Perl Script confusion
openbsd-flipp
02-16-2005, 11:34 AM
I am working on a perl script to connect via sockets to all of our servers with an end result will be an email to the admins of a problem. The problem that I have is that I do not know to much about the basics of perl (even though I can program sockets -- go figure) and I am having a serious brain fart. I do not want to have a bunch of
die "Could not create socket: smtp_ssh \n" unless $smtp_ssh;
die "Could not create socket: smtp_smtp\n" unless $smtp_smtp;
die "Could not create socket: smtp_http\n" unless $smtp_http;
unless it is absolutely necessary and if it dies I will never get the email. Does any one have any ideas about this.
spankers
02-16-2005, 12:29 PM
If you're doing this to monitor services you may be trying to reinvent the wheel... have you taken a look at something like this?
http://www.nagios.org/about.php
openbsd-flipp
02-16-2005, 12:33 PM
We are currently using it but we want to be able to do alot of parsing on the data directly into mysql. My boss found it to be very unsatisfactory and it sometimes is easier to write a new project then to customize an existing one.
spankers
02-16-2005, 12:54 PM
Bummer. I'd figure someone else would have the need to parse data from Nagios and have written scripts already.
My Perl skills are essentially nil. Wish I could help.
openbsd-flipp
02-16-2005, 01:39 PM
The problem is more of with my boss wanting to do everything at once. The more I work on it the more he wants me to add to it. This would not be so bad if I was hired as a programmer but I was hired as a network engineer and he is the company programmer! Thanks for the suggestions though Spankers.
haven't worked in perl in about 7 years, so no help from me.
spankers
02-16-2005, 02:08 PM
Thanks for the suggestions though Spankers.
No worries... it might be time to pick up a couple of O'Reilly Perl books...
or just do a google search. :)
why not something like:
if(!$smtp_ssh)
{
$error_number = 1; // for ssh
send_failure_email($error_number);
die("Could not create socket: smtp_ssh \n");
}
i do not know if die in perl requires () either... but you can get the concept.
openbsd-flipp
02-17-2005, 10:46 AM
Is there a limit on the number of condions in an if statement?
such as
if ($Error1 && $Error2 && $Error3 && $Error4 && $Error5 && $Error6 != '')
after causing the errors (purposfully) i get strings in the errors but the if statement is bybassed.
znaps
02-17-2005, 11:05 AM
Shouldn't those be logical ORs?
spankers
02-17-2005, 11:29 AM
Would that be expressed as ($Error1 || $Error2 || $Error3... ) in Perl. Just curious.
spankers
02-17-2005, 11:32 AM
Sorry... I should just RTFM and STFU.
openbsd-flipp
02-17-2005, 11:58 AM
If I run it with the first four errors in it and trigger an error, the system works. Past four it starts generatiing errors and tries to tell me then there is no errors but at the bottom i have it display the contents of the eror variables it shows that there is an error.
Now that you point it out to me I think that way will work better. I am going to give it a shot and let you know.
openbsd-flipp
02-17-2005, 12:04 PM
Works muche better now. Although I think that this way of checking for error codes is going to be a pain in the neck. What I think I will try once the system starts to get larger is just creating a variable called $NumErrors and set it to 0. Later when I check it for errors i will just do a simple $NumErrors = $NumErrors +1; which would make my code much cleaner and allow fo checking on the number of errors and depending on the depth level I go into the number of errors per section.
Yes, they should be ORs and their is no limit other then it being ugly. Why not do an append to $error for each error, with a newline & carriage return to put each error on a new line. Then you only have to do one check on $error instead of checks on 6 different variables.
If you use &&, then all of the variables must meet the condition. If you use ORs, then only one of them has to meet it.
llbbl
02-21-2005, 05:44 PM
in perl the OR operator can be either "||" or "or"
llbbl
02-21-2005, 06:21 PM
Why not create an array of sockets and IP's then loop through the number of socket:ports you have with the follow code
$destaddr = sockaddr_in($port, inet_aton($remote));
connect SOCK, $desaddr
or die "Can't connect to $remote at port $port: %!";
Then store which ones fail in an $err array and then at the end when you want to send an email or not do something like this.
if defined ($err)
send_email($err);
or you could append the $err messages to a logfile and then write some code to display the logfile on a webpage.
You might look into using the standard module Carp for custom error messages
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.