Additional modules are now available that allow users to monitor FTP, FTPS, SFTP, LDAP, TCP, and SMTP in addition to the HTTP, HTTPS, DNS, ICMP, SNMP, and traceroute capabilities that have been historically available in Synthetics. These simplified examples will get you started using these new modules to quickly expand protocol coverage. Additional assertions and error checks may be needed to verify success. Use the individual module links below to learn more about module usage and capabilities.
FTP/FTPS
Module: basic-ftp
Example Description:
- Connect to a FTP server using a password that is stored as a secure credential
- Lists the current directory contents
- Closes the connection
Example Monitor:
var ftp = require('basic-ftp');
example()
function example() {
const client = new ftp.Client()
client.ftp.verbose = true
client.access({
host: "MyFTPServer",
user: "FTPUser",
password: $secure.FTP_PASSWORD
}).then(function() {
return client.list();
})
.then(function(list) {
console.log(list)
})
.then(function() {
client.close()
})
}
SFTP
Module: ssh2-sftp-client
Example Description:
- Connect to a SFTP server using a password that is stored as a secure credential
- Create a test .txt file that will be used to test upload capabilities
- Delete the test file from the SFTP server if found
- Upload the test file to the SFTP server
- Download the test file from the SFTP server
- Delete the test file from the SFTP server
Example Monitor:
const Client = require('ssh2-sftp-client');
var fs = require ('fs');
var endpoint = 'YourSFTPServer';
// Create a simple test file.
fs.writeFileSync('test-sftp.txt', 'Hello World from New Relic Synthetics');
var testFile = fs.createReadStream('test-sftp.txt');
var remotePath = '/test-sftp.txt';
const config = {
host: endpoint,
username: 'MyUsername',
password: $secure.MY_PASSWORD,
// You only need to define algorithms if your server is still using ssh-dss
algorithms: {
serverHostKey: ['ssh-rsa', 'ssh-dss']
}
}
const sftp = new Client('US');
// Connect to the SFTP server using the configuration defined above.
sftp.connect(config)
// Check if our test file exists before issuing put. Delete it and then proceed if so.
.then(function() {
return sftp.exists(remotePath);
})
.then(function(bool) {
if(bool)
{
console.log('Test file from previous run exists. Removing.')
return sftp.delete(remotePath);
}
else
{
console.log('No test files from previous executions. Proceeding.');
}
})
.then(function () {
console.log('Putting test file on SFTP server.');
return sftp.put(testFile, remotePath);
})
.then(function () {
console.log('Getting test file from SFTP server.');
var destination = fs.createWriteStream('test-sftp-download.txt');
return sftp.get(remotePath, destination)
})
.then(function () {
var fileContents = fs.readFileSync('test-sftp-download.txt');
console.log('Reading downloaded test file.');
console.log(fileContents.toString())
})
.then(function() {
console.log('Deleting test file from FTP server.');
return sftp.delete(remotePath)
})
.then(function (){
return sftp.end();
})
.catch(function(err) {
throw err;
})
LDAP
Module: ldapauth-fork
Example Description:
- Connect to a LDAP server using a password that is stored as a secure credential
- Test authentication of a different user
Example Monitor:
var LdapAuth = require('ldapauth-fork');
var options = {
url: 'ldap://YourLdapServer:389',
bindDN: 'BindUsername',
bindCredentials: $secure.LDAP_BIND_PASSWORD,
searchBase: 'dc=yourDomain,dc=com',
searchFilter: '(cn=)'
}
// Connect to LDAP server
var ldap = newLdapAuth(options);
ldap.on('error', function(err) {
throw new Error('Unable to connect to LDAP server. Error: '+err);
})
// Test user authentication
ldap.authenticate('TestUser', $secure.LDAP_TEST_PASSWORD, function(err, user) {
if (err) {
ldap.close();
throw new Error('Unable to authenticate user. Error: '+err);
}
else {
console.log('User authentication successful.');
ldap.close();
}
})
TCP
Module: net
Example Description:
- Open a TCP connection to Google using port 80
- Write to the connection to simulate a HTTP GET
- Log the response
- Close the connection
Example Monitor:
var net = require('net');
var client = net.connect({
port: 80,
host: 'www.google.com'
}, () => {
console.log('connected to server!');
client.write('GET / HTTP/1.1\r\n');
client.write('Host: www.google.com\r\n');
client.write('User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n');
client.write('\r\n')
})
client.on('data', function(data){
console.log('Received: ' +data);
client.destroy();
})
client.on('end', function() {
console.log('Connection closed');
})
SMTP
Module: nodemailer
Example Description:
Use Gmail to send a test email utilizing a password stored as a secure credential.
Example Monitor:
var assert = require('assert');
var nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
auth: {
user: "YourGmailAccount",
pass: $secure.GMAILAPPPASSWORD
}
});
var message = {
from: 'YourGmailAccount',
to: 'EmailDestinationAccount',
subject: 'Test message from New Relic Synthetic monitor',
text: 'Testing the nodemailer package.',
}
transporter.sendMail(message, function(err, info, response){
assert.ok(!err, "Error sending email: "+err)
})