Page 1 of 1

Creating Nagios Plugin Using TCL

Posted: Fri Mar 14, 2014 12:30 am
by anubhav65
Hi I want to integrate a TCL Script into Nagios , so that if the script doesn't runs up it may give up a email notification about the error i.e. "a sort of backup not done for this host".....
So please can any body help me in integrating my script into Nagios what can be the changes done in the script.

Thnks people.

Re: Creating Nagios Plugin Using TCL

Posted: Fri Mar 14, 2014 10:40 am
by slansing
Can you attach a copy of this script? Are you actually going to use this script to check something, and then make it responsible for telling nagios when an issue occurs? Such as a plugin? Or are you going to use a preexisting plugin to check something.

Re: Creating Nagios Plugin Using TCL

Posted: Mon Mar 17, 2014 8:20 am
by anubhav65
Actually I want to take automative backup of configuration files of different switches by ftp'ing into them, The symantics of the script is like this that my script will read(*from a txt file) the destination address and ftp into it, change directory as specified and get the file required so if in this process any anomaly occurs , Nagios should come into play and drop me a notification mail about the event, that for this certain switch , backup is not possible because of certain reason.
*the txt file will have destination ip's of different switches and it would be fetched by my script.

Re: Creating Nagios Plugin Using TCL

Posted: Mon Mar 17, 2014 4:44 pm
by lmiltchev
Can you attach a copy of this script?

Re: Creating Nagios Plugin Using TCL

Posted: Mon Mar 17, 2014 11:30 pm
by anubhav65
heres the code......

Code: Select all

#Prerequiste: keep the txt file from where we want to read the addresses in the same folder where tcl file is and change the username and password as per ur server.

package require Expect
#--------------------------------------
			set ftp_prompt "ftp>"    		
			#when protocol used is ftp
			set username "ur_username"        	
			#usr name of switch ftp server u need to change when u want a different switch
			set password "ur_password"		
			#psswrd of switch to be logged in, u need to change when u want a different switch
			set scp_prompt "scp>"			
			#when protocol used is scp
#--------------------------------------
set file1 [open "backup3.txt" r]			
# read the file and get the ip of switch to be logged in and the file address and name to be backed up
set counter1 0
while {![eof $file1]} {						#file1 is a file handler and reach uptill eof 
		gets $file1 line;
		set fileline1($counter1) $line;		#store the line in 1 element of array and store whole array in different lines
		incr counter1;#puts $fileline1([expr $counter1-1])		
	}
	#puts $counter1
for  {set i 0} { $i < $counter1-2 } { incr i 4} {		#since in backup3.txt file for each switch there would be 3 parameters in 3 different lines hence for each switch mentioned in file there should be a break of 3 lines for our ease 
	set f [expr $i+1]									
	# every 4th line in the txt file there is a break given to ease visibility for looking the number of switches files given to be backed up, hence this for loop is to identify each switch after a beak of 4 lines and backup there config.
	puts $f
	#puts [expr $i%3]
	
	if {$f%4!=0} {								#to check whether the line selected is not a blank file					
		#puts $f 
		set a2 ""
		regexp {:(.+)} "$fileline1($i)" pat a1    
		# to get the host IP in a1
		regexp {:(.*)/(.+)} "$fileline1([expr $i+1])" pat a2 a3  
		# to get directory in a2 and filename in a3
		regexp {:(.+)} "$fileline1([expr $i+2])" pat a4     
		# to get type of service u want to access in a4 like ftp or scp
		#puts "$fileline1([expr $i+2])"
		#puts $a1
		#puts $a2
		#puts $a3
		#puts $a4
		
			if {$a4=="ftp"} {    # to chk the protocol is ftp
				puts $a2
				set hostname $a1	
				puts stdout "Connecting to $hostname to backup"
				spawn ftp $hostname
				#spawn creates a session with a program or remote device that Expect can manage. Spawned sessions can be automated using other Expect commands
				expect -re "(Name|login|Login|User).*:.*" {					#if we get a similar prompt in our window 
					exp_send "$username\r"									
					#send the username stored
				}	eof {
						exp_send_user "sent username\n"						
						exp_send_user "could not connect\n"
					}
				expect "Password:" {
					exp_send "$password\r"									
					#send the password
				}
				if {$a2!=""} {
					expect -re $ftp_prompt {
						exp_send "cd $a2\r"									
						#move to the directory where the file is located
					}
				}
				expect -re $ftp_prompt {
						exp_send "bin\r"									
						#change the mode to binary
					}
				expect -re $ftp_prompt {
					exp_send "get $a3\r"									
					#download the file from server
				}
				expect -re $ftp_prompt {
					exp_send "bye\r"										
					#log out of the ftp service
				}
					
			}	
	} 
	set directory [ pwd]
	file mkdir $directory/$a1   
	#this will make a directory of specified switch ip in the same directory from which you are running the file from
	file copy -force $directory/boot.cfg $directory/$a1
	#this will copy the config.cfg file into the specified switch name folder
}

and content of backup3.txt(its format):

Code: Select all

IP:12.1.21.2
File:/fla/xyz/boot.cfg
Protocol:ftp

IP:12.1.21.3
File:/fla/xyz/boot.cfg
Protocol:ftp

IP:12.1.21.5
File:/fla/xyz/boot.cfg
Protocol:ftp
now the script will read the backup3.txt file and ftp into the server and get the file and make a corresponding directory for that switch ip and store up into that. the file downloaded.

Re: Creating Nagios Plugin Using TCL

Posted: Tue Mar 18, 2014 1:57 pm
by slansing
So you could essentially have nagios monitor a log to see if the file was received, if the file is present, or the directory. Something along those lines, if that file is not present, or directory created, or line in a log present, then it could alert you. Nagios is not a mechanism for automating backups, it is a network infrastructure monitoring system, so you would have to monitor something that can be checked via a script/plugin.

Re: Creating Nagios Plugin Using TCL

Posted: Mon Mar 31, 2014 4:03 am
by anubhav65
Hi can anybody tell me If I want to create a simple script in TCL and run it from nagios as a plugin ....how will it be done?????
Like if anybody has any script for the plugin of nagios ...plz can u upload it here.....it would be a great help......

Re: Creating Nagios Plugin Using TCL

Posted: Mon Mar 31, 2014 10:59 am
by abrist
You should definitely look at the plugin development guidelines: https://nagios-plugins.org/doc/guidelines.html
Creating a plugin out of a working script is usually a trivial matter for simple implementations. You need to exit with 0,1,2, or 3 depending on the the check and result, and then you can echo status and performance text in the following format:

Code: Select all

<status text> | <properly formatted performance data>
Check the doc above for more information.