Nagios Host creator, .txt manipulator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
VFaretrajr
Posts: 1
Joined: 20 Sep 2018 06:25

Nagios Host creator, .txt manipulator

#1 Post by VFaretrajr » 20 Sep 2018 06:46

Hopefully this little batch script will save hundreds - if not thousands - of LAN / WAN engineers (specifically using Nagios) in adding / creating hosts to import.

So, I have a .txt file which contains over 1200+ lines of building id's, ipaddress. So, to be clear, the format of data in my txt file example looks like this:

00Q0VA,10.1.1.2
00Q0VB,10.1.1.3
00Q0VC,10.1.1.4
etc....

What I need is a DOS script which takes the all the data from this one txt file and output into another SINGLE txt file ONLY with adding lines which include the static data from the original modified to be imported into nagios. So, to be clear using the example above, the single output file should look identical to this:

define host{
use pos_mgr_template ; Inherit default values from a template
host_name 00Q0VA ; The name we're giving to this host
alias 00Q0VACAFE ; A longer name associated with the host
address 10.1.1.2 ; IP address of the host
}

define host{
use pos_mgr_template ; Inherit default values from a template
host_name 00Q0VB ; The name we're giving to this host
alias 00Q0VBCAFE ; A longer name associated with the host
address 10.1.1.3 ; IP address of the host
}

define host{
use pos_mgr_template ; Inherit default values from a template
host_name 00Q0VC ; The name we're giving to this host
alias 00Q0VCCAFE ; A longer name associated with the host
address 10.1.1.4 ; IP address of the host
}

etc....
Notice the inputted change lines are the 3rd (host_name), 4th (building ID with the word "CAFE" appended to it), and 5th (the IP)

I've gotten as far as trying to manipulate a do while, for /f token %%a, however, it's where I hit a wall with adding the lines.

Any help would be greatly appreciated and, if in the NYC area, gets a free pizza pie! LOL

Squashman
Expert
Posts: 4114
Joined: 23 Dec 2011 13:59

Re: Nagios Host creator, .txt manipulator

#2 Post by Squashman » 20 Sep 2018 07:00

Code: Select all

@echo off

(FOR /F "TOKENS=1,2 DELIMS=," %%G IN (input.txt) DO (
	echo define host{
	echo use pos_mgr_template ; Inherit default values from a template
	echo host_name %%G ; The name we're giving to this host
	echo alias %%GCAFE ; A longer name associated with the host
	echo address %%H ; IP address of the host
	echo }
)
)>Output.txt

Post Reply