check services in another hosts trough sockets

Need help with C, C++, perl, python, etc?

check services in another hosts trough sockets

Postby germain » 2007-02-01 17:16

I like to develop a plugin for Nagios for cheking some special services on my network, one of them is a program I already made so I know how it works.

I need a socket to connect to that machine and listen the data it sends for, let's say, 30 seconds, so I want to know if there's some way the package "netcat" can do this in order to use it in a shell script. How to do this?
- Use netcat to connect to the host and close the connection after 30 seconds
- Pipe the output to a shell script that analises it and return a value of 0 if everithing went ok

My problem is that I don't know how to stop netcat after 30 seconds, I already read the man page and there's an option to close the connection after x seconds if netcat receives an "EOF" from the remote host and another one for close the connection after x seconds if it can't establish the connection.

Any ideas?
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!

Postby germain » 2007-02-02 17:37

I'm already advancing on this subject, I made a python script using netcat to store the data it receives into a file, then I call that file and analyse its contents, but it fails to execute from nagios beacuse it doesn't create the file (I supose nagios don't have the neccesary permissions)

Here's the script:

Code: Select all
#! /usr/bin/python

#---------------------------------------------------------------------------------------------
import os         #importar la libreria para usar los comandos de la terminal (os.system)
import sys        #para usar los argumentos con que se llamo al programa (sys.argv)
#---------------------------------------------------------------------------------------------


#invocar a netcat para conectarse, redirigir la salida al archivo temp y desconectarse de la consola
os.system("netcat " + sys.argv[1] + " " + sys.argv[2] + " > temp &")

#esperar la cantidad de tiempo dada en el tercer argumento del script
os.system("sleep " + sys.argv[3])

#terminar netcat
os.system("killall netcat")



#abrir el archivo temp como solo lectura
temp = open('temp', 'r')

#guardar en "a" la primera linea del archivo
a = temp.readline()

#checar si a no esta vacia y luego checar si el primer caracter es "P" o "A"
if a != "":
  if a[0] == "P" or a[0] == "A":
     print "OK - El software de placas esta funcionando correctamente"
     sys.exit(0)
  else:
     print "WARNING - El software de placas esta funcionando de una manera extrana"
     sys.exit(1)   
 
else:
  print "CRITICAL - Puede que el software de placas este caido"
  sys.exit(2)




So I need to store the output of netcat into a python variable instead of a file but I don't know how to do this
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!

Postby germain » 2007-02-08 00:45

Well, I'm talking to myself but I did it!!! :D

It was very difficult to me to get the data from netcat and stop it, so I learnt the use of socktes in python and it was so easy!

Finally, here's the script:

Code: Select all
#!/usr/bin/python

#---------------------------------------------------------------------------------------
import socket    #Para usar los sockets
import sys       #Para usar los argumentos de entrada
#---------------------------------------------------------------------------------------

#Crear un socket para conectarse a un equipo remoto
s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)

#Poner el timeout con el tercer argumento de llamada
timeout = sys.argv[3]
s.settimeout(float(timeout))

try:
    #Conectarse a la ip dada en el primer argumento y al puerto
    #dado en el segundo
    s.connect((sys.argv[1], int(sys.argv[2])))
    #Recibir 1 byte de datos
    datos = s.recv(1)
    #Y si todo salio bien salir con exit status de cero
    print 'OK - Se recibio una placa en ' + timeout + ' segundos'
    s.close()
    sys.exit(0)
except socket.timeout:
    print 'WARNING - No se recibio placa despues de ' + timeout + ' segundos o menos'
    s.close()
    sys.exit(1)
except socket.error:
    print "CRITICAL - No se pudo conectar al software de placas:", sys.exc_info()[1]
    s.close()
    sys.exit(2)
   
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable