Results 1 to 6 of 6

Thread: Error when filling up my configuration files to set up Nagios

  1. #1
    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
    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.

  2. #2
    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).

  3. #3
    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??

  4. #4
    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.

  5. #5
    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.

  6. #6
    Join Date
    Nov 2008
    Posts
    1,185

    Re: Error when filling up my configuration files to set up Nagios

    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.

Similar Threads

  1. Replies: 3
    Last Post: 22-05-2012, 05:32 PM
  2. Create more users for Nagios web interface
    By Svana in forum Operating Systems
    Replies: 4
    Last Post: 12-11-2010, 06:32 AM
  3. Nagios VS Zabbix
    By KuNaLBuntU in forum Polls & Voting
    Replies: 3
    Last Post: 25-05-2010, 10:48 AM
  4. How to install Nagios on Debian
    By Virtuous in forum Networking & Security
    Replies: 5
    Last Post: 02-03-2010, 11:13 AM
  5. Filling multiple XML files from an XLS file
    By MAHAH in forum Software Development
    Replies: 4
    Last Post: 09-12-2009, 08:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •