daniels
daniels

Reputation: 19203

Best way to monitor services on a few servers with python

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?

Upvotes: 1

Views: 780

Answers (1)

Igal Serban
Igal Serban

Reputation: 10684

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.

Upvotes: 2

Related Questions