-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchksftp.py
93 lines (73 loc) · 2.96 KB
/
chksftp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import time
import logging
import pysftp
from prometheus_client import generate_latest, Gauge
from flask import Flask, Response
logger = logging.getLogger(__name__)
app = Flask(__name__)
CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
listenPort = int(os.getenv('WEBPORT', '9816'))
logger.info(listenPort)
myHostname = os.environ['SFTPHOST']
myUsername = os.environ['SFTPUSER']
myPassword = os.environ['SFTPPASS']
randomFile = os.getenv('FILENAME', 'testfile.bin')
print(os.environ)
# Create a metric to track time spent and requests made.
AUTH_SUCCESS = Gauge('sftp_auth_success', 'Whether authentication succceeded', ['sftp_host'])
TIME_TO_AUTH = Gauge('sftp_auth_seconds', 'Amount of time to authenticate', ['sftp_host'])
TRANSFER_SUCCESS = Gauge('sftp_transfer_success', 'Whether transfer succceeded', ['sftp_host'])
TIME_TO_TRANSFER = Gauge('sftp_transfer_seconds', 'Amount of time to transfer 5M file', ['sftp_host'])
# Decorate function with metric.
def process_request():
auth_duration = 0
auth_success_value = 0
file_success = 0
file_duration_value = 0
try:
# Hacky workaround to get it working in containers
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
beginSFTP = time.time()
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts, log=True) as sftp:
endConnection = time.time()
auth_success_value = 1
auth_duration = endConnection-beginSFTP
# Create a Random 5 MB file
with open(randomFile, 'wb') as fout:
fout.write(os.urandom(5242880))
# We've established a connection, now put a file
localFilePath = randomFile
remoteFilePath = randomFile
# Start Time to put a file
startPutFile = time.time()
if sftp.put(localFilePath, remoteFilePath):
file_success = 1
endPutFile = time.time()
file_duration_value = endPutFile-startPutFile
except Exception:
logger.error('SFTP Connection failed')
AUTH_SUCCESS.labels(myHostname).set(auth_success_value)
TRANSFER_SUCCESS.labels(myHostname).set(file_success)
TIME_TO_AUTH.labels(myHostname).set(auth_duration)
TIME_TO_TRANSFER.labels(myHostname).set(file_duration_value)
# END process_request()
# Mini server to replicate prometheus norms
@app.route('/metrics', methods=['GET'])
def get_data():
"""Returns all data as plaintext."""
process_request()
return Response(generate_latest(), mimetype=CONTENT_TYPE_LATEST)
@app.route('/', methods=['GET'])
def show_home():
output = """<html>
<head><title>SFTP Exporter</title></head>
<body>
<h1>SFTP Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>"""
return Response(output)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=listenPort)