build hosts file from database

User / Community contributed content: modifications, patches, extensions, modules, scripts etc.

build hosts file from database

Postby naboj » Fri Jun 18, 2010 15:44

As nconf is holding pairs of IP and hostname in the database I thought it would be smart to export these pairs to a hosts like file.
Therefore i applied this patch to ExportNagios.pm
Code: Select all
--- ExportNagios.pm   2010-06-01 14:34:45.000000000 +0200
+++ ExportNagios.pm.new   2010-06-18 15:22:04.574410628 +0200
@@ -187,6 +187,8 @@
             &write_htpasswd_file("$global_path/nagios.htpasswd",\@class_items);
         }
     }
+    # create global hosts file
+    &write_hosts_file("$global_path/hosts");
 }
 
 ########################################################################################
@@ -1027,6 +1029,37 @@
     close(FILE);
     $files_written{$path} = $path; # remember which files were already written (needed for appending logic)
 }
+########################################################################################
+# SUB write_hosts_file
+# Create a hosts file consisting of ip - hostname pairs
+
+sub write_hosts_file {
+    &logger(5,"Entered write_hosts_file()");
+
+    # read params passed
+    my $path = $_[0];
+    my $sql = undef;
+
+    # fetch collector-specific options, these will be added to each service
+    $sql = "SELECT x.attr_value AS ip, y.attr_value AS hostname
+    FROM ConfigValues AS y, ConfigValues AS x
+    WHERE y.fk_id_attr =15
+    AND x.fk_id_attr =17
+    AND y.fk_id_item = x.fk_id_item
+    ORDER BY x.attr_value";
+
+    my @hosts = &queryExecRead($sql, "Fetching ip and hostnames", "all");
+
+    open(FILE,">$path") or &logger(1,"Could not open $path for writing");
+    &logger(4,"Writing file '$path'");
+
+    foreach my $row (@hosts){
+       print FILE "$row->[0]\t$row->[1]\n";
+   }
+
+    close(FILE);
+}
+
 
 ########################################################################################
 # SUB write_htpasswd_file
naboj
 

Re: build hosts file from database

Postby Tontonitch » Tue Dec 27, 2011 00:47

Hi,

Old post, but the following info may interest some users.
This is now directly possible to generate such a list by using the get_items.pl script located in <nconf_dir>/bin:

Example 1:
[icinga@snoopy bin]$ ./get_items.pl -c host -a host_name,address

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

host_name: hplj2605dn
address: 192.168.1.30

host_name: linksys-srw224p
address: 192.168.1.253

host_name: localhost
address: 127.0.0.1

host_name: test
address: test

host_name: winserver
address: 192.168.1.2

[icinga@snoopy bin]$


Example 2:
[icinga@snoopy bin]$ ./get_items.pl -c host -a host_name,address -e -d ": "

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

host_name: address
hplj2605dn: 192.168.1.30
linksys-srw224p: 192.168.1.253
localhost: 127.0.0.1
test: test
winserver: 192.168.1.2
[icinga@snoopy bin]$


BR,
Yannick
User avatar
Tontonitch
NConf addict
NConf addict
 
Posts: 254
Joined: Fri May 07, 2010 10:14
Location: Thionville, France


Return to Contributions

Who is online

Users browsing this forum: No registered users and 1 guest

cron