Skip to content

Commit

Permalink
Changes based on Leo's review including cleaner code, better error ha…
Browse files Browse the repository at this point in the history
…ndling
  • Loading branch information
whatarthurcodes committed Sep 15, 2014
1 parent 562042e commit 1cf67c1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
83 changes: 83 additions & 0 deletions checks.d/sftp_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# stdlib
import time
import socket
# 3p
import paramiko
# project
from checks import AgentCheck

class CheckSSH(AgentCheck):

def _load_conf(self, instance):
if 'host' not in instance or not instance['host']:
raise Exception ("No host has been specified") #maybe use dict instead
else:
host = instance['host']

if 'port' not in instance:
self.log.info("No port specified, defaulted to 22")
port = 22
else:
port = int(instance['port'])

if 'username' not in instance or not instance['username']:
raise Exception ("No username has been specified")
else:
username = instance['username']

if 'password' not in instance or not instance['password']:
self.log.info("No password specified")
password = None
else:
password = instance['password']

if 'private_key' not in instance or not instance['private_key']:
self.log.info("No private_key specified")
private_key = None
else:
private_key = instance['private_key']

return host, port, username, password, private_key

def check(self, instance):
host, port, username, password, private_key = self._load_conf(instance)

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()

try:
exception_message = 'No errors'
client.connect(host, port=port, username=username, password=password, pkey=private_key)

except Exception as e:
exception_message = "{0}".format(e)

#Service Availability to see if up or down
if exception_message == 'No errors':
try:
sftp = client.open_sftp()
except Exception as e:
exception_message = "{0}".format(e)
status = AgentCheck.CRITICAL

start_time = time.time()

try:
result = sftp.listdir('.')
except Exception as e:
exception_message = "{0}".format(e)
status = AgentCheck.CRITICAL

if result is not None:

This comment has been minimized.

Copy link
@whatarthurcodes

whatarthurcodes Sep 15, 2014

Author Contributor

@LeoCavaille
If an exception occurs, will result simply be defined as 'None'?

This comment has been minimized.

Copy link
@LeoCavaille

LeoCavaille Sep 15, 2014

Member

Try to exec this sample code

def f(x):
    return x/0

try:
    never_defined = f(3)
except Exception:
    print "Oops"

print never_defined
status = AgentCheck.OK
end_time = time.time()
time_taken = end_time - start_time
self.gauge('ssh.response_time', time_taken)
else:
status = AgentCheck.CRITICAL
exception_message = 'An error has occured'

This comment has been minimized.

Copy link
@whatarthurcodes

whatarthurcodes Sep 15, 2014

Author Contributor

@LeoCavaille
Unsure of what to do in this situation, does this represent a failure?

This comment has been minimized.

Copy link
@LeoCavaille

LeoCavaille Sep 15, 2014

Member

So this the case where listdir returns None, do you know in the doc what does this mean? Probably something like 'Unexpected result when executing an SFTP command' ?

This comment has been minimized.

Copy link
@whatarthurcodes

whatarthurcodes Sep 15, 2014

Author Contributor

Doc doesn't say anything about this, the expectation is just that: result = sftp.listdir('.') should actually return something and as long as it is not empty, it worked properly

else:
status = AgentCheck.CRITICAL

self.service_check('sftp.can_connect', status, message=exception_message)
9 changes: 9 additions & 0 deletions conf.d/sftp_check.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
init_config:

instances:
- host: localhost
#port defaults to 22 if not specified
port: 22
username: test
password: abcd
private_key:

0 comments on commit 1cf67c1

Please sign in to comment.