Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Error when filling up my configuration files to set up Nagios

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
Member
 
Join Date: Jan 2010
Posts: 54
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.

Reply With Quote
  #2  
Old 12-02-2010
Member
 
Join Date: Nov 2008
Posts: 996
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).
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: Jan 2010
Posts: 54
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??
Reply With Quote
  #4  
Old 12-02-2010
Member
 
Join Date: Nov 2008
Posts: 996
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.
Reply With Quote
  #5  
Old 12-02-2010
Member
 
Join Date: Jan 2010
Posts: 54
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.
Reply With Quote
  #6  
Old 12-02-2010
Member
 
Join Date: Nov 2008
Posts: 1,185
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Error when filling up my configuration files to set up Nagios"
Thread Thread Starter Forum Replies Last Post
Windows temp folder is filling up because of etilqs_ files builded by WDsmartware Girish^techie Hardware Peripherals 3 22-05-2012 05:32 PM
Create more users for Nagios web interface Svana Operating Systems 4 12-11-2010 06:32 AM
Nagios VS Zabbix KuNaLBuntU Polls & Voting 3 25-05-2010 10:48 AM
How to install Nagios on Debian Virtuous Networking & Security 5 02-03-2010 11:13 AM
Filling multiple XML files from an XLS file MAHAH Software Development 4 09-12-2009 08:21 PM


All times are GMT +5.5. The time now is 08:55 PM.