Permalink
Browse files

* Bugfix: ftp source was not calling back on error

* Refactored ftp source
* ftp source will not consider 6xx as errors (which is more compliant with return code specifications)
  • Loading branch information...
1 parent f894d24 commit 7115c130580afa56e9b47227299c8c043f4f6318 @stelcheck stelcheck committed Jan 23, 2013
Showing with 57 additions and 26 deletions.
  1. +57 −26 lib/sources/ftp.js
View
@@ -4,36 +4,54 @@
var net = require('net');
-var Ftp = function (service, stream) {
+var Ftp = function (service, stream, callback) {
this.service = service;
this.stream = stream;
-};
-Ftp.prototype.error = function(status, message) {
- this.service.status = "down";
- this.service.statusCode = status;
- this.service.message = message;
- callback(this.service.status, this.service);
+ /**
+ Actually required to get the SERVER READY out of the way
+ I am also assuming this might catch potential
+ on-connection ftp error
+ codes...
+ */
+ this.command('', callback);
};
Ftp.prototype.command = function(command, callback) {
- var self = this;
this.stream.write(command + '\r\n');
- var dataCallback = function(data) {
- self.stream.removeListener('data', dataCallback);
+ this.stream.once('data', function(data) {
var status = data.toString().match(/^\d\d\d/);
- if (parseInt(status[0], 10) > 399) {
- ftp.error(self.service, status[0], data.toString());
- } else {
- return callback(data);
+ status = status[0] ? parseInt(status[0], 10) : 0;
+
+ /**
+ TODO?
+
+ 4xx and 5xx should be treated differently. 4xx should be critical,
+ while 5xx should be (potentially) considered as down
+
+ Removed 600 from potential error responses
+ */
+ if ((status > 399 && status < 600) || status > 999) {
+ return callback(status, data.toString());
}
- };
- self.stream.addListener('data', dataCallback);
+ return callback(null, data.toString());
+ });
+};
+
+Ftp.prototype.returnServiceStatus = function(service, status, statusCode, message, callback) {
+ service.status = status;
+ service.statusCode = statusCode;
+ service.message = message;
+
+ this.stream.end();
+ this.stream.destroy();
+
+ callback(service.status, service);
};
exports.check = function(serviceDefinition, service, callback) {
@@ -45,22 +63,35 @@ exports.check = function(serviceDefinition, service, callback) {
var stream = net.createConnection(serviceDefinition.port, serviceDefinition.host);
stream.addListener('connect', function () {
- var ftp = new Ftp(service, stream);
-
- ftp.command('USER ' + service.username, function(data) {
- ftp.command('PASS ' + service.password, function(data) {
- service.status = "up";
- service.statusCode = 0;
- service.message = "";
- callback(service.status, service);
+
+ var ftp = new Ftp(service, stream, function (err, data) {
+
+ if (err) {
+ return ftp.returnServiceStatus(service, 'down', err, data, callback);
+ }
+
+ ftp.command('USER ' + serviceDefinition.username, function(err, data) {
+
+ if (err) {
+ return ftp.returnServiceStatus(service, 'critical', err, data, callback);
+ }
+
+ ftp.command('PASS ' + serviceDefinition.password, function(err, data) {
+ if (err) {
+ return ftp.returnServiceStatus(service, 'critical', err, data, callback);
+ }
+
+ return ftp.returnServiceStatus(service, 'up', 0, '', callback);
+ });
});
});
});
stream.addListener('error', function (e) {
- service.status = "down";
+ service.status = 'down';
service.statusCode = 0;
service.message = e.message;
- callback(service.status, service);
+
+ return callback(service.status, service);
});
};

0 comments on commit 7115c13

Please sign in to comment.