tags:

views:

183

answers:

1

What would be the best way to monitor services like HTTP/FTP/IMAP/POP3/SMTP for a few servers from python? Using sockets and trying to connect to service port http-80, ftp-21, etc... and if connection successful assume service is ok or use python libs to connect to specified services and handle exceptions/return codes/etc...

For example for ftp is it better to

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect(("ftp.domain.tld", 21))
    print "ok"
except:
    print "failed"
s.close()

or to

from ftplib import FTP

try:
    ftp = FTP('ftp.domain.tld')
    print "FTP service OK"
    ftp.close()
except:
    print "FTP err"

same goes for the rest, socket vs urllib2, imaplib, etc... and if i go the lib way how do i test for smtp?

+2  A: 

update 1:

Actually, In your code there is no difference between the two option ( in FTP ). The second option should be Preferred for code readability. But way not login to the ftp server, And maybe read some file?

update 0:

When monitoring, testing the full stack is better. Because otherwise you can miss problems that don't manifest themselves in the socket level. The smtp can be tested with the smtplib. The best way is actually send mail with it. And read it from the target with the imaplib. Or, You can just use the smtplib to converse with the smtp server. It is better to do the whole end to end thing. But development and configuration resources should be considered against the impact of missing problems.

Igal Serban
it was just a quick code to be able to explain what i was thinking about, the real code will handle exceptions the right way. and also for http with urllib2 i could also get the response code 404, 403, etc.. thanks, i'll go with using libs
daniels