New Relic Syntheticsではスクリプトを記述することでさまざまな外形監視を実現できます。今回、HTTPではない接続として、FTP、SFTP、LDAP、TCPそしてSMTPを使った接続のテストが記述できるようになりました。すべてのパブリックロケーションで利用できます。本記事はExplores Hubsへの2つの投稿を日本語としてまとめています。
Proactively monitor non-http connections with New Relic Synthetics
FTP, SFTP, LDAP, TCP, and SMTP Examples
パブリックロケーションで利用可能なnpmとして追加されているため、すぐに使うことができます。具体的な記述方法は以下のサンプルを参考にしてください。また、このリリースより前から作成されたSyntheticsモニターは以前のランタイムをそのまま利用するため、より新しいバージョンで提供されている機能は利用できません。以下のドキュメントを参考にランタイムをアップデートしてください。
また、プライベートロケーションを利用している場合もランタイムを更新する必要があります。
Install containerized private minions (CPMs)
それぞれのサンプルを投稿より引用しています。なお、Syntheticsがランタイムとして提供しているパッケージ以外のパッケージを利用したい場合は、プライベートロケーションを利用するとランタイムにライブラリを追加することができます。プライベートロケーションはInternetに公開していないエンドポイントを開始する以外にもこのような利用方法があります。
FTPおよびFTPS
basic-ftpをライブラリとして利用できます。
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
ssh2-sftp-clientをライブラリとして利用できます。
const Client = require('ssh2-sftp-client');
var fs = require ('fs');
var endpoint = 'YourSFTPServer';
//テストファイルの作成
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,
// ssh-dssを利用している場合のみ指定が必要です
algorithms: {
serverHostKey: ['ssh-rsa', 'ssh-dss']
}
}
const sftp = new Client('US');
// 設定した内容で接続
sftp.connect(config)
// 作成したファイルが存在するかを確認し、存在していれば削除
.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
ldapauth-forkをライブラリとして利用します。
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=)'
}
// LDAPサーバーに接続します
var ldap = newLdapAuth(options);
ldap.on('error', function(err) {
throw new Error('Unable to connect to LDAP server. Error: '+err);
})
// ユーザー認証をテストします
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
netモジュールを利用します。
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
nodemailerをライブラリとして利用します。
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)
})
The views expressed on this blog are those of the author and do not necessarily reflect the views of New Relic. Any solutions offered by the author are environment-specific and not part of the commercial solutions or support offered by New Relic. Please join us exclusively at the Explorers Hub (discuss.newrelic.com) for questions and support related to this blog post. This blog may contain links to content on third-party sites. By providing such links, New Relic does not adopt, guarantee, approve or endorse the information, views or products available on such sites.