Error when filling up my configuration files to set up Nagios
I started in Perl and especially in programming. I am enthusiastic to set up Nagios in my business, very good solution that way, and I want to make a script to more easily fill my configuration files (and especially avoid errors). They advise me to use for my Perl scripts. So now the targets of my scripts:
They must complete the configuration files with a string long enough after all, or only two elements are modified (using variable). Therefore it's been a while since I got stuck with a course of a user.
So here is what I have laid (of course it does not work, otherwise I would not bother you).
Code:
#! /usr/bin/perl -w
my $i = 0;
my $v = 52;
for ($i < $v; $i+=1)
{
open (FIC, ">>/usr/local/nagios/etc/objects/network.cfg")
printf ("define service{\n");
printf ("use generic-service\n");
printf ("hostgroups FI026-Switch\n");
printf ("service_description type interface $i\n");
printf ("check_command check_snmp!-C public -o ifType.$i\n");
printf ("}\n");
close (FIC);
exit(1)
}
The variable "i" is on the line "service_description" and "check_command. The two errors that I have in execution are
Quote:
syntax error at ./define-network.pl line 7, near "1)"
syntax error at ./define-network.pl line 12, near ");"
Execution of ./define-network.pl aborted due to compilation errors.
Re: Error when filling up my configuration files to set up Nagios
Several remarks to begin:
1) Learn to use pragmas strict and warnings from the outset that you will forge a way to program cleaner.
To indicate the beginning of the script:
Code:
#! /usr/bin/perl -w
use strict;
use warnings;
2) Your loop seems incomplete. Unless it finished changing, there are 3 conditions in a loop: initialization, loop end, increment.
Your loop should it not be rather:
Code:
for ($i = 0; $i < $v; ++$i)
?
3) The semicolon is missing at the end of this line:
Code:
open (FIC, ">>/usr/local/nagios/etc/objects/network.cfg")
4) I do not understand why you open 50 times the same file, why you write anything in it and why you use the printf function while print seems easier to use (especially here).
Re: Error when filling up my configuration files to set up Nagios
First thank you for responsiveness, it's nice! I will correct what you said. Just one thing what is this?
use strict;
use warnings;
Finally, the question is rather what's the use??
Re: Error when filling up my configuration files to set up Nagios
strict requires the developer to declare all the variables he uses in his script.
warnings makes the Perl compiler/interpreter more talkative, which lets you know if your code is correct or if there are risks such as certain variables are not properly met.
Re: Error when filling up my configuration files to set up Nagios
Well I modified my code and I placed the opening of the file before the declaration of the loop. This gives the following code:
Code:
#! /usr/bin/perl -w
use strict;
use warnings;
my $i;
my $v = 52;
open (FIC, ">>/usr/local/nagios/etc/objects/network.cfg");
for ($i=0; $i < $v; ++$i)
{
printf ("define service{\n");
printf ("use generic-service\n");
printf ("hostgroups FI026-Switch\n");
printf ("service_description type interface $i\n");
printf ("check_command check_snmp!-C public -o ifType.$i\n");
printf ("}\n");
}
close (FIC);
exit(1)
Although it gives me the result I want in the shell, but it wrote nothing in the file. I run the program with the root user (logically this is not an issue of user rights).
So I guess it isn't printf that must be used to write to a file (EOF which is more). If anyone knows please let me know. I continue my research on my side.
Re: Error when filling up my configuration files to set up Nagios
Quote:
So I guess it isn't printf that must be used to write to a file (EOF which is more).
Code:
open (FIC, ">>/usr/local/nagios/etc/objects/network.cfg");
for ($i=0; $i < $v; ++$i)
{
print FIC "blablabla";
}
close (FIC);
printf is used for formatting.