CSV Import Problem with Zero-Values (0) (with suggestion)

Support requests, bug reports, questions etc.

CSV Import Problem with Zero-Values (0) (with suggestion)

Postby breml_ » Mon Dec 16, 2013 09:25

Hi

I tried to import host-templates via CSV files, but I ran into problems. The CSV importer doesn't like "0" as values. This is especialy a problem, if one tries to set a required field to the value "0", which is the normal case for the field "register" in templates.

CSV-File:
Code: Select all
name;register;active_checks_enabled;passive_checks_enabled;notifications_enabled;check_freshness;freshness_threshold
Host_Template;0;1;0;1;0;86400


Command:
Code: Select all
/var/www/html/nconf/bin/add_items_from_csv.pl -c host-template -f /root/nconf/static/host-template.csv


Output:
Code: Select all
[ Initializing NConf perl-API (library version 0.3, written by A. Gargiulo) ]
[ Copyright (c) 2006 - 2012 Sunrise Communications AG, Zurich, Switzerland  ]

[INFO]  Started executing /var/www/html/nconf/bin/add_items_from_csv.pl
[INFO]  CSV syntax found in file header. Using it.
[INFO]  Adding host-template 'Host_Template'
[WARN]  Mandatory attribute 'register' missing for host-template 'Host_Template'.
[ERROR] Failed to add host-template 'Host_Template'. Aborting


After looking into the code I found two locations, where the content of a variable is checked and where the value 0 resolves to false. This leads to the removal of the value for the respective field (register in my example).
I suggest to replace these checks with a variant, which handles the string "0" correctly.

1. nconf/bin/lib/NConf/ImportCsv.pm, line 153
I replaced
Code: Select all
if (my $value = shift @$row) {

with
Code: Select all
my $value = shift @$row;
if ($value || length $value) {


2. nconf/bin/lib/NConf/DB/Modify.pm, line 379
I replaced
Code: Select all
if((!$main_hash{$man_attr} && $main_hash{$man_attr} != 0) || $main_hash{$man_attr} eq ""){

with
Code: Select all
if((!$main_hash{$man_attr} && $main_hash{$man_attr} != 0) || $main_hash{$man_attr} eq "" || !length $main_hash{$man_attr}){


I'm not a perl specialist, so there may be drawbacks with my suggestion.
I would love to see a proper bug-fix for nconf, which allows to import CSV-files with "0" values.

Regards,
Lucas
breml_
 

Return to Support (NConf 1.3.0)

Who is online

Users browsing this forum: No registered users and 3 guests

cron